diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2008-10-24 22:51:06 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2008-10-24 22:51:06 +0000 |
commit | 8bec3f9b64a649ca3cc6e7f458e6a2242841b44f (patch) | |
tree | 6ee41bdd4ab2be1c34fae4615187f674eddd4e91 /branches/sca-equinox/modules/extensibility/src/main | |
parent | 425fe3be953a92eccbd767c5ee79387cdc2bbbc4 (diff) |
Fix the newFactoryClassInstance and bring up the sample-calculator-equinox
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@707774 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'branches/sca-equinox/modules/extensibility/src/main')
4 files changed, 36 insertions, 57 deletions
diff --git a/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/DefaultFactoryExtensionPoint.java b/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/DefaultFactoryExtensionPoint.java index 1d0da12f91..c1221aad7a 100644 --- a/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/DefaultFactoryExtensionPoint.java +++ b/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/DefaultFactoryExtensionPoint.java @@ -33,13 +33,14 @@ import org.apache.tuscany.sca.extensibility.ServiceDiscovery; * @version $Rev$ $Date$ */ public class DefaultFactoryExtensionPoint implements FactoryExtensionPoint { - + private ExtensionPointRegistry extensionPointRegistry; private HashMap<Class<?>, Object> factories = new HashMap<Class<?>, Object>(); /** * Constructs a new DefaultModelFactoryExtensionPoint. */ - public DefaultFactoryExtensionPoint() { + public DefaultFactoryExtensionPoint(ExtensionPointRegistry extensionPointRegistry) { + this.extensionPointRegistry = extensionPointRegistry; } /** @@ -88,43 +89,35 @@ public class DefaultFactoryExtensionPoint implements FactoryExtensionPoint { public <T> T getFactory(Class<T> factoryInterface) { Object factory = factories.get(factoryInterface); if (factory == null) { - - if (factoryInterface.isInterface()) { - - // Dynamically load a factory class declared under META-INF/services - try { - ServiceDeclaration factoryDeclaration = ServiceDiscovery.getInstance().getFirstServiceDeclaration(factoryInterface.getName()); - if (factoryDeclaration != null) { - Class<?> factoryClass = factoryDeclaration.loadClass(); - + + // Dynamically load a factory class declared under META-INF/services + try { + ServiceDeclaration factoryDeclaration = + ServiceDiscovery.getInstance().getFirstServiceDeclaration(factoryInterface.getName()); + if (factoryDeclaration != null) { + Class<?> factoryClass = factoryDeclaration.loadClass(); + + try { + // Default empty constructor + Constructor<?> constructor = factoryClass.getConstructor(); + factory = constructor.newInstance(); + } catch (NoSuchMethodException e) { try { - // Default empty constructor - Constructor<?> constructor = factoryClass.getConstructor(); - factory = constructor.newInstance(); - } catch (NoSuchMethodException e) { - // Constructor taking the model factory extension point Constructor<?> constructor = factoryClass.getConstructor(FactoryExtensionPoint.class); factory = constructor.newInstance(this); + } catch (NoSuchMethodException e1) { + // Constructor taking the extension point registry + Constructor<?> constructor = factoryClass.getConstructor(ExtensionPointRegistry.class); + factory = constructor.newInstance(extensionPointRegistry); } - - // Cache the loaded factory - addFactory(factory); } - } catch (Exception e) { - throw new IllegalArgumentException(e); - } - } else { - // Call the newInstance static method on the factory abstract class - try { - factory = ServiceDiscovery.getInstance().newFactoryClassInstance(factoryInterface.getName()); - } catch (Exception e) { - throw new IllegalArgumentException(e); + // Cache the loaded factory + addFactory(factory); } - - // Cache the factory - addFactory(factory); + } catch (Exception e) { + throw new IllegalArgumentException(e); } } return factoryInterface.cast(factory); 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 0f408600aa..7d9dc0e9ef 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,8 +24,6 @@ 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; @@ -45,7 +43,7 @@ import java.util.logging.Logger; /** * A ServiceDiscoverer that find META-INF/services/... using the Context ClassLoader. * - * @version $Rev: $ $Date: $ + * @version $Rev$ $Date$ */ public class ContextClassLoaderServiceDiscoverer implements ServiceDiscoverer { private static final Logger logger = Logger.getLogger(ContextClassLoaderServiceDiscoverer.class.getName()); @@ -243,11 +241,4 @@ 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/ServiceDiscoverer.java b/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscoverer.java index 679e3d1f57..f89a25d199 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 @@ -47,16 +47,4 @@ public interface ServiceDiscoverer { */ 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 552691ab6b..e78cb136d3 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 @@ -20,7 +20,6 @@ package org.apache.tuscany.sca.extensibility; import java.io.IOException; -import java.lang.reflect.InvocationTargetException; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.Set; @@ -84,9 +83,17 @@ public class ServiceDiscovery implements ServiceDiscoverer { return service; } - public Object newFactoryClassInstance(String name) throws SecurityException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException { - Object factory = getServiceDiscoverer().newFactoryClassInstance(name); - return factory; + public Object newFactoryClassInstance(String name) { + try { + ServiceDeclaration declaration = getFirstServiceDeclaration(name); + if (declaration == null) { + return null; + } + Class<?> factoryClass = declaration.loadClass(); + return factoryClass.newInstance(); + } catch (Exception e) { + throw new IllegalArgumentException(e); + } } } |