diff options
Diffstat (limited to 'java')
15 files changed, 162 insertions, 28 deletions
diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorActivator.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorActivator.java index a7b6b748b1..b06474cb46 100644 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorActivator.java +++ b/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorActivator.java @@ -21,6 +21,7 @@ package calculator; import java.util.Dictionary; import java.util.Hashtable; +import java.util.logging.Logger; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; @@ -30,18 +31,20 @@ import org.osgi.framework.ServiceRegistration; * */ public class CalculatorActivator implements BundleActivator { + private Logger logger = Logger.getLogger(CalculatorActivator.class.getName()); + private ServiceRegistration registration; public void start(BundleContext context) throws Exception { Dictionary<String, Object> props = new Hashtable<String, Object>(); props.put("sca.service", "CalculatorComponent#service-name(Calculator)"); - registration = context.registerService(CalculatorService.class.getName(), - new CalculatorServiceImpl(), - props); + logger.info("Registering " + CalculatorService.class.getName()); + registration = context.registerService(CalculatorService.class.getName(), new CalculatorServiceImpl(), props); } public void stop(BundleContext context) throws Exception { - context.ungetService(registration.getReference()); + logger.info("UnRegistering " + registration); + registration.unregister(); } } diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorServiceImpl.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorServiceImpl.java index bcfd8871b6..c2c65463ea 100644 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorServiceImpl.java +++ b/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorServiceImpl.java @@ -18,7 +18,11 @@ */ package calculator; -import org.oasisopen.sca.annotation.Reference; +import calculator.operations.AddService; +import calculator.operations.DivideService; +import calculator.operations.MultiplyService; +import calculator.operations.SubtractService; + /** * An implementation of the Calculator service. @@ -30,22 +34,18 @@ public class CalculatorServiceImpl implements CalculatorService { private MultiplyService multiplyService; private DivideService divideService; - @Reference public void setAddService(AddService addService) { this.addService = addService; } - @Reference public void setSubtractService(SubtractService subtractService) { this.subtractService = subtractService; } - @Reference public void setDivideService(DivideService divideService) { this.divideService = divideService; } - @Reference public void setMultiplyService(MultiplyService multiplyService) { this.multiplyService = multiplyService; } diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/AddService.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/AddService.java index 19e43317eb..2fe405a322 100644 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/AddService.java +++ b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/AddService.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package calculator; +package calculator.operations; import org.oasisopen.sca.annotation.Remotable; diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/AddServiceImpl.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/AddServiceImpl.java index 0865ddb5fe..685d641b1c 100644 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/AddServiceImpl.java +++ b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/AddServiceImpl.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package calculator; +package calculator.operations; import java.util.logging.Level; import java.util.logging.Logger; diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/DivideService.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/DivideService.java index ac05068e56..dfc5df6784 100644 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/DivideService.java +++ b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/DivideService.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package calculator; +package calculator.operations; import org.oasisopen.sca.annotation.Remotable; diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/DivideServiceImpl.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/DivideServiceImpl.java index cb1fe7ed48..945b0315b0 100644 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/DivideServiceImpl.java +++ b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/DivideServiceImpl.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package calculator; +package calculator.operations; import java.util.logging.Level; import java.util.logging.Logger; diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/MultiplyService.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/MultiplyService.java index c89e10e9e2..b70c233f1a 100644 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/MultiplyService.java +++ b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/MultiplyService.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package calculator; +package calculator.operations; import org.oasisopen.sca.annotation.Remotable; diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/MultiplyServiceImpl.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/MultiplyServiceImpl.java index 0b86bda2b7..175322c14d 100644 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/MultiplyServiceImpl.java +++ b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/MultiplyServiceImpl.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package calculator; +package calculator.operations; import java.util.logging.Level; import java.util.logging.Logger; diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/OperationsActivator.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/OperationsActivator.java index 4968191a0e..03260b312b 100644 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/OperationsActivator.java +++ b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/OperationsActivator.java @@ -17,7 +17,7 @@ * under the License. */ -package calculator; +package calculator.operations; import java.util.ArrayList; import java.util.Dictionary; @@ -39,12 +39,20 @@ public class OperationsActivator implements BundleActivator { public void start(BundleContext context) throws Exception { Dictionary<String, Object> props = new Hashtable<String, Object>(); + + logger.info("Registering " + AddService.class.getName()); props.put("sca.service", "AddComponent#service-name(Add)"); registrations.add(context.registerService(AddService.class.getName(), new AddServiceImpl(), props)); + + logger.info("Registering " + SubtractService.class.getName()); props.put("sca.service", "SubtractComponent#service-name(Subtract)"); registrations.add(context.registerService(SubtractService.class.getName(), new SubtractServiceImpl(), props)); + + logger.info("Registering " + MultiplyService.class.getName()); props.put("sca.service", "MultiplyComponent#service-name(Multiply)"); registrations.add(context.registerService(MultiplyService.class.getName(), new MultiplyServiceImpl(), props)); + + logger.info("Registering " + DivideService.class.getName()); props.put("sca.service", "DivideComponent#service-name(Divide)"); registrations.add(context.registerService(DivideService.class.getName(), new DivideServiceImpl(), props)); } @@ -52,7 +60,7 @@ public class OperationsActivator implements BundleActivator { public void stop(BundleContext context) throws Exception { for (ServiceRegistration registration : registrations) { logger.info("Unregistering " + registration); - context.ungetService(registration.getReference()); + registration.unregister(); } } diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/SubtractService.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/SubtractService.java index 3326a08a3b..1d83a23229 100644 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/SubtractService.java +++ b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/SubtractService.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package calculator; +package calculator.operations; import org.oasisopen.sca.annotation.Remotable; diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/SubtractServiceImpl.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/SubtractServiceImpl.java index 610803dab7..cf1744bc61 100644 --- a/java/sca/modules/implementation-osgi/src/test/java/calculator/SubtractServiceImpl.java +++ b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/SubtractServiceImpl.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package calculator; +package calculator.operations; import java.util.logging.Level; import java.util.logging.Logger; diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/test/CalculatorOSGiTestCase.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/test/CalculatorOSGiTestCase.java new file mode 100644 index 0000000000..1d37e69a03 --- /dev/null +++ b/java/sca/modules/implementation-osgi/src/test/java/calculator/test/CalculatorOSGiTestCase.java @@ -0,0 +1,104 @@ +/* + * 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.test; + +import java.net.URL; +import java.util.HashSet; +import java.util.Set; + +import org.apache.tuscany.sca.implementation.osgi.test.OSGiTestBundles; +import org.apache.tuscany.sca.node.equinox.launcher.EquinoxHost; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; + +import calculator.CalculatorActivator; +import calculator.CalculatorService; +import calculator.CalculatorServiceImpl; +import calculator.operations.AddService; +import calculator.operations.AddServiceImpl; +import calculator.operations.DivideService; +import calculator.operations.DivideServiceImpl; +import calculator.operations.MultiplyService; +import calculator.operations.MultiplyServiceImpl; +import calculator.operations.OperationsActivator; +import calculator.operations.SubtractService; +import calculator.operations.SubtractServiceImpl; + +/** + * + */ +public class CalculatorOSGiTestCase { + private static EquinoxHost host; + + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + Set<URL> bundles = new HashSet<URL>(); + bundles.add(OSGiTestBundles.createBundle("target/test-classes/calculator-bundle.jar", + "calculator", + "calculator.operations", + CalculatorService.class, + CalculatorServiceImpl.class, CalculatorActivator.class)); + + bundles.add(OSGiTestBundles.createBundle("target/test-classes/operations-bundle.jar", + "operations", + null, + OperationsActivator.class, + AddService.class, + AddServiceImpl.class, + SubtractService.class, + SubtractServiceImpl.class, + MultiplyService.class, + MultiplyServiceImpl.class, + DivideService.class, DivideServiceImpl.class)); + try { + host = new EquinoxHost(bundles); + BundleContext context = host.start(); + for (Bundle b : context.getBundles()) { + System.out.println(b); + b.start(); + } + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testOSGi() { + + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + if (host != null) { + host.stop(); + } + } + +} diff --git a/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiTestCase.java b/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiTestCase.java index 76f41a6021..92d99a04ba 100644 --- a/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiTestCase.java +++ b/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiTestCase.java @@ -52,8 +52,8 @@ public class OSGiTestCase { compositeName = "osgitest.composite"; OSGiTestBundles.createBundle("target/test-classes/OSGiTestService.jar", OSGiTestInterface.class.getName(), - OSGiTestImpl.class, - OSGiTestInterface.class); + null, + OSGiTestImpl.class, OSGiTestInterface.class); node = host.createNode("osgitest.composite", new Contribution("c1", new File("target/test-classes").toURI() diff --git a/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/test/OSGiTestBundles.java b/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/test/OSGiTestBundles.java index 882f29126e..15cbd77c4e 100644 --- a/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/test/OSGiTestBundles.java +++ b/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/test/OSGiTestBundles.java @@ -20,11 +20,14 @@ package org.apache.tuscany.sca.implementation.osgi.test; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; +import java.util.HashSet; +import java.util.Set; import java.util.jar.JarOutputStream; import java.util.jar.Manifest; import java.util.zip.ZipEntry; @@ -45,16 +48,21 @@ public class OSGiTestBundles { return index == -1 ? "" : name.substring(0, index); } - public static void createBundle(String jarName, String bundleName, Class<?>... classes) throws Exception { + public static URL createBundle(String jarName, String bundleName, String imports, Class<?>... classes) + throws Exception { Class<?> activator = null; + Set<String> packages = new HashSet<String>(); StringBuffer exports = new StringBuffer(); for (Class<?> cls : classes) { - if (cls.isAssignableFrom(BundleActivator.class)) { + if (BundleActivator.class.isAssignableFrom(cls)) { activator = cls; } if (cls.isInterface()) { - exports.append(getPackageName(cls)).append(","); + String pkg = getPackageName(cls); + if (packages.add(pkg)) { + exports.append(pkg).append(","); + } } } if (exports.length() > 0) { @@ -62,12 +70,20 @@ public class OSGiTestBundles { } Manifest manifest = new Manifest(); + // This attribute Manifest-Version is required so that the MF will be added to the jar + manifest.getMainAttributes().putValue("Manifest-Version", "1.0"); manifest.getMainAttributes().putValue(Constants.BUNDLE_MANIFESTVERSION, "2"); manifest.getMainAttributes().putValue(Constants.BUNDLE_SYMBOLICNAME, bundleName); manifest.getMainAttributes().putValue(Constants.BUNDLE_VERSION, "1.0.0"); manifest.getMainAttributes().putValue(Constants.BUNDLE_NAME, bundleName); manifest.getMainAttributes().putValue(Constants.EXPORT_PACKAGE, exports.toString()); - manifest.getMainAttributes().putValue(Constants.IMPORT_PACKAGE, "org.osgi.framework," + exports.toString()); + String importPackages = null; + if (imports != null) { + importPackages = "org.osgi.framework," + imports; + } else { + importPackages = "org.osgi.framework"; + } + manifest.getMainAttributes().putValue(Constants.IMPORT_PACKAGE, importPackages); if (activator != null) { manifest.getMainAttributes().putValue(Constants.BUNDLE_ACTIVATOR, activator.getName()); @@ -83,10 +99,12 @@ public class OSGiTestBundles { jarOut.close(); out.close(); - FileOutputStream fileOut = new FileOutputStream(jarName); + File jar = new File(jarName); + FileOutputStream fileOut = new FileOutputStream(jar); fileOut.write(out.toByteArray()); fileOut.close(); + return jar.toURI().toURL(); } private static void addClass(JarOutputStream jarOut, Class<?> javaClass) throws IOException, FileNotFoundException { @@ -102,5 +120,6 @@ public class OSGiTestBundles { byte[] fileContents = new byte[file.available()]; file.read(fileContents); jarOut.write(fileContents); + jarOut.closeEntry(); } } diff --git a/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiReadImplTestCase.java b/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiReadImplTestCase.java index c4c7f502ed..ca458a2d4e 100644 --- a/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiReadImplTestCase.java +++ b/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiReadImplTestCase.java @@ -72,8 +72,8 @@ public class OSGiReadImplTestCase { OSGiTestBundles.createBundle("target/test-classes/OSGiTestService.jar", OSGiTestInterface.class.getName(), - OSGiTestImpl.class, - OSGiTestInterface.class); + null, + OSGiTestImpl.class, OSGiTestInterface.class); } |