From 65e3fd65b82c7cd08cfdeee8819f571212b3b1fe Mon Sep 17 00:00:00 2001 From: antelder Date: Fri, 21 Nov 2008 12:16:29 +0000 Subject: Simplify the Node API for a common default case so that creating a Node for simple samples and tests can be: node = NodeFactory.newInstance().createNode(); instead of: NodeFactory factory = NodeFactory.newInstance(); String root = ContributionLocationHelper.getContributionLocation("Calculator.composite"); Contribution contribution = new Contribution(root, root); node = factory.createNode("Calculator.composite", contribution); git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@719565 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/tuscany/sca/node/NodeFactory.java | 7 +++ .../tuscany/sca/node/impl/NodeFactoryImpl.java | 4 ++ .../org/apache/tuscany/sca/node/impl/NodeImpl.java | 65 ++++++++++++++++++++++ .../tuscany/sca/node/impl/NodeImplTestCase.java | 6 +- .../src/main/java/calculator/CalculatorClient.java | 7 +-- .../test/java/calculator/CalculatorTestCase.java | 8 +-- 6 files changed, 83 insertions(+), 14 deletions(-) diff --git a/java/sca/modules/node-api/src/main/java/org/apache/tuscany/sca/node/NodeFactory.java b/java/sca/modules/node-api/src/main/java/org/apache/tuscany/sca/node/NodeFactory.java index a9f75fc8be..05ef39f461 100644 --- a/java/sca/modules/node-api/src/main/java/org/apache/tuscany/sca/node/NodeFactory.java +++ b/java/sca/modules/node-api/src/main/java/org/apache/tuscany/sca/node/NodeFactory.java @@ -155,6 +155,13 @@ public abstract class NodeFactory { } } + /** + * Creates a new SCA node using defaults for the contribution location and runnable composite + * + * @return a new SCA node. + */ + public abstract Node createNode(); + /** * Creates a new SCA node from the configuration URL * diff --git a/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java b/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java index 62758e8332..5ee68899a9 100644 --- a/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java +++ b/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java @@ -47,4 +47,8 @@ public class NodeFactoryImpl extends NodeFactory { return new NodeImpl(compositeURI, compositeContent, contributions); } + @Override + public Node createNode() { + return new NodeImpl(); + } } diff --git a/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java b/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java index effccd0f4d..73ef98010d 100644 --- a/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java +++ b/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java @@ -76,6 +76,7 @@ import org.apache.tuscany.sca.monitor.MonitorFactory; import org.apache.tuscany.sca.monitor.Problem; import org.apache.tuscany.sca.monitor.Problem.Severity; import org.apache.tuscany.sca.node.Client; +import org.apache.tuscany.sca.node.ContributionLocationHelper; import org.apache.tuscany.sca.node.Node; import org.apache.tuscany.sca.runtime.RuntimeComponent; import org.apache.tuscany.sca.runtime.RuntimeComponentContext; @@ -122,6 +123,50 @@ public class NodeImpl implements Node, Client { private WorkScheduler workScheduler; private Definitions systemDefinitions; + /** + * Constructs a new SCA node. + */ + NodeImpl() { + configurationName = "default"; + logger.log(Level.INFO, "Creating node: " + configurationName); + + String root = ContributionLocationHelper.getContributionLocation("META-INF/sca-contribution.xml"); + if (root == null) { + throw new ServiceRuntimeException("no META-INF/sca-contribution.xml found"); + } + + String compositeURI = null; + org.apache.tuscany.sca.node.Contribution[] contributions = new org.apache.tuscany.sca.node.Contribution[]{new org.apache.tuscany.sca.node.Contribution(root, root)}; + + try { + // Initialize the runtime + init(); + + // Create a node configuration + NodeImplementationFactory nodeImplementationFactory = modelFactories.getFactory(NodeImplementationFactory.class); + ConfiguredNodeImplementation configuration = nodeImplementationFactory.createConfiguredNodeImplementation(); + + if (compositeURI != null) { + Composite composite = assemblyFactory.createComposite(); + composite.setURI(compositeURI); + composite.setUnresolved(true); + configuration.setComposite(composite); + } + + // Create contribution models + for (org.apache.tuscany.sca.node.Contribution c : contributions) { + Contribution contribution = contribution(contributionFactory, c); + configuration.getContributions().add(contribution); + } + + // Configure the node + configureNode(configuration); + + } catch (Exception e) { + throw new ServiceRuntimeException(e); + } + } + /** * Constructs a new SCA node. * @@ -401,6 +446,10 @@ public class NodeImpl implements Node, Client { composite = configuration.getComposite(); + if (composite == null) { + setDefaultComposite(configuration, workspace); + } + // Find the composite in the given contributions boolean found = false; Artifact compositeFile = contributionFactory.createArtifact(); @@ -590,4 +639,20 @@ public class NodeImpl implements Node, Client { } } + /* + * Sets a default composite by using any deployable one. + */ + private void setDefaultComposite(ConfiguredNodeImplementation configuration, Workspace workspace) { + // just use the first deployable composte + for (Contribution contribution : workspace.getContributions()) { + for (Composite c : contribution.getDeployables()) { + composite = assemblyFactory.createComposite(); + composite.setURI(c.getURI()); + composite.setUnresolved(true); + configuration.setComposite(composite); + return; + } + } + throw new ServiceRuntimeException("no deployable composite found"); + } } diff --git a/java/sca/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java b/java/sca/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java index bd760d613d..0ccbd6d30e 100644 --- a/java/sca/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java +++ b/java/sca/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java @@ -25,7 +25,6 @@ import java.io.File; import junit.framework.Assert; -import org.apache.tuscany.sca.node.Client; import org.apache.tuscany.sca.node.Contribution; import org.apache.tuscany.sca.node.Node; import org.apache.tuscany.sca.node.NodeFactory; @@ -63,6 +62,11 @@ public class NodeImplTestCase { testNode(node); } + @Test + public void testDefaultNode() { + testNode(new NodeFactoryImpl().createNode()); + } + private void testNode(Node node) { node.start(); HelloWorld hw = node.getService(HelloWorld.class, "HelloWorld"); diff --git a/java/sca/samples/calculator/src/main/java/calculator/CalculatorClient.java b/java/sca/samples/calculator/src/main/java/calculator/CalculatorClient.java index 5496521525..03fc56ceb3 100644 --- a/java/sca/samples/calculator/src/main/java/calculator/CalculatorClient.java +++ b/java/sca/samples/calculator/src/main/java/calculator/CalculatorClient.java @@ -20,8 +20,6 @@ package calculator; import org.apache.tuscany.sca.node.Client; -import org.apache.tuscany.sca.node.Contribution; -import org.apache.tuscany.sca.node.ContributionLocationHelper; import org.apache.tuscany.sca.node.Node; import org.apache.tuscany.sca.node.NodeFactory; @@ -32,10 +30,7 @@ import org.apache.tuscany.sca.node.NodeFactory; public class CalculatorClient { public static void main(String[] args) throws Exception { - NodeFactory factory = NodeFactory.newInstance(); - String root = ContributionLocationHelper.getContributionLocation("Calculator.composite"); - Contribution contribution = new Contribution(root, root); - Node node = factory.createNode("Calculator.composite", contribution); + Node node = NodeFactory.newInstance().createNode(); node.start(); diff --git a/java/sca/samples/calculator/src/test/java/calculator/CalculatorTestCase.java b/java/sca/samples/calculator/src/test/java/calculator/CalculatorTestCase.java index 464950f08c..6bc7e21606 100644 --- a/java/sca/samples/calculator/src/test/java/calculator/CalculatorTestCase.java +++ b/java/sca/samples/calculator/src/test/java/calculator/CalculatorTestCase.java @@ -21,8 +21,6 @@ package calculator; import junit.framework.TestCase; import org.apache.tuscany.sca.node.Client; -import org.apache.tuscany.sca.node.Contribution; -import org.apache.tuscany.sca.node.ContributionLocationHelper; import org.apache.tuscany.sca.node.Node; import org.apache.tuscany.sca.node.NodeFactory; @@ -36,11 +34,7 @@ public class CalculatorTestCase extends TestCase { @Override protected void setUp() throws Exception { - NodeFactory factory = NodeFactory.newInstance(); - String root = ContributionLocationHelper.getContributionLocation("Calculator.composite"); - Contribution contribution = new Contribution(root, root); - node = factory.createNode("Calculator.composite", contribution); - + node = NodeFactory.newInstance().createNode(); node.start(); calculatorService = ((Client)node).getService(CalculatorService.class, "CalculatorServiceComponent"); -- cgit v1.2.3