diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2010-01-27 04:33:47 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2010-01-27 04:33:47 +0000 |
commit | 0c1527f0415fb7d6c06b0ad2a1d4ce73cd728852 (patch) | |
tree | 7d0658a2e64c4fdec2957090fc70dfd0efed09d7 /sca-java-2.x/trunk/samples/dosgi-dynamic-calculator/src/main/java/calculator/dosgi/impl | |
parent | c2dfd0917dbb0793b7934ccc298e03187e6e3418 (diff) |
Fix multiple concurrency related issues
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@903542 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/samples/dosgi-dynamic-calculator/src/main/java/calculator/dosgi/impl')
-rw-r--r-- | sca-java-2.x/trunk/samples/dosgi-dynamic-calculator/src/main/java/calculator/dosgi/impl/CalculatorServiceImpl.java | 44 |
1 files changed, 19 insertions, 25 deletions
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<Class<?>, ServiceTracker> remoteServices = new HashMap<Class<?>, 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> T getService(Class<T> 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"); } |