diff options
Diffstat (limited to 'java/sca')
12 files changed, 42 insertions, 205 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 a07a6ed810..19e212f355 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 @@ -28,8 +28,6 @@ import java.security.AccessController; import java.security.PrivilegedAction; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; -import java.util.Collections; -import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -44,19 +42,17 @@ import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; /** - * The ServiceDiscoverer that find META-INF/services/... in installed bundles + * A ServiceDiscoverer that find META-INF/services/... in installed bundles * * @version $Rev: $ $Date: $ */ public class EquinoxServiceDiscoverer implements ServiceDiscoverer { private static final Logger logger = Logger.getLogger(EquinoxServiceDiscoverer.class.getName()); + private BundleContext context; - private ClassLoader classLoader; public EquinoxServiceDiscoverer(BundleContext context) { this.context = context; - // http://wiki.eclipse.org/index.php/Context_Class_Loader_Enhancements - this.classLoader = new ClassLoaderImpl(); } public static class ServiceDeclarationImpl implements ServiceDeclaration { @@ -118,87 +114,6 @@ public class EquinoxServiceDiscoverer implements ServiceDiscoverer { } - private final static ClassLoader getTCCL() { - return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { - public ClassLoader run() { - return Thread.currentThread().getContextClassLoader(); - } - }); - } - - public class ClassLoaderImpl extends ClassLoader { - private ClassLoader delegate; - - public ClassLoaderImpl() { - super(EquinoxServiceDiscoverer.class.getClassLoader()); - this.delegate = getTCCL(); - } - - @Override - protected Class<?> findClass(String name) throws ClassNotFoundException { - return delegate.loadClass(name); - } - - /** - * Open a back-door to expose the META-INF/services resources - */ - @Override - protected URL findResource(String name) { - int index = name.lastIndexOf('/'); - if (index == -1) { - return null; - } - String path = name.substring(0, index); - if (path.startsWith("/")) { - path = path.substring(1); - } - - if (!path.startsWith("META-INF/services")) { - return null; - } - - for (Bundle bundle : context.getBundles()) { - URL url = bundle.getEntry(name); - if (url != null) { - return url; - } - } - - return null; - } - - /** - * Open a back-door to expose the META-INF/services resources - */ - @Override - protected Enumeration<URL> findResources(String name) throws IOException { - int index = name.lastIndexOf('/'); - if (index == -1) { - return null; - } - String path = name.substring(0, index); - String file = name.substring(index + 1); - if (path.startsWith("/")) { - path = path.substring(1); - } - - if (!path.startsWith("META-INF/services")) { - return null; - } - - Set<URL> urlSet = new HashSet<URL>(); - - for (Bundle bundle : context.getBundles()) { - Enumeration<URL> urls = bundle.findEntries(path, file, false); - if (urls != null) { - urlSet.addAll(Collections.list(urls)); - } - } - return Collections.enumeration(urlSet); - } - - } - private static String toString(Bundle b) { StringBuffer sb = new StringBuffer(); sb.append(b.getBundleId()).append(" ").append(b.getSymbolicName()); @@ -232,7 +147,7 @@ public class EquinoxServiceDiscoverer implements ServiceDiscoverer { * @param declaration * @return a map of attributes */ - protected static Map<String, String> parseServiceDeclaration(String declaration) { + private static Map<String, String> parseServiceDeclaration(String declaration) { Map<String, String> attributes = new HashMap<String, String>(); int index = declaration.indexOf(';'); if (index != -1) { @@ -260,20 +175,13 @@ public class EquinoxServiceDiscoverer implements ServiceDiscoverer { return attributes; } - @SuppressWarnings("unchecked") public Set<ServiceDeclaration> discover(String serviceName, boolean firstOnly) { boolean debug = logger.isLoggable(Level.FINE); Set<ServiceDeclaration> descriptors = new HashSet<ServiceDeclaration>(); serviceName = "META-INF/services/" + serviceName; - // int index = serviceName.lastIndexOf('/'); - // String path = serviceName.substring(0, index); - // String file = serviceName.substring(index + 1); - for (Bundle bundle : context.getBundles()) { - // Enumeration<URL> urls = bundle.findEntries(path, file, false); - // Enumeration<URL> urls = bundle.findEntries(path, file, false); // This is expensive final URL url = bundle.getEntry(serviceName); if (url == null) { continue; @@ -338,19 +246,6 @@ public class EquinoxServiceDiscoverer implements ServiceDiscoverer { } } return descriptors; - - } - - /** - * This class loader can be set as the thread context class loader for non-OSGi code - * @return - */ - public ClassLoader getContextClassLoader() { - return classLoader; - } - - public <T> T getContext() { - return (T)context; } } diff --git a/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoveryActivator.java b/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoveryActivator.java index 0bd1c37113..1cb1d09f8d 100644 --- a/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoveryActivator.java +++ b/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoveryActivator.java @@ -38,7 +38,6 @@ public class EquinoxServiceDiscoveryActivator implements BundleActivator { EquinoxServiceDiscoverer discoverer = new EquinoxServiceDiscoverer(context); ServiceDiscovery.getInstance().setServiceDiscoverer(discoverer); logger.info("Installed service discovery"); - Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); } public void stop(BundleContext context) throws Exception { 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 5f951baf84..a536684b2e 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 @@ -267,10 +267,6 @@ public class OSGiServiceDiscoverer implements ServiceDiscoverer { return classLoader; } - public <T> T getContext() { - return (T)context; - } - @SuppressWarnings("unchecked") public Set<ServiceDeclaration> discover(String serviceName, boolean firstOnly) { boolean debug = logger.isLoggable(Level.FINE); diff --git a/java/sca/modules/extensibility/pom.xml b/java/sca/modules/extensibility/pom.xml index 01cd2d0ca9..b4e3b9ead7 100644 --- a/java/sca/modules/extensibility/pom.xml +++ b/java/sca/modules/extensibility/pom.xml @@ -41,6 +41,7 @@ <Bundle-SymbolicName>org.apache.tuscany.sca.extensibility</Bundle-SymbolicName> <Bundle-Description>${pom.name}</Bundle-Description> <Export-Package>org.apache.tuscany.sca.*</Export-Package> + <DynamicImport-Package>*</DynamicImport-Package> </instructions> </configuration> </plugin> 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/ContextClassLoaderServiceDiscoverer.java index ed7260c30c..2e11234095 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/ContextClassLoaderServiceDiscoverer.java @@ -30,7 +30,6 @@ import java.security.AccessController; import java.security.PrivilegedAction; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -43,9 +42,12 @@ import java.util.logging.Level; import java.util.logging.Logger; /** - * + * A ServiceDiscoverer that find META-INF/services/... using the Context ClassLoader. + * + * @version $Rev: $ $Date: $ */ -public class ClasspathServiceDiscoverer implements ServiceDiscoverer { +public class ContextClassLoaderServiceDiscoverer implements ServiceDiscoverer { + private static final Logger logger = Logger.getLogger(ContextClassLoaderServiceDiscoverer.class.getName()); public class ServiceDeclarationImpl implements ServiceDeclaration { private URL url; @@ -83,16 +85,12 @@ public class ClasspathServiceDiscoverer implements ServiceDiscoverer { } public Class<?> loadClass(String className) throws ClassNotFoundException { - return getContextClassLoader().loadClass(className); - } - - private ClasspathServiceDiscoverer getOuterType() { - return ClasspathServiceDiscoverer.this; + return classLoaderReference.get().loadClass(className); } public String toString() { StringBuffer sb = new StringBuffer(); - sb.append("ClassLoader: ").append(getContextClassLoader()); + sb.append("ClassLoader: ").append(classLoaderReference.get()); sb.append(" Attributes: ").append(attributes); return sb.toString(); } @@ -100,7 +98,7 @@ public class ClasspathServiceDiscoverer implements ServiceDiscoverer { public URL getResource(final String name) { return AccessController.doPrivileged(new PrivilegedAction<URL>() { public URL run() { - return getContextClassLoader().getResource(name); + return classLoaderReference.get().getResource(name); } }); } @@ -108,56 +106,26 @@ public class ClasspathServiceDiscoverer implements ServiceDiscoverer { } private WeakReference<ClassLoader> classLoaderReference; - private boolean useTCCL = false; - private static final Logger logger = Logger.getLogger(ClasspathServiceDiscoverer.class.getName()); - public ClasspathServiceDiscoverer() { - // ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - ClassLoader classLoader = ClasspathServiceDiscoverer.class.getClassLoader(); + public ContextClassLoaderServiceDiscoverer() { + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); this.classLoaderReference = new WeakReference<ClassLoader>(classLoader); - this.useTCCL = true; } - public ClasspathServiceDiscoverer(ClassLoader classLoader) { - this.classLoaderReference = new WeakReference<ClassLoader>(classLoader); - } - - protected List<URL> getResources(final String name, final boolean firstOnly) throws IOException { + private List<URL> getResources(final String name, final boolean firstOnly) throws IOException { try { return AccessController.doPrivileged(new PrivilegedExceptionAction<List<URL>>() { public List<URL> run() throws IOException { if (firstOnly) { - URL url = getContextClassLoader().getResource(name); + URL url = classLoaderReference.get().getResource(name); if (url != null) { return Arrays.asList(url); } else { - if (useTCCL) { - // Try thread context classloader - ClassLoader tccl = Thread.currentThread().getContextClassLoader(); - if (tccl != getContextClassLoader()) { - url = tccl.getResource(name); - } - if (url != null) { - return Arrays.asList(url); - } - } return Collections.emptyList(); } } else { - List<URL> urls = Collections.list(getContextClassLoader().getResources(name)); - if (!useTCCL) { - return urls; - } - // Try thread context classloader - ClassLoader tccl = Thread.currentThread().getContextClassLoader(); - if (tccl != getContextClassLoader()) { - urls.addAll(Collections.list(tccl.getResources(name))); - // Remove duplicate entries - Set<URL> urlSet = new HashSet<URL>(urls); - return new ArrayList<URL>(urlSet); - } else { - return urls; - } + List<URL> urls = Collections.list(classLoaderReference.get().getResources(name)); + return urls; } } }); @@ -166,14 +134,6 @@ public class ClasspathServiceDiscoverer implements ServiceDiscoverer { } } - 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 * return a map of attributes @@ -181,7 +141,7 @@ public class ClasspathServiceDiscoverer implements ServiceDiscoverer { * @param declaration * @return a map of attributes */ - protected static Map<String, String> parseServiceDeclaration(String declaration) { + private static Map<String, String> parseServiceDeclaration(String declaration) { Map<String, String> attributes = new HashMap<String, String>(); int index = declaration.indexOf(';'); if (index != -1) { 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 b4fdfd4d8a..1722cd5f49 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,13 +33,5 @@ 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/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java b/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java index 8ad7009f47..79b92e0390 100644 --- a/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java +++ b/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java @@ -53,20 +53,9 @@ public class ServiceDiscovery { return INSTANCE; } - /** - * Get a classloader-based service discovery instance - * @param classLoader - * @return - */ - public static ServiceDiscovery getInstance(ClassLoader classLoader) { - ServiceDiscovery discovery = new ServiceDiscovery(); - discovery.setServiceDiscoverer(new ClasspathServiceDiscoverer(classLoader)); - return discovery; - } - - public ServiceDiscoverer getServiceDiscoverer() { + private ServiceDiscoverer getServiceDiscoverer() { if (discoverer == null) { - discoverer = new ClasspathServiceDiscoverer(); + discoverer = new ContextClassLoaderServiceDiscoverer(); } return discoverer; } diff --git a/java/sca/modules/extensibility/src/test/java/org/apache/tuscany/sca/extensibility/ClasspathServiceDiscovererTestCase.java b/java/sca/modules/extensibility/src/test/java/org/apache/tuscany/sca/extensibility/ContextClassLoaderServiceDiscovererTestCase.java index 71faca2732..fd508402fc 100644 --- a/java/sca/modules/extensibility/src/test/java/org/apache/tuscany/sca/extensibility/ClasspathServiceDiscovererTestCase.java +++ b/java/sca/modules/extensibility/src/test/java/org/apache/tuscany/sca/extensibility/ContextClassLoaderServiceDiscovererTestCase.java @@ -30,15 +30,15 @@ import org.junit.Test; /** * Test Case for ClasspathServiceDiscover */ -public class ClasspathServiceDiscovererTestCase { - private static ClasspathServiceDiscoverer discover; +public class ContextClassLoaderServiceDiscovererTestCase { + private static ContextClassLoaderServiceDiscoverer discover; /** * @throws java.lang.Exception */ @BeforeClass public static void setUpBeforeClass() throws Exception { - discover = new ClasspathServiceDiscoverer(ClasspathServiceDiscoverer.class.getClassLoader()); + discover = new ContextClassLoaderServiceDiscoverer(); } @Test diff --git a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/JarFileFinder.java b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/JarFileFinder.java index 476c65958e..0f595df758 100644 --- a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/JarFileFinder.java +++ b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/JarFileFinder.java @@ -277,10 +277,7 @@ public class JarFileFinder { * * @return */ - public static List<URL> getClassPathEntries(ClassLoader classLoader, boolean recursive) { - if (classLoader == null) { - classLoader = Thread.currentThread().getContextClassLoader(); - } + static List<URL> getClassPathEntries(ClassLoader classLoader, boolean recursive) { Set<URL> entries = new HashSet<URL>(); list(entries, classLoader, recursive); return new ArrayList<URL>(entries); diff --git a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/LibrariesBundleFileFactoryHook.java b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/LibrariesBundleFileFactoryHook.java index 2713dd7f52..db0524a7d7 100644 --- a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/LibrariesBundleFileFactoryHook.java +++ b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/LibrariesBundleFileFactoryHook.java @@ -64,6 +64,7 @@ public class LibrariesBundleFileFactoryHook implements org.eclipse.osgi.baseadap @Override public InputStream getInputStream() throws IOException { + System.out.println(new String(bytes)); return new ByteArrayInputStream(bytes); } diff --git a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherUtil.java b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherUtil.java index f4c4f1369c..c058589433 100644 --- a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherUtil.java +++ b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherUtil.java @@ -19,6 +19,12 @@ package org.apache.tuscany.sca.node.equinox.launcher; +import static org.osgi.framework.Constants.BUNDLE_CLASSPATH; +import static org.osgi.framework.Constants.BUNDLE_MANIFESTVERSION; +import static org.osgi.framework.Constants.BUNDLE_SYMBOLICNAME; +import static org.osgi.framework.Constants.DYNAMICIMPORT_PACKAGE; +import static org.osgi.framework.Constants.EXPORT_PACKAGE; + import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -260,10 +266,11 @@ final class NodeLauncherUtil { Manifest manifest = new Manifest(); Attributes attributes = manifest.getMainAttributes(); attributes.putValue("Manifest-Version", "1.0"); - attributes.putValue(Constants.BUNDLE_MANIFESTVERSION, "2"); - attributes.putValue(Constants.BUNDLE_SYMBOLICNAME, "org.apache.tuscany.sca.node.launcher.equinox.libraries"); - attributes.putValue(Constants.EXPORT_PACKAGE, exports.substring(0, exports.length() -1)); - attributes.putValue(Constants.BUNDLE_CLASSPATH, classpath.substring(0, classpath.length() -1)); + attributes.putValue(BUNDLE_MANIFESTVERSION, "2"); + attributes.putValue(BUNDLE_SYMBOLICNAME, "org.apache.tuscany.sca.node.launcher.equinox.libraries"); + attributes.putValue(EXPORT_PACKAGE, exports.substring(0, exports.length() -1)); + attributes.putValue(BUNDLE_CLASSPATH, classpath.substring(0, classpath.length() -1)); + attributes.putValue(DYNAMICIMPORT_PACKAGE, "*"); return manifest; } catch (IOException e) { diff --git a/java/sca/modules/node-launcher-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/launcher/FelixOSGiHost.java b/java/sca/modules/node-launcher-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/launcher/FelixOSGiHost.java index 4eeb706767..5e4a0ed338 100644 --- a/java/sca/modules/node-launcher-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/launcher/FelixOSGiHost.java +++ b/java/sca/modules/node-launcher-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/launcher/FelixOSGiHost.java @@ -41,7 +41,7 @@ public class FelixOSGiHost implements OSGiHost { private Felix felix; private LauncherBundleActivator activator; - private ClassLoader tccl; + //private ClassLoader tccl; private final static String systemPackages = "org.osgi.framework; version=1.3.0," + "org.osgi.service.packageadmin; version=1.2.0, " @@ -137,8 +137,8 @@ public class FelixOSGiHost implements OSGiHost { // Now start Felix instance. felix.start(); - tccl = Thread.currentThread().getContextClassLoader(); - Thread.currentThread().setContextClassLoader(getContextClassLoader(felix.getBundleContext())); + //tccl = Thread.currentThread().getContextClassLoader(); + //Thread.currentThread().setContextClassLoader(getContextClassLoader(felix.getBundleContext())); } @@ -168,7 +168,7 @@ public class FelixOSGiHost implements OSGiHost { if (felix != null) { felix.stopAndWait(); } - Thread.currentThread().setContextClassLoader(tccl); + //Thread.currentThread().setContextClassLoader(tccl); } } |