summaryrefslogtreecommitdiffstats
path: root/java/sca/modules/node-launcher-equinox
diff options
context:
space:
mode:
authorrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2008-09-06 01:46:45 +0000
committerrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2008-09-06 01:46:45 +0000
commit9eadabfbf44beef2a1825f6a9c877449c374f420 (patch)
tree7227cf0569cc98986924ccc9bd3524dfd467cc81 /java/sca/modules/node-launcher-equinox
parent9168af7fc1cec7a6f781dd1f7af14700cbd53a13 (diff)
Starting to add support Classloading for 3rd party jars
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@692604 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules/node-launcher-equinox')
-rw-r--r--java/sca/modules/node-launcher-equinox/pom.xml4
-rw-r--r--java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/EquinoxOSGiHost.java66
-rw-r--r--java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/LauncherBundleActivator.java13
-rw-r--r--java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncher.java15
-rw-r--r--java/sca/modules/node-launcher-equinox/src/test/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherTestCase.java11
5 files changed, 90 insertions, 19 deletions
diff --git a/java/sca/modules/node-launcher-equinox/pom.xml b/java/sca/modules/node-launcher-equinox/pom.xml
index 0356d142d6..9a6e350ba1 100644
--- a/java/sca/modules/node-launcher-equinox/pom.xml
+++ b/java/sca/modules/node-launcher-equinox/pom.xml
@@ -87,6 +87,7 @@
<build>
<plugins>
+ <!--
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
@@ -107,7 +108,8 @@
</configuration>
</execution>
</executions>
- </plugin>
+ </plugin>
+ -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
diff --git a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/EquinoxOSGiHost.java b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/EquinoxOSGiHost.java
index 0a7cf67cc2..bacfc4df87 100644
--- a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/EquinoxOSGiHost.java
+++ b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/EquinoxOSGiHost.java
@@ -20,12 +20,15 @@
package org.apache.tuscany.sca.node.equinox.launcher;
import java.io.File;
+import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.runtime.adaptor.EclipseStarter;
import org.eclipse.core.runtime.adaptor.LocationManager;
+import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
/**
* Wraps the Equinox runtime.
@@ -33,7 +36,8 @@ import org.osgi.framework.BundleContext;
public class EquinoxOSGiHost {
private LauncherBundleActivator activator = new LauncherBundleActivator();
private BundleContext context;
-
+ private ClassLoader tccl;
+
private final static String systemPackages =
"org.osgi.framework; version=1.3.0," + "org.osgi.service.packageadmin; version=1.2.0, "
+ "org.osgi.service.startlevel; version=1.0.0, "
@@ -81,6 +85,26 @@ public class EquinoxOSGiHost {
throw new IllegalStateException(e);
}
}
+
+ public Bundle findBundle(String symbolicName, String version) {
+ if (context == null) {
+ return null;
+ }
+ Bundle[] bundles = context.getBundles();
+ if (version == null) {
+ version = "0.0.0";
+ }
+ for (Bundle b : bundles) {
+ String v = (String)b.getHeaders().get(Constants.BUNDLE_VERSION);
+ if (v == null) {
+ v = "0.0.0";
+ }
+ if (b.getSymbolicName().equals(symbolicName) && (version.equals("0.0.0") || v.equals(version))) {
+ return b;
+ }
+ }
+ return null;
+ }
public void stop() {
try {
@@ -90,6 +114,10 @@ public class EquinoxOSGiHost {
}
}
+ public BundleContext getBundleContext() {
+ return context;
+ }
+
private BundleContext startup() throws Exception {
String args[] = {};
Map<Object, Object> props = new HashMap<Object, Object>();
@@ -101,16 +129,50 @@ public class EquinoxOSGiHost {
props.put(LocationManager.PROP_INSTALL_AREA, new File("target/eclipse/install").toURI().toString());
props.put(LocationManager.PROP_CONFIG_AREA, new File("target/eclipse/config").toURI().toString());
props.put(LocationManager.PROP_USER_AREA, new File("target/eclipse/user").toURI().toString());
-
+
EclipseStarter.setInitialProperties(props);
context = EclipseStarter.startup(args, null);
activator.start(context);
+
+// [rfeng] This is useful to report bundle resolving issues
+// for (Bundle b : context.getBundles()) {
+// System.out.println("Starting: " + b);
+// try {
+// b.start();
+// } catch (BundleException e) {
+// e.printStackTrace();
+// }
+// }
+ tccl = Thread.currentThread().getContextClassLoader();
+ Thread.currentThread().setContextClassLoader(getContextClassLoader());
return context;
}
+ private ClassLoader getContextClassLoader() {
+ Bundle b = findBundle("org.apache.tuscany.sca.extensibility.equinox", null);
+ if (b != null) {
+ try {
+ b.start();
+ Class<?> discovererClass = b.loadClass("org.apache.tuscany.sca.extensibility.ServiceDiscovery");
+ Method getInstance = discovererClass.getMethod("getInstance");
+ Object instance = getInstance.invoke(null);
+ Method getter = discovererClass.getMethod("getServiceDiscoverer");
+ Object discoverer = getter.invoke(instance);
+
+ Method getCL = discoverer.getClass().getMethod("getContextClassLoader");
+ ClassLoader cl = (ClassLoader)getCL.invoke(discoverer);
+ return cl;
+ } catch (Exception e) {
+ throw new IllegalStateException(e);
+ }
+ }
+ throw new IllegalStateException("Bundle org.apache.tuscany.sca.extensibility.equinox is not installed");
+ }
+
private void shutdown() throws Exception {
activator.stop(context);
EclipseStarter.shutdown();
+ Thread.currentThread().setContextClassLoader(tccl);
}
}
diff --git a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/LauncherBundleActivator.java b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/LauncherBundleActivator.java
index d5358726df..3f259c33a7 100644
--- a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/LauncherBundleActivator.java
+++ b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/LauncherBundleActivator.java
@@ -88,10 +88,12 @@ public class LauncherBundleActivator implements BundleActivator, Constants, Bund
for (Bundle b : bundleContext.getBundles()) {
try {
- if ("org.apache.tuscany.sca.contribution.osgi".equals(b.getSymbolicName())) {
- b.start();
- logger.info(toString(b, false) + " " + b.getState());
- break;
+ if ("org.apache.tuscany.sca.contribution.osgi".equals(b.getSymbolicName()) || "org.apache.tuscany.sca.extensibility.equinox"
+ .equals(b.getSymbolicName())) {
+ if (b.getHeaders().get("Fragment-Host") == null) {
+ b.start();
+ logger.info(toString(b, false) + " " + b.getState());
+ }
}
} catch (Exception e) {
logger.log(Level.SEVERE, e.getMessage(), e);
@@ -223,11 +225,14 @@ public class LauncherBundleActivator implements BundleActivator, Constants, Bund
} else {
return null;
}
+ } else if (file != null && !file.exists()) {
+ return null;
}
Manifest manifest = readManifest(bundleFile);
boolean isOSGiBundle = manifest != null && manifest.getMainAttributes().getValue(BUNDLE_SYMBOLICNAME) != null;
if (!isOSGiBundle) {
+ // return null;
manifest = updateBundleManifest(bundleFile, manifest);
}
diff --git a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncher.java b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncher.java
index bc9e3f24f2..e3313e7bdf 100644
--- a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncher.java
+++ b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncher.java
@@ -33,11 +33,14 @@ import java.util.logging.Logger;
public class NodeLauncher {
static final Logger logger = Logger.getLogger(NodeLauncher.class.getName());
+ private EquinoxOSGiHost host;
/**
* Constructs a new node launcher.
*/
private NodeLauncher() {
+ host = new EquinoxOSGiHost();
+ host.start();
}
/**
@@ -115,15 +118,11 @@ public class NodeLauncher {
// Create a node launcher
NodeLauncher launcher = newInstance();
- EquinoxOSGiHost equinox = null;
+ EquinoxOSGiHost equinox = launcher.host;
Object node = null;
ShutdownThread shutdown = null;
try {
- // Start the OSGi host
- equinox = new EquinoxOSGiHost();
- equinox.start();
-
if (args.length ==1) {
// Create a node from a configuration URI
@@ -180,6 +179,12 @@ public class NodeLauncher {
}
}
}
+
+ public void destroy() {
+ if (host != null) {
+ host.stop();
+ }
+ }
/**
* Stop the given node.
diff --git a/java/sca/modules/node-launcher-equinox/src/test/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherTestCase.java b/java/sca/modules/node-launcher-equinox/src/test/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherTestCase.java
index 9e30c1695e..d393637848 100644
--- a/java/sca/modules/node-launcher-equinox/src/test/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherTestCase.java
+++ b/java/sca/modules/node-launcher-equinox/src/test/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherTestCase.java
@@ -32,26 +32,23 @@ import org.junit.Test;
*
*/
public class NodeLauncherTestCase {
- private static EquinoxOSGiHost equinox;
+ private static NodeLauncher launcher;
@BeforeClass
public static void setUp() {
- // System.setProperty("TUSCANY_HOME", "target/tuscany");
- equinox = new EquinoxOSGiHost();
- equinox.start();
+ launcher = NodeLauncher.newInstance();
}
@AfterClass
public static void tearDown() {
- if (equinox != null) {
- equinox.stop();
+ if (launcher != null) {
+ launcher.destroy();
}
}
@Test
public void testLaunch() throws Exception {
- NodeLauncher launcher = NodeLauncher.newInstance();
SCANode node = launcher.createNodeFromClassLoader("HelloWorld.composite", getClass().getClassLoader());
node.start();