diff options
author | Thomas Watson | 2020-06-26 15:14:43 +0000 |
---|---|---|
committer | Thomas Watson | 2020-06-26 15:14:43 +0000 |
commit | 62dd11004a08ea3ab6620100494e9d07ef9e5f34 (patch) | |
tree | 86f769ee6b5676773fff17aa807a8eacedef4b8d | |
parent | a0d6fe0ccc9164c09c5b2d527ea0eb19d781f5ec (diff) | |
download | rt.equinox.bundles-62dd11004a08ea3ab6620100494e9d07ef9e5f34.tar.gz rt.equinox.bundles-62dd11004a08ea3ab6620100494e9d07ef9e5f34.tar.xz rt.equinox.bundles-62dd11004a08ea3ab6620100494e9d07ef9e5f34.zip |
Bug 563987 - add a current method for ServiceCallerI20200628-1800I20200626-1800
Many cases it is useful to just get the current available service from
the ServiceCaller instance.
Change-Id: I6a1cc3968c1aca55bea30265f2d97fb21500455e
Signed-off-by: Thomas Watson <tjwatson@us.ibm.com>
2 files changed, 19 insertions, 1 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 fb71dd2fc..b77e5bfbd 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 @@ -116,6 +116,10 @@ public class ServiceCallerTest extends CoreTest { assertFalse("Should not be called.", thisClassCallerFilter.call(IServiceExample::call)); assertFalse("Should not be called.", otherClassCaller.call(IServiceExample::call)); + assertFalse("Should not be present.", thisClassCaller.current().isPresent()); + assertFalse("Should not be present.", thisClassCallerFilter.current().isPresent()); + assertFalse("Should not be present.", otherClassCaller.current().isPresent()); + ServiceExampleFactory factory = new ServiceExampleFactory(); Dictionary<String, String> props = new Hashtable<>(); props.put("test", "value1"); @@ -123,6 +127,8 @@ public class ServiceCallerTest extends CoreTest { try { assertTrue("Call returned false.", thisClassCaller.call(IServiceExample::call)); assertTrue("Service called successfully", factory.lastCreated.called); + assertTrue("Should be present.", thisClassCaller.current().isPresent()); + assertEquals("Unexpected current.", thisClassCaller.current().get(), factory.lastCreated); assertFalse("Should not be called.", thisClassCallerFilter.call(IServiceExample::call)); @@ -137,7 +143,7 @@ public class ServiceCallerTest extends CoreTest { assertEquals("Wrong createCount", 1, factory.getCreateCount(testBundle)); Bundle[] users = reg.getReference().getUsingBundles(); - assertNotNull("Didn't expect users.", users); + assertNotNull("Expected some users", users); Collection<Bundle> userCollection = Arrays.asList(users); assertTrue("Missing bundle.", userCollection.contains(bundle)); @@ -146,10 +152,13 @@ public class ServiceCallerTest extends CoreTest { reg.unregister(); assertFalse("Should not be called.", thisClassCaller.call(IServiceExample::call)); assertFalse("Should not be called.", otherClassCaller.call(IServiceExample::call)); + assertFalse("Should not be present.", thisClassCaller.current().isPresent()); props.put("test", "value2"); reg = context.registerService(IServiceExample.class, factory, props); + assertTrue("Should be present.", thisClassCaller.current().isPresent()); + assertEquals("Unexpected current.", thisClassCaller.current().get(), factory.lastCreated); assertTrue("Call returned false.", thisClassCaller.call(IServiceExample::call)); assertTrue("Call returned false.", thisClassCallerFilter.call(IServiceExample::call)); assertTrue("Call returned false.", otherClassCaller.call(IServiceExample::call)); @@ -176,6 +185,7 @@ public class ServiceCallerTest extends CoreTest { reg.setProperties(props); assertTrue("Call returned false.", thisClassCaller.call(IServiceExample::call)); assertFalse("Should not be called.", thisClassCallerFilter.call(IServiceExample::call)); + assertFalse("Should not be present.", thisClassCallerFilter.current().isPresent()); } finally { testBundle.uninstall(); thisClassCaller.unget(); 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 b05c05a10..483ff7c73 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 @@ -262,6 +262,14 @@ public class ServiceCaller<Service> { }).orElse(Boolean.FALSE); } + /** + * Return the currently available service. + * @return the currently available service or empty if the service cannot be found. + */ + public Optional<Service> current() { + return trackCurrent().map((r) -> r.instance); + } + private Optional<ReferenceAndService> trackCurrent() { ReferenceAndService current = service; if (current != null) { |