diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2008-10-27 16:38:16 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2008-10-27 16:38:16 +0000 |
commit | 52ce7df1c3f1076410836b444d95b9da2eb14c28 (patch) | |
tree | e61ddc82957299116f4b02f0fe54be0b379fb695 /branches/sca-equinox/modules/extensibility/src | |
parent | ced88ce66721126b9d6e4daf0c3325306284ec7f (diff) |
Bring up calculator-osgi using equinox run config
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@708234 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'branches/sca-equinox/modules/extensibility/src')
2 files changed, 54 insertions, 5 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 c1221aad7a..f5041866ee 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 @@ -20,6 +20,10 @@ package org.apache.tuscany.sca.core; import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.security.AccessController; +import java.security.PrivilegedAction; import java.util.HashMap; import org.apache.tuscany.sca.extensibility.ServiceDeclaration; @@ -81,6 +85,24 @@ public class DefaultFactoryExtensionPoint implements FactoryExtensionPoint { } } + private ClassLoader getContextClassLoader() { + return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { + public ClassLoader run() { + return Thread.currentThread().getContextClassLoader(); + } + }); + } + + private ClassLoader setContextClassLoader(final ClassLoader classLoader) { + return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { + public ClassLoader run() { + ClassLoader tccl = Thread.currentThread().getContextClassLoader(); + Thread.currentThread().setContextClassLoader(classLoader); + return tccl; + } + }); + } + /** * Get a factory implementing the given interface. * @param factoryInterface The lookup key (factory interface) @@ -96,8 +118,22 @@ public class DefaultFactoryExtensionPoint implements FactoryExtensionPoint { ServiceDiscovery.getInstance().getFirstServiceDeclaration(factoryInterface.getName()); if (factoryDeclaration != null) { Class<?> factoryClass = factoryDeclaration.loadClass(); - try { + if (!factoryInterface.isInterface() && Modifier.isAbstract(factoryInterface.getModifiers())) { + try { + Method newInstanceMethod = factoryInterface.getDeclaredMethod("newInstance"); + ClassLoader tccl = setContextClassLoader(factoryClass.getClassLoader()); + try { + factory = newInstanceMethod.invoke(null); + factories.put(factoryInterface, factory); + return factoryInterface.cast(factory); + } finally { + setContextClassLoader(tccl); + } + } catch (NoSuchMethodException e) { + // Ignore + } + } // Default empty constructor Constructor<?> constructor = factoryClass.getConstructor(); factory = constructor.newInstance(); @@ -114,7 +150,7 @@ public class DefaultFactoryExtensionPoint implements FactoryExtensionPoint { } // Cache the loaded factory - addFactory(factory); + factories.put(factoryInterface, factory); } } catch (Exception e) { throw new IllegalArgumentException(e); 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 ff8f2399df..bb4f23a00c 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 @@ -37,7 +37,10 @@ public class ServiceDiscovery implements ServiceDiscoverer { private final static ServiceDiscovery INSTANCE = new ServiceDiscovery(); private ServiceDiscoverer discoverer; - + + private ServiceDiscovery() { + super(); + } /** * Get an instance of Service discovery, one instance is created per * ClassLoader that this class is loaded from @@ -49,9 +52,19 @@ public class ServiceDiscovery implements ServiceDiscoverer { } public ServiceDiscoverer getServiceDiscoverer() { - if (discoverer == null) { - discoverer = new ContextClassLoaderServiceDiscoverer(); + if (discoverer != null) { + return discoverer; + } + try { + Class<?> cls = Class.forName("org.apache.tuscany.sca.extensibility.equinox.EquinoxServiceDiscoverer"); + System.out.println(cls); + if (discoverer != null) { + return discoverer; + } + } catch (Throwable e) { + e.printStackTrace(); } + discoverer = new ContextClassLoaderServiceDiscoverer(); return discoverer; } |