diff options
author | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2008-09-19 05:31:14 +0000 |
---|---|---|
committer | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2008-09-19 05:31:14 +0000 |
commit | 6a633dee838417ed68cf91fbd2e9b3b9fa07bd8f (patch) | |
tree | 658d78b2087d2f851518be4d27ed9d913ac47ebb /branches/sca-equinox/modules/extensibility/src/main/java/org/apache | |
parent | bd126f649a92dd53cd3b3ccef5029ab273436291 (diff) |
Simplified ServiceDiscoverer. Started to use it to create factories to resolve ClassNotFoundExceptions. Renamed calculator-osgi to calculator-equinox and a few fixes to get it working in Eclipse.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@696924 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'branches/sca-equinox/modules/extensibility/src/main/java/org/apache')
7 files changed, 79 insertions, 121 deletions
diff --git a/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/DefaultExtensionPointRegistry.java b/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/DefaultExtensionPointRegistry.java index 48f240559c..12979723b1 100644 --- a/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/DefaultExtensionPointRegistry.java +++ b/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/DefaultExtensionPointRegistry.java @@ -28,6 +28,7 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import org.apache.tuscany.sca.extensibility.ServiceDeclaration; import org.apache.tuscany.sca.extensibility.ServiceDiscovery; /** @@ -103,9 +104,10 @@ public class DefaultExtensionPointRegistry implements ExtensionPointRegistry { // Dynamically load an extension point class declared under META-INF/services try { - Class<?> extensionPointClass = - ServiceDiscovery.getInstance().loadFirstServiceClass(extensionPointType); - if (extensionPointClass != null) { + ServiceDeclaration extensionPointDeclaration = ServiceDiscovery.getInstance().getFirstServiceDeclaration(extensionPointType.getName()); + if (extensionPointDeclaration != null) { + Class<?> extensionPointClass = extensionPointDeclaration.loadClass(); + // Construct the extension point Constructor<?>[] constructors = extensionPointClass.getConstructors(); Constructor<?> constructor = getConstructor(constructors, new Class<?>[] {ExtensionPointRegistry.class}); diff --git a/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/DefaultModuleActivatorExtensionPoint.java b/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/DefaultModuleActivatorExtensionPoint.java index 8ca030c0dd..d84f28f11e 100644 --- a/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/DefaultModuleActivatorExtensionPoint.java +++ b/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/DefaultModuleActivatorExtensionPoint.java @@ -65,7 +65,7 @@ public class DefaultModuleActivatorExtensionPoint implements ModuleActivatorExte // Get the activator service declarations Set<ServiceDeclaration> activatorDeclarations; try { - activatorDeclarations = ServiceDiscovery.getInstance().getServiceDeclarations(ModuleActivator.class); + activatorDeclarations = ServiceDiscovery.getInstance().getServiceDeclarations(ModuleActivator.class.getName()); } catch (IOException e) { throw new IllegalStateException(e); } diff --git a/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/DefaultUtilityExtensionPoint.java b/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/DefaultUtilityExtensionPoint.java index 6f7c83c8d9..9906bef909 100644 --- a/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/DefaultUtilityExtensionPoint.java +++ b/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/DefaultUtilityExtensionPoint.java @@ -28,6 +28,7 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import org.apache.tuscany.sca.extensibility.ServiceDeclaration; import org.apache.tuscany.sca.extensibility.ServiceDiscovery; /** @@ -103,9 +104,10 @@ public class DefaultUtilityExtensionPoint implements UtilityExtensionPoint { // Dynamically load a utility class declared under META-INF/utilities try { - Class<?> utilityClass = - ServiceDiscovery.getInstance().loadFirstServiceClass(utilityType); - if (utilityClass != null) { + ServiceDeclaration utilityDeclaration =ServiceDiscovery.getInstance().getFirstServiceDeclaration(utilityType.getName()); + if (utilityDeclaration != null) { + Class<?> utilityClass = utilityDeclaration.loadClass(); + // Construct the utility Constructor<?>[] constructors = utilityClass.getConstructors(); Constructor<?> constructor = getConstructor(constructors, new Class<?>[] {ExtensionPointRegistry.class}); diff --git a/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ContextClassLoaderServiceDiscoverer.java b/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ContextClassLoaderServiceDiscoverer.java index 2e11234095..0f408600aa 100644 --- a/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ContextClassLoaderServiceDiscoverer.java +++ b/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ContextClassLoaderServiceDiscoverer.java @@ -24,13 +24,14 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.ref.WeakReference; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.net.URL; import java.net.URLConnection; import java.security.AccessController; import java.security.PrivilegedAction; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; -import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -85,7 +86,7 @@ public class ContextClassLoaderServiceDiscoverer implements ServiceDiscoverer { } public Class<?> loadClass(String className) throws ClassNotFoundException { - return classLoaderReference.get().loadClass(className); + return Class.forName(className, false, classLoaderReference.get()); } public String toString() { @@ -112,21 +113,12 @@ public class ContextClassLoaderServiceDiscoverer implements ServiceDiscoverer { this.classLoaderReference = new WeakReference<ClassLoader>(classLoader); } - private List<URL> getResources(final String name, final boolean firstOnly) throws IOException { + private List<URL> getResources(final String name) throws IOException { try { return AccessController.doPrivileged(new PrivilegedExceptionAction<List<URL>>() { public List<URL> run() throws IOException { - if (firstOnly) { - URL url = classLoaderReference.get().getResource(name); - if (url != null) { - return Arrays.asList(url); - } else { - return Collections.emptyList(); - } - } else { - List<URL> urls = Collections.list(classLoaderReference.get().getResources(name)); - return urls; - } + List<URL> urls = Collections.list(classLoaderReference.get().getResources(name)); + return urls; } }); } catch (PrivilegedActionException e) { @@ -169,13 +161,22 @@ public class ContextClassLoaderServiceDiscoverer implements ServiceDiscoverer { return attributes; } - public Set<ServiceDeclaration> discover(String serviceName, boolean firstOnly) { + public ServiceDeclaration getFirstServiceDeclaration(String name) throws IOException { + Set<ServiceDeclaration> declarations = getServiceDeclarations(name); + if (declarations.isEmpty()) { + return null; + } else { + return declarations.iterator().next(); + } + } + + public Set<ServiceDeclaration> getServiceDeclarations(String serviceName) { Set<ServiceDeclaration> descriptors = new HashSet<ServiceDeclaration>(); String name = "META-INF/services/" + serviceName; boolean debug = logger.isLoggable(Level.FINE); try { - for (final URL url : getResources(name, firstOnly)) { + for (final URL url : getResources(name)) { if (debug) { logger.fine("Reading service provider file: " + url.toExternalForm()); } @@ -223,9 +224,6 @@ public class ContextClassLoaderServiceDiscoverer implements ServiceDiscoverer { } ServiceDeclarationImpl descriptor = new ServiceDeclarationImpl(url, className, attributes); descriptors.add(descriptor); - if (firstOnly) { - return descriptors; - } } } } finally { @@ -245,4 +243,11 @@ public class ContextClassLoaderServiceDiscoverer implements ServiceDiscoverer { } + public Object newFactoryClassInstance(String name) throws SecurityException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException { + Class<?> factoryClass = Class.forName(name, false, classLoaderReference.get()); + Method newInstanceMethod = factoryClass.getMethod("newInstance"); + Object factory = newInstanceMethod.invoke(null); + return factory; + } + } diff --git a/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDeclaration.java b/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDeclaration.java index 403b6aa7a7..d7ac8b449f 100644 --- a/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDeclaration.java +++ b/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDeclaration.java @@ -37,11 +37,13 @@ public interface ServiceDeclaration { * @throws ClassNotFoundException */ Class<?> loadClass(String className) throws ClassNotFoundException; + /** * Get the java class for the service impl * @return The java class */ Class<?> loadClass() throws ClassNotFoundException; + /** * Get all attributes (name=value pairs) defined for the given entry * @return All attributes keyed by name diff --git a/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscoverer.java b/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscoverer.java index 1722cd5f49..679e3d1f57 100644 --- a/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscoverer.java +++ b/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscoverer.java @@ -19,19 +19,44 @@ package org.apache.tuscany.sca.extensibility; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.util.Set; /** * A SPI that allows different implementations of discovering service declarations */ public interface ServiceDiscoverer { + /** - * Discover the service descriptors by name - * @param serviceName The name of the service - * @param firstOnly A flag to indicate if only the first instance is to be discovered - * - * @return A set of service descriptors + * Get all service declarations for this interface + * + * @param name + * @return set of service declarations + * @throws IOException */ - Set<ServiceDeclaration> discover(String serviceName, boolean firstOnly); - + public Set<ServiceDeclaration> getServiceDeclarations(String name) throws IOException; + + /** + * Get first service declaration class for the given interface + * + * @param name + * @return service implementation class + * @throws IOException + * @throws ClassNotFoundException + */ + public ServiceDeclaration getFirstServiceDeclaration(String name) throws IOException; + + /** + * Create a new instance of a factory service class. + * + * @param name + * @return service implementation class + * @throws SecurityException + * @throws NoSuchMethodException + * @throws InvocationTargetException + * @throws IllegalAccessException + */ + public Object newFactoryClassInstance(String name) throws SecurityException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException; + } diff --git a/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java b/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java index ce41a44068..552691ab6b 100644 --- a/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java +++ b/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java @@ -21,12 +21,9 @@ package org.apache.tuscany.sca.extensibility; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.security.AccessController; import java.security.PrivilegedAction; -import java.util.HashSet; import java.util.Set; -import java.util.logging.Level; import java.util.logging.Logger; /** @@ -37,13 +34,12 @@ import java.util.logging.Logger; * * @version $Rev$ $Date$ */ -public class ServiceDiscovery { +public class ServiceDiscovery implements ServiceDiscoverer { private static final Logger logger = Logger.getLogger(ServiceDiscovery.class.getName()); private final static ServiceDiscovery INSTANCE = new ServiceDiscovery(); private ServiceDiscoverer discoverer; - private Set<ClassLoader> registeredClassLoaders = new HashSet<ClassLoader>(); /** * Get an instance of Service discovery, one instance is created per @@ -69,101 +65,27 @@ public class ServiceDiscovery { discoverer = sd; } - /** - * @deprecated - * Register a ClassLoader with this discovery mechanism. Tuscany extension - * ClassLoaders are registered here. - * - * @param classLoader - */ - @Deprecated - public synchronized void registerClassLoader(ClassLoader classLoader) { - registeredClassLoaders.add(classLoader); - } - - /** - * @deprecated - * Unregister a ClassLoader with this discovery mechanism. - * - * @param classLoader - */ - @Deprecated - public synchronized void unregisterClassLoader(ClassLoader classLoader) { - registeredClassLoaders.remove(classLoader); - } - - /** - * Get all service declarations for this name - * - * @param name - * @return set of service declarations - * @throws IOException - */ public Set<ServiceDeclaration> getServiceDeclarations(String name) throws IOException { - Set<ServiceDeclaration> services = getServiceDiscoverer().discover(name, false); + Set<ServiceDeclaration> services = getServiceDiscoverer().getServiceDeclarations(name); return services; } - /** - * Get all service declarations for this interface - * - * @param serviceInterface - * @return set of service declarations - * @throws IOException - */ - public Set<ServiceDeclaration> getServiceDeclarations(Class<?> serviceInterface) throws IOException { - - return getServiceDeclarations(serviceInterface.getName()); - } - - /** - * Load one service implementation class for this interface - * - * @param serviceInterface - * @return service implementation class - * @throws IOException - * @throws ClassNotFoundException - */ - public Class<?> loadFirstServiceClass(final Class<?> serviceInterface) throws IOException, ClassNotFoundException { + public ServiceDeclaration getFirstServiceDeclaration(final String name) throws IOException { // Try System property first String className = AccessController.doPrivileged(new PrivilegedAction<String>() { public String run() { - return System.getProperty(serviceInterface.getName()); + return System.getProperty(name); } }); - if (className != null) { - try { - // Try the classloader for the service interface first - return Class.forName(className, false, serviceInterface.getClassLoader()); - } catch (ClassNotFoundException e) { - try { - // Try the thread context classloader - return Class.forName(className, false, Thread.currentThread().getContextClassLoader()); - } catch (ClassNotFoundException ex) { - logger.log(Level.WARNING, ex.getMessage(), ex); - } - } - } - Set<ServiceDeclaration> services = getServiceDiscoverer().discover(serviceInterface.getName(), true); - if (services.isEmpty()) { - return null; + if (className == null) { + className = name; } - return services.iterator().next().loadClass(); + ServiceDeclaration service = getServiceDiscoverer().getFirstServiceDeclaration(className); + return service; } - /** - * Create a new instance of a factory service class. - * - * @param serviceInterface - * @return service implementation class - * @throws SecurityException - * @throws NoSuchMethodException - * @throws InvocationTargetException - * @throws IllegalAccessException - */ - public Object newFactoryClassInstance(final Class<?> serviceInterface) throws SecurityException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Method newInstanceMethod = serviceInterface.getMethod("newInstance"); - Object factory = newInstanceMethod.invoke(null); + public Object newFactoryClassInstance(String name) throws SecurityException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException { + Object factory = getServiceDiscoverer().newFactoryClassInstance(name); return factory; } |