diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2010-02-13 01:19:32 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2010-02-13 01:19:32 +0000 |
commit | c11d800db46ab42e99d7837427dd07dccaffc66a (patch) | |
tree | 3f2c5ec0e5f0a6f6e46c2a01bad010450ecaa80e /sca-java-2.x/trunk/modules/node-api/src/main/java/org/apache | |
parent | 788e5e52292aad11f345c247b418386138976cea (diff) |
Start to make extension points/extensions configurable from NodeFactory/ServiceDiscovery
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@909678 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/modules/node-api/src/main/java/org/apache')
-rw-r--r-- | sca-java-2.x/trunk/modules/node-api/src/main/java/org/apache/tuscany/sca/node/NodeFactory.java | 58 |
1 files changed, 42 insertions, 16 deletions
diff --git a/sca-java-2.x/trunk/modules/node-api/src/main/java/org/apache/tuscany/sca/node/NodeFactory.java b/sca-java-2.x/trunk/modules/node-api/src/main/java/org/apache/tuscany/sca/node/NodeFactory.java index 88d62e5ac8..22f021d298 100644 --- a/sca-java-2.x/trunk/modules/node-api/src/main/java/org/apache/tuscany/sca/node/NodeFactory.java +++ b/sca-java-2.x/trunk/modules/node-api/src/main/java/org/apache/tuscany/sca/node/NodeFactory.java @@ -32,6 +32,7 @@ import java.net.URLConnection; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Map; import org.apache.tuscany.sca.node.configuration.DefaultNodeConfigurationFactory; import org.apache.tuscany.sca.node.configuration.NodeConfiguration; @@ -57,9 +58,10 @@ public abstract class NodeFactory extends DefaultNodeConfigurationFactory { private static final String SCA_CONTRIBUTION_GENERATED_META = "META-INF/sca-contribution-generated.xml"; protected static NodeFactory instance; + protected static Class<?> factoryImplClass; protected static void setNodeFactory(NodeFactory factory) { - NodeFactory.instance = factory; + instance = factory; } public static class NodeProxy implements Node { @@ -151,11 +153,11 @@ public abstract class NodeFactory extends DefaultNodeConfigurationFactory { * * @return the SCA node factory */ - public static NodeFactory getInstance() { - if (NodeFactory.instance == null) { - NodeFactory.instance = newInstance(); + public synchronized static NodeFactory getInstance() { + if (instance == null) { + instance = newInstance(); } - return NodeFactory.instance; + return instance; } /** @@ -166,34 +168,58 @@ public abstract class NodeFactory extends DefaultNodeConfigurationFactory { public static NodeFactory newInstance() { NodeFactory nodeFactory = null; try { + Class<?> factoryClass = getFactoryImplClass(); + nodeFactory = (NodeFactory)factoryClass.newInstance(); + + } catch (Exception e) { + throw new ServiceRuntimeException(e); + } + return nodeFactory; + } + + public static NodeFactory newInstance(Map<String, Map<String, String>> attributes) { + NodeFactory nodeFactory = null; + try { + Class<?> factoryClass = getFactoryImplClass(); + nodeFactory = (NodeFactory)factoryClass.newInstance(); + nodeFactory.configure(attributes); + } catch (Exception e) { + throw new ServiceRuntimeException(e); + } + return nodeFactory; + } + + /** + * Configure the NodeFactory instance + * @param attributes + */ + protected void configure(Map<String, Map<String, String>> attributes) { + } + + private synchronized static Class<?> getFactoryImplClass() throws Exception { + if (factoryImplClass == null) { // Use reflection APIs to call ServiceDiscovery to avoid hard dependency to tuscany-extensibility try { Class<?> discoveryClass = Class.forName("org.apache.tuscany.sca.extensibility.ServiceDiscovery"); Object instance = discoveryClass.getMethod("getInstance").invoke(null); Object factoryDeclaration = - discoveryClass.getMethod("getServiceDeclaration", Class.class).invoke(instance, - NodeFactory.class); + discoveryClass.getMethod("getServiceDeclaration", Class.class).invoke(instance, NodeFactory.class); if (factoryDeclaration != null) { - Class<?> factoryImplClass = + factoryImplClass = (Class<?>)factoryDeclaration.getClass().getMethod("loadClass").invoke(factoryDeclaration); - nodeFactory = (NodeFactory)factoryImplClass.newInstance(); } } catch (ClassNotFoundException e) { // Ignore } - if (nodeFactory == null) { + if (factoryImplClass == null) { // Fail back to default impl String className = "org.apache.tuscany.sca.node.impl.NodeFactoryImpl"; - Class<?> cls = Class.forName(className); - nodeFactory = (NodeFactory)cls.newInstance(); + factoryImplClass = Class.forName(className); } - - } catch (Exception e) { - throw new ServiceRuntimeException(e); } - return nodeFactory; + return factoryImplClass; } /** |