Add OSGi DS files

git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@752694 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
rfeng 2009-03-11 23:34:46 +00:00
parent 7dcff5e92b
commit bf1d7c27e0
14 changed files with 335 additions and 16 deletions

View file

@ -41,6 +41,13 @@
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.osgi</groupId>
<artifactId>services</artifactId>
<version>3.1.200-v20070605</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-node-api</artifactId>

View file

@ -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> T getService(Class<T> 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);
}
}

View file

@ -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> T getService(Class<T> cls) {
for (Object s : tracker.getServices()) {

View file

@ -62,17 +62,18 @@ public class CalculatorOSGiTestCase {
Set<URL> bundles = new HashSet<URL>();
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,

View file

@ -54,6 +54,7 @@ public class OSGiTestCase {
OSGiTestInterface.class.getName(),
null,
null,
(String[]) null,
OSGiTestImpl.class, OSGiTestInterface.class);
node =

View file

@ -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<String> packages = new HashSet<String>();
@ -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";
URL url = javaClass.getClassLoader().getResource(interfaceClassName);
ClassLoader cl = javaClass.getClassLoader();
addResource(jarOut, cl, classFile);
}
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);

View file

@ -74,6 +74,7 @@ public class OSGiReadImplTestCase {
OSGiTestInterface.class.getName(),
null,
null,
(String[]) null,
OSGiTestImpl.class, OSGiTestInterface.class);
}

View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
-->
<component name="AddComponent" xmlns="http://www.osgi.org/xmlns/scr/v1.0.0">
<implementation class="calculator.operations.AddServiceImpl" />
<service>
<provide interface="calculator.operations.AddService" />
</service>
</component>

View file

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
-->
<component name="CalculatorComponent" xmlns="http://www.osgi.org/xmlns/scr/v1.0.0">
<implementation class="calculator.CalculatorServiceImpl" />
<service>
<provide interface="calculator.CalculatorService" />
</service>
<reference name="addService" interface="calculator.AddService" bind="setAddService" unbind="setAddService"
policy="dynamic" />
<reference name="subtractService" interface="calculator.SubtractService" bind="setSubtractService" unbind="setSubtractService"
policy="dynamic" />
<reference name="multiplyService" interface="calculator.MultiplyService" bind="setMultiplyService" unbind="setMultiplyService"
policy="dynamic" />
<reference name="divideService" interface="calculator.DivideService" bind="setDivideService" unbind="setDivideService"
policy="dynamic" />
</component>

View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
-->
<component name="DivideComponent" xmlns="http://www.osgi.org/xmlns/scr/v1.0.0">
<implementation class="calculator.operations.DivideServiceImpl" />
<service>
<provide interface="calculator.operations.DivideService" />
</service>
</component>

View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
-->
<component name="MultiplyComponent" xmlns="http://www.osgi.org/xmlns/scr/v1.0.0">
<implementation class="calculator.operations.MultiplyServiceImpl" />
<service>
<provide interface="calculator.operations.MultiplyService" />
</service>
</component>

View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
-->
<component name="SubtractComponent" xmlns="http://www.osgi.org/xmlns/scr/v1.0.0">
<implementation class="calculator.operations.SubtractServiceImpl" />
<service>
<provide interface="calculator.operations.SubtractService" />
</service>
</component>

View file

@ -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

View file

@ -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