From cc8967d7638d980d02cb44be647f76d979b02071 Mon Sep 17 00:00:00 2001 From: rfeng Date: Wed, 3 Sep 2008 00:48:29 +0000 Subject: Make a few fields non-static to ServiceDiscovery Enable Equinox launcher to load tuscany jars git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@691447 13f79535-47bb-0310-9956-ffa450edef68 --- .../equinox/EquinoxServiceDiscoverer.java | 138 +++++++++++---------- .../equinox/EquinoxServiceDiscoveryActivator.java | 2 +- .../extensibility/osgi/OSGiServiceDiscoverer.java | 30 +++-- .../osgi/OSGiServiceDiscoveryActivator.java | 2 +- .../osgi/OSGiServiceDiscovererTestCase.java | 11 ++ .../sca/extensibility/ServiceDiscovery.java | 21 +++- .../sca/node/equinox/launcher/EquinoxOSGiHost.java | 15 ++- .../equinox/launcher/LauncherBundleActivator.java | 9 +- .../sca/osgi/runtime/OSGiBundleActivator.java | 2 +- 9 files changed, 140 insertions(+), 90 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 cbcca659a8..f662f44a24 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 @@ -97,7 +97,7 @@ public class EquinoxServiceDiscoverer implements ServiceDiscoverer { public URL getLocation() { return url; } - + public URL getResource(final String name) { return AccessController.doPrivileged(new PrivilegedAction() { public URL run() { @@ -117,11 +117,11 @@ public class EquinoxServiceDiscoverer implements ServiceDiscoverer { } public class ClassLoaderImpl extends SecureClassLoader { - + public ClassLoaderImpl() { super(EquinoxServiceDiscoverer.class.getClassLoader()); } - + /** * Open a back-door to expose the META-INF/services resources */ @@ -135,21 +135,21 @@ public class EquinoxServiceDiscoverer implements ServiceDiscoverer { 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 */ @@ -164,13 +164,13 @@ public class EquinoxServiceDiscoverer implements ServiceDiscoverer { if (path.startsWith("/")) { path = path.substring(1); } - + if (!path.startsWith("META-INF/services")) { return null; } - + Set urlSet = new HashSet(); - + for (Bundle bundle : context.getBundles()) { Enumeration urls = bundle.findEntries(path, file, false); if (urls != null) { @@ -179,7 +179,7 @@ public class EquinoxServiceDiscoverer implements ServiceDiscoverer { } return Collections.enumeration(urlSet); } - + } private static String toString(Bundle b) { @@ -250,72 +250,74 @@ public class EquinoxServiceDiscoverer implements ServiceDiscoverer { serviceName = "META-INF/services/" + serviceName; - int index = serviceName.lastIndexOf('/'); - String path = serviceName.substring(0, index); - String file = serviceName.substring(index + 1); + // int index = serviceName.lastIndexOf('/'); + // String path = serviceName.substring(0, index); + // String file = serviceName.substring(index + 1); for (Bundle bundle : context.getBundles()) { - Enumeration urls = bundle.findEntries(path, file, false); - while (urls != null && urls.hasMoreElements()) { - final URL url = urls.nextElement(); - if (debug) { - logger.fine("Reading service provider file: " + url.toExternalForm()); + // Enumeration urls = bundle.findEntries(path, file, false); + // Enumeration urls = bundle.findEntries(path, file, false); // This is expensive + final URL url = bundle.getEntry(serviceName); + if (url == null) { + continue; + } + if (debug) { + logger.fine("Reading service provider file: " + url.toExternalForm()); + } + try { + // Allow privileged access to open URL stream. Add FilePermission to added to security + // policy file. + InputStream is; + try { + is = AccessController.doPrivileged(new PrivilegedExceptionAction() { + public InputStream run() throws IOException { + return url.openStream(); + } + }); + } catch (PrivilegedActionException e) { + throw (IOException)e.getException(); } + BufferedReader reader = null; try { - // Allow privileged access to open URL stream. Add FilePermission to added to security - // policy file. - InputStream is; - try { - is = AccessController.doPrivileged(new PrivilegedExceptionAction() { - public InputStream run() throws IOException { - return url.openStream(); + reader = new BufferedReader(new InputStreamReader(is)); + int count = 0; + while (true) { + String line = reader.readLine(); + if (line == null) + break; + line = line.trim(); + if (!line.startsWith("#") && !"".equals(line)) { + String reg = line.trim(); + if (debug) { + logger.fine("Registering service provider: " + reg); } - }); - } catch (PrivilegedActionException e) { - throw (IOException)e.getException(); - } - BufferedReader reader = null; - try { - reader = new BufferedReader(new InputStreamReader(is)); - int count = 0; - while (true) { - String line = reader.readLine(); - if (line == null) - break; - line = line.trim(); - if (!line.startsWith("#") && !"".equals(line)) { - String reg = line.trim(); - if (debug) { - logger.fine("Registering service provider: " + reg); - } - - Map attributes = parseServiceDeclaration(reg); - String className = attributes.get("class"); - if (className == null) { - // Add a unique class name to prevent equals() from returning true - className = "_class_" + count; - count++; - } - ServiceDeclarationImpl descriptor = - new ServiceDeclarationImpl(bundle, url, className, attributes); - descriptors.add(descriptor); - if (firstOnly) { - return descriptors; - } + + Map attributes = parseServiceDeclaration(reg); + String className = attributes.get("class"); + if (className == null) { + // Add a unique class name to prevent equals() from returning true + className = "_class_" + count; + count++; } - } - } finally { - if (reader != null) { - try { - reader.close(); - } catch (IOException e) { - // Ignore + ServiceDeclarationImpl descriptor = + new ServiceDeclarationImpl(bundle, url, className, attributes); + descriptors.add(descriptor); + if (firstOnly) { + return descriptors; } } } - } catch (IOException e) { - logger.log(Level.SEVERE, e.getMessage(), e); + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e) { + // Ignore + } + } } + } catch (IOException e) { + logger.log(Level.SEVERE, e.getMessage(), e); } } return descriptors; @@ -331,7 +333,7 @@ public class EquinoxServiceDiscoverer implements ServiceDiscoverer { } public T getContext() { - return (T) context; + 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 4b2e37f07a..0d00c1c3dd 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 @@ -33,7 +33,7 @@ public class EquinoxServiceDiscoveryActivator implements BundleActivator { if (bundleContext == null) { bundleContext = context; EquinoxServiceDiscoverer discoverer = new EquinoxServiceDiscoverer(bundleContext); - ServiceDiscovery.setServiceDiscoverer(discoverer); + ServiceDiscovery.getInstance().setServiceDiscoverer(discoverer); } } 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 10954b1f5f..5f951baf84 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 @@ -254,7 +254,7 @@ public class OSGiServiceDiscoverer implements ServiceDiscoverer { } return attributes; } - + public BundleContext getBundleContext() { return context; } @@ -266,9 +266,9 @@ public class OSGiServiceDiscoverer implements ServiceDiscoverer { public ClassLoader getContextClassLoader() { return classLoader; } - + public T getContext() { - return (T) context; + return (T)context; } @SuppressWarnings("unchecked") @@ -278,14 +278,19 @@ public class OSGiServiceDiscoverer implements ServiceDiscoverer { serviceName = "META-INF/services/" + serviceName; - int index = serviceName.lastIndexOf('/'); - String path = serviceName.substring(0, index); - String file = serviceName.substring(index + 1); + // int index = serviceName.lastIndexOf('/'); + // String path = serviceName.substring(0, index); + // String file = serviceName.substring(index + 1); + + // long start = System.currentTimeMillis(); + try { + for (Bundle bundle : context.getBundles()) { + // Enumeration urls = bundle.findEntries(path, file, false); // This is expensive + final URL url = bundle.getEntry(serviceName); + if (url == null) { + continue; + } - for (Bundle bundle : context.getBundles()) { - Enumeration urls = bundle.findEntries(path, file, false); - while (urls != null && urls.hasMoreElements()) { - final URL url = urls.nextElement(); if (debug) { logger.fine("Reading service provider file: " + url.toExternalForm()); } @@ -345,6 +350,11 @@ public class OSGiServiceDiscoverer implements ServiceDiscoverer { logger.log(Level.SEVERE, e.getMessage(), e); } } + } finally { +// long end = System.currentTimeMillis(); +// if (true) { +// logger.info("Duration: " + (end - start) + " ms"); +// } } return descriptors; diff --git a/java/sca/modules/extensibility-osgi/src/main/java/org/apache/tuscany/sca/extensibility/osgi/OSGiServiceDiscoveryActivator.java b/java/sca/modules/extensibility-osgi/src/main/java/org/apache/tuscany/sca/extensibility/osgi/OSGiServiceDiscoveryActivator.java index b69f36d383..208ef1a74f 100644 --- a/java/sca/modules/extensibility-osgi/src/main/java/org/apache/tuscany/sca/extensibility/osgi/OSGiServiceDiscoveryActivator.java +++ b/java/sca/modules/extensibility-osgi/src/main/java/org/apache/tuscany/sca/extensibility/osgi/OSGiServiceDiscoveryActivator.java @@ -33,7 +33,7 @@ public class OSGiServiceDiscoveryActivator implements BundleActivator { if (bundleContext == null) { bundleContext = context; OSGiServiceDiscoverer discoverer = new OSGiServiceDiscoverer(bundleContext); - ServiceDiscovery.setServiceDiscoverer(discoverer); + ServiceDiscovery.getInstance().setServiceDiscoverer(discoverer); } } diff --git a/java/sca/modules/extensibility-osgi/src/test/java/org/apache/tuscany/sca/extensibility/osgi/OSGiServiceDiscovererTestCase.java b/java/sca/modules/extensibility-osgi/src/test/java/org/apache/tuscany/sca/extensibility/osgi/OSGiServiceDiscovererTestCase.java index 8a9b8d485c..472cfbc17c 100644 --- a/java/sca/modules/extensibility-osgi/src/test/java/org/apache/tuscany/sca/extensibility/osgi/OSGiServiceDiscovererTestCase.java +++ b/java/sca/modules/extensibility-osgi/src/test/java/org/apache/tuscany/sca/extensibility/osgi/OSGiServiceDiscovererTestCase.java @@ -19,8 +19,12 @@ package org.apache.tuscany.sca.extensibility.osgi; +import java.io.IOException; import java.io.InputStream; +import java.net.URL; import java.util.ArrayList; +import java.util.Collections; +import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -109,4 +113,11 @@ public class OSGiServiceDiscovererTestCase { descriptors = discoverer.discover("notthere", true); Assert.assertEquals(0, descriptors.size()); } + + @Test + public void testClassLoader () throws IOException { + Enumeration resources = discoverer.getContextClassLoader().getResources("META-INF/services/org.apache.tuscany.sca.endpointresolver.EndpointResolverFactory"); + List list = Collections.list(resources); + Assert.assertEquals(1, list.size()); + } } 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 2c2016d7e3..8ad7009f47 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 @@ -38,9 +38,9 @@ import java.util.logging.Logger; public class ServiceDiscovery { private static final Logger logger = Logger.getLogger(ServiceDiscovery.class.getName()); - private final static ServiceDiscovery instance = new ServiceDiscovery(); + private final static ServiceDiscovery INSTANCE = new ServiceDiscovery(); - private static ServiceDiscoverer discoverer; + private ServiceDiscoverer discoverer; private Set registeredClassLoaders = new HashSet(); /** @@ -50,17 +50,28 @@ public class ServiceDiscovery { * @return */ public static ServiceDiscovery getInstance() { - return instance; + return INSTANCE; } - public static ServiceDiscoverer getServiceDiscoverer() { + /** + * 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() { if (discoverer == null) { discoverer = new ClasspathServiceDiscoverer(); } return discoverer; } - public static void setServiceDiscoverer(ServiceDiscoverer sd) { + public void setServiceDiscoverer(ServiceDiscoverer sd) { if (discoverer != null) { throw new IllegalStateException("The ServiceDiscoverer cannot be reset"); } 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 46b1ebb808..effe9b298d 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 @@ -31,7 +31,9 @@ import org.osgi.framework.BundleContext; * */ public class EquinoxOSGiHost implements OSGiHost { - + private LauncherBundleActivator activator = new LauncherBundleActivator(); + private BundleContext context; + 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, " @@ -92,15 +94,22 @@ public class EquinoxOSGiHost implements OSGiHost { String args[] = {}; Map props = new HashMap(); props.put("org.osgi.framework.system.packages", systemPackages); + // Set the extension bundle + // props.put("osgi.framework.extensions", "org.apache.tuscany.sca.extensibility.equinox"); props.put(EclipseStarter.PROP_CLEAN, "true"); props.put(LocationManager.PROP_INSTANCE_AREA, new File("target/workspace").toURI().toString()); - props.put(LocationManager.PROP_INSTALL_AREA, new File("target/eclipse").toURI().toString()); + 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); - BundleContext context = EclipseStarter.startup(args, null); + context = EclipseStarter.startup(args, null); + activator.start(context); return context; } private void shutdown() throws Exception { + activator.stop(context); EclipseStarter.shutdown(); } 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 2a5af89171..778eb0f690 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 @@ -142,7 +142,8 @@ public class LauncherBundleActivator implements BundleActivator, Constants, Bund for (URL url : urls) { File file = new File(url.toURI()); - if (file.getName().startsWith("org.apache.felix.") || file.getName().startsWith("org.osgi.")) { + if (file.getName().startsWith("org.apache.felix.") || file.getName().startsWith("osgi-") + || file.getName().startsWith("org.osgi.")) { continue; } try { @@ -205,6 +206,12 @@ public class LauncherBundleActivator implements BundleActivator, Constants, Bund } String symbolicName = manifest.getMainAttributes().getValue(BUNDLE_SYMBOLICNAME); + if (symbolicName != null) { + int index = symbolicName.indexOf(';'); + if (index != -1) { + symbolicName = symbolicName.substring(0, index); + } + } String version = manifest.getMainAttributes().getValue(BUNDLE_VERSION); Bundle bundle = findBundle(bundleContext, symbolicName, version); if (bundle != null) { diff --git a/java/sca/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiBundleActivator.java b/java/sca/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiBundleActivator.java index e0e520c368..b87f70deef 100644 --- a/java/sca/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiBundleActivator.java +++ b/java/sca/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiBundleActivator.java @@ -79,7 +79,7 @@ public class OSGiBundleActivator implements BundleActivator, BundleListener { private void initializeTuscanyClassLoaders(BundleContext bundleContext) { OSGiServiceDiscoverer discoverer = new OSGiServiceDiscoverer(bundleContext); - ServiceDiscovery.setServiceDiscoverer(discoverer); + ServiceDiscovery.getInstance().setServiceDiscoverer(discoverer); thisBundle = bundleContext.getBundle(); origTCCL = Thread.currentThread().getContextClassLoader(); -- cgit v1.2.3