summaryrefslogtreecommitdiffstats
path: root/java
diff options
context:
space:
mode:
authorrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2009-03-18 00:29:15 +0000
committerrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2009-03-18 00:29:15 +0000
commit50a856b26b629cbbfb0b18c9f8f8c8418209d19c (patch)
treea4e45bf017e4efd292dcfaa186a7b64425164066 /java
parentefec32910420ff0a3316914668ff44d069d4a744 (diff)
Add filter to select local or remote services
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@755452 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java')
-rw-r--r--java/sca/modules/node-impl-osgi/src/test/java/calculator/dosgi/CalculatorServiceImpl.java55
-rw-r--r--java/sca/modules/node-impl-osgi/src/test/java/calculator/dosgi/test/CalculatorOSGiNodeTestCase.java5
-rw-r--r--java/sca/modules/node-impl-osgi/src/test/java/calculator/dosgi/test/CalculatorOSGiTestCase.java49
3 files changed, 54 insertions, 55 deletions
diff --git a/java/sca/modules/node-impl-osgi/src/test/java/calculator/dosgi/CalculatorServiceImpl.java b/java/sca/modules/node-impl-osgi/src/test/java/calculator/dosgi/CalculatorServiceImpl.java
index 6dc2e4c621..1634a1f2a4 100644
--- a/java/sca/modules/node-impl-osgi/src/test/java/calculator/dosgi/CalculatorServiceImpl.java
+++ b/java/sca/modules/node-impl-osgi/src/test/java/calculator/dosgi/CalculatorServiceImpl.java
@@ -34,12 +34,8 @@ import calculator.dosgi.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 ServiceTracker tracker;
+ private ServiceTracker remoteServices;
+ private ServiceTracker localServices;
public CalculatorServiceImpl() {
super();
@@ -47,41 +43,34 @@ public class CalculatorServiceImpl implements CalculatorService {
public CalculatorServiceImpl(BundleContext context) {
super();
- Filter filter = null;
+ Filter remoteFilter = null, localFilter = null;
try {
- filter = context.createFilter("(&(" + OBJECTCLASS + "=calculator.dosgi.operations.*) (sca.reference=*))");
+ remoteFilter =
+ context.createFilter("(&(" + OBJECTCLASS + "=calculator.dosgi.operations.*) (sca.reference=*))");
+ localFilter =
+ context.createFilter("(&(" + OBJECTCLASS + "=calculator.dosgi.operations.*) (!(sca.reference=*)))");
} 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;
+ this.remoteServices = new ServiceTracker(context, remoteFilter, null);
+ remoteServices.open();
+ this.localServices = new ServiceTracker(context, localFilter, null);
+ localServices.open();
}
- 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 : tracker.getServices()) {
+ Object[] remoteObjects = remoteServices.getServices();
+ if (remoteObjects != null) {
+ for (Object s : remoteObjects) {
+ if (cls.isInstance(s)) {
+ System.out.println("Remote service: " + s);
+ return cls.cast(s);
+ }
+ }
+ }
+ for (Object s : localServices.getServices()) {
if (cls.isInstance(s)) {
- System.out.println(s);
+ System.out.println("Local service: " + s);
return cls.cast(s);
}
}
diff --git a/java/sca/modules/node-impl-osgi/src/test/java/calculator/dosgi/test/CalculatorOSGiNodeTestCase.java b/java/sca/modules/node-impl-osgi/src/test/java/calculator/dosgi/test/CalculatorOSGiNodeTestCase.java
index 508c332a2b..c64ee52563 100644
--- a/java/sca/modules/node-impl-osgi/src/test/java/calculator/dosgi/test/CalculatorOSGiNodeTestCase.java
+++ b/java/sca/modules/node-impl-osgi/src/test/java/calculator/dosgi/test/CalculatorOSGiNodeTestCase.java
@@ -95,6 +95,11 @@ public class CalculatorOSGiNodeTestCase {
{"calculator/dosgi/calculator.composite",
"OSGI-INF/sca/bundle.composite"}},
CalculatorService.class,
+ // Package the interfaces so that the operations bundle can be remote
+ AddService.class,
+ SubtractService.class,
+ MultiplyService.class,
+ DivideService.class,
CalculatorServiceImpl.class,
CalculatorServiceDSImpl.class,
CalculatorActivator.class));
diff --git a/java/sca/modules/node-impl-osgi/src/test/java/calculator/dosgi/test/CalculatorOSGiTestCase.java b/java/sca/modules/node-impl-osgi/src/test/java/calculator/dosgi/test/CalculatorOSGiTestCase.java
index cb9c20be0c..82204a0272 100644
--- a/java/sca/modules/node-impl-osgi/src/test/java/calculator/dosgi/test/CalculatorOSGiTestCase.java
+++ b/java/sca/modules/node-impl-osgi/src/test/java/calculator/dosgi/test/CalculatorOSGiTestCase.java
@@ -82,27 +82,26 @@ public class CalculatorOSGiTestCase {
CalculatorServiceDSImpl.class,
CalculatorActivator.class));
- bundles.add(OSGiTestBundles
- .createBundle("target/test-classes/operations-bundle.jar",
- "calculator/dosgi/operations/META-INF/MANIFEST.MF",
- new String[][] {
- {"OSGI-INF/add-component.xml", null},
- {"OSGI-INF/subtract-component.xml", null},
- {"OSGI-INF/multiply-component.xml", null},
- {"OSGI-INF/divide-component.xml", null},
- {"calculator/dosgi/operations/bundle.componentType",
- "OSGI-INF/sca/bundle.componentType"},
- {"calculator/dosgi/operations/operations.composite",
- "OSGI-INF/sca/bundle.composite"}},
- OperationsActivator.class,
- AddService.class,
- AddServiceImpl.class,
- SubtractService.class,
- SubtractServiceImpl.class,
- MultiplyService.class,
- MultiplyServiceImpl.class,
- DivideService.class,
- DivideServiceImpl.class));
+ bundles.add(OSGiTestBundles.createBundle("target/test-classes/operations-bundle.jar",
+ "calculator/dosgi/operations/META-INF/MANIFEST.MF",
+ new String[][] {
+ {"OSGI-INF/add-component.xml", null},
+ {"OSGI-INF/subtract-component.xml", null},
+ {"OSGI-INF/multiply-component.xml", null},
+ {"OSGI-INF/divide-component.xml", null},
+ {"calculator/dosgi/operations/bundle.componentType",
+ "OSGI-INF/sca/bundle.componentType"},
+ {"calculator/dosgi/operations/operations.composite",
+ "OSGI-INF/sca/bundle.composite"}},
+ OperationsActivator.class,
+ AddService.class,
+ AddServiceImpl.class,
+ SubtractService.class,
+ SubtractServiceImpl.class,
+ MultiplyService.class,
+ MultiplyServiceImpl.class,
+ DivideService.class,
+ DivideServiceImpl.class));
try {
host = new EquinoxHost(bundles);
BundleContext context = host.start();
@@ -118,7 +117,13 @@ public class CalculatorOSGiTestCase {
System.out.println(string(b, false));
}
}
- ServiceReference ref = context.getServiceReference(CalculatorService.class.getName());
+
+ // Sleep for 1 sec so that the DS is available
+ Thread.sleep(1000);
+ // Use the DS version
+ String filter = "(component.name=CalculatorComponent)";
+ System.out.println(filter);
+ ServiceReference ref = context.getServiceReferences(CalculatorService.class.getName(), filter)[0];
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));