diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2008-08-08 22:53:11 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2008-08-08 22:53:11 +0000 |
commit | 286087d1aefc942ea9cafa0f2523ee655c5179f8 (patch) | |
tree | 702913e225176ca36e2b6efeb129c1b6a09b260d | |
parent | e30b75d984cf32851023d4550463c535a12f0980 (diff) |
Support to start OSGi from a list of jar files
Add more context to ServiceDiscoverer
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@684135 13f79535-47bb-0310-9956-ffa450edef68
8 files changed, 170 insertions, 31 deletions
diff --git a/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoverer.java b/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoverer.java index ffe0407a68..cbcca659a8 100644 --- a/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoverer.java +++ b/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoverer.java @@ -326,8 +326,12 @@ public class EquinoxServiceDiscoverer implements ServiceDiscoverer { * This class loader can be set as the thread context class loader for non-OSGi code * @return */ - public ClassLoader getClassLoader() { + public ClassLoader getContextClassLoader() { return classLoader; } + public <T> T getContext() { + return (T) context; + } + } diff --git a/java/sca/modules/extensibility-osgi/src/main/java/org/apache/tuscany/sca/extensibility/osgi/OSGiServiceDiscoverer.java b/java/sca/modules/extensibility-osgi/src/main/java/org/apache/tuscany/sca/extensibility/osgi/OSGiServiceDiscoverer.java index b208310a58..fa594be8d6 100644 --- a/java/sca/modules/extensibility-osgi/src/main/java/org/apache/tuscany/sca/extensibility/osgi/OSGiServiceDiscoverer.java +++ b/java/sca/modules/extensibility-osgi/src/main/java/org/apache/tuscany/sca/extensibility/osgi/OSGiServiceDiscoverer.java @@ -262,9 +262,13 @@ public class OSGiServiceDiscoverer implements ServiceDiscoverer { * This class loader can be set as the thread context class loader for non-OSGi code * @return */ - public ClassLoader getClassLoader() { + public ClassLoader getContextClassLoader() { return classLoader; } + + public <T> T getContext() { + return (T) context; + } @SuppressWarnings("unchecked") public Set<ServiceDeclaration> discover(String serviceName, boolean firstOnly) { diff --git a/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ClasspathServiceDiscoverer.java b/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ClasspathServiceDiscoverer.java index da94931087..59c50785a6 100644 --- a/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ClasspathServiceDiscoverer.java +++ b/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ClasspathServiceDiscoverer.java @@ -81,7 +81,7 @@ public class ClasspathServiceDiscoverer implements ServiceDiscoverer { } public Class<?> loadClass(String className) throws ClassNotFoundException { - return getClassLoader().loadClass(className); + return getContextClassLoader().loadClass(className); } private ClasspathServiceDiscoverer getOuterType() { @@ -90,7 +90,7 @@ public class ClasspathServiceDiscoverer implements ServiceDiscoverer { public String toString() { StringBuffer sb = new StringBuffer(); - sb.append("ClassLoader: ").append(getClassLoader()); + sb.append("ClassLoader: ").append(getContextClassLoader()); sb.append(" Attributes: ").append(attributes); return sb.toString(); } @@ -98,7 +98,7 @@ public class ClasspathServiceDiscoverer implements ServiceDiscoverer { public URL getResource(final String name) { return AccessController.doPrivileged(new PrivilegedAction<URL>() { public URL run() { - return getClassLoader().getResource(name); + return getContextClassLoader().getResource(name); } }); } @@ -123,14 +123,14 @@ public class ClasspathServiceDiscoverer implements ServiceDiscoverer { return AccessController.doPrivileged(new PrivilegedExceptionAction<List<URL>>() { public List<URL> run() throws IOException { if (firstOnly) { - URL url = getClassLoader().getResource(name); + URL url = getContextClassLoader().getResource(name); if (url != null) { return Arrays.asList(url); } else { return Collections.emptyList(); } } else { - return Collections.list(getClassLoader().getResources(name)); + return Collections.list(getContextClassLoader().getResources(name)); } } }); @@ -139,9 +139,13 @@ public class ClasspathServiceDiscoverer implements ServiceDiscoverer { } } - private ClassLoader getClassLoader() { + public ClassLoader getContextClassLoader() { return classLoaderReference.get(); } + + public <T> T getContext() { + return (T) getContextClassLoader(); + } /** * Parse a service declaration in the form class;attr=value,attr=value and diff --git a/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscoverer.java b/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscoverer.java index 4079f86d99..b4fdfd4d8a 100644 --- a/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscoverer.java +++ b/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscoverer.java @@ -33,4 +33,13 @@ public interface ServiceDiscoverer { * @return A set of service descriptors */ Set<ServiceDeclaration> discover(String serviceName, boolean firstOnly); + + /** + * Get the context for the service discoverer + * @param <T> It can be a ClassLoader for JSE or BundleContext for OSGi + * @return The context + */ + <T> T getContext(); + + ClassLoader getContextClassLoader(); } diff --git a/java/sca/modules/node2-impl/pom.xml b/java/sca/modules/node2-impl/pom.xml index b9abde5257..62846f08b2 100644 --- a/java/sca/modules/node2-impl/pom.xml +++ b/java/sca/modules/node2-impl/pom.xml @@ -83,7 +83,7 @@ <Bundle-Version>${tuscany.version}</Bundle-Version> <Bundle-SymbolicName>org.apache.tuscany.sca.node2.impl</Bundle-SymbolicName> <Bundle-Description>${pom.name}</Bundle-Description> - <Export-Package>org.apache.tuscany.sca.node*</Export-Package> + <Export-Package>org.apache.tuscany.sca.node.impl*</Export-Package> </instructions> </configuration> </plugin> diff --git a/java/sca/modules/node2-launcher-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/launcher/FelixOSGiHost.java b/java/sca/modules/node2-launcher-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/launcher/FelixOSGiHost.java index cdb13decdd..a755e6d3ec 100644 --- a/java/sca/modules/node2-launcher-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/launcher/FelixOSGiHost.java +++ b/java/sca/modules/node2-launcher-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/launcher/FelixOSGiHost.java @@ -40,6 +40,7 @@ import org.osgi.framework.Constants; public class FelixOSGiHost implements OSGiHost { private Felix felix; + private LauncherBundleActivator activator; private ClassLoader tccl; private final static String systemPackages = @@ -82,6 +83,17 @@ public class FelixOSGiHost implements OSGiHost { + "javax.transaction, " + "javax.transaction.xa"; + public LauncherBundleActivator getActivator() { + if (activator == null) { + activator = new LauncherBundleActivator(); + } + return activator; + } + + public void setActivator(LauncherBundleActivator activator) { + this.activator = activator; + } + public BundleContext start() { try { startup(); @@ -116,7 +128,7 @@ public class FelixOSGiHost implements OSGiHost { configMap.put(BundleCache.CACHE_PROFILE_DIR_PROP, "target/.felix"); List<BundleActivator> list = new ArrayList<BundleActivator>(); - list.add(new LauncherBundleActivator()); + list.add(getActivator()); // Now create an instance of the framework with // our configuration properties and activator. @@ -139,7 +151,7 @@ public class FelixOSGiHost implements OSGiHost { Method getter = discovererClass.getMethod("getServiceDiscoverer"); Object discoverer = getter.invoke(null); - Method getCL = discoverer.getClass().getMethod("getClassLoader"); + Method getCL = discoverer.getClass().getMethod("getContextClassLoader"); ClassLoader cl = (ClassLoader)getCL.invoke(discoverer); return cl; } catch (Exception e) { diff --git a/java/sca/modules/node2-launcher-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/launcher/LauncherBundleActivator.java b/java/sca/modules/node2-launcher-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/launcher/LauncherBundleActivator.java index 3778521159..13af11238e 100644 --- a/java/sca/modules/node2-launcher-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/launcher/LauncherBundleActivator.java +++ b/java/sca/modules/node2-launcher-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/launcher/LauncherBundleActivator.java @@ -40,8 +40,17 @@ public class LauncherBundleActivator implements BundleActivator, Constants, Bund private BundleContext bundleContext; private List<Bundle> tuscanyBundles = new ArrayList<Bundle>(); - - private List<URL> jarFiles = new ArrayList<URL>(); + + private List<URL> jarFiles; + + public LauncherBundleActivator() { + super(); + } + + public LauncherBundleActivator(List<URL> jarFiles) { + super(); + this.jarFiles = jarFiles; + } public static String toString(Bundle b, boolean verbose) { StringBuffer sb = new StringBuffer(); @@ -124,10 +133,12 @@ public class LauncherBundleActivator implements BundleActivator, Constants, Bund // FIXME: SDO bundles dont have the correct dependencies System.setProperty("commonj.sdo.impl.HelperProvider", "org.apache.tuscany.sdo.helper.HelperProviderImpl"); - File tuscanyInstallDir = findTuscanyInstallDir(bundleContext.getBundle()); + List<URL> urls = jarFiles; + if (urls == null) { + File tuscanyInstallDir = findTuscanyInstallDir(bundleContext.getBundle()); - List<URL> urls = - JarFileFinder.findJarFiles(tuscanyInstallDir, new JarFileFinder.StandAloneJARFileNameFilter()); + urls = JarFileFinder.findJarFiles(tuscanyInstallDir, new JarFileFinder.StandAloneJARFileNameFilter()); + } for (URL url : urls) { File file = new File(url.toURI()); @@ -254,11 +265,11 @@ public class LauncherBundleActivator implements BundleActivator, Constants, Bund } return null; } - + private String getFileName(URL url) { String name = url.getPath(); int index = name.lastIndexOf('/'); - return name.substring(index+1); + return name.substring(index + 1); } private void addFileToJar(URL file, JarOutputStream jarOut) throws IOException { @@ -314,7 +325,7 @@ public class LauncherBundleActivator implements BundleActivator, Constants, Bund InputStream in = mf.openStream(); manifest.read(in); in.close(); - } catch(IOException e) { + } catch (IOException e) { // Ignore } } diff --git a/java/sca/tools/maven/maven-osgi-junit/src/main/java/org/apache/tuscany/tools/sca/osgi/junit/plugin/OSGiJUnitMojo.java b/java/sca/tools/maven/maven-osgi-junit/src/main/java/org/apache/tuscany/tools/sca/osgi/junit/plugin/OSGiJUnitMojo.java index 82700b5b47..dec8c08ebd 100644 --- a/java/sca/tools/maven/maven-osgi-junit/src/main/java/org/apache/tuscany/tools/sca/osgi/junit/plugin/OSGiJUnitMojo.java +++ b/java/sca/tools/maven/maven-osgi-junit/src/main/java/org/apache/tuscany/tools/sca/osgi/junit/plugin/OSGiJUnitMojo.java @@ -20,17 +20,23 @@ package org.apache.tuscany.tools.sca.osgi.junit.plugin; import java.io.File; import java.lang.reflect.Method; +import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; +import java.util.ArrayList; import java.util.HashSet; import java.util.List; import junit.framework.Assert; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.resolver.ArtifactResolver; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.resolver.ArtifactNotFoundException; +import org.apache.maven.artifact.resolver.ArtifactResolutionException; +import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; +import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; import org.apache.tuscany.sca.node.osgi.launcher.FelixOSGiHost; import org.apache.tuscany.sca.node.osgi.launcher.LauncherBundleActivator; @@ -63,22 +69,109 @@ public class OSGiJUnitMojo extends AbstractMojo { protected File basedir; /** - * @component + * Used to look up Artifacts in the remote repository. + * + * @parameter expression="${component.org.apache.maven.artifact.factory.ArtifactFactory}" + * @required + * @readonly */ - protected ArtifactResolver resolver; + protected org.apache.maven.artifact.factory.ArtifactFactory factory; - protected ArtifactRepository localRepository; + /** + * Used to look up Artifacts in the remote repository. + * + * @parameter expression="${component.org.apache.maven.artifact.resolver.ArtifactResolver}" + * @required + * @readonly + */ + protected org.apache.maven.artifact.resolver.ArtifactResolver resolver; + + /** + * Location of the local repository. + * + * @parameter expression="${localRepository}" + * @readonly + * @required + */ + protected org.apache.maven.artifact.repository.ArtifactRepository local; + + /** + * List of Remote Repositories used by the resolver + * + * @parameter expression="${project.remoteArtifactRepositories}" + * @readonly + * @required + */ + protected java.util.List remoteRepos; + + /** + * @parameter + */ + protected String osgiRuntime; + + protected Artifact getArtifact(String groupId, String artifactId) throws MojoExecutionException { + Artifact artifact; + VersionRange vr; + try { + vr = VersionRange.createFromVersionSpec(project.getVersion()); + } catch (InvalidVersionSpecificationException e1) { + vr = VersionRange.createFromVersion(project.getVersion()); + } + artifact = factory.createDependencyArtifact(groupId, artifactId, vr, "jar", null, Artifact.SCOPE_TEST); + + try { + resolver.resolve(artifact, remoteRepos, local); + } catch (ArtifactResolutionException e) { + throw new MojoExecutionException("Unable to resolve artifact.", e); + } catch (ArtifactNotFoundException e) { + throw new MojoExecutionException("Unable to find artifact.", e); + } + + return artifact; + } public void execute() throws MojoExecutionException { if (project.getPackaging().equals("pom")) { return; } - String home = new File(basedir, "target/tuscany").toString(); - System.setProperty("TUSCANY_HOME", home); - getLog().info(home); + Log log = getLog(); + List<URL> jarFiles = new ArrayList<URL>(); + for (Object o : project.getArtifacts()) { + Artifact a = (Artifact)o; + try { + if (log.isDebugEnabled()) { + log.debug("Adding: " + a); + } + jarFiles.add(a.getFile().toURI().toURL()); + } catch (MalformedURLException e) { + getLog().error(e); + } + } + + /* + * Add org.apache.tuscany.sca:tuscany-extensibility-osgi module + */ + String aid = "equinox".equals(osgiRuntime) ? "tuscany-extensibility-equinox" : "tuscany-extensibility-osgi"; + Artifact ext = getArtifact("org.apache.tuscany.sca", aid); + try { + URL url = ext.getFile().toURI().toURL(); + if (!jarFiles.contains(url)) { + if (log.isDebugEnabled()) { + log.debug("Adding: " + ext); + } + jarFiles.add(url); + } + } catch (MalformedURLException e) { + getLog().error(e); + } + + // String home = new File(basedir, "target/tuscany").toString(); + // System.setProperty("TUSCANY_HOME", home); + // getLog().info(home); try { FelixOSGiHost host = new FelixOSGiHost(); + host.setActivator(new LauncherBundleActivator(jarFiles)); BundleContext context = host.start(); for (Bundle b : context.getBundles()) { @@ -102,9 +195,10 @@ public class OSGiJUnitMojo extends AbstractMojo { host.stop(); } catch (Throwable e) { throw new MojoExecutionException(e.getMessage(), e); - } finally { - System.clearProperty("TUSCANY_HOME"); } + // finally { + // System.clearProperty("TUSCANY_HOME"); + // } } @@ -148,6 +242,7 @@ public class OSGiJUnitMojo extends AbstractMojo { public int runTestCase(ClassLoader testClassLoader, Class testClass) throws Exception { if (testClass.getName().endsWith("TestCase")) { + getLog().info("Running: " + testClass.getName()); Class coreClass = Class.forName("org.junit.runner.JUnitCore", true, testClassLoader); Object core = coreClass.newInstance(); Class reqClass = Class.forName("org.junit.runner.Request", true, testClassLoader); @@ -168,10 +263,10 @@ public class OSGiJUnitMojo extends AbstractMojo { } else { errors++; } - ((Throwable)ex).printStackTrace(); + getLog().error((Throwable)ex); } - System.out.println("Test Runs: " + runs + getLog().info("Test Runs: " + runs + ", Failures: " + failures + ", Errors: " |