diff options
author | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2010-01-18 12:00:21 +0000 |
---|---|---|
committer | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2010-01-18 12:00:21 +0000 |
commit | c0b480ce1460f90294bd2b4e95573c0e7ae64118 (patch) | |
tree | c34c364b81a9962ab623f311c54c85e238f45d33 /sca-java-2.x/trunk/modules/node-api/src/main/java/org | |
parent | 852025a434345de1a4fada20943282a78c905869 (diff) |
Change Node getInstance/newInstance methods so that newInstance really creates a new instance, and getInstance returns the singleton instance
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@900354 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/modules/node-api/src/main/java/org')
-rw-r--r-- | sca-java-2.x/trunk/modules/node-api/src/main/java/org/apache/tuscany/sca/node/NodeFactory.java | 95 |
1 files changed, 39 insertions, 56 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 f650268c73..defb607b16 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 @@ -31,10 +31,7 @@ import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import java.util.Map.Entry; import org.apache.tuscany.sca.node.configuration.DefaultNodeConfigurationFactory; import org.apache.tuscany.sca.node.configuration.NodeConfiguration; @@ -59,13 +56,10 @@ public abstract class NodeFactory extends DefaultNodeConfigurationFactory { */ private static final String SCA_CONTRIBUTION_GENERATED_META = "META-INF/sca-contribution-generated.xml"; - protected static Map<String, NodeFactory> nodeFactories = new HashMap<String, NodeFactory>(); + protected static NodeFactory instance; protected static void setNodeFactory(NodeFactory factory) { -// if (nodeFactories.get(Node.DEFAULT_DOMAIN_URI) != null) { -// throw new IllegalStateException(); -// } - nodeFactories.put(Node.DEFAULT_DOMAIN_URI, factory); + NodeFactory.instance = factory; } public static class NodeProxy implements Node, Client { @@ -162,44 +156,15 @@ public abstract class NodeFactory extends DefaultNodeConfigurationFactory { } /** - * Returns a new SCA node factory instance. + * Returns the SCA node factory instance. * - * @return a new SCA node factory + * @return the SCA node factory */ - public static NodeFactory getInstance(String domainURI) { - NodeFactory nodeFactory = nodeFactories.get(domainURI); - if (nodeFactory == null) { - try { - // 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); - if (factoryDeclaration != null) { - Class<?> factoryImplClass = - (Class<?>)factoryDeclaration.getClass().getMethod("loadClass").invoke(factoryDeclaration); - nodeFactory = (NodeFactory)factoryImplClass.newInstance(); - } - } catch (ClassNotFoundException e) { - // Ignore - } - - if (nodeFactory == null) { - // Fail back to default impl - String className = "org.apache.tuscany.sca.node.impl.NodeFactoryImpl"; - - Class<?> cls = Class.forName(className); - nodeFactory = (NodeFactory)cls.newInstance(); - } - - } catch (Exception e) { - throw new ServiceRuntimeException(e); - } - nodeFactories.put(domainURI, nodeFactory); + public static NodeFactory getInstance() { + if (NodeFactory.instance == null) { + NodeFactory.instance = newInstance(); } - return nodeFactory; + return NodeFactory.instance; } /** @@ -208,7 +173,36 @@ public abstract class NodeFactory extends DefaultNodeConfigurationFactory { * @return a new SCA node factory */ public static NodeFactory newInstance() { - return getInstance(Node.DEFAULT_DOMAIN_URI); + NodeFactory nodeFactory = null; + try { + // 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); + if (factoryDeclaration != null) { + Class<?> factoryImplClass = + (Class<?>)factoryDeclaration.getClass().getMethod("loadClass").invoke(factoryDeclaration); + nodeFactory = (NodeFactory)factoryImplClass.newInstance(); + } + } catch (ClassNotFoundException e) { + // Ignore + } + + if (nodeFactory == null) { + // Fail back to default impl + String className = "org.apache.tuscany.sca.node.impl.NodeFactoryImpl"; + + Class<?> cls = Class.forName(className); + nodeFactory = (NodeFactory)cls.newInstance(); + } + + } catch (Exception e) { + throw new ServiceRuntimeException(e); + } + return nodeFactory; } /** @@ -415,7 +409,6 @@ public abstract class NodeFactory extends DefaultNodeConfigurationFactory { private NodeConfiguration createConfiguration(Contribution... contributions) { NodeConfigurationFactory factory = this; NodeConfiguration configuration = factory.createNodeConfiguration(); - configuration.setDomainURI(getDomainURI()); // Make sure a unique node URI is created for the same node factory configuration.setURI(Node.DEFAULT_NODE_URI+(count++)); if (contributions != null) { @@ -447,18 +440,8 @@ public abstract class NodeFactory extends DefaultNodeConfigurationFactory { } public void destroy() { - nodeFactories.remove(getDomainURI()); - } - - public String getDomainURI() { - for (Entry<String, NodeFactory> es : nodeFactories.entrySet()) { - if (es.getValue().equals(this)) { - return es.getKey(); - } - } - return Node.DEFAULT_DOMAIN_URI; } - + /** * Create a new SCA node based on the configuration * @param configuration The configuration of a node |