From 400713c5c4c89dfcc7e77f502003af63e1098c15 Mon Sep 17 00:00:00 2001 From: rfeng Date: Mon, 18 Aug 2008 19:59:14 +0000 Subject: Fix the webapp regression where the no explicit deployable composite is passed into the Node API git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@686861 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/tuscany/sca/node/impl/NodeImpl.java | 108 ++++++++++----------- .../tuscany/sca/node/impl/NodeImplTestCase.java | 51 ++++++---- .../test/resources/META-INF/sca-contribution.xml | 23 +++++ 3 files changed, 105 insertions(+), 77 deletions(-) create mode 100644 java/sca/modules/node2-impl/src/test/resources/META-INF/sca-contribution.xml (limited to 'java') diff --git a/java/sca/modules/node2-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java b/java/sca/modules/node2-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java index fc16257bff..835829db1a 100644 --- a/java/sca/modules/node2-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java +++ b/java/sca/modules/node2-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java @@ -51,7 +51,6 @@ import org.apache.tuscany.sca.assembly.xml.CompositeDocumentProcessor; import org.apache.tuscany.sca.contribution.Artifact; import org.apache.tuscany.sca.contribution.Contribution; import org.apache.tuscany.sca.contribution.ContributionFactory; -import org.apache.tuscany.sca.contribution.ContributionMetadata; import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; @@ -101,8 +100,6 @@ public class NodeImpl implements SCANode2, SCAClient { private Monitor monitor; private List contributions; - private ContributionMetadata metadata; - // The composite loaded into this node private Composite composite; @@ -168,12 +165,20 @@ public class NodeImpl implements SCANode2, SCAClient { } } + /** + * Discover the contribution on the classpath + * @param compositeURI + * @param classLoader + * @return A configured node implementation + * @throws Exception + */ private ConfiguredNodeImplementation findNodeConfiguration(final String compositeURI, ClassLoader classLoader) throws Exception { NodeImplementationFactory nodeImplementationFactory = modelFactories.getFactory(NodeImplementationFactory.class); ConfiguredNodeImplementation config = nodeImplementationFactory.createConfiguredNodeImplementation(); + // Default to thread context classloader if (classLoader == null) { classLoader = Thread.currentThread().getContextClassLoader(); } @@ -188,43 +193,25 @@ public class NodeImpl implements SCANode2, SCAClient { Composite composite = createComposite(compositeURI); config.setComposite(composite); } else { - - // Here the SCADomain was started without any reference to a composite file - // We are going to look for an sca-contribution.xml or sca-contribution-generated.xml - - // Look for META-INF/sca-contribution.xml + // No composite is specified, tring to search the SCA metadata files contributionArtifactPath = Contribution.SCA_CONTRIBUTION_META; - contributionArtifactURL = getResource(classLoader, contributionArtifactPath); + contributionArtifactURL = getResource(classLoader, Contribution.SCA_CONTRIBUTION_META); - // Look for META-INF/sca-contribution-generated.xml if (contributionArtifactURL == null) { contributionArtifactPath = Contribution.SCA_CONTRIBUTION_GENERATED_META; - contributionArtifactURL = getResource(classLoader, contributionArtifactPath); + contributionArtifactURL = getResource(classLoader, Contribution.SCA_CONTRIBUTION_GENERATED_META); } - - // Look for META-INF/sca-deployables directory if (contributionArtifactURL == null) { contributionArtifactPath = Contribution.SCA_CONTRIBUTION_DEPLOYABLES; - contributionArtifactURL = getResource(classLoader, contributionArtifactPath); - } else { - StAXArtifactProcessor processor = - artifactProcessors.getProcessor(ContributionMetadata.class); - XMLStreamReader reader = inputFactory.createXMLStreamReader(contributionArtifactURL.openStream()); - reader.nextTag(); - metadata = processor.read(reader); - reader.close(); - if (metadata.getDeployables().isEmpty()) { - throw new IllegalArgumentException( - "No deployable composite is declared in " + contributionArtifactPath); - } - Composite composite = metadata.getDeployables().get(0); - config.setComposite(composite); + contributionArtifactURL = getResource(classLoader, Contribution.SCA_CONTRIBUTION_DEPLOYABLES); } - } - - if (contributionArtifactURL == null) { - throw new IllegalArgumentException( - "Can't determine contribution deployables. Either specify a composite file, or use an sca-contribution.xml file to specify the deployables."); + + // No contribution can be discovered + if (contributionArtifactURL == null) { + throw new IllegalArgumentException("No default contribution can be discovered on the classpath"); + } + + // No composite will be created, all deployable composites will be used later } Contribution c = getContribution(contributionArtifactURL, contributionArtifactPath); @@ -314,10 +301,10 @@ public class NodeImpl implements SCANode2, SCAClient { // Initialize the runtime initRuntime(); - URI uri = URI.create(compositeURI); + URI uri = compositeURI == null ? null : URI.create(compositeURI); ConfiguredNodeImplementation configuration = null; if (contributions == null || contributions.length == 0) { - if (uri.getScheme() != null) { + if (uri != null && uri.getScheme() != null) { throw new IllegalArgumentException("No SCA contributions are provided"); } configuration = findNodeConfiguration(compositeURI, null); @@ -328,8 +315,9 @@ public class NodeImpl implements SCANode2, SCAClient { modelFactories.getFactory(NodeImplementationFactory.class); configuration = nodeImplementationFactory.createConfiguredNodeImplementation(); - Composite composite = createComposite(compositeURI); + Composite composite = compositeURI == null ? null : createComposite(compositeURI); configuration.setComposite(composite); + // Create contribution models ContributionFactory contributionFactory = modelFactories.getFactory(ContributionFactory.class); @@ -498,32 +486,14 @@ public class NodeImpl implements SCANode2, SCAClient { composite = configuration.getComposite(); - // Resolve the metadata within the context of the contribution - // FIXME The deployable composite should have been picked by the SCA Domain Manager already and we should not - // pollute the Node impl to deal with this - if (metadata != null) { - StAXArtifactProcessor processor = - artifactProcessors.getProcessor(ContributionMetadata.class); + // FIXME: This is a hack to get a list of deployable composites. By design, the deployment composite should + // has been configured + if (composite == null) { + List deployables = new ArrayList(); for (Contribution c : contributions) { - processor.resolve(metadata, c.getModelResolver()); - if (!metadata.isUnresolved()) { - break; - } - } - List composites = metadata.getDeployables(); - if (composites.size() == 0) { - throw new IllegalArgumentException("No deployable composite is declared"); - } else if (composites.size() == 1) { - composite = composites.get(0); - } else { - // This is temporary to include all composites - AssemblyFactory assemblyFactory = runtime.getAssemblyFactory(); - Composite tempComposite = assemblyFactory.createComposite(); - tempComposite.setName(new QName("http://tempuri.org", "aggregated")); - tempComposite.setURI("http://tempuri.org/aggregated"); - tempComposite.getIncludes().addAll(composites); - composite = tempComposite; + deployables.addAll(c.getDeployables()); } + aggregate(deployables); configuration.setComposite(composite); } @@ -591,6 +561,26 @@ public class NodeImpl implements SCANode2, SCAClient { analyzeProblems(); } + /** + * Create a deployment composite that includes a list of deployable composites + * @param composites + */ + private void aggregate(List composites) { + if (composites.size() == 0) { + throw new IllegalArgumentException("No deployable composite is declared"); + } else if (composites.size() == 1) { + composite = composites.get(0); + } else { + // Include all composites + AssemblyFactory assemblyFactory = runtime.getAssemblyFactory(); + Composite aggregated = assemblyFactory.createComposite(); + aggregated.setName(new QName("http://tempuri.org", "aggregated")); + aggregated.setURI("http://tempuri.org/aggregated"); + aggregated.getIncludes().addAll(composites); + composite = aggregated; + } + } + /** * Returns the artifact representing the given composite. * diff --git a/java/sca/modules/node2-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java b/java/sca/modules/node2-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java index bdf3dfca4f..38fe211e7d 100644 --- a/java/sca/modules/node2-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java +++ b/java/sca/modules/node2-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java @@ -48,24 +48,33 @@ public class NodeImplTestCase { @Test public void testNodeWithCompositeContent() { SCANode2Factory factory = new NodeFactoryImpl(); - SCAContribution contribution = new SCAContribution("c1", new File("target/classes").toURI().toString()); + SCAContribution contribution = new SCAContribution("c1", new File("target/test-classes").toURI().toString()); SCANode2 node = factory.createSCANode("HelloWorld.composite", composite, contribution); - node.start(); - HelloWorld hw = ((SCAClient)node).getService(HelloWorld.class, "HelloWorld"); - Assert.assertEquals("Hello, Node", hw.hello("Node")); - node.stop(); + testNode(node); } + + @Test + public void testNodeWithCompositeContentAndNoContribution() { + SCANode2Factory factory = new NodeFactoryImpl(); + SCANode2 node = factory.createSCANode("HelloWorld.composite", composite); + testNode(node); + } + @Test + public void testNodeWithoutCompositeURI() { + SCANode2Factory factory = new NodeFactoryImpl(); + SCAContribution contribution = new SCAContribution("c1", new File("target/test-classes").toURI().toString()); + SCANode2 node = factory.createSCANode(null, contribution); + testNode(node); + } + @Test public void testNodeWithCompositeURI() { SCANode2Factory factory = new NodeFactoryImpl(); - SCAContribution contribution = new SCAContribution("c1", new File("target/classes").toURI().toString()); + SCAContribution contribution = new SCAContribution("c1", new File("target/test-classes").toURI().toString()); String compositeURI = new File("target/test-classes/HelloWorld.composite").toURI().toString(); SCANode2 node = factory.createSCANode(compositeURI, contribution); - node.start(); - HelloWorld hw = ((SCAClient)node).getService(HelloWorld.class, "HelloWorld"); - Assert.assertEquals("Hello, Node", hw.hello("Node")); - node.stop(); + testNode(node); } @Test @@ -74,10 +83,7 @@ public class NodeImplTestCase { SCAContribution contribution = new SCAContribution("c1", new File("target/test-classes").toURI().toString()); String compositeURI = "HelloWorld.composite"; SCANode2 node = factory.createSCANode(compositeURI, contribution); - node.start(); - HelloWorld hw = ((SCAClient)node).getService(HelloWorld.class, "HelloWorld"); - Assert.assertEquals("Hello, Node", hw.hello("Node")); - node.stop(); + testNode(node); } @Test @@ -85,10 +91,7 @@ public class NodeImplTestCase { SCANode2Factory factory = new NodeFactoryImpl(); String compositeURI = "HelloWorld.composite"; SCANode2 node = factory.createSCANode(compositeURI, new SCAContribution[0]); - node.start(); - HelloWorld hw = ((SCAClient)node).getService(HelloWorld.class, "HelloWorld"); - Assert.assertEquals("Hello, Node", hw.hello("Node")); - node.stop(); + testNode(node); } @Test @@ -96,9 +99,21 @@ public class NodeImplTestCase { SCANode2Factory factory = new NodeFactoryImpl(); String compositeURI = "HelloWorld.composite"; SCANode2 node = factory.createSCANodeFromClassLoader(compositeURI, HelloWorld.class.getClassLoader()); + testNode(node); + } + + @Test + public void testNodeWithClassLoaderAndNullComposite() { + SCANode2Factory factory = new NodeFactoryImpl(); + SCANode2 node = factory.createSCANodeFromClassLoader(null, HelloWorld.class.getClassLoader()); + testNode(node); + } + + private void testNode(SCANode2 node) { node.start(); HelloWorld hw = ((SCAClient)node).getService(HelloWorld.class, "HelloWorld"); Assert.assertEquals("Hello, Node", hw.hello("Node")); node.stop(); } + } diff --git a/java/sca/modules/node2-impl/src/test/resources/META-INF/sca-contribution.xml b/java/sca/modules/node2-impl/src/test/resources/META-INF/sca-contribution.xml new file mode 100644 index 0000000000..1d37bcebe0 --- /dev/null +++ b/java/sca/modules/node2-impl/src/test/resources/META-INF/sca-contribution.xml @@ -0,0 +1,23 @@ + + + + + + -- cgit v1.2.3