From bf1d7c27e03d6734dc431c83507bdcc6864e2a09 Mon Sep 17 00:00:00 2001 From: rfeng Date: Wed, 11 Mar 2009 23:34:46 +0000 Subject: Add OSGi DS files git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@752694 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/calculator/CalculatorServiceDSImpl.java | 90 ++++++++++++++++++++++ .../java/calculator/CalculatorServiceImpl.java | 14 ++-- .../calculator/test/CalculatorOSGiTestCase.java | 13 ++-- .../implementation/osgi/runtime/OSGiTestCase.java | 1 + .../implementation/osgi/test/OSGiTestBundles.java | 53 ++++++++++++- .../osgi/xml/OSGiReadImplTestCase.java | 1 + .../src/test/resources/OSGI-INF/add-component.xml | 25 ++++++ .../resources/OSGI-INF/calculator-component.xml | 35 +++++++++ .../test/resources/OSGI-INF/divide-component.xml | 25 ++++++ .../test/resources/OSGI-INF/multiply-component.xml | 25 ++++++ .../test/resources/OSGI-INF/subtract-component.xml | 25 ++++++ .../test/resources/calculator/META-INF/MANIFEST.MF | 18 +++++ .../calculator/operations/META-INF/MANIFEST.MF | 19 +++++ 13 files changed, 328 insertions(+), 16 deletions(-) create mode 100644 java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorServiceDSImpl.java create mode 100644 java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/add-component.xml create mode 100644 java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/calculator-component.xml create mode 100644 java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/divide-component.xml create mode 100644 java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/multiply-component.xml create mode 100644 java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/subtract-component.xml create mode 100644 java/sca/modules/implementation-osgi/src/test/resources/calculator/META-INF/MANIFEST.MF create mode 100644 java/sca/modules/implementation-osgi/src/test/resources/calculator/operations/META-INF/MANIFEST.MF (limited to 'java/sca/modules/implementation-osgi/src/test') diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorServiceDSImpl.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorServiceDSImpl.java new file mode 100644 index 0000000000..941dad4fb1 --- /dev/null +++ b/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorServiceDSImpl.java @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package calculator; + +import calculator.operations.AddService; +import calculator.operations.DivideService; +import calculator.operations.MultiplyService; +import calculator.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(); + } + + /* + protected void activate(ComponentContext context) { + } + + protected void deactivate(ComponentContext context) { + } + */ + + /* + * The following setters can be used for DS injection + */ + public void setAddService(AddService addService) { + 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; + } + + 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/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorServiceImpl.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorServiceImpl.java index e99f87aae0..2d36ba76ad 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 @@ -33,17 +33,17 @@ import calculator.operations.SubtractService; * An implementation of the Calculator service. */ public class CalculatorServiceImpl implements CalculatorService { - private AddService addService; - private SubtractService subtractService; - private MultiplyService multiplyService; - private DivideService divideService; +// private AddService addService; +// private SubtractService subtractService; +// private MultiplyService multiplyService; +// private DivideService divideService; + + private ServiceTracker tracker; public CalculatorServiceImpl() { super(); } - private ServiceTracker tracker; - public CalculatorServiceImpl(BundleContext context) { super(); Filter filter = null; @@ -59,6 +59,7 @@ public class CalculatorServiceImpl implements CalculatorService { /* * The following setters can be used for DS injection */ + /* public void setAddService(AddService addService) { this.addService = addService; } @@ -74,6 +75,7 @@ public class CalculatorServiceImpl implements CalculatorService { public void setMultiplyService(MultiplyService multiplyService) { this.multiplyService = multiplyService; } + */ private T getService(Class cls) { for (Object s : tracker.getServices()) { 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 index af4ab80d0e..ef50c22011 100644 --- 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 @@ -62,17 +62,18 @@ public class CalculatorOSGiTestCase { Set bundles = new HashSet(); bundles.add(OSGiTestBundles .createBundle("target/test-classes/calculator-bundle.jar", - "calculator", - "calculator", - "calculator.operations,org.osgi.service.packageadmin,org.osgi.util.tracker", + "calculator/META-INF/MANIFEST.MF", + new String[] {"OSGI-INF/calculator-component.xml"}, CalculatorService.class, CalculatorServiceImpl.class, CalculatorActivator.class)); bundles.add(OSGiTestBundles.createBundle("target/test-classes/operations-bundle.jar", - "calculator.operations", - "calculator.operations", - "calculator.operations", + "calculator/operations/META-INF/MANIFEST.MF", + new String[] {"OSGI-INF/add-component.xml", + "OSGI-INF/subtract-component.xml", + "OSGI-INF/multiply-component.xml", + "OSGI-INF/divide-component.xml"}, OperationsActivator.class, AddService.class, AddServiceImpl.class, 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 26bf78b395..6cdc2db347 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 @@ -54,6 +54,7 @@ public class OSGiTestCase { OSGiTestInterface.class.getName(), null, null, + (String[]) null, OSGiTestImpl.class, OSGiTestInterface.class); node = 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 c062aba40b..716d89c8cd 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 @@ -25,6 +25,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.net.URL; import java.util.HashSet; import java.util.Set; @@ -48,11 +49,42 @@ public class OSGiTestBundles { return index == -1 ? "" : name.substring(0, index); } + public static URL createBundle(String jarName, String mfFile, String[] resources, Class... classes) + throws IOException { + InputStream is = OSGiTestBundles.class.getClassLoader().getResourceAsStream(mfFile); + Manifest manifest = new Manifest(is); + is.close(); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + JarOutputStream jarOut = new JarOutputStream(out, manifest); + + for (Class cls : classes) { + addClass(jarOut, cls); + } + + if (resources != null) { + for (String resource : resources) { + addResource(jarOut, OSGiTestBundles.class.getClassLoader(), resource); + } + } + + jarOut.close(); + out.close(); + + File jar = new File(jarName); + FileOutputStream fileOut = new FileOutputStream(jar); + fileOut.write(out.toByteArray()); + fileOut.close(); + + return jar.toURI().toURL(); + } + public static URL createBundle(String jarName, String bundleName, String exports, String imports, - Class... classes) throws Exception { + String[] resources, + Class... classes) throws IOException { Class activator = null; Set packages = new HashSet(); @@ -103,6 +135,12 @@ public class OSGiTestBundles { addClass(jarOut, cls); } + if (resources != null) { + for (String resource : resources) { + addResource(jarOut, OSGiTestBundles.class.getClassLoader(), resource); + } + } + jarOut.close(); out.close(); @@ -115,12 +153,19 @@ public class OSGiTestBundles { } private static void addClass(JarOutputStream jarOut, Class javaClass) throws IOException, FileNotFoundException { - String interfaceClassName = javaClass.getName().replaceAll("\\.", "/") + ".class"; + String classFile = javaClass.getName().replace('.', '/') + ".class"; + + ClassLoader cl = javaClass.getClassLoader(); + + addResource(jarOut, cl, classFile); + } - URL url = javaClass.getClassLoader().getResource(interfaceClassName); + private static void addResource(JarOutputStream jarOut, ClassLoader cl, String resourceName) throws IOException, + FileNotFoundException { + URL url = cl.getResource(resourceName); String path = url.getPath(); - ZipEntry ze = new ZipEntry(interfaceClassName); + ZipEntry ze = new ZipEntry(resourceName); jarOut.putNextEntry(ze); FileInputStream file = new FileInputStream(path); 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 7be96b32c5..7e9433751e 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 @@ -74,6 +74,7 @@ public class OSGiReadImplTestCase { OSGiTestInterface.class.getName(), null, null, + (String[]) null, OSGiTestImpl.class, OSGiTestInterface.class); } diff --git a/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/add-component.xml b/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/add-component.xml new file mode 100644 index 0000000000..fd1f907999 --- /dev/null +++ b/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/add-component.xml @@ -0,0 +1,25 @@ + + + + + + + + diff --git a/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/calculator-component.xml b/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/calculator-component.xml new file mode 100644 index 0000000000..52ac572a25 --- /dev/null +++ b/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/calculator-component.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + diff --git a/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/divide-component.xml b/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/divide-component.xml new file mode 100644 index 0000000000..8351554a16 --- /dev/null +++ b/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/divide-component.xml @@ -0,0 +1,25 @@ + + + + + + + + diff --git a/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/multiply-component.xml b/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/multiply-component.xml new file mode 100644 index 0000000000..8033916595 --- /dev/null +++ b/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/multiply-component.xml @@ -0,0 +1,25 @@ + + + + + + + + diff --git a/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/subtract-component.xml b/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/subtract-component.xml new file mode 100644 index 0000000000..a4e5711786 --- /dev/null +++ b/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/subtract-component.xml @@ -0,0 +1,25 @@ + + + + + + + + diff --git a/java/sca/modules/implementation-osgi/src/test/resources/calculator/META-INF/MANIFEST.MF b/java/sca/modules/implementation-osgi/src/test/resources/calculator/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..808cc60c79 --- /dev/null +++ b/java/sca/modules/implementation-osgi/src/test/resources/calculator/META-INF/MANIFEST.MF @@ -0,0 +1,18 @@ +Manifest-Version: 1.0 +Export-Package: calculator;version="1.0.0" +Bundle-Version: 1.0.0 +Bundle-Name: calculator +Bundle-Activator: calculator.CalculatorActivator +Bundle-ManifestVersion: 2 +Import-Package: calculator.operations;version="1.0.0", + org.osgi.framework, + org.osgi.service.packageadmin, + org.osgi.util.tracker, + org.osgi.service.component;resolution:=optional +Bundle-SymbolicName: calculator +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: OSGI-INF/calculator-component.xml diff --git a/java/sca/modules/implementation-osgi/src/test/resources/calculator/operations/META-INF/MANIFEST.MF b/java/sca/modules/implementation-osgi/src/test/resources/calculator/operations/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..1414d227b0 --- /dev/null +++ b/java/sca/modules/implementation-osgi/src/test/resources/calculator/operations/META-INF/MANIFEST.MF @@ -0,0 +1,19 @@ +Manifest-Version: 1.0 +Export-Package: calculator.operations;version="1.0.0" +Bundle-Version: 1.0.0 +Bundle-Name: calculator.operations +Bundle-Activator: calculator.operations.OperationsActivator +Bundle-ManifestVersion: 2 +Import-Package: calculator.operations;version="1.0.0", + org.osgi.framework, + org.osgi.service.component;resolution:=optional +Bundle-SymbolicName: calculator.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: OSGI-INF/add-component.xml, + OSGI-INF/subtract-component.xml, + OSGI-INF/multiply-component.xml, + OSGI-INF/divide-component.xml -- cgit v1.2.3