From 440ffbd09d670be213432f399549788ad2cb2826 Mon Sep 17 00:00:00 2001 From: slaws Date: Tue, 8 Nov 2011 09:23:01 +0000 Subject: bringing the static distributed osgi sample back into trunk again. Not in build yet git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1199163 13f79535-47bb-0310-9956-ffa450edef68 --- .../distributed-osgi-static/README.odt | Bin 0 -> 99272 bytes .../calculator-bundle/META-INF/MANIFEST.MF | 20 +++ .../OSGI-INF/blueprint/calculator-module.xml | 43 ++++++ .../OSGI-INF/calculator-component.xml | 36 +++++ .../OSGI-INF/sca/bundle.componentType | 64 ++++++++ .../OSGI-INF/sca/bundle.composite | 45 ++++++ .../calculator-bundle/pom.xml | 153 ++++++++++++++++++++ .../java/calculator/dosgi/CalculatorService.java | 36 +++++ .../calculator/dosgi/impl/CalculatorActivator.java | 76 ++++++++++ .../dosgi/impl/CalculatorServiceDSImpl.java | 114 +++++++++++++++ .../dosgi/impl/CalculatorServiceImpl.java | 105 ++++++++++++++ .../calculator/dosgi/operations/AddService.java | 31 ++++ .../calculator/dosgi/operations/DivideService.java | 31 ++++ .../dosgi/operations/MultiplyService.java | 31 ++++ .../dosgi/operations/SubtractService.java | 31 ++++ .../java/calculator/rmi/OperationsRMIServer.java | 93 ++++++++++++ .../calculator/rmi/OperationsRMIServer_Stub.java | 132 +++++++++++++++++ .../main/java/calculator/rmi/OperationsRemote.java | 37 +++++ .../main/resources/META-INF/sca-contribution.xml | 24 +++ .../java/calculator/dosgi/test/CalculatorNode.java | 43 ++++++ .../dosgi/test/CalculatorOSGiNodeTestCase.java | 152 +++++++++++++++++++ .../java/calculator/dosgi/test/OSGiTestUtils.java | 105 ++++++++++++++ .../operations-bundle/META-INF/MANIFEST.MF | 22 +++ .../operations-bundle/OSGI-INF/add-component.xml | 25 ++++ .../OSGI-INF/blueprint/operations-module.xml | 40 +++++ .../OSGI-INF/divide-component.xml | 25 ++++ .../OSGI-INF/multiply-component.xml | 25 ++++ .../OSGI-INF/sca/bundle.componentType | 54 +++++++ .../OSGI-INF/sca/bundle.composite | 43 ++++++ .../OSGI-INF/subtract-component.xml | 25 ++++ .../operations-bundle/pom.xml | 161 +++++++++++++++++++++ .../calculator/dosgi/operations/AddService.java | 31 ++++ .../calculator/dosgi/operations/DivideService.java | 31 ++++ .../dosgi/operations/MultiplyService.java | 31 ++++ .../dosgi/operations/SubtractService.java | 31 ++++ .../dosgi/operations/impl/AddServiceImpl.java | 37 +++++ .../dosgi/operations/impl/DivideServiceImpl.java | 37 +++++ .../dosgi/operations/impl/MultiplyServiceImpl.java | 37 +++++ .../dosgi/operations/impl/OperationsActivator.java | 88 +++++++++++ .../dosgi/operations/impl/SubtractServiceImpl.java | 37 +++++ .../main/resources/META-INF/sca-contribution.xml | 23 +++ .../dosgi/operations/test/OSGiTestUtils.java | 105 ++++++++++++++ .../dosgi/operations/test/OperationsNode.java | 43 ++++++ .../test/OperationsOSGiNodeTestCase.java | 105 ++++++++++++++ .../learning-more/distributed-osgi-static/pom.xml | 44 ++++++ 45 files changed, 2502 insertions(+) create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/README.odt create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/META-INF/MANIFEST.MF create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/OSGI-INF/blueprint/calculator-module.xml create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/OSGI-INF/calculator-component.xml create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/OSGI-INF/sca/bundle.componentType create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/OSGI-INF/sca/bundle.composite create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/pom.xml create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/CalculatorService.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/impl/CalculatorActivator.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/impl/CalculatorServiceDSImpl.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/impl/CalculatorServiceImpl.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/operations/AddService.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/operations/DivideService.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/operations/MultiplyService.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/operations/SubtractService.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/rmi/OperationsRMIServer.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/rmi/OperationsRMIServer_Stub.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/rmi/OperationsRemote.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/resources/META-INF/sca-contribution.xml create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/test/java/calculator/dosgi/test/CalculatorNode.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/test/java/calculator/dosgi/test/CalculatorOSGiNodeTestCase.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/test/java/calculator/dosgi/test/OSGiTestUtils.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/META-INF/MANIFEST.MF create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/add-component.xml create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/blueprint/operations-module.xml create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/divide-component.xml create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/multiply-component.xml create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/sca/bundle.componentType create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/sca/bundle.composite create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/subtract-component.xml create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/pom.xml create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/AddService.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/DivideService.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/MultiplyService.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/SubtractService.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/impl/AddServiceImpl.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/impl/DivideServiceImpl.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/impl/MultiplyServiceImpl.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/impl/OperationsActivator.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/impl/SubtractServiceImpl.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/resources/META-INF/sca-contribution.xml create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/test/java/calculator/dosgi/operations/test/OSGiTestUtils.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/test/java/calculator/dosgi/operations/test/OperationsNode.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/test/java/calculator/dosgi/operations/test/OperationsOSGiNodeTestCase.java create mode 100644 sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/pom.xml (limited to 'sca-java-2.x/trunk/samples/learning-more') diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/README.odt b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/README.odt new file mode 100644 index 0000000000..22b73cfc79 Binary files /dev/null and b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/README.odt differ diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/META-INF/MANIFEST.MF b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..542b53b085 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/META-INF/MANIFEST.MF @@ -0,0 +1,20 @@ +Manifest-Version: 1.0 +Export-Package: calculator.dosgi;version="1.0.0", + calculator.dosgi.operations;version="1.0.0" +Bundle-Version: 1.0.0 +Bundle-Name: calculator.dosgi +Bundle-Activator: calculator.dosgi.impl.CalculatorActivator +Bundle-ManifestVersion: 2 +Import-Package: org.oasisopen.sca.annotation;version="2.0.0", + org.osgi.framework, + org.osgi.service.component;resolution:=optional, + org.osgi.service.packageadmin, + org.osgi.util.tracker +Bundle-SymbolicName: calculator.dosgi +Bundle-Vendor: The Apache Software Foundation +Bundle-ActivationPolicy: lazy +Eclipse-LazyStart: true +Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt +Bundle-DocURL: http://www.apache.org/ +Service-Component-Disabled: OSGI-INF/calculator-component.xml +Bundle-RequiredExecutionEnvironment: J2SE-1.5,JavaSE-1.6 diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/OSGI-INF/blueprint/calculator-module.xml b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/OSGI-INF/blueprint/calculator-module.xml new file mode 100644 index 0000000000..fe0bbf21df --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/OSGI-INF/blueprint/calculator-module.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/OSGI-INF/calculator-component.xml b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/OSGI-INF/calculator-component.xml new file mode 100644 index 0000000000..457588bfb6 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/OSGI-INF/calculator-component.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/OSGI-INF/sca/bundle.componentType b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/OSGI-INF/sca/bundle.componentType new file mode 100644 index 0000000000..5e8e7ebcd3 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/OSGI-INF/sca/bundle.componentType @@ -0,0 +1,64 @@ + + + + + + + + + + 1 + ABC + + + + + + + + 1 + ABC + + + + + + 1 + ABC + + + + + + 1 + ABC + + + + + + 1 + ABC + + + + diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/OSGI-INF/sca/bundle.composite b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/OSGI-INF/sca/bundle.composite new file mode 100644 index 0000000000..7ebb64eb8f --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/OSGI-INF/sca/bundle.composite @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/pom.xml b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/pom.xml new file mode 100644 index 0000000000..d4488cf982 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/pom.xml @@ -0,0 +1,153 @@ + + + + 4.0.0 + + org.apache.tuscany.sca.samples + tuscany-samples + 2.0-SNAPSHOT + ../../../pom.xml + + + sample-calculator-bundle + Apache Tuscany SCA Sample OSGi Remote Services Calculator + + + + org.apache.tuscany.sca + tuscany-feature-ejava + 2.0-SNAPSHOT + pom + + + org.apache.tuscany.sca + tuscany-feature-webservice + 2.0-SNAPSHOT + pom + runtime + + + org.apache.tuscany.sca + tuscany-node-launcher-equinox + 2.0-SNAPSHOT + test + + + + org.apache.tuscany.sca + tuscany-node-impl-osgi + 2.0-SNAPSHOT + runtime + + + + org.eclipse.osgi + services + 3.2.0-v20090520-1800 + + + + junit + junit + 4.8.1 + test + + + + + org.eclipse.equinox + ds + 1.1.0-v20090601 + test + + + + org.eclipse.equinox + util + 1.0.100-v20090520-1800 + test + + + + + + ${project.artifactId} + + + maven-eclipse-plugin + 2.5.1 + + + org.eclipse.pde.ManifestBuilder + org.eclipse.jdt.core.javabuilder + + + org.eclipse.jdt.core.javanature + org.eclipse.pde.PluginNature + + + org.eclipse.jdt.launching.JRE_CONTAINER + + + + + + + maven-jar-plugin + + + ${basedir}/META-INF/MANIFEST.MF + + + + + + org.apache.tuscany.maven.plugins + maven-osgi-junit-plugin + 1.0 + + + org.apache.tuscany.sca + tuscany-node-launcher-equinox + ${project.version} + + + + + osgi-test + test + + test + + + + + osgi.configuration.area + ${project.build.directory}/equinox + + + + + + + + + + diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/CalculatorService.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/CalculatorService.java new file mode 100644 index 0000000000..796b1afe61 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/CalculatorService.java @@ -0,0 +1,36 @@ +/* + * 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.dosgi; + +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/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/impl/CalculatorActivator.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/impl/CalculatorActivator.java new file mode 100644 index 0000000000..b7babcc0af --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/impl/CalculatorActivator.java @@ -0,0 +1,76 @@ +/* + * 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.dosgi.impl; + +import java.util.Dictionary; +import java.util.Hashtable; +import java.util.logging.Logger; + +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; +import org.osgi.service.packageadmin.PackageAdmin; + +import calculator.dosgi.CalculatorService; +import calculator.dosgi.operations.AddService; + +/** + * + */ +public class CalculatorActivator implements BundleActivator { + private Logger logger = Logger.getLogger(CalculatorActivator.class.getName()); + + private Bundle getBundle(BundleContext bundleContext, Class cls) { + PackageAdmin packageAdmin = null; + // PackageAdmin is used to resolve bundles + ServiceReference ref = bundleContext.getServiceReference("org.osgi.service.packageadmin.PackageAdmin"); + if (ref != null) { + packageAdmin = (PackageAdmin)bundleContext.getService(ref); + Bundle bundle = packageAdmin.getBundle(cls); + if (bundle != null) { + logger.info(cls.getName() + " is loaded by bundle: " + bundle.getSymbolicName()); + } + bundleContext.ungetService(ref); + return bundle; + } + return null; + } + + public void start(BundleContext context) throws Exception { + logger.info("Starting " + context.getBundle()); + Dictionary props = new Hashtable(); + props.put("sca.service", "CalculatorComponent#service-name(Calculator)"); + props.put("calculator", "Calculator"); + + logger.info("Registering " + CalculatorService.class.getName()); + CalculatorService calculator = new CalculatorServiceImpl(context); + context.registerService(CalculatorService.class.getName(), calculator, props); + + getBundle(context, AddService.class); + + } + + public void stop(BundleContext context) throws Exception { + logger.info("Stopping " + context.getBundle()); + // Registered services will be automatically unregistered + } + +} diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/impl/CalculatorServiceDSImpl.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/impl/CalculatorServiceDSImpl.java new file mode 100644 index 0000000000..9c80c5beb9 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/impl/CalculatorServiceDSImpl.java @@ -0,0 +1,114 @@ +/* + * 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.dosgi.impl; + +import org.osgi.service.component.ComponentContext; + +import calculator.dosgi.CalculatorService; +import calculator.dosgi.operations.AddService; +import calculator.dosgi.operations.DivideService; +import calculator.dosgi.operations.MultiplyService; +import calculator.dosgi.operations.SubtractService; + +/** + * An implementation of the Calculator service. + */ +public class CalculatorServiceDSImpl implements CalculatorService { + private AddService addService; + private SubtractService subtractService; + private MultiplyService multiplyService; + private DivideService divideService; + + public CalculatorServiceDSImpl() { + super(); + System.out.println("CalculatorServiceDSImpl()"); + } + + protected void activate(ComponentContext context) { + System.out.println("Activating " + context); + } + + protected void deactivate(ComponentContext context) { + System.out.println("Deactivating " + context); + } + + /* + * The following setters can be used for DS injection + */ + public void setAddService(AddService addService) { + System.out.println("setAddService()"); + this.addService = addService; + } + + public void setSubtractService(SubtractService subtractService) { + this.subtractService = subtractService; + } + + public void setDivideService(DivideService divideService) { + this.divideService = divideService; + } + + public void setMultiplyService(MultiplyService multiplyService) { + this.multiplyService = multiplyService; + } + + /* + * The following setters can be used for DS injection + */ + public void unsetAddService(AddService addService) { + System.out.println("unsetAddService()"); + this.addService = null; + } + + public void unsetSubtractService(SubtractService subtractService) { + this.subtractService = null; + } + + public void unsetDivideService(DivideService divideService) { + this.divideService = null; + } + + public void unsetMultiplyService(MultiplyService multiplyService) { + this.multiplyService = null; + } + private T getService(Class cls) { + for (Object s : new Object[] {addService, subtractService, multiplyService, divideService}) { + if (cls.isInstance(s)) { + return cls.cast(s); + } + } + throw new IllegalStateException(cls.getSimpleName() + " is not available"); + } + + public double add(double n1, double n2) { + return getService(AddService.class).add(n1, n2); + } + + public double subtract(double n1, double n2) { + return getService(SubtractService.class).subtract(n1, n2); + } + + public double multiply(double n1, double n2) { + return getService(MultiplyService.class).multiply(n1, n2); + } + + public double divide(double n1, double n2) { + return getService(DivideService.class).divide(n1, n2); + } +} diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/impl/CalculatorServiceImpl.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/impl/CalculatorServiceImpl.java new file mode 100644 index 0000000000..906fabfff1 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/impl/CalculatorServiceImpl.java @@ -0,0 +1,105 @@ +/* + * 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.dosgi.impl; + +import static org.osgi.framework.Constants.OBJECTCLASS; + +import org.osgi.framework.BundleContext; +import org.osgi.framework.Filter; +import org.osgi.framework.InvalidSyntaxException; +import org.osgi.util.tracker.ServiceTracker; + +import calculator.dosgi.CalculatorService; +import calculator.dosgi.operations.AddService; +import calculator.dosgi.operations.DivideService; +import calculator.dosgi.operations.MultiplyService; +import calculator.dosgi.operations.SubtractService; + +/** + * An implementation of the Calculator service. + */ +public class CalculatorServiceImpl implements CalculatorService { + private ServiceTracker remoteServices; + private ServiceTracker localServices; + + public CalculatorServiceImpl() { + super(); + } + + public CalculatorServiceImpl(BundleContext context) { + super(); + Filter remoteFilter = null, localFilter = null; + try { + remoteFilter = + context.createFilter("(&(" + OBJECTCLASS + "=calculator.dosgi.operations.*) (service.imported=*))"); + localFilter = + context.createFilter("(&(" + OBJECTCLASS + "=calculator.dosgi.operations.*) (!(service.imported=*)))"); + } catch (InvalidSyntaxException e) { + e.printStackTrace(); + } + this.remoteServices = new ServiceTracker(context, remoteFilter, null); + remoteServices.open(); + this.localServices = new ServiceTracker(context, localFilter, null); + localServices.open(); + } + + private T getService(Class cls) { + try { + // Wait for 10 seconds until the remote services are imported + remoteServices.waitForService(10000); + } catch (InterruptedException e) { + throw new IllegalStateException(cls.getSimpleName() + " is not available"); + } + Object[] remoteObjects = remoteServices.getServices(); + if (remoteObjects != null) { + for (Object s : remoteObjects) { + if (cls.isInstance(s)) { + System.out.println("Remote service: " + s); + return cls.cast(s); + } + } + } + Object[] localObjects = localServices.getServices(); + if (localObjects != null) { + for (Object s : localObjects) { + if (cls.isInstance(s)) { + System.out.println("Local service: " + s); + return cls.cast(s); + } + } + } + throw new IllegalStateException(cls.getSimpleName() + " is not available"); + } + + public double add(double n1, double n2) { + return getService(AddService.class).add(n1, n2); + } + + public double subtract(double n1, double n2) { + return getService(SubtractService.class).subtract(n1, n2); + } + + public double multiply(double n1, double n2) { + return getService(MultiplyService.class).multiply(n1, n2); + } + + public double divide(double n1, double n2) { + return getService(DivideService.class).divide(n1, n2); + } +} diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/operations/AddService.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/operations/AddService.java new file mode 100644 index 0000000000..7d68dbd5c5 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/operations/AddService.java @@ -0,0 +1,31 @@ +/* + * 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.dosgi.operations; + +import org.oasisopen.sca.annotation.Remotable; + +/** + * The interface for the add service + */ +@Remotable +public interface AddService { + + double add(double n1, double n2); + +} diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/operations/DivideService.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/operations/DivideService.java new file mode 100644 index 0000000000..998f6e9554 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/operations/DivideService.java @@ -0,0 +1,31 @@ +/* + * 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.dosgi.operations; + +import org.oasisopen.sca.annotation.Remotable; + +/** + * The interface for the divide service + */ +@Remotable +public interface DivideService { + + double divide(double n1, double n2); + +} diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/operations/MultiplyService.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/operations/MultiplyService.java new file mode 100644 index 0000000000..7ed6db2e6b --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/operations/MultiplyService.java @@ -0,0 +1,31 @@ +/* + * 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.dosgi.operations; + +import org.oasisopen.sca.annotation.Remotable; + +/** + * The interface for the multiply service + */ +@Remotable +public interface MultiplyService { + + double multiply(double n1, double n2); + +} diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/operations/SubtractService.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/operations/SubtractService.java new file mode 100644 index 0000000000..cb98b8fb67 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/dosgi/operations/SubtractService.java @@ -0,0 +1,31 @@ +/* + * 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.dosgi.operations; + +import org.oasisopen.sca.annotation.Remotable; + +/** + * The interface for the subtract service + */ +@Remotable +public interface SubtractService { + + double subtract(double n1, double n2); + +} diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/rmi/OperationsRMIServer.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/rmi/OperationsRMIServer.java new file mode 100644 index 0000000000..1a3e36f571 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/rmi/OperationsRMIServer.java @@ -0,0 +1,93 @@ +/* + * 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.rmi; + +import java.io.Serializable; +import java.rmi.Remote; +import java.rmi.RemoteException; +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; +import java.rmi.server.UnicastRemoteObject; + +/** + * + */ +public class OperationsRMIServer implements OperationsRemote, Serializable { + + private static final long serialVersionUID = 6081008315263103012L; + private transient Registry registry; + + public OperationsRMIServer() throws RemoteException { + super(); + } + + public double add(double n1, double n2) { + return n1 + n2; + } + + public double subtract(double n1, double n2) { + return n1 - n2; + } + + public double divide(double n1, double n2) { + return n1 / n2; + } + + public double multiply(double n1, double n2) { + return n1 * n2; + } + + public void start() throws RemoteException { + Thread thread = new Thread() { + public void run() { + try { + System.out.println("Starting the RMI server for calculator operations..."); + Remote stub = UnicastRemoteObject.exportObject(OperationsRMIServer.this); + registry = LocateRegistry.createRegistry(8085); + registry.bind("AddService", stub); + registry.bind("SubtractService", stub); + registry.bind("MultiplyService", stub); + registry.bind("DivideService", stub); + System.out.println("RMI server for calculator operations is now started."); + } catch (Exception e) { + e.printStackTrace(); + } + } + }; + thread.start(); + } + + public void stop() { + if (registry != null) { + try { + registry.unbind("AddService"); + registry.unbind("SubtractService"); + registry.unbind("MultiplyService"); + registry.unbind("DivideService"); + UnicastRemoteObject.unexportObject(this, false); + UnicastRemoteObject.unexportObject(registry, false); + registry = null; + } catch (Exception e) { + e.printStackTrace(); + } + } + } + +} diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/rmi/OperationsRMIServer_Stub.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/rmi/OperationsRMIServer_Stub.java new file mode 100644 index 0000000000..d573f6b8fa --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/rmi/OperationsRMIServer_Stub.java @@ -0,0 +1,132 @@ +/* + * 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. + */ + +// Stub class generated by rmic, do not edit. +// Contents subject to change without notice. +package calculator.rmi; + +public final class OperationsRMIServer_Stub extends java.rmi.server.RemoteStub implements calculator.rmi.OperationsRemote { + private static final long serialVersionUID = 2; + + private static java.lang.reflect.Method $method_add_0; + private static java.lang.reflect.Method $method_divide_1; + private static java.lang.reflect.Method $method_multiply_2; + private static java.lang.reflect.Method $method_subtract_3; + + static { + try { + $method_add_0 = + calculator.rmi.OperationsRemote.class.getMethod("add", new java.lang.Class[] {double.class, double.class}); + $method_divide_1 = + calculator.rmi.OperationsRemote.class.getMethod("divide", + new java.lang.Class[] {double.class, double.class}); + $method_multiply_2 = + calculator.rmi.OperationsRemote.class.getMethod("multiply", new java.lang.Class[] {double.class, + double.class}); + $method_subtract_3 = + calculator.rmi.OperationsRemote.class.getMethod("subtract", new java.lang.Class[] {double.class, + double.class}); + } catch (java.lang.NoSuchMethodException e) { + throw new java.lang.NoSuchMethodError("stub class initialization failed"); + } + } + + // constructors + public OperationsRMIServer_Stub(java.rmi.server.RemoteRef ref) { + super(ref); + } + + // methods from remote interfaces + + // implementation of add(double, double) + public double add(double $param_double_1, double $param_double_2) throws java.rmi.RemoteException { + try { + Object $result = + ref.invoke(this, + $method_add_0, + new java.lang.Object[] {new java.lang.Double($param_double_1), + new java.lang.Double($param_double_2)}, + 864055858262779977L); + return ((java.lang.Double)$result).doubleValue(); + } catch (java.lang.RuntimeException e) { + throw e; + } catch (java.rmi.RemoteException e) { + throw e; + } catch (java.lang.Exception e) { + throw new java.rmi.UnexpectedException("undeclared checked exception", e); + } + } + + // implementation of divide(double, double) + public double divide(double $param_double_1, double $param_double_2) throws java.rmi.RemoteException { + try { + Object $result = + ref.invoke(this, + $method_divide_1, + new java.lang.Object[] {new java.lang.Double($param_double_1), + new java.lang.Double($param_double_2)}, + 8097593626497421928L); + return ((java.lang.Double)$result).doubleValue(); + } catch (java.lang.RuntimeException e) { + throw e; + } catch (java.rmi.RemoteException e) { + throw e; + } catch (java.lang.Exception e) { + throw new java.rmi.UnexpectedException("undeclared checked exception", e); + } + } + + // implementation of multiply(double, double) + public double multiply(double $param_double_1, double $param_double_2) throws java.rmi.RemoteException { + try { + Object $result = + ref.invoke(this, + $method_multiply_2, + new java.lang.Object[] {new java.lang.Double($param_double_1), + new java.lang.Double($param_double_2)}, + -346155016949350695L); + return ((java.lang.Double)$result).doubleValue(); + } catch (java.lang.RuntimeException e) { + throw e; + } catch (java.rmi.RemoteException e) { + throw e; + } catch (java.lang.Exception e) { + throw new java.rmi.UnexpectedException("undeclared checked exception", e); + } + } + + // implementation of subtract(double, double) + public double subtract(double $param_double_1, double $param_double_2) throws java.rmi.RemoteException { + try { + Object $result = + ref.invoke(this, + $method_subtract_3, + new java.lang.Object[] {new java.lang.Double($param_double_1), + new java.lang.Double($param_double_2)}, + -610707357620578750L); + return ((java.lang.Double)$result).doubleValue(); + } catch (java.lang.RuntimeException e) { + throw e; + } catch (java.rmi.RemoteException e) { + throw e; + } catch (java.lang.Exception e) { + throw new java.rmi.UnexpectedException("undeclared checked exception", e); + } + } +} diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/rmi/OperationsRemote.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/rmi/OperationsRemote.java new file mode 100644 index 0000000000..d735ce8172 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/java/calculator/rmi/OperationsRemote.java @@ -0,0 +1,37 @@ +/* + * 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.rmi; + +import java.rmi.Remote; +import java.rmi.RemoteException; + +/** + * RMI Remote interface + */ +public interface OperationsRemote extends Remote { + double add(double n1, double n2) throws RemoteException; + + double subtract(double n1, double n2) throws RemoteException; + + double multiply(double n1, double n2) throws RemoteException; + + double divide(double n1, double n2) throws RemoteException; + +} diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/resources/META-INF/sca-contribution.xml b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/resources/META-INF/sca-contribution.xml new file mode 100644 index 0000000000..2fae5f5d28 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/main/resources/META-INF/sca-contribution.xml @@ -0,0 +1,24 @@ + + + + + + diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/test/java/calculator/dosgi/test/CalculatorNode.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/test/java/calculator/dosgi/test/CalculatorNode.java new file mode 100644 index 0000000000..7f96067764 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/test/java/calculator/dosgi/test/CalculatorNode.java @@ -0,0 +1,43 @@ +/* + * 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.dosgi.test; + +import org.apache.tuscany.sca.node.equinox.launcher.NodeLauncher; + +/** + * + */ +public class CalculatorNode { + + /** + * @param args + */ + public static void main(String[] args) { + if (args.length == 0) { + args = new String[] {"-bundles"}; + } + try { + NodeLauncher.main(args); + } catch (Exception e) { + e.printStackTrace(); + } + } + +} diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/test/java/calculator/dosgi/test/CalculatorOSGiNodeTestCase.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/test/java/calculator/dosgi/test/CalculatorOSGiNodeTestCase.java new file mode 100644 index 0000000000..96a5f7d4eb --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/calculator-bundle/src/test/java/calculator/dosgi/test/CalculatorOSGiNodeTestCase.java @@ -0,0 +1,152 @@ +/* + * 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.dosgi.test; + +import static calculator.dosgi.test.OSGiTestUtils.bundleStatus; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.net.URL; + +import org.apache.tuscany.sca.node.equinox.launcher.EquinoxHost; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Constants; +import org.osgi.framework.ServiceReference; + +import calculator.dosgi.CalculatorService; +import calculator.rmi.OperationsRMIServer; + +/** + * + */ +public class CalculatorOSGiNodeTestCase { + private static EquinoxHost host; + private static BundleContext context; + private static Bundle calculatorBundle; + private static OperationsRMIServer rmiServer; + + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + try { + rmiServer = new OperationsRMIServer(); + rmiServer.start(); + + host = new EquinoxHost(); + context = host.start(); + + for (Bundle b : context.getBundles()) { + System.out.println(b); + // debug to print out exported packages + // handy if you want to find aplit packages + //Object exports = b.getHeaders().get(Constants.EXPORT_PACKAGE); + //if (exports != null){ + // System.out.println(exports.toString()); + //} + if (b.getSymbolicName().equals("org.eclipse.equinox.ds") || b.getSymbolicName() + .startsWith("org.apache.tuscany.sca.")) { + try { + if (b.getHeaders().get(Constants.FRAGMENT_HOST) == null) { + // Start the non-fragment bundle + b.start(); + } + } catch (Exception e) { + e.printStackTrace(); + } + System.out.println(bundleStatus(b, false)); + } else { + System.out.println(bundleStatus(b, false)); + } + if ("calculator.dosgi".equals(b.getSymbolicName())) { + calculatorBundle = b; + } + } + + if (calculatorBundle != null) { + calculatorBundle.start(); + System.out.println(bundleStatus(calculatorBundle, false)); + } + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testOSGi() { + ServiceReference ref = + calculatorBundle.getBundleContext().getServiceReference(CalculatorService.class.getName()); + Assert.assertNotNull(ref); + Object service = context.getService(ref); + Assert.assertNotNull(service); + CalculatorService calculator = OSGiTestUtils.cast(service, CalculatorService.class); + System.out.println("2.0 + 1.0 = " + calculator.add(2.0, 1.0)); + System.out.println("2.0 - 1.0 = " + calculator.subtract(2.0, 1.0)); + System.out.println("2.0 * 1.0 = " + calculator.multiply(2.0, 1.0)); + System.out.println("2.0 / 1.0 = " + calculator.divide(2.0, 1.0)); + } + + @Test + /** + * Test the Web service exposed by the Calculator + */ + public void testWS() throws Exception { + URL url = new URL("http://localhost:8086/CalculatorService?wsdl"); + InputStream is = url.openStream(); + Reader reader = new InputStreamReader(is); + char[] content = new char[10240]; // 10k + int len = 0; + while (true) { + int size = reader.read(content, len, content.length - len); + if (size < 0) { + break; + } + len += size; + } + Assert.assertTrue(len > 0); + String str = new String(content, 0, len); + System.out.println(str); + Assert.assertTrue(str.indexOf(" + * @param obj + * @param cls + * @return + */ + public static T cast(Object obj, Class cls) { + if (cls.isInstance(obj)) { + return cls.cast(obj); + } else { + return cls.cast(Proxy.newProxyInstance(cls.getClassLoader(), + new Class[] {cls}, + new InvocationHandlerImpl(obj))); + } + } +} diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/META-INF/MANIFEST.MF b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..82a803e3a6 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/META-INF/MANIFEST.MF @@ -0,0 +1,22 @@ +Manifest-Version: 1.0 +Export-Package: calculator.dosgi.operations;version="1.0.0" +Bundle-Version: 1.0.0 +Bundle-Name: calculator.dosgi.operations +Bundle-Activator: calculator.dosgi.operations.impl.OperationsActivator +Bundle-ManifestVersion: 2 +Import-Package: calculator.dosgi.operations;version="[1.0.0,1.0.0]", + org.oasisopen.sca.annotation;version="2.0.0", + org.osgi.framework, + org.osgi.service.component;resolution:=optional, + org.osgi.service.packageadmin +Bundle-SymbolicName: calculator.dosgi.operations +Bundle-Vendor: The Apache Software Foundation +Bundle-ActivationPolicy: lazy +Eclipse-LazyStart: true +Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt +Bundle-DocURL: http://www.apache.org/ +Service-Component-Disabled: OSGI-INF/add-component.xml, + OSGI-INF/subtract-component.xml, + OSGI-INF/multiply-component.xml, + OSGI-INF/divide-component.xml +Bundle-RequiredExecutionEnvironment: J2SE-1.5,JavaSE-1.6 diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/add-component.xml b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/add-component.xml new file mode 100644 index 0000000000..bf4ccc3d28 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/add-component.xml @@ -0,0 +1,25 @@ + + + + + + + + diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/blueprint/operations-module.xml b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/blueprint/operations-module.xml new file mode 100644 index 0000000000..e3b690f24a --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/blueprint/operations-module.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/divide-component.xml b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/divide-component.xml new file mode 100644 index 0000000000..b939a5de80 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/divide-component.xml @@ -0,0 +1,25 @@ + + + + + + + + diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/multiply-component.xml b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/multiply-component.xml new file mode 100644 index 0000000000..2bf897d72a --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/multiply-component.xml @@ -0,0 +1,25 @@ + + + + + + + + diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/sca/bundle.componentType b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/sca/bundle.componentType new file mode 100644 index 0000000000..74704be28d --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/sca/bundle.componentType @@ -0,0 +1,54 @@ + + + + + + + + + + 1 + ABC + + + + + + 1 + ABC + + + + + + 1 + ABC + + + + + + 1 + ABC + + + diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/sca/bundle.composite b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/sca/bundle.composite new file mode 100644 index 0000000000..816ba3c89c --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/sca/bundle.composite @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/subtract-component.xml b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/subtract-component.xml new file mode 100644 index 0000000000..4af00695da --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/OSGI-INF/subtract-component.xml @@ -0,0 +1,25 @@ + + + + + + + + diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/pom.xml b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/pom.xml new file mode 100644 index 0000000000..b1790f1b4d --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/pom.xml @@ -0,0 +1,161 @@ + + + + 4.0.0 + + org.apache.tuscany.sca.samples + tuscany-samples + 2.0-SNAPSHOT + ../../../pom.xml + + + sample-operations-bundle + Apache Tuscany SCA Sample OSGi Remote Services Calculator Operations + + + + org.apache.tuscany.sca + tuscany-base-runtime-pom + 2.0-SNAPSHOT + pom + + + org.apache.tuscany.sca + tuscany-osgi-runtime-pom + 2.0-SNAPSHOT + pom + + + org.apache.tuscany.sca + tuscany-binding-rmi-runtime + 2.0-SNAPSHOT + + + + org.apache.tuscany.sca + tuscany-node-launcher-equinox + 2.0-SNAPSHOT + test + + + + org.apache.tuscany.sca + tuscany-node-impl-osgi + 2.0-SNAPSHOT + runtime + + + + org.eclipse.osgi + services + 3.2.0-v20090520-1800 + test + + + + + org.eclipse.equinox + ds + 1.1.0-v20090601 + test + + + + org.eclipse.equinox + util + 1.0.100-v20090520-1800 + test + + + + junit + junit + 4.8.1 + test + + + + + + ${project.artifactId} + + + maven-eclipse-plugin + 2.5.1 + + + org.eclipse.pde.ManifestBuilder + org.eclipse.jdt.core.javabuilder + + + org.eclipse.jdt.core.javanature + org.eclipse.pde.PluginNature + + + org.eclipse.jdt.launching.JRE_CONTAINER + + + + + + + maven-jar-plugin + + + ${basedir}/META-INF/MANIFEST.MF + + + + + + + + + diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/AddService.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/AddService.java new file mode 100644 index 0000000000..7d68dbd5c5 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/AddService.java @@ -0,0 +1,31 @@ +/* + * 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.dosgi.operations; + +import org.oasisopen.sca.annotation.Remotable; + +/** + * The interface for the add service + */ +@Remotable +public interface AddService { + + double add(double n1, double n2); + +} diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/DivideService.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/DivideService.java new file mode 100644 index 0000000000..998f6e9554 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/DivideService.java @@ -0,0 +1,31 @@ +/* + * 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.dosgi.operations; + +import org.oasisopen.sca.annotation.Remotable; + +/** + * The interface for the divide service + */ +@Remotable +public interface DivideService { + + double divide(double n1, double n2); + +} diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/MultiplyService.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/MultiplyService.java new file mode 100644 index 0000000000..7ed6db2e6b --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/MultiplyService.java @@ -0,0 +1,31 @@ +/* + * 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.dosgi.operations; + +import org.oasisopen.sca.annotation.Remotable; + +/** + * The interface for the multiply service + */ +@Remotable +public interface MultiplyService { + + double multiply(double n1, double n2); + +} diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/SubtractService.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/SubtractService.java new file mode 100644 index 0000000000..cb98b8fb67 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/SubtractService.java @@ -0,0 +1,31 @@ +/* + * 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.dosgi.operations; + +import org.oasisopen.sca.annotation.Remotable; + +/** + * The interface for the subtract service + */ +@Remotable +public interface SubtractService { + + double subtract(double n1, double n2); + +} diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/impl/AddServiceImpl.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/impl/AddServiceImpl.java new file mode 100644 index 0000000000..e0a6622136 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/impl/AddServiceImpl.java @@ -0,0 +1,37 @@ +/* + * 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.dosgi.operations.impl; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import calculator.dosgi.operations.AddService; + +/** + * An implementation of the Add service + */ +public class AddServiceImpl implements AddService { + + public double add(double n1, double n2) { + Logger logger = Logger.getLogger("calculator"); + logger.log(Level.INFO, "Adding " + n1 + " and " + n2); + return n1 + n2; + } + +} diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/impl/DivideServiceImpl.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/impl/DivideServiceImpl.java new file mode 100644 index 0000000000..028f860885 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/impl/DivideServiceImpl.java @@ -0,0 +1,37 @@ +/* + * 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.dosgi.operations.impl; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import calculator.dosgi.operations.DivideService; + +/** + * An implementation of the Divide service. + */ +public class DivideServiceImpl implements DivideService { + + public double divide(double n1, double n2) { + Logger logger = Logger.getLogger("calculator"); + logger.log(Level.INFO, "Dividing " + n1 + " with " + n2); + return n1 / n2; + } + +} diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/impl/MultiplyServiceImpl.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/impl/MultiplyServiceImpl.java new file mode 100644 index 0000000000..e928317c2e --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/impl/MultiplyServiceImpl.java @@ -0,0 +1,37 @@ +/* + * 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.dosgi.operations.impl; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import calculator.dosgi.operations.MultiplyService; + +/** + * An implementation of the Multiply service. + */ +public class MultiplyServiceImpl implements MultiplyService { + + public double multiply(double n1, double n2) { + Logger logger = Logger.getLogger("calculator"); + logger.log(Level.INFO, "Multiplying " + n1 + " with " + n2); + return n1 * n2; + } + +} diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/impl/OperationsActivator.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/impl/OperationsActivator.java new file mode 100644 index 0000000000..6bc14c1c78 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/impl/OperationsActivator.java @@ -0,0 +1,88 @@ +/* + * 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.dosgi.operations.impl; + +import java.util.Dictionary; +import java.util.Hashtable; +import java.util.logging.Logger; + +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; +import org.osgi.service.packageadmin.PackageAdmin; + +import calculator.dosgi.operations.AddService; +import calculator.dosgi.operations.DivideService; +import calculator.dosgi.operations.MultiplyService; +import calculator.dosgi.operations.SubtractService; + +/** + * + */ +public class OperationsActivator implements BundleActivator { + private Logger logger = Logger.getLogger(OperationsActivator.class.getName()); + + public void start(BundleContext context) throws Exception { + logger.info("Starting " + context.getBundle()); + + Dictionary props = new Hashtable(); + + logger.info("Registering " + AddService.class.getName()); + props.put("sca.service", "AddComponent#service-name(Add)"); + context.registerService(AddService.class.getName(), new AddServiceImpl(), props); + + logger.info("Registering " + SubtractService.class.getName()); + props.put("sca.service", "SubtractComponent#service-name(Subtract)"); + context.registerService(SubtractService.class.getName(), new SubtractServiceImpl(), props); + + logger.info("Registering " + MultiplyService.class.getName()); + props.put("sca.service", "MultiplyComponent#service-name(Multiply)"); + context.registerService(MultiplyService.class.getName(), new MultiplyServiceImpl(), props); + + logger.info("Registering " + DivideService.class.getName()); + props.put("sca.service", "DivideComponent#service-name(Divide)"); + context.registerService(DivideService.class.getName(), new DivideServiceImpl(), props); + + getBundle(context, AddService.class); + } + + public void stop(BundleContext context) throws Exception { + logger.info("Stopping " + context.getBundle()); + // Registered services will be automatically unregistered + } + + private Bundle getBundle(BundleContext bundleContext, Class cls) { + PackageAdmin packageAdmin = null; + // PackageAdmin is used to resolve bundles + ServiceReference ref = bundleContext.getServiceReference("org.osgi.service.packageadmin.PackageAdmin"); + if (ref != null) { + packageAdmin = (PackageAdmin)bundleContext.getService(ref); + Bundle bundle = packageAdmin.getBundle(cls); + if (bundle != null) { + logger.info(cls.getName() + " is loaded by bundle: " + bundle.getSymbolicName()); + } + bundleContext.ungetService(ref); + return bundle; + } + return null; + } + +} diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/impl/SubtractServiceImpl.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/impl/SubtractServiceImpl.java new file mode 100644 index 0000000000..565670963f --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/java/calculator/dosgi/operations/impl/SubtractServiceImpl.java @@ -0,0 +1,37 @@ +/* + * 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.dosgi.operations.impl; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import calculator.dosgi.operations.SubtractService; + +/** + * An implementation of the subtract service. + */ +public class SubtractServiceImpl implements SubtractService { + + public double subtract(double n1, double n2) { + Logger logger = Logger.getLogger("calculator"); + logger.log(Level.INFO, "Subtracting " + n1 + " from " + n2); + return n1 - n2; + } + +} diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/resources/META-INF/sca-contribution.xml b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/resources/META-INF/sca-contribution.xml new file mode 100644 index 0000000000..66d93248f0 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/main/resources/META-INF/sca-contribution.xml @@ -0,0 +1,23 @@ + + + + + diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/test/java/calculator/dosgi/operations/test/OSGiTestUtils.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/test/java/calculator/dosgi/operations/test/OSGiTestUtils.java new file mode 100644 index 0000000000..3407507e3b --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/test/java/calculator/dosgi/operations/test/OSGiTestUtils.java @@ -0,0 +1,105 @@ +/* + * 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.dosgi.operations.test; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +import org.osgi.framework.Bundle; + +/** + * + * Utility class to create OSGi bundles + * + * @version $Rev: 796166 $ $Date: 2009-07-21 08:03:47 +0100 (Tue, 21 Jul 2009) $ + */ +public class OSGiTestUtils { + private static class InvocationHandlerImpl implements InvocationHandler { + private Object instance; + + public InvocationHandlerImpl(Object instance) { + super(); + this.instance = instance; + } + + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + Method m = instance.getClass().getMethod(method.getName(), method.getParameterTypes()); + return m.invoke(instance, args); + } + + } + + /** + * Returns a string representation of the given bundle. + * + * @param b + * @param verbose + * @return + */ + public static String bundleStatus(Bundle bundle, boolean verbose) { + StringBuffer sb = new StringBuffer(); + sb.append(bundle.getBundleId()).append(" ").append(bundle.getSymbolicName()); + int s = bundle.getState(); + if ((s & Bundle.UNINSTALLED) != 0) { + sb.append(" UNINSTALLED"); + } + if ((s & Bundle.INSTALLED) != 0) { + sb.append(" INSTALLED"); + } + if ((s & Bundle.RESOLVED) != 0) { + sb.append(" RESOLVED"); + } + if ((s & Bundle.STARTING) != 0) { + sb.append(" STARTING"); + } + if ((s & Bundle.STOPPING) != 0) { + sb.append(" STOPPING"); + } + if ((s & Bundle.ACTIVE) != 0) { + sb.append(" ACTIVE"); + } + + if (verbose) { + sb.append(" ").append(bundle.getLocation()); + sb.append(" ").append(bundle.getHeaders()); + } + return sb.toString(); + } + + /** + * A utility to cast the object to the given interface. If the class for the object + * is loaded by a different classloader, a proxy will be created. + * + * @param + * @param obj + * @param cls + * @return + */ + public static T cast(Object obj, Class cls) { + if (cls.isInstance(obj)) { + return cls.cast(obj); + } else { + return cls.cast(Proxy.newProxyInstance(cls.getClassLoader(), + new Class[] {cls}, + new InvocationHandlerImpl(obj))); + } + } +} diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/test/java/calculator/dosgi/operations/test/OperationsNode.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/test/java/calculator/dosgi/operations/test/OperationsNode.java new file mode 100644 index 0000000000..faf61d11e4 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/test/java/calculator/dosgi/operations/test/OperationsNode.java @@ -0,0 +1,43 @@ +/* + * 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.dosgi.operations.test; + +import org.apache.tuscany.sca.node.equinox.launcher.NodeLauncher; + +/** + * + */ +public class OperationsNode { + + /** + * @param args + */ + public static void main(String[] args) { + if (args.length == 0) { + args = new String[] {"-bundles"}; + } + try { + NodeLauncher.main(args); + } catch (Exception e) { + e.printStackTrace(); + } + } + +} diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/test/java/calculator/dosgi/operations/test/OperationsOSGiNodeTestCase.java b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/test/java/calculator/dosgi/operations/test/OperationsOSGiNodeTestCase.java new file mode 100644 index 0000000000..ef58d1f60b --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/operations-bundle/src/test/java/calculator/dosgi/operations/test/OperationsOSGiNodeTestCase.java @@ -0,0 +1,105 @@ +/* + * 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.dosgi.operations.test; + +import static calculator.dosgi.operations.test.OSGiTestUtils.bundleStatus; + +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; + +import org.apache.tuscany.sca.node.equinox.launcher.EquinoxHost; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Constants; + +import calculator.dosgi.operations.AddService; + +/** + * + */ +public class OperationsOSGiNodeTestCase { + private static EquinoxHost host; + private static BundleContext context; + private static Bundle operationsBundle; + + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + try { + host = new EquinoxHost(); + context = host.start(); + + for (Bundle b : context.getBundles()) { + if (b.getSymbolicName().equals("org.eclipse.equinox.ds") || b.getSymbolicName() + .startsWith("org.apache.tuscany.sca.")) { + try { + if (b.getHeaders().get(Constants.FRAGMENT_HOST) == null) { + // Start the non-fragment bundle + b.start(); + } + } catch (Exception e) { + System.out.println("Error for bundle: " + b.getSymbolicName()); + e.printStackTrace(); + } + System.out.println(bundleStatus(b, false)); + } + if ("calculator.dosgi.operations".equals(b.getSymbolicName())) { + operationsBundle = b; + } + } + + if (operationsBundle != null) { + operationsBundle.start(); + System.out.println(bundleStatus(operationsBundle, false)); + } + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testOSGi() throws Exception { + Registry registry = LocateRegistry.getRegistry(8085); + Object add = registry.lookup("AddService"); + AddService addService = OSGiTestUtils.cast(add, AddService.class); + double sum = addService.add(1.0, 2.0); + Assert.assertEquals(3.0, sum, 0.0); + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + if (host != null) { + host.stop(); + context = null; + } + } + +} diff --git a/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/pom.xml b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/pom.xml new file mode 100644 index 0000000000..7e77997f36 --- /dev/null +++ b/sca-java-2.x/trunk/samples/learning-more/distributed-osgi-static/pom.xml @@ -0,0 +1,44 @@ + + + + 4.0.0 + + org.apache.tuscany.sca.samples + tuscany-samples-learning-more + 2.0-SNAPSHOT + ../pom.xml + + tuscany-samples-distributed-osgi-static + pom + Apache Tuscany SCA Distributed OSGI Statically Configured Samples + + + + default + + true + + + calculator-bundle + operations-bundle + + + + -- cgit v1.2.3