From 0c1527f0415fb7d6c06b0ad2a1d4ce73cd728852 Mon Sep 17 00:00:00 2001 From: rfeng Date: Wed, 27 Jan 2010 04:33:47 +0000 Subject: Fix multiple concurrency related issues git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@903542 13f79535-47bb-0310-9956-ffa450edef68 --- .../dosgi/impl/CalculatorServiceImpl.java | 44 ++++++++++------------ 1 file changed, 19 insertions(+), 25 deletions(-) (limited to 'sca-java-2.x/trunk/samples/dosgi-dynamic-calculator') diff --git a/sca-java-2.x/trunk/samples/dosgi-dynamic-calculator/src/main/java/calculator/dosgi/impl/CalculatorServiceImpl.java b/sca-java-2.x/trunk/samples/dosgi-dynamic-calculator/src/main/java/calculator/dosgi/impl/CalculatorServiceImpl.java index a9ea37585a..aa09b27139 100644 --- a/sca-java-2.x/trunk/samples/dosgi-dynamic-calculator/src/main/java/calculator/dosgi/impl/CalculatorServiceImpl.java +++ b/sca-java-2.x/trunk/samples/dosgi-dynamic-calculator/src/main/java/calculator/dosgi/impl/CalculatorServiceImpl.java @@ -20,6 +20,9 @@ package calculator.dosgi.impl; import static org.osgi.framework.Constants.OBJECTCLASS; +import java.util.HashMap; +import java.util.Map; + import org.osgi.framework.BundleContext; import org.osgi.framework.Filter; import org.osgi.framework.InvalidSyntaxException; @@ -35,8 +38,7 @@ import calculator.dosgi.operations.SubtractService; * An implementation of the Calculator service. */ public class CalculatorServiceImpl implements CalculatorService { - private ServiceTracker remoteServices; - private ServiceTracker localServices; + private Map, ServiceTracker> remoteServices = new HashMap, ServiceTracker>(); public CalculatorServiceImpl() { super(); @@ -44,29 +46,30 @@ public class CalculatorServiceImpl implements CalculatorService { public CalculatorServiceImpl(BundleContext context) { super(); - Filter remoteFilter = null, localFilter = null; - try { - remoteFilter = - context.createFilter("(&(" + OBJECTCLASS + "=calculator.dosgi.operations.*) (service.imported=*))"); - localFilter = - context.createFilter("(&(" + OBJECTCLASS + "=calculator.dosgi.operations.*) (!(service.imported=*)))"); - } catch (InvalidSyntaxException e) { - e.printStackTrace(); + for (Class cls : new Class[] {AddService.class, SubtractService.class, MultiplyService.class, + DivideService.class}) { + Filter remoteFilter = null; + try { + remoteFilter = + context.createFilter("(&(" + OBJECTCLASS + "=" + cls.getName() + ") (service.imported=*))"); + } catch (InvalidSyntaxException e) { + e.printStackTrace(); + } + ServiceTracker tracker = new ServiceTracker(context, remoteFilter, null); + this.remoteServices.put(cls, tracker); + tracker.open(); } - this.remoteServices = new ServiceTracker(context, remoteFilter, null); - remoteServices.open(); - this.localServices = new ServiceTracker(context, localFilter, null); - localServices.open(); } private T getService(Class cls) { + ServiceTracker tracker = remoteServices.get(cls); try { // Wait for 10 seconds until the remote services are imported - remoteServices.waitForService(10000); + tracker.waitForService(10000); } catch (InterruptedException e) { throw new IllegalStateException(cls.getSimpleName() + " is not available"); } - Object[] remoteObjects = remoteServices.getServices(); + Object[] remoteObjects = tracker.getServices(); if (remoteObjects != null) { for (Object s : remoteObjects) { if (cls.isInstance(s)) { @@ -75,15 +78,6 @@ public class CalculatorServiceImpl implements CalculatorService { } } } - Object[] localObjects = localServices.getServices(); - if (localObjects != null) { - for (Object s : localObjects) { - if (cls.isInstance(s)) { - System.out.println("Local service: " + s); - return cls.cast(s); - } - } - } throw new IllegalStateException(cls.getSimpleName() + " is not available"); } -- cgit v1.2.3