@Test public void testGetServiceWithServiceCustomizer() { final Registry registry = RegistryUtil.getRegistry(); try (ServiceTrackerMap<String, TrackedTwo> serviceTrackerMap = ServiceTrackerCollections.singleValueMap( TrackedOne.class, "target", new ServiceTrackerCustomizer<TrackedOne, TrackedTwo>() { @Override public TrackedTwo addingService(ServiceReference<TrackedOne> serviceReference) { return new TrackedTwo(registry.getService(serviceReference)); } @Override public void modifiedService( ServiceReference<TrackedOne> serviceReference, TrackedTwo service) { removedService(serviceReference, service); } @Override public void removedService( ServiceReference<TrackedOne> serviceReference, TrackedTwo service) { registry.ungetService(serviceReference); } })) { serviceTrackerMap.open(); TrackedOne trackedOne1 = new TrackedOne(); ServiceRegistration<TrackedOne> serviceRegistration1 = registerService(trackedOne1, "trackedOne1"); TrackedOne trackedOne2 = new TrackedOne(); ServiceRegistration<TrackedOne> serviceRegistration2 = registerService(trackedOne2, "trackedOne2"); TrackedTwo trackedTwo1 = serviceTrackerMap.getService("trackedOne1"); Assert.assertEquals(trackedOne1, trackedTwo1.getTrackedOne()); TrackedTwo trackedTwo2 = serviceTrackerMap.getService("trackedOne2"); Assert.assertEquals(trackedOne2, trackedTwo2.getTrackedOne()); serviceRegistration1.unregister(); serviceRegistration2.unregister(); } }
@Test public void testGetServiceWithServiceCustomizerAndServiceReferenceMapper() { final Registry registry = RegistryUtil.getRegistry(); try (ServiceTrackerMap<String, TrackedTwo> serviceTrackerMap = ServiceTrackerCollections.singleValueMap( TrackedOne.class, ("(target=*)"), new ServiceReferenceMapper<String, TrackedOne>() { @Override public void map( ServiceReference<TrackedOne> serviceReference, Emitter<String> emitter) { TrackedOne service = registry.getService(serviceReference); String targetProperty = (String) serviceReference.getProperty("target"); emitter.emit(targetProperty + "-" + service.getKey()); registry.ungetService(serviceReference); } }, new ServiceTrackerCustomizer<TrackedOne, TrackedTwo>() { @Override public TrackedTwo addingService(ServiceReference<TrackedOne> serviceReference) { return new TrackedTwo(registry.getService(serviceReference)); } @Override public void modifiedService( ServiceReference<TrackedOne> serviceReference, TrackedTwo service) { removedService(serviceReference, service); } @Override public void removedService( ServiceReference<TrackedOne> serviceReference, TrackedTwo service) { registry.ungetService(serviceReference); } })) { serviceTrackerMap.open(); TrackedOne trackedOne1 = new TrackedOne("1"); ServiceRegistration<TrackedOne> serviceRegistration1 = registerService(trackedOne1, "trackedOne1"); TrackedOne trackedOne2 = new TrackedOne("2"); ServiceRegistration<TrackedOne> serviceRegistration2 = registerService(trackedOne2, "trackedOne2"); TrackedTwo trackedTwo1 = serviceTrackerMap.getService("trackedOne1-1"); Assert.assertEquals(trackedOne1, trackedTwo1.getTrackedOne()); TrackedTwo trackedTwo2 = serviceTrackerMap.getService("trackedOne2-2"); Assert.assertEquals(trackedOne2, trackedTwo2.getTrackedOne()); serviceRegistration1.unregister(); serviceRegistration2.unregister(); } }