diff options
Diffstat (limited to 'sca-java-2.x/trunk/contrib/samples')
4 files changed, 385 insertions, 0 deletions
diff --git a/sca-java-2.x/trunk/contrib/samples/reporting/README b/sca-java-2.x/trunk/contrib/samples/reporting/README new file mode 100644 index 0000000000..b33446cde8 --- /dev/null +++ b/sca-java-2.x/trunk/contrib/samples/reporting/README @@ -0,0 +1,6 @@ +Reporting Sample
+================
+
+A simple sample to motivate the implementation of some mechanisms for
+retiriving information about an active domain and it's nodes.
+
diff --git a/sca-java-2.x/trunk/contrib/samples/reporting/pom.xml b/sca-java-2.x/trunk/contrib/samples/reporting/pom.xml new file mode 100644 index 0000000000..103734b9f7 --- /dev/null +++ b/sca-java-2.x/trunk/contrib/samples/reporting/pom.xml @@ -0,0 +1,94 @@ +<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * 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.
+-->
+<project>
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-samples</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ <relativePath>../pom.xml</relativePath>
+ </parent>
+ <artifactId>sample-reporting</artifactId>
+ <name>Apache Tuscany SCA Sample Reporting</name>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-feature-api</artifactId>
+ <type>pom</type>
+ <version>2.0-SNAPSHOT</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-feature-core</artifactId>
+ <type>pom</type>
+ <version>2.0-SNAPSHOT</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-feature-webservice</artifactId>
+ <type>pom</type>
+ <version>2.0-SNAPSHOT</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-feature-ejava</artifactId>
+ <type>pom</type>
+ <version>2.0-SNAPSHOT</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-implementation-script-runtime</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-binding-jsonrpc-runtime</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-databinding-json</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.8.1</version>
+ <scope>test</scope>
+ </dependency>
+
+ </dependencies>
+
+ <build>
+ <finalName>${artifactId}</finalName>
+
+ <plugins>
+
+ </plugins>
+ </build>
+</project>
diff --git a/sca-java-2.x/trunk/contrib/samples/reporting/src/main/java/calculator/CalculatorService.java b/sca-java-2.x/trunk/contrib/samples/reporting/src/main/java/calculator/CalculatorService.java new file mode 100644 index 0000000000..12d80ffd1c --- /dev/null +++ b/sca-java-2.x/trunk/contrib/samples/reporting/src/main/java/calculator/CalculatorService.java @@ -0,0 +1,38 @@ +/*
+ * 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 calculator;
+
+import org.oasisopen.sca.annotation.Remotable;
+
+
+/**
+ * The Calculator service interface.
+ */
+@Remotable
+public interface CalculatorService {
+
+ double add(double n1, double n2);
+
+ double subtract(double n1, double n2);
+
+ double multiply(double n1, double n2);
+
+ double divide(double n1, double n2);
+
+}
diff --git a/sca-java-2.x/trunk/contrib/samples/reporting/src/test/java/reporting/JSELauncherReportingTestCase.java b/sca-java-2.x/trunk/contrib/samples/reporting/src/test/java/reporting/JSELauncherReportingTestCase.java new file mode 100644 index 0000000000..e25e51e48f --- /dev/null +++ b/sca-java-2.x/trunk/contrib/samples/reporting/src/test/java/reporting/JSELauncherReportingTestCase.java @@ -0,0 +1,247 @@ +/*
+ * 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 reporting;
+
+import java.io.ByteArrayOutputStream;
+import java.io.StringWriter;
+import java.util.Map;
+
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.tuscany.sca.assembly.Composite;
+import org.apache.tuscany.sca.assembly.Endpoint;
+import org.apache.tuscany.sca.contribution.Artifact;
+import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor;
+import org.apache.tuscany.sca.contribution.processor.ProcessorContext;
+import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor;
+import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint;
+import org.apache.tuscany.sca.core.ExtensionPointRegistry;
+import org.apache.tuscany.sca.core.FactoryExtensionPoint;
+import org.apache.tuscany.sca.definitions.Definitions;
+import org.apache.tuscany.sca.deployment.Deployer;
+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.node.configuration.ContributionConfiguration;
+import org.apache.tuscany.sca.node.configuration.NodeConfiguration;
+import org.apache.tuscany.sca.node.impl.NodeFactoryImpl;
+import org.apache.tuscany.sca.node.impl.NodeImpl;
+import org.apache.tuscany.sca.runtime.DomainRegistryFactory;
+import org.apache.tuscany.sca.runtime.EndpointRegistry;
+import org.apache.tuscany.sca.runtime.ExtensibleDomainRegistryFactory;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter;
+
+import calculator.CalculatorService;
+
+/**
+ * This client program shows how to extract useful(?) information from the Tuscany SCA runtime
+ */
+public class JSELauncherReportingTestCase {
+
+ private static NodeFactory nodeFactory;
+ private static Node node1;
+ private static Node node2;
+ private static CalculatorService calculator;
+
+
+ public static void main(String[] args) throws Exception {
+ JSELauncherReportingTestCase launcher = new JSELauncherReportingTestCase ();
+ launcher.setUp();
+ //launcher.callCalulator();
+ //launcher.listNodeConfigurations();
+ launcher.listEndpoints();
+ launcher.tearDown();
+ }
+
+ @BeforeClass
+ public static void setUp() throws Exception {
+ try {
+/* new
+ org.apache.tuscany.sca.node2.NodeFactory nodeFactoryNew = org.apache.tuscany.sca.node2.NodeFactory.newInstance();
+ org.apache.tuscany.sca.node2.Node node1New = nodeFactoryNew.createNode();
+ node1New.installContribution("../domain/distributed-calculator/contribution-add/target/classes");
+
+ org.apache.tuscany.sca.node2.Node node2New = nodeFactoryNew.createNode();
+ node2New.installContribution("../domain/distributed-calculator/contribution-calculator/target/classes");
+
+ calculator = node2New.getService(CalculatorService.class, "CalculatorServiceComponent");
+*/
+
+/* old */
+ nodeFactory = NodeFactory.newInstance();
+ node1 = nodeFactory.createNode(new Contribution("c1", "../domain/distributed-calculator/contribution-add/target/classes"));
+ node1.start();
+
+ node2 = nodeFactory.createNode(new Contribution("c1", "../domain/distributed-calculator/contribution-calculator/target/classes"));
+ node2.start();
+
+ calculator = node2.getService(CalculatorService.class, "CalculatorServiceComponent");
+
+ } catch (Exception ex){
+ ex.printStackTrace();
+ }
+ }
+
+ @AfterClass
+ public static void tearDown() throws Exception {
+ node1.stop();
+ node2.stop();
+ }
+
+ @Test
+ public void callCalulator(){
+ printTestName("callCalulator");
+ double result = calculator.add(3, 2);
+ System.out.println("3 + 2 = " + result);
+ }
+
+ @Test
+ public void listNodes(){
+ printTestName("listNodes");
+ Map<Object, Node> nodes = ((NodeFactoryImpl)nodeFactory).getNodes();
+ for (Object nodeKey : nodes.keySet()){
+ System.out.println(nodeKey);
+ }
+ }
+
+ @Test
+ public void listNodeConfigurations(){
+ printTestName("listNodeConfigurations");
+ Map<Object, Node> nodes = ((NodeFactoryImpl)nodeFactory).getNodes();
+ for (Node node : nodes.values()){
+ System.out.println("Node: " + ((NodeImpl)node).getURI());
+ printXML(((NodeImpl)node).getConfiguration());
+ }
+ }
+
+ @Test
+ public void listContributions(){
+ printTestName("listContributions");
+ Map<Object, Node> nodes = ((NodeFactoryImpl)nodeFactory).getNodes();
+ for (Node node : nodes.values()){
+ System.out.println("Node: " + ((NodeImpl)node).getURI());
+ NodeConfiguration nodeConfiguration = ((NodeImpl)node).getConfiguration();
+ for (org.apache.tuscany.sca.contribution.Contribution contribution : ((NodeImpl)node).getContributions()){
+ System.out.println("Contribution: " + contribution.getURI() + " location " + contribution.getLocation());
+ for (Artifact artifact : contribution.getArtifacts()){
+ System.out.println(" Artifact: " + artifact.getURI() + " location " + artifact.getLocation());
+ }
+ }
+ }
+ }
+
+ @Test
+ public void listDomainComposite(){
+ printTestName("listDomainComposite");
+ Map<Object, Node> nodes = ((NodeFactoryImpl)nodeFactory).getNodes();
+ for (Node node : nodes.values()){
+ System.out.println("Node: " + ((NodeImpl)node).getURI());
+ printXML(((NodeImpl)node).getDomainComposite());
+ }
+ }
+
+ @Test
+ public void listDomainDefinitions(){
+ printTestName("listDomainDefinitions");
+ Deployer deployer = ((NodeFactoryImpl)nodeFactory).getDeployer();
+ Definitions systemDefinitions = deployer.getSystemDefinitions();
+ printXML(systemDefinitions);
+ }
+
+ @Test
+ public void listEndpoints(){
+ printTestName("listEndpoints");
+ ExtensionPointRegistry registry = ((NodeFactoryImpl)nodeFactory).getExtensionPointRegistry();
+ Map<Object, Node> nodes = ((NodeFactoryImpl)nodeFactory).getNodes();
+ Node firstNode = nodes.values().iterator().next();
+ NodeConfiguration firstNodeConfig = ((NodeImpl)firstNode).getConfiguration();
+
+ DomainRegistryFactory domainRegistryFactory = ExtensibleDomainRegistryFactory.getInstance(registry);
+ // TODO - I don't understand where the scheme gets set/used
+ EndpointRegistry endpointRegistry = domainRegistryFactory.getEndpointRegistry("vm:" + firstNodeConfig.getDomainRegistryURI(), firstNodeConfig.getDomainURI());
+
+ for (Endpoint endpoint : endpointRegistry.getEndpoints()){
+ System.out.println(endpoint);
+ printEndpointXML(endpoint);
+ }
+ }
+
+ // utils
+
+ private void printTestName(String name){
+ System.out.println("=====================================================================");
+ System.out.println(name);
+ System.out.println("=====================================================================");
+ }
+
+ private void printXML(Object model){
+ try {
+ ExtensionPointRegistry registry = ((NodeFactoryImpl)nodeFactory).getExtensionPointRegistry();
+ XMLInputFactory inputFactory = XMLInputFactory.newInstance();
+
+ StAXArtifactProcessorExtensionPoint xmlProcessors = registry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class);
+ StAXArtifactProcessor<Object> xmlProcessor = new ExtensibleStAXArtifactProcessor(xmlProcessors, inputFactory, null);
+
+ ProcessorContext context = new ProcessorContext(registry);
+
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ XMLOutputFactory outputFactory = registry.getExtensionPoint(FactoryExtensionPoint.class).getFactory(XMLOutputFactory.class);
+ XMLStreamWriter writer = new IndentingXMLStreamWriter(outputFactory.createXMLStreamWriter(bos));
+
+ xmlProcessor.write(model, writer, context);
+ writer.flush();
+
+ System.out.println(bos.toString());
+ } catch(Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+
+ // TODO - we don't have a processor registered for RuntimeEndpointImpl?
+ private void printEndpointXML(Endpoint model){
+ try {
+ ExtensionPointRegistry registry = ((NodeFactoryImpl)nodeFactory).getExtensionPointRegistry();
+ XMLInputFactory inputFactory = XMLInputFactory.newInstance();
+
+ StAXArtifactProcessorExtensionPoint xmlProcessors = registry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class);
+ StAXArtifactProcessor<Endpoint> xmlProcessor = xmlProcessors.getProcessor(Endpoint.class);
+
+ ProcessorContext context = new ProcessorContext(registry);
+
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ XMLOutputFactory outputFactory = registry.getExtensionPoint(FactoryExtensionPoint.class).getFactory(XMLOutputFactory.class);
+ XMLStreamWriter writer = new IndentingXMLStreamWriter(outputFactory.createXMLStreamWriter(bos));
+
+ xmlProcessor.write(model, writer, context);
+ writer.flush();
+
+ System.out.println(bos.toString());
+ } catch(Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+
+}
|