diff options
author | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2010-08-31 00:15:25 +0000 |
---|---|---|
committer | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2010-08-31 00:15:25 +0000 |
commit | 4cac6f63be4e919b1d97624bb0e255a39e600190 (patch) | |
tree | e69ad1f763d2ace074cdd343f5decb38b06837fb | |
parent | 7b3556ec8db208f41abe5fb7a1df77db2d8a8ef2 (diff) |
Add a test case showing how to register a ProviderFactory instance. Minor cleanup, add a createNode method that takes a list of contributions and remove reference to NodeFactory implementation class.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@991044 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
14 files changed, 236 insertions, 34 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 2d2df84f42..95d1adc92e 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 @@ -609,6 +609,13 @@ public abstract class NodeFactory extends DefaultNodeConfigurationFactory { * @return The SCA node */ public abstract Node createNode(NodeConfiguration configuration); + + /** + * Create an SCA node from a list of pre-built o.a.t.sca.contribution.Contribution objects. + * Pass java.lang.Objects for now as this class doesn't have direct dependencies on + * o.a.t.sca.contribution.Contribution. + */ + public abstract Node createNode(List<?> contributions); /** * Create the node configuration from the XML document 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 fc975c0072..f7a439699e 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 @@ -33,6 +33,7 @@ import java.net.URL; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -77,6 +78,7 @@ import org.apache.tuscany.sca.node.configuration.BindingConfiguration; import org.apache.tuscany.sca.node.configuration.ContributionConfiguration; import org.apache.tuscany.sca.node.configuration.DeploymentComposite; import org.apache.tuscany.sca.node.configuration.NodeConfiguration; +import org.apache.tuscany.sca.node.configuration.impl.NodeConfigurationImpl; import org.apache.tuscany.sca.runtime.DomainRegistryFactory; import org.apache.tuscany.sca.runtime.EndpointReferenceBinder; import org.apache.tuscany.sca.runtime.EndpointRegistry; @@ -123,6 +125,12 @@ public class NodeFactoryImpl extends NodeFactory { return node; } + @SuppressWarnings("unchecked") + @Override + public Node createNode(List<?> contributions) { + return new NodeImpl(this, new NodeConfigurationImpl(), new ArrayList<Contribution>((List<Contribution>)contributions)); + } + protected void addNode(NodeConfiguration configuration, Node node) { nodes.put(getNodeKey(configuration), node); } diff --git a/sca-java-2.x/trunk/samples/implementation-extension/src/main/java/sample/impl/EmbedUtil.java b/sca-java-2.x/trunk/samples/implementation-extension/src/main/java/sample/impl/EmbedUtil.java index 079df6b15e..1db0e9a717 100644 --- a/sca-java-2.x/trunk/samples/implementation-extension/src/main/java/sample/impl/EmbedUtil.java +++ b/sca-java-2.x/trunk/samples/implementation-extension/src/main/java/sample/impl/EmbedUtil.java @@ -57,17 +57,20 @@ import org.apache.tuscany.sca.interfacedef.wsdl.WSDLFactory; import org.apache.tuscany.sca.interfacedef.wsdl.WSDLInterface; import org.apache.tuscany.sca.interfacedef.wsdl.WSDLObject; import org.apache.tuscany.sca.node.Node; -import org.apache.tuscany.sca.node.configuration.NodeConfiguration; -import org.apache.tuscany.sca.node.configuration.impl.NodeConfigurationImpl; -import org.apache.tuscany.sca.node.impl.NodeFactoryImpl; -import org.apache.tuscany.sca.node.impl.NodeImpl; +import org.apache.tuscany.sca.node.NodeFactory; +import org.apache.tuscany.sca.provider.ProviderFactoryExtensionPoint; /** - * Simple DSL functions to help assemble and run SCDL. + * A few utility functions to help embed a Tuscany runtime, and a simple DSL + * to help assemble and run SCDL. */ public class EmbedUtil { - static final NodeFactoryImpl nf; + /** + * Initialize a Node factory and get the various registries, factories and + * extension points we need. + */ + static final NodeFactory nf; static final ExtensionPointRegistry epr; static final FactoryExtensionPoint fep; static final ContributionFactory cf; @@ -77,8 +80,9 @@ public class EmbedUtil { static final URLArtifactProcessorExtensionPoint apep; static final ExtensibleURLArtifactProcessor aproc; static final ModelResolverExtensionPoint mrep; + static final ProviderFactoryExtensionPoint pfep; static { - nf = new NodeFactoryImpl(); + nf = NodeFactory.newInstance(); epr = nf.getExtensionPointRegistry(); fep = epr.getExtensionPoint(FactoryExtensionPoint.class); cf = fep.getFactory(ContributionFactory.class); @@ -88,8 +92,12 @@ public class EmbedUtil { apep = epr.getExtensionPoint(URLArtifactProcessorExtensionPoint.class); aproc = new ExtensibleURLArtifactProcessor(apep); mrep = epr.getExtensionPoint(ModelResolverExtensionPoint.class); + pfep = epr.getExtensionPoint(ProviderFactoryExtensionPoint.class); } + /** + * A mini DSL to help build and assemble contributions and SCDL composites. + */ static Contribution contrib(final String uri, final String loc, final Artifact... artifacts) { final Contribution c = cf.createContribution(); c.setURI(uri); @@ -137,20 +145,6 @@ public class EmbedUtil { return impl; } - static WSDLInterface wsdli(final String uri, final String ns, final String name, final Contribution c) throws InvalidInterfaceException, ContributionException, IOException, URISyntaxException { - final ProcessorContext ctx = new ProcessorContext(); - final WSDLDefinition wd = aproc.read(null, new URI(uri), new URL(new URL(c.getLocation()), uri), ctx, WSDLDefinition.class); - c.getModelResolver().addModel(wd, ctx); - c.getModelResolver().resolveModel(WSDLDefinition.class, wd, ctx); - final WSDLObject<PortType> pt = wd.getWSDLObject(PortType.class, new QName(ns, name)); - if(pt == null) - throw new ContributionResolveException("Couldn't find " + name); - final WSDLInterface nwi = wif.createWSDLInterface(pt.getElement(), wd, c.getModelResolver(), null); - nwi.setWsdlDefinition(wd); - nwi.resetDataBinding(DOMDataBinding.NAME); - return nwi; - } - static Reference reference(final String name, final Class<?> c) throws InvalidInterfaceException { return ImplUtil.reference(name, c, jif, af); } @@ -178,6 +172,37 @@ public class EmbedUtil { } /** + * Load a WSDL into a contribution. + */ + static WSDLInterface wsdli(final String uri, final String ns, final String name, final Contribution c) throws InvalidInterfaceException, ContributionException, IOException, URISyntaxException { + final ProcessorContext ctx = new ProcessorContext(); + final WSDLDefinition wd = aproc.read(null, new URI(uri), new URL(new URL(c.getLocation()), uri), ctx, WSDLDefinition.class); + c.getModelResolver().addModel(wd, ctx); + c.getModelResolver().resolveModel(WSDLDefinition.class, wd, ctx); + final WSDLObject<PortType> pt = wd.getWSDLObject(PortType.class, new QName(ns, name)); + if(pt == null) + throw new ContributionResolveException("Couldn't find " + name); + final WSDLInterface nwi = wif.createWSDLInterface(pt.getElement(), wd, c.getModelResolver(), null); + nwi.setWsdlDefinition(wd); + nwi.resetDataBinding(DOMDataBinding.NAME); + return nwi; + } + + /** + * Return the extension point registry used by our nodes. + */ + static ExtensionPointRegistry extensionPoints() { + return epr; + } + + /** + * Return the provider factory extension point used by our nodes. + */ + static ProviderFactoryExtensionPoint providerFactories() { + return pfep; + } + + /** * Add a deployable composite to a contribution. */ static Contribution deploy(final Contribution contrib, final Composite... comps) { @@ -185,14 +210,12 @@ public class EmbedUtil { contrib.getDeployables().add(c); return contrib; } - + /** * Configure a node with a list of contributions. */ - static Node node(final String uri, final Contribution... contributions) { - final NodeConfiguration cfg = new NodeConfigurationImpl(); - cfg.setURI(uri); - return new NodeImpl(nf, cfg, Arrays.asList(contributions)); + static Node node(final Contribution... contributions) { + return nf.createNode(Arrays.asList(contributions)); } } diff --git a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/Client.java b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/Client.java index 4c473c987f..1b7e11c786 100644 --- a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/Client.java +++ b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/Client.java @@ -21,6 +21,11 @@ package sample; import org.oasisopen.sca.annotation.Remotable; +/** + * Sample service interface. + * + * @version $Rev$ $Date$ + */ @Remotable public interface Client { diff --git a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/ClientTest.java b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/ClientTest.java index 6b5c3983d1..3b635708ec 100644 --- a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/ClientTest.java +++ b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/ClientTest.java @@ -36,7 +36,7 @@ import sample.api.WSDL; import sample.api.WSDLReference; /** - * Sample component implementation. + * Sample component implementation that uses a mix of Java and WSDL interfaces. * * @version $Rev$ $Date$ */ diff --git a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/Hello.java b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/Hello.java index f6e02591e5..247fc8aa78 100644 --- a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/Hello.java +++ b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/Hello.java @@ -21,6 +21,11 @@ package sample; import org.oasisopen.sca.annotation.Remotable; +/** + * Sample service interface. + * + * @version $Rev$ $Date$ + */ @Remotable public interface Hello { diff --git a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/JelloTest.java b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/JelloTest.java index 53ed006132..5c999a6c80 100644 --- a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/JelloTest.java +++ b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/JelloTest.java @@ -23,7 +23,7 @@ import static java.lang.System.out; import sample.api.Java; /** - * Sample component implementation. + * Sample component implementation that uses Java interfaces. * * @version $Rev$ $Date$ */ diff --git a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/Upper.java b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/Upper.java index 04e929ade7..9c772b3487 100644 --- a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/Upper.java +++ b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/Upper.java @@ -21,6 +21,11 @@ package sample; import org.oasisopen.sca.annotation.Remotable; +/** + * Sample service interface. + * + * @version $Rev$ $Date$ + */ @Remotable public interface Upper { diff --git a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/UpperTest.java b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/UpperTest.java index fe90097efa..5399e4410d 100644 --- a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/UpperTest.java +++ b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/UpperTest.java @@ -23,7 +23,7 @@ import static java.lang.System.out; import sample.api.Java; /** - * Sample component implementation. + * Sample component implementation that uses Java interfaces. * * @version $Rev$ $Date$ */ diff --git a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/WelloTest.java b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/WelloTest.java index 9da5083dda..3b3a6a4566 100644 --- a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/WelloTest.java +++ b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/WelloTest.java @@ -36,7 +36,7 @@ import sample.api.WSDL; import sample.api.WSDLReference; /** - * Sample component implementation. + * Sample component implementation that uses WSDL interfaces. * * @version $Rev$ $Date$ */ diff --git a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/impl/EmbedTestCase.java b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/impl/EmbedTestCase.java index 98057bf34e..41f6d95f3c 100644 --- a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/impl/EmbedTestCase.java +++ b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/impl/EmbedTestCase.java @@ -49,7 +49,8 @@ import sample.UpperTest; import sample.WelloTest; /** - * Test assemble and run SCDL. + * Test how to assemble a contribution, a SCDL composite and run it on an embedded + * Tuscany runtime node. * * @version $Rev$ $Date$ */ @@ -93,7 +94,7 @@ public class EmbedTestCase { service(Upper.class)))); // Run with it - node = node("test", deploy(contrib, comp)); + node = node(deploy(contrib, comp)); node.start(); } diff --git a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/impl/InControlTestCase.java b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/impl/InControlTestCase.java new file mode 100644 index 0000000000..6523e0b233 --- /dev/null +++ b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/impl/InControlTestCase.java @@ -0,0 +1,147 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package sample.impl; + +import static java.lang.System.out; +import static org.junit.Assert.assertEquals; +import static sample.impl.EmbedUtil.component; +import static sample.impl.EmbedUtil.composite; +import static sample.impl.EmbedUtil.contrib; +import static sample.impl.EmbedUtil.deploy; +import static sample.impl.EmbedUtil.extensionPoints; +import static sample.impl.EmbedUtil.implementation; +import static sample.impl.EmbedUtil.node; +import static sample.impl.EmbedUtil.providerFactories; +import static sample.impl.EmbedUtil.reference; +import static sample.impl.EmbedUtil.service; +import static sample.impl.EmbedUtil.wsdli; +import static sample.impl.TestUtil.here; + +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLInterface; +import org.apache.tuscany.sca.node.Node; +import org.apache.tuscany.sca.provider.ImplementationProvider; +import org.apache.tuscany.sca.provider.ProviderFactory; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import sample.Client; +import sample.ClientTest; +import sample.Hello; +import sample.JelloTest; +import sample.Upper; +import sample.UpperTest; +import sample.WelloTest; + +/** + * Test how to assemble a contribution, a SCDL composite and run it on an embedded + * Tuscany runtime node. Same as EmbedTestCase but shows how stay in control and + * pass in a ProviderFactory instead of having it loaded and constructed by the + * runtime node. + * + * @version $Rev$ $Date$ + */ +public class InControlTestCase { + static Node node; + + @BeforeClass + public static void setUp() throws Exception { + + // Load the test WSDL definitions (could also construct + // the WSDL and XSD models in code but that'd be quite + // painful, so just load them from XML for now) + final Contribution contrib = contrib("test", here()); + WSDLInterface Hello_wsdl = wsdli("Hello.wsdl", "http://sample", "Hello", contrib); + WSDLInterface Upper_wsdl = wsdli("Upper.wsdl", "http://sample", "Upper", contrib); + + // Assemble a test composite model (see EmbedUtil + // for the little DSL used here, much more concise + // than using the assembly model interfaces) + final Composite comp = + composite("http://sample", "test", + component("client-test", + implementation(ClientTest.class, + service(Client.class), + reference("jello", Hello.class), + reference("wello", Hello_wsdl)), + reference("jello", "jello-test"), + reference("wello", "wello-test")), + component("wello-test", + implementation(WelloTest.class, + service(Hello_wsdl), + reference("upper", Upper_wsdl)), + reference("upper", "upper-test")), + component("jello-test", + implementation(JelloTest.class, + service(Hello.class), + reference("upper", Upper.class)), + reference("upper", "upper-test")), + component("upper-test", + implementation(UpperTest.class, + service(Upper.class)))); + + // Register a test instance of our sample implementation ProviderFactory + providerFactories().addProviderFactory(testProviderFactory()); + + // Run with it + node = node(deploy(contrib, comp)); + node.start(); + } + + static ProviderFactory<SampleImplementation> testProviderFactory() { + // This shows that the embedder can take control and get called + // when a provider is created, and return whatever provider, then + // invoker, implementation instance etc + return new SampleProviderFactory(extensionPoints()) { + public ImplementationProvider createImplementationProvider(RuntimeComponent comp, SampleImplementation impl) { + out.println("Creating a provider for component " + comp.getName()); + return super.createImplementationProvider(comp, impl); + } + }; + } + + @AfterClass + public static void tearDown() throws Exception { + node.stop(); + } + + @Test + public void jello() { + out.println("RunTestCase.jello"); + final String r = client().jello("Java"); + out.println(r); + assertEquals("HELLO JAVA", r); + } + + @Test + public void wello() { + out.println("RunTestCase.wello"); + final String r = client().wello("WSDL"); + out.println(r); + assertEquals("HELLO WSDL", r); + } + + static Client client() { + return node.getService(Client.class, "client-test/Client"); + } +} diff --git a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/impl/ReadWriteTestCase.java b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/impl/ReadWriteTestCase.java index ad507f80ac..8a4768cd9e 100644 --- a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/impl/ReadWriteTestCase.java +++ b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/impl/ReadWriteTestCase.java @@ -43,7 +43,7 @@ import org.junit.BeforeClass; import org.junit.Test; /** - * Test reading/writing Sample implementations. + * Test read/write Sample SCDL implementation elements. * * @version $Rev$ $Date$ */ diff --git a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/impl/RunTestCase.java b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/impl/RunTestCase.java index a2e1f9ed45..29bf02d26b 100644 --- a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/impl/RunTestCase.java +++ b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/impl/RunTestCase.java @@ -33,7 +33,8 @@ import org.junit.Test; import sample.Client; /** - * Test run. + * Test how to run an SCA contribution containing a test composite on a + * Tuscany runtime node. * * @version $Rev$ $Date$ */ |