You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by rf...@apache.org on 2009/03/11 21:47:20 UTC

svn commit: r752607 - in /tuscany/java/sca/modules/implementation-osgi/src/test: java/calculator/ java/calculator/operations/ java/calculator/test/ resources/OSGI-INF/

Author: rfeng
Date: Wed Mar 11 20:47:19 2009
New Revision: 752607

URL: http://svn.apache.org/viewvc?rev=752607&view=rev
Log:
Implement the osgi services using the ServiceTracker

Added:
    tuscany/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/
Modified:
    tuscany/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorActivator.java
    tuscany/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorServiceImpl.java
    tuscany/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/OperationsActivator.java
    tuscany/java/sca/modules/implementation-osgi/src/test/java/calculator/test/CalculatorOSGiTestCase.java

Modified: tuscany/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorActivator.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorActivator.java?rev=752607&r1=752606&r2=752607&view=diff
==============================================================================
--- tuscany/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorActivator.java (original)
+++ tuscany/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorActivator.java Wed Mar 11 20:47:19 2009
@@ -27,7 +27,6 @@
 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 @@
 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 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
     }
 
 }

Modified: tuscany/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorServiceImpl.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorServiceImpl.java?rev=752607&r1=752606&r2=752607&view=diff
==============================================================================
--- tuscany/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorServiceImpl.java (original)
+++ tuscany/java/sca/modules/implementation-osgi/src/test/java/calculator/CalculatorServiceImpl.java Wed Mar 11 20:47:19 2009
@@ -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 @@
         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);
     }
 }

Modified: tuscany/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/OperationsActivator.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/OperationsActivator.java?rev=752607&r1=752606&r2=752607&view=diff
==============================================================================
--- tuscany/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/OperationsActivator.java (original)
+++ tuscany/java/sca/modules/implementation-osgi/src/test/java/calculator/operations/OperationsActivator.java Wed Mar 11 20:47:19 2009
@@ -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 @@
 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    
     }
 
 }

Modified: tuscany/java/sca/modules/implementation-osgi/src/test/java/calculator/test/CalculatorOSGiTestCase.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/implementation-osgi/src/test/java/calculator/test/CalculatorOSGiTestCase.java?rev=752607&r1=752606&r2=752607&view=diff
==============================================================================
--- tuscany/java/sca/modules/implementation-osgi/src/test/java/calculator/test/CalculatorOSGiTestCase.java (original)
+++ tuscany/java/sca/modules/implementation-osgi/src/test/java/calculator/test/CalculatorOSGiTestCase.java Wed Mar 11 20:47:19 2009
@@ -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.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 @@
     @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 @@
             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() {