Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Blewitt2020-07-17 16:33:23 +0000
committerAlex Blewitt2020-07-17 17:17:56 +0000
commit38ebba91d4b83c716904f2955998b08edbe5a136 (patch)
treefe9096db83668e9a20cd3aba4bf08e48e8840609
parenta32b7c929f5b5b87ae7aef1a26d3302d6a15322e (diff)
downloadrt.equinox.bundles-38ebba91d4b83c716904f2955998b08edbe5a136.tar.gz
rt.equinox.bundles-38ebba91d4b83c716904f2955998b08edbe5a136.tar.xz
rt.equinox.bundles-38ebba91d4b83c716904f2955998b08edbe5a136.zip
The ServiceCaller supports passing through an OSGi filter to reduce the set of services considered for calling to be returned. However, this is not exposed on the `callOnce` method. Resolve this by adding a variant to `callOnce` that provides the overload to pass through an OSGi filter. Change-Id: Iab07a376e6aa4732372f8db84f351428e6f0bc45 Signed-off-by: Alex Blewitt <alex.blewitt@gmail.com>
-rw-r--r--bundles/org.eclipse.equinox.common.tests/src/org/eclipse/equinox/common/tests/ServiceCallerTest.java18
-rw-r--r--bundles/org.eclipse.equinox.common/src/org/eclipse/core/runtime/ServiceCaller.java14
2 files changed, 30 insertions, 2 deletions
diff --git a/bundles/org.eclipse.equinox.common.tests/src/org/eclipse/equinox/common/tests/ServiceCallerTest.java b/bundles/org.eclipse.equinox.common.tests/src/org/eclipse/equinox/common/tests/ServiceCallerTest.java
index 1be98fb45..68f7ed3b9 100644
--- a/bundles/org.eclipse.equinox.common.tests/src/org/eclipse/equinox/common/tests/ServiceCallerTest.java
+++ b/bundles/org.eclipse.equinox.common.tests/src/org/eclipse/equinox/common/tests/ServiceCallerTest.java
@@ -78,7 +78,10 @@ public class ServiceCallerTest extends CoreTest {
ServiceExampleFactory factory = new ServiceExampleFactory();
ServiceRegistration<IServiceExample> reg = null;
try {
- reg = context.registerService(IServiceExample.class, factory, null);
+ Dictionary<String, String> props = new Hashtable<>();
+ props.put("test", "value");
+
+ reg = context.registerService(IServiceExample.class, factory, props);
ServiceCaller.callOnce(IServiceExample.class, IServiceExample.class, IServiceExample::call);
ServiceExample lastCreated1 = factory.lastCreated;
assertTrue("Service called successfully", lastCreated1.called);
@@ -86,12 +89,16 @@ public class ServiceCallerTest extends CoreTest {
Bundle[] users = reg.getReference().getUsingBundles();
assertNull("Didn't expect users.", users);
- ServiceCaller.callOnce(IServiceExample.class, IServiceExample.class, IServiceExample::call);
+ ServiceCaller.callOnce(IServiceExample.class, IServiceExample.class, "(test=value)", IServiceExample::call);
ServiceExample lastCreated2 = factory.lastCreated;
assertTrue("Service called successfully", lastCreated2.called);
assertNotEquals("Should have new service each call", lastCreated1, lastCreated2);
+ boolean result = ServiceCaller.callOnce(IServiceExample.class, IServiceExample.class, "(test!=value)",
+ IServiceExample::call);
+ assertFalse("Should not have found service with filter", result);
+
assertEquals("Unexpected createCount", 2, factory.getCreateCount(bundle));
} finally {
reg.unregister();
@@ -208,6 +215,13 @@ public class ServiceCallerTest extends CoreTest {
} catch (IllegalArgumentException e) {
assertTrue("Unexpected cause.", e.getCause() instanceof InvalidSyntaxException);
}
+ try {
+ ServiceCaller.callOnce(getClass(), IServiceExample.class, "invalid filter", (example) -> {
+ });
+ fail("Expected an exception on invalid filter.");
+ } catch (IllegalArgumentException e) {
+ assertTrue("Unexpected cause.", e.getCause() instanceof InvalidSyntaxException);
+ }
}
public void testRank() {
diff --git a/bundles/org.eclipse.equinox.common/src/org/eclipse/core/runtime/ServiceCaller.java b/bundles/org.eclipse.equinox.common/src/org/eclipse/core/runtime/ServiceCaller.java
index 655cfe898..9d79748b7 100644
--- a/bundles/org.eclipse.equinox.common/src/org/eclipse/core/runtime/ServiceCaller.java
+++ b/bundles/org.eclipse.equinox.common/src/org/eclipse/core/runtime/ServiceCaller.java
@@ -126,6 +126,20 @@ public class ServiceCaller<Service> {
return new ServiceCaller<>(caller, serviceType).getCallUnget(consumer);
}
+ /**
+ * As {@link #callOnce(Class, Class, Consumer)} with an additional OSGi filter.
+ * @param caller a class from the bundle that will use service
+ * @param serviceType the OSGi service type to look up
+ * @param consumer the consumer of the OSGi service
+ * @param filter an OSGi filter to restrict the services found
+ * @param <Service> the OSGi service type to look up
+ * @return true if the OSGi service was located and called successfully, false otherwise
+ * @throws NullPointerException if any of the parameters are null
+ */
+ public static <Service> boolean callOnce(Class<?> caller, Class<Service> serviceType, String filter, Consumer<Service> consumer) {
+ return new ServiceCaller<>(caller, serviceType, filter).getCallUnget(consumer);
+ }
+
static int getRank(ServiceReference<?> ref) {
Object rank = ref.getProperty(Constants.SERVICE_RANKING);
if (rank instanceof Integer) {

Back to the top