diff options
8 files changed, 158 insertions, 26 deletions
diff --git a/sca-java-2.x/trunk/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadDocumentTestCase.java b/sca-java-2.x/trunk/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadDocumentTestCase.java index b71764e0a4..473e483d31 100644 --- a/sca-java-2.x/trunk/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadDocumentTestCase.java +++ b/sca-java-2.x/trunk/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadDocumentTestCase.java @@ -24,6 +24,7 @@ import static org.junit.Assert.assertNotNull; import java.net.URI; import java.net.URL; +import java.util.HashMap; import javax.xml.XMLConstants; import javax.xml.parsers.SAXParserFactory; @@ -128,7 +129,7 @@ public class ReadDocumentTestCase { @Test public void testReadImplementation() throws Exception { ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); - ValidationSchemaExtensionPoint schemas = new DefaultValidationSchemaExtensionPoint(registry); + ValidationSchemaExtensionPoint schemas = new DefaultValidationSchemaExtensionPoint(registry, new HashMap<String, String>()); schemas.addSchema(getClass().getClassLoader().getResource(TUSCANY_11_XSD).toString()); XMLInputFactory validatingInputFactory = new DefaultValidatingXMLInputFactory(inputFactory, schemas); DefaultFactoryExtensionPoint factories = new DefaultFactoryExtensionPoint(registry); @@ -157,7 +158,7 @@ public class ReadDocumentTestCase { public void testReadBinding() throws Exception { ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); - ValidationSchemaExtensionPoint schemas = new DefaultValidationSchemaExtensionPoint(registry); + ValidationSchemaExtensionPoint schemas = new DefaultValidationSchemaExtensionPoint(registry, new HashMap<String, String>()); schemas.addSchema(getClass().getClassLoader().getResource(TUSCANY_11_XSD).toString()); XMLInputFactory validatingInputFactory = new DefaultValidatingXMLInputFactory(inputFactory, schemas); DefaultFactoryExtensionPoint factories = new DefaultFactoryExtensionPoint(registry); diff --git a/sca-java-2.x/trunk/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultValidationSchemaExtensionPoint.java b/sca-java-2.x/trunk/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultValidationSchemaExtensionPoint.java index da7a57a687..4cb83cd5ef 100644 --- a/sca-java-2.x/trunk/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultValidationSchemaExtensionPoint.java +++ b/sca-java-2.x/trunk/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultValidationSchemaExtensionPoint.java @@ -26,6 +26,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Map; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.extensibility.ServiceDeclaration; @@ -41,9 +42,15 @@ public class DefaultValidationSchemaExtensionPoint implements ValidationSchemaEx private boolean loaded; private ExtensionPointRegistry registry; - public DefaultValidationSchemaExtensionPoint(ExtensionPointRegistry registry) { + public DefaultValidationSchemaExtensionPoint(ExtensionPointRegistry registry, Map<String, String> attributes) { super(); this.registry = registry; + if (attributes != null) { + String attr = attributes.get("enabled"); + if (attr != null) { + enabled = Boolean.parseBoolean(attr); + } + } } public void addSchema(String uri) { diff --git a/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/DefaultDomainRegistryFactoryExtensionPoint.java b/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/DefaultDomainRegistryFactoryExtensionPoint.java index bd59103d59..f74d80b882 100644 --- a/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/DefaultDomainRegistryFactoryExtensionPoint.java +++ b/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/DefaultDomainRegistryFactoryExtensionPoint.java @@ -44,9 +44,13 @@ public class DefaultDomainRegistryFactoryExtensionPoint implements DomainRegistr /** * @param registry */ - public DefaultDomainRegistryFactoryExtensionPoint(ExtensionPointRegistry registry) { + public DefaultDomainRegistryFactoryExtensionPoint(ExtensionPointRegistry registry, Map<String, String> attributes) { super(); this.registry = registry; + // Populate the domainRegistryMapping + domainRegistryMapping.putAll(attributes); + domainRegistryMapping.remove("class"); + domainRegistryMapping.remove("ranking"); } public void addDomainRegistryFactory(DomainRegistryFactory factory) { diff --git a/sca-java-2.x/trunk/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java b/sca-java-2.x/trunk/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java index ea3cf6d74f..00a037b4ed 100644 --- a/sca-java-2.x/trunk/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java +++ b/sca-java-2.x/trunk/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java @@ -24,12 +24,13 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; +import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; -import org.apache.tuscany.sca.extensibility.impl.ClassLoaderDelegate; import org.apache.tuscany.sca.extensibility.impl.LDAPFilter; /** @@ -44,6 +45,7 @@ public final class ServiceDiscovery implements ServiceDiscoverer { private final static Logger logger = Logger.getLogger(ServiceDiscovery.class.getName()); private final static ServiceDiscovery INSTANCE = new ServiceDiscovery(); + private final Map<String, Map<String, String>> serviceAttributes = new HashMap<String, Map<String, String>>(); private ServiceDiscoverer discoverer; private ServiceDiscovery() { @@ -98,6 +100,15 @@ public final class ServiceDiscovery implements ServiceDiscoverer { public Collection<ServiceDeclaration> getServiceDeclarations(String name, boolean byRanking) throws IOException { Collection<ServiceDeclaration> declarations = getServiceDiscoverer().getServiceDeclarations(name); + // Check if any of the service declarations has attributes that are overrided + if (!serviceAttributes.isEmpty()) { + for (ServiceDeclaration declaration : declarations) { + Map<String, String> attrs = getAttributes(name); + if (attrs != null) { + declaration.getAttributes().putAll(attrs); + } + } + } if (!byRanking) { return declarations; } @@ -237,5 +248,37 @@ public final class ServiceDiscovery implements ServiceDiscoverer { public ClassLoader getContextClassLoader() { return discoverer.getContextClassLoader(); } + + /** + * Set the attributes for a given service type + * @param serviceType + * @param attributes + */ + public void setAttribute(String serviceType, Map<String, String> attributes) { + serviceAttributes.put(serviceType, attributes); + } + + /** + * Set an attribute to the given value for a service type + * @param serviceType The service type + * @param attribute The attribute name + * @param value The attribute value + */ + public void setAttribute(String serviceType, String attribute, String value) { + Map<String, String> attributes = serviceAttributes.get(serviceType); + if (attributes == null) { + attributes = new HashMap<String, String>(); + serviceAttributes.put(serviceType, attributes); + } + attributes.put(attribute, value); + } + /** + * Return a map of attributes for a given service type + * @param serviceType + * @return + */ + public Map<String, String> getAttributes(String serviceType) { + return serviceAttributes.get(serviceType); + } } diff --git a/sca-java-2.x/trunk/modules/extensibility/src/test/java/org/apache/tuscany/sca/extensibility/ServiceDiscoveryTestCase.java b/sca-java-2.x/trunk/modules/extensibility/src/test/java/org/apache/tuscany/sca/extensibility/ServiceDiscoveryTestCase.java index 7a2f680d16..e13a47cc1b 100644 --- a/sca-java-2.x/trunk/modules/extensibility/src/test/java/org/apache/tuscany/sca/extensibility/ServiceDiscoveryTestCase.java +++ b/sca-java-2.x/trunk/modules/extensibility/src/test/java/org/apache/tuscany/sca/extensibility/ServiceDiscoveryTestCase.java @@ -65,6 +65,20 @@ public class ServiceDiscoveryTestCase { Assert.assertEquals("org.apache.tuscany.sca.extensibility.test.Test2Impl", sd1.getClassName()); Assert.assertEquals("org.apache.tuscany.sca.extensibility.test.TestImpl", sd2.getClassName()); } + + @Test + /** + * Test if the external attributes override the one in the META-INF/services/<SPI> + */ + public void testAttributes() throws Exception { + ServiceDiscovery serviceDiscovery = ServiceDiscovery.getInstance(new ContextClassLoaderServiceDiscoverer()); + serviceDiscovery.setAttribute(TestInterface.class.getName(), "attr", "value"); + serviceDiscovery.setAttribute(TestInterface.class.getName(), "attr1", "value1"); + for (ServiceDeclaration sd : serviceDiscovery.getServiceDeclarations(TestInterface.class)) { + Assert.assertEquals("value1", sd.getAttributes().get("attr1")); + Assert.assertEquals("value", sd.getAttributes().get("attr")); + } + } @Test public void testFilter() throws Exception { 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; } /** diff --git a/sca-java-2.x/trunk/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java b/sca-java-2.x/trunk/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java index 32aaf91b23..20984a9d6b 100644 --- a/sca-java-2.x/trunk/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java +++ b/sca-java-2.x/trunk/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java @@ -67,6 +67,7 @@ import org.apache.tuscany.sca.core.invocation.ExtensibleProxyFactory; import org.apache.tuscany.sca.core.invocation.ProxyFactory; import org.apache.tuscany.sca.core.invocation.ProxyFactoryExtensionPoint; import org.apache.tuscany.sca.deployment.Deployer; +import org.apache.tuscany.sca.extensibility.ServiceDiscovery; import org.apache.tuscany.sca.monitor.Monitor; import org.apache.tuscany.sca.monitor.MonitorFactory; import org.apache.tuscany.sca.monitor.Problem; @@ -210,6 +211,11 @@ public class NodeFactoryImpl extends NodeFactory { } public ExtensionPointRegistry getExtensionPoints() { + if (registry == null) { + // Create extension point registry + registry = createExtensionPointRegistry(); + registry.start(); + } return registry; } @@ -219,11 +225,7 @@ public class NodeFactoryImpl extends NodeFactory { } long start = currentTimeMillis(); - if (registry == null) { - // Create extension point registry - registry = createExtensionPointRegistry(); - registry.start(); - } + getExtensionPoints(); // Use the runtime-enabled assembly factory FactoryExtensionPoint modelFactories = registry.getExtensionPoint(FactoryExtensionPoint.class); @@ -461,5 +463,14 @@ public class NodeFactoryImpl extends NodeFactory { return buf.toString(); } } + + @Override + protected void configure(Map<String, Map<String, String>> attributes) { + ServiceDiscovery discovery = getExtensionPoints().getServiceDiscovery(); + for (Map.Entry<String, Map<String, String>> e : attributes.entrySet()) { + discovery.setAttribute(e.getKey(), e.getValue()); + } + super.configure(attributes); + } } diff --git a/sca-java-2.x/trunk/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java b/sca-java-2.x/trunk/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java index 3fff10328f..f0e6675e8d 100644 --- a/sca-java-2.x/trunk/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java +++ b/sca-java-2.x/trunk/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java @@ -25,12 +25,16 @@ import java.io.File; import java.io.StringReader; import java.net.MalformedURLException; import java.net.URI; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.contribution.processor.ValidationSchemaExtensionPoint; import org.apache.tuscany.sca.node.Contribution; import org.apache.tuscany.sca.node.Node; import org.apache.tuscany.sca.node.NodeFactory; +import org.apache.tuscany.sca.runtime.DomainRegistryFactoryExtensionPoint; import org.junit.Assert; import org.junit.Test; @@ -115,4 +119,26 @@ public class NodeImplTestCase { Assert.assertEquals("Hello, Node", hw.hello("Node")); node.stop(); } + + @Test + public void testNodeFactoryAttributes() { + Map<String, Map<String, String>> attrs = new HashMap<String, Map<String, String>>(); + Map<String, String> map = new HashMap<String, String>(); + map.put("enabled", "false"); + attrs.put(ValidationSchemaExtensionPoint.class.getName(), map); + + Map<String, String> map2 = new HashMap<String, String>(); + map2.put("urn:MyDomain", "multicast://200.0.0.100:50000/MyDomain"); + attrs.put(DomainRegistryFactoryExtensionPoint.class.getName(), map2); + + NodeFactoryImpl factory = (NodeFactoryImpl)NodeFactory.newInstance(attrs); + Assert.assertFalse(factory.getExtensionPoints().getExtensionPoint(ValidationSchemaExtensionPoint.class) + .isEnabled()); + + DomainRegistryFactoryExtensionPoint domainRegistryFactoryExtensionPoint = + factory.getExtensionPoints().getExtensionPoint(DomainRegistryFactoryExtensionPoint.class); + Map<String, String> mapping = domainRegistryFactoryExtensionPoint.getDomainRegistryMapping(); + Assert.assertEquals(1, mapping.size()); + Assert.assertEquals("multicast://200.0.0.100:50000/MyDomain", mapping.get("urn:MyDomain")); + } } |