From 2aa39a40c33cb185382ebfedea10ef0e444e79fc Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Thu, 25 Sep 2008 04:53:56 +0000 Subject: Rationalized how to locate an SCA contribution given a known Java class contained in that contribution, and how to locate an SCA contribution represented by an OSGi bundle. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@698815 13f79535-47bb-0310-9956-ffa450edef68 --- .../launcher/ContributionLocationHelper.java | 66 ++++++++++++++++++++++ .../sca/node/equinox/launcher/EquinoxHost.java | 6 +- .../sca/node/equinox/launcher/NodeLauncher.java | 24 +------- .../node/equinox/launcher/NodeLauncherUtil.java | 7 --- .../equinox/launcher/NodeLauncherTestCase.java | 3 +- 5 files changed, 74 insertions(+), 32 deletions(-) create mode 100644 branches/sca-equinox/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/ContributionLocationHelper.java (limited to 'branches/sca-equinox/modules/node-launcher-equinox/src') diff --git a/branches/sca-equinox/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/ContributionLocationHelper.java b/branches/sca-equinox/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/ContributionLocationHelper.java new file mode 100644 index 0000000000..fd7aaed4c8 --- /dev/null +++ b/branches/sca-equinox/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/ContributionLocationHelper.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.node.equinox.launcher; + +import java.io.File; +import java.net.URL; +import java.security.AccessController; +import java.security.PrivilegedAction; + +import org.osgi.framework.Bundle; + +/** + * ContributionLocationHelper + * + * @version $Rev: $ $Date: $ + */ +public class ContributionLocationHelper { + + /** + * Returns the location of the SCA contribution containing the given class. + * + * @param anchorClass + * @return + */ + public static String getContributionLocation(final Class anchorClass) { + URL url = AccessController.doPrivileged(new PrivilegedAction() { + public URL run() { + return anchorClass.getProtectionDomain().getCodeSource().getLocation(); + } + }); + String uri = url.toString(); + return uri; + } + + /** + * Returns the location of the SCA contribution represented by the given bundle. + * + * @param anchorClass + * @return + */ + public static String getContributionLocation(final Bundle bundle) { + String uri = bundle.getLocation(); + uri = uri.substring(uri.indexOf("file:") + 5); + File file = new File(uri); + uri = file.toURI().toString(); + return uri; + } + +} diff --git a/branches/sca-equinox/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/EquinoxHost.java b/branches/sca-equinox/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/EquinoxHost.java index 2307e94907..d3c5ee2a9d 100644 --- a/branches/sca-equinox/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/EquinoxHost.java +++ b/branches/sca-equinox/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/EquinoxHost.java @@ -48,7 +48,7 @@ import org.osgi.framework.BundleContext; /** * Wraps the Equinox runtime. */ -public class EquinoxHost { +class EquinoxHost { private static Logger logger = Logger.getLogger(EquinoxHost.class.getName()); private BundleContext bundleContext; @@ -118,7 +118,7 @@ public class EquinoxHost { * * @return */ - public BundleContext start() { + BundleContext start() { try { if (!EclipseStarter.isRunning()) { @@ -292,7 +292,7 @@ public class EquinoxHost { /** * Stop the Equinox host. */ - public void stop() { + void stop() { try { // Uninstall all the bundles we've installed diff --git a/branches/sca-equinox/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncher.java b/branches/sca-equinox/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncher.java index 790b1ab48c..614c34b01e 100644 --- a/branches/sca-equinox/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncher.java +++ b/branches/sca-equinox/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncher.java @@ -65,7 +65,7 @@ public class NodeLauncher { * @throws LauncherException */ public T createNodeFromURL(String configurationURL) throws LauncherException { - return (T)node(configurationURL, null, null, null, null, bundleContext); + return (T)node(configurationURL, null, null, null, bundleContext); } /** @@ -80,7 +80,7 @@ public class NodeLauncher { * @throws LauncherException */ public T createNode(String compositeURI, Contribution... contributions) throws LauncherException { - return (T)node(null, compositeURI, null, contributions, null, bundleContext); + return (T)node(null, compositeURI, null, contributions, bundleContext); } /** @@ -94,25 +94,7 @@ public class NodeLauncher { */ public T createNode(String compositeURI, String compositeContent, Contribution... contributions) throws LauncherException { - return (T)node(null, compositeURI, compositeContent, contributions, null, bundleContext); - } - - /** - * Create a SCA node based on the discovery of the contribution on the classpath for the - * given classloader. This method should be treated a convenient shortcut with the following - * assumptions: - *
    - *
  • This is a standalone application and there is a deployable composite file on the classpath. - *
  • There is only one contribution which contains the deployable composite file physically in its packaging hierarchy. - *
- * - * @param compositeURI The URI of the composite file relative to the root of the enclosing contribution - * @param classLoader The ClassLoader used to load the composite file as a resource. If the value is null, - * then thread context classloader will be used - * @return A newly created SCA node - */ - public T createNodeFromClassLoader(String compositeURI, ClassLoader classLoader) throws LauncherException { - return (T)node(null, compositeURI, null, null, classLoader, bundleContext); + return (T)node(null, compositeURI, compositeContent, contributions, bundleContext); } public static void main(String[] args) throws Exception { diff --git a/branches/sca-equinox/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherUtil.java b/branches/sca-equinox/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherUtil.java index c2544a1872..c6379886cc 100644 --- a/branches/sca-equinox/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherUtil.java +++ b/branches/sca-equinox/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherUtil.java @@ -103,7 +103,6 @@ final class NodeLauncherUtil { String compositeURI, String compositeContent, Contribution[] contributions, - ClassLoader contributionClassLoader, BundleContext bundleContext) throws LauncherException { try { @@ -129,12 +128,6 @@ final class NodeLauncherUtil { // Construct the node with a configuration URI bootstrap = bootstrapClass.getConstructor(String.class).newInstance(configurationURI); - } else if (contributionClassLoader != null) { - - // Construct the node with a compositeURI and a classloader - Constructor constructor = bootstrapClass.getConstructor(String.class, ClassLoader.class); - bootstrap = constructor.newInstance(compositeURI, contributionClassLoader); - } else if (compositeContent != null) { // Construct the node with a composite URI, the composite content and diff --git a/branches/sca-equinox/modules/node-launcher-equinox/src/test/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherTestCase.java b/branches/sca-equinox/modules/node-launcher-equinox/src/test/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherTestCase.java index b16045a103..b109b690c5 100644 --- a/branches/sca-equinox/modules/node-launcher-equinox/src/test/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherTestCase.java +++ b/branches/sca-equinox/modules/node-launcher-equinox/src/test/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherTestCase.java @@ -50,7 +50,8 @@ public class NodeLauncherTestCase { @Test public void testLaunch() throws Exception { - SCANode node = launcher.createNodeFromClassLoader("HelloWorld.composite", getClass().getClassLoader()); + String location = ContributionLocationHelper.getContributionLocation(getClass()); + SCANode node = launcher.createNode("HelloWorld.composite", new Contribution("test", location)); node.start(); node.stop(); } -- cgit v1.2.3