summaryrefslogtreecommitdiffstats
path: root/java
diff options
context:
space:
mode:
authorrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2009-03-11 20:47:19 +0000
committerrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2009-03-11 20:47:19 +0000
commit7dcff5e92b2ae71fdd9ff3c7e644c2ece2863991 (patch)
tree49e2b6008b861777632d95e90d961c4b3624e03e /java
parent7f794d8ae4785c0f23dc5b007e7488a59327aa7b (diff)
Implement the osgi services using the ServiceTracker
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@752607 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java')
-rw-r--r--java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorActivator.java12
-rw-r--r--java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorServiceImpl.java46
-rw-r--r--java/sca/modules/implementation-osgi/src/test/java/calculator/operations/OperationsActivator.java22
-rw-r--r--java/sca/modules/implementation-osgi/src/test/java/calculator/test/CalculatorOSGiTestCase.java51
4 files changed, 98 insertions, 33 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 b31a960d14..1489ae2b07 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
@@ -27,7 +27,6 @@ import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
-import org.osgi.framework.ServiceRegistration;
import org.osgi.service.packageadmin.PackageAdmin;
import calculator.operations.AddService;
@@ -38,8 +37,6 @@ import calculator.operations.AddService;
public class CalculatorActivator implements BundleActivator {
private Logger logger = Logger.getLogger(CalculatorActivator.class.getName());
- private ServiceRegistration registration;
-
private Bundle getBundle(BundleContext bundleContext, Class<?> cls) {
PackageAdmin packageAdmin = null;
// PackageAdmin is used to resolve bundles
@@ -57,18 +54,21 @@ public class CalculatorActivator implements BundleActivator {
}
public void start(BundleContext context) throws Exception {
+ logger.info("Starting " + context.getBundle());
Dictionary<String, Object> props = new Hashtable<String, Object>();
props.put("sca.service", "CalculatorComponent#service-name(Calculator)");
+ props.put("calculator", "Calculator");
logger.info("Registering " + CalculatorService.class.getName());
- registration = context.registerService(CalculatorService.class.getName(), new CalculatorServiceImpl(), props);
+ 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("UnRegistering " + registration);
- registration.unregister();
+ logger.info("Stopping " + context.getBundle());
+ // Registered services will be automatically unregistered
}
}
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 c2c65463ea..e99f87aae0 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,22 +18,47 @@
*/
package calculator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.Filter;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.util.tracker.ServiceTracker;
+
import calculator.operations.AddService;
import calculator.operations.DivideService;
import calculator.operations.MultiplyService;
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;
+ public CalculatorServiceImpl() {
+ super();
+ }
+
+ private ServiceTracker tracker;
+
+ public CalculatorServiceImpl(BundleContext context) {
+ super();
+ Filter filter = null;
+ try {
+ filter = context.createFilter("(" + Constants.OBJECTCLASS + "=calculator.operations.*)");
+ } catch (InvalidSyntaxException e) {
+ e.printStackTrace();
+ }
+ this.tracker = new ServiceTracker(context, filter, null);
+ tracker.open();
+ }
+
+ /*
+ * The following setters can be used for DS injection
+ */
public void setAddService(AddService addService) {
this.addService = addService;
}
@@ -50,19 +75,28 @@ public class CalculatorServiceImpl implements CalculatorService {
this.multiplyService = multiplyService;
}
+ private <T> T getService(Class<T> cls) {
+ for (Object s : tracker.getServices()) {
+ if (cls.isInstance(s)) {
+ return cls.cast(s);
+ }
+ }
+ throw new IllegalStateException(cls.getSimpleName() + " is not available");
+ }
+
public double add(double n1, double n2) {
- return addService.add(n1, n2);
+ return getService(AddService.class).add(n1, n2);
}
public double subtract(double n1, double n2) {
- return subtractService.subtract(n1, n2);
+ return getService(SubtractService.class).subtract(n1, n2);
}
public double multiply(double n1, double n2) {
- return multiplyService.multiply(n1, n2);
+ return getService(MultiplyService.class).multiply(n1, n2);
}
public double divide(double n1, double n2) {
- return divideService.divide(n1, n2);
+ return getService(DivideService.class).divide(n1, n2);
}
}
diff --git a/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/OperationsActivator.java b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/OperationsActivator.java
index 03260b312b..5da79ee803 100644
--- a/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/OperationsActivator.java
+++ b/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/OperationsActivator.java
@@ -19,15 +19,12 @@
package calculator.operations;
-import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
-import java.util.List;
import java.util.logging.Logger;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceRegistration;
/**
*
@@ -35,33 +32,32 @@ import org.osgi.framework.ServiceRegistration;
public class OperationsActivator implements BundleActivator {
private Logger logger = Logger.getLogger(OperationsActivator.class.getName());
- private List<ServiceRegistration> registrations = new ArrayList<ServiceRegistration>();
-
public void start(BundleContext context) throws Exception {
+ logger.info("Starting " + context.getBundle());
+
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));
+ 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));
+ 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));
+ 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));
+ context.registerService(DivideService.class.getName(), new DivideServiceImpl(), props);
+
}
public void stop(BundleContext context) throws Exception {
- for (ServiceRegistration registration : registrations) {
- logger.info("Unregistering " + registration);
- registration.unregister();
- }
+ logger.info("Stopping " + context.getBundle());
+ // Registered services will be automatically unregistered
}
}
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 1acd581d22..af4ab80d0e 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
@@ -19,6 +19,9 @@
package calculator.test;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
@@ -30,6 +33,7 @@ import org.junit.BeforeClass;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
import calculator.CalculatorActivator;
import calculator.CalculatorService;
@@ -56,13 +60,14 @@ public class CalculatorOSGiTestCase {
@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",
- "calculator.operations,org.osgi.service.packageadmin",
- CalculatorService.class,
- CalculatorServiceImpl.class,
- CalculatorActivator.class));
+ bundles.add(OSGiTestBundles
+ .createBundle("target/test-classes/calculator-bundle.jar",
+ "calculator",
+ "calculator",
+ "calculator.operations,org.osgi.service.packageadmin,org.osgi.util.tracker",
+ CalculatorService.class,
+ CalculatorServiceImpl.class,
+ CalculatorActivator.class));
bundles.add(OSGiTestBundles.createBundle("target/test-classes/operations-bundle.jar",
"calculator.operations",
@@ -81,15 +86,45 @@ public class CalculatorOSGiTestCase {
host = new EquinoxHost(bundles);
BundleContext context = host.start();
for (Bundle b : context.getBundles()) {
- System.out.println(b);
b.start();
}
+ ServiceReference ref = context.getServiceReference(CalculatorService.class.getName());
+ CalculatorService calculator = cast(context.getService(ref), 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));
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
+ private static <T> T cast(Object obj, Class<T> cls) {
+ if (cls.isInstance(obj)) {
+ return cls.cast(obj);
+ } else {
+ return cls.cast(Proxy.newProxyInstance(cls.getClassLoader(),
+ new Class<?>[] {cls},
+ new InvocationHandlerImpl(obj)));
+ }
+ }
+
+ 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);
+ }
+
+ }
+
@Test
public void testOSGi() {