use of org.osgi.framework.ServiceEvent in project karaf by apache.
the class GuardProxyCatalogTest method testHandleServiceModified2.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testHandleServiceModified2() throws Exception {
// no configuration used in this test...
BundleContext bc = mockConfigAdminBundleContext();
GuardProxyCatalog gpc = new GuardProxyCatalog(bc);
// The service being proxied has these properties
long serviceID = 1L;
final Hashtable<String, Object> serviceProps = new Hashtable<>();
serviceProps.put(Constants.OBJECTCLASS, new String[] { TestServiceAPI.class.getName() });
serviceProps.put(Constants.SERVICE_ID, serviceID);
BundleContext providerBC = EasyMock.createNiceMock(BundleContext.class);
EasyMock.expect(providerBC.registerService(EasyMock.aryEq(new String[] { TestServiceAPI.class.getName() }), EasyMock.anyObject(), EasyMock.anyObject(Dictionary.class))).andAnswer((IAnswer) () -> {
final Dictionary props = (Dictionary) EasyMock.getCurrentArguments()[2];
assertEquals(Boolean.TRUE, props.get(GuardProxyCatalog.PROXY_SERVICE_KEY));
ServiceRegistration reg = EasyMock.createMock(ServiceRegistration.class);
ServiceReference sr = mockServiceReference(props);
EasyMock.expect(reg.getReference()).andReturn(sr).anyTimes();
reg.setProperties(EasyMock.isA(Dictionary.class));
EasyMock.expectLastCall().andAnswer(() -> {
ArrayList<String> oldKeys = Collections.list(props.keys());
for (String key : oldKeys) {
props.remove(key);
}
Dictionary<String, Object> newProps = (Dictionary<String, Object>) EasyMock.getCurrentArguments()[0];
for (String key : Collections.list(newProps.keys())) {
props.put(key, newProps.get(key));
}
return null;
}).once();
EasyMock.replay(reg);
return reg;
}).anyTimes();
EasyMock.replay(providerBC);
// In some cases the proxy-creating code is looking for a classloader (e.g. when run through
// a coverage tool such as EclEmma). This will satisfy that.
BundleWiring bw = EasyMock.createMock(BundleWiring.class);
EasyMock.expect(bw.getClassLoader()).andReturn(getClass().getClassLoader()).anyTimes();
EasyMock.replay(bw);
// The mock bundle that provides the original service (and also the proxy is registered with this)
Bundle providerBundle = EasyMock.createNiceMock(Bundle.class);
EasyMock.expect(providerBundle.getBundleContext()).andReturn(providerBC).anyTimes();
EasyMock.expect(providerBundle.adapt(BundleWiring.class)).andReturn(bw).anyTimes();
EasyMock.replay(providerBundle);
ServiceReference sr = mockServiceReference(providerBundle, serviceProps);
gpc.proxyIfNotAlreadyProxied(sr);
GuardProxyCatalog.CreateProxyRunnable runnable = gpc.createProxyQueue.take();
runnable.run(getProxyManager());
ServiceRegistrationHolder holder = gpc.proxyMap.get(serviceID);
ServiceRegistration<?> reg = holder.registration;
assertFalse("No roles defined for this service using configuration, so roles property should not be set", Arrays.asList(reg.getReference().getPropertyKeys()).contains(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY));
for (String key : serviceProps.keySet()) {
assertEquals(serviceProps.get(key), reg.getReference().getProperty(key));
}
assertEquals(Boolean.TRUE, reg.getReference().getProperty(GuardProxyCatalog.PROXY_SERVICE_KEY));
// now change the original service and let the proxy react
serviceProps.put(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY, "foobar");
assertEquals("Precondition, the mocked reference should have picked up this change", "foobar", sr.getProperty(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY));
gpc.serviceChanged(new ServiceEvent(ServiceEvent.MODIFIED, sr));
assertEquals("Changing the service should not change the number of proxies", 1, gpc.proxyMap.size());
assertFalse("The roles property set on the modified service should have been removed", Arrays.asList(reg.getReference().getPropertyKeys()).contains(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY));
assertEquals(Boolean.TRUE, reg.getReference().getProperty(GuardProxyCatalog.PROXY_SERVICE_KEY));
}
use of org.osgi.framework.ServiceEvent in project karaf by apache.
the class GuardingEventHookTest method testEventHookEvents.
@SuppressWarnings("unchecked")
@Test
public void testEventHookEvents() throws Exception {
BundleContext frameworkBC = mockBundleContext(0L);
Dictionary<String, Object> config = new Hashtable<>();
config.put("service.guard", "(service.id=*)");
BundleContext hookBC = mockConfigAdminBundleContext(frameworkBC, config);
GuardProxyCatalog gpc = new GuardProxyCatalog(hookBC);
Filter serviceFilter = FrameworkUtil.createFilter("(foo=bar)");
GuardingEventHook geh = new GuardingEventHook(hookBC, gpc, serviceFilter);
Dictionary<String, Object> props = new Hashtable<>();
props.put(Constants.SERVICE_ID, 13L);
ServiceReference<?> sref = mockServiceReference(props);
BundleContext client1BC = mockBundleContext(123L);
Map<BundleContext, Collection<ListenerInfo>> listeners = new HashMap<>();
listeners.put(client1BC, Collections.emptyList());
// Send the event. It should have no effect because the service doens't match the filter
assertEquals("Precondition", 0, gpc.proxyMap.size());
geh.event(new ServiceEvent(ServiceEvent.REGISTERED, sref), listeners);
assertEquals("No proxy should have been created because the service doesn't match the filter", 0, gpc.proxyMap.size());
assertEquals("Nothing should have been removed from the listeners", 1, listeners.size());
long service2ID = 887L;
Dictionary<String, Object> props2 = new Hashtable<>();
props2.put(Constants.SERVICE_ID, service2ID);
props2.put("a", "b");
props2.put("foo", "bar");
ServiceReference<?> sref2 = mockServiceReference(props2);
ServiceEvent se2 = new ServiceEvent(ServiceEvent.REGISTERED, sref2);
// Send the event to the system bundle and the bundle that contains the hook, should have no effect
Map<BundleContext, Collection<ListenerInfo>> listeners2 = new Hashtable<>();
listeners2.put(frameworkBC, Collections.emptyList());
listeners2.put(hookBC, Collections.emptyList());
geh.event(se2, listeners2);
assertEquals("No proxies to be created for the hook bundle or the system bundle", 0, gpc.proxyMap.size());
assertEquals("Nothing should have been removed from the listeners", 2, listeners2.size());
// This is the first time that a proxy actually does get created
Map<BundleContext, Collection<ListenerInfo>> listeners3 = new HashMap<>();
listeners3.put(client1BC, Collections.emptyList());
geh.event(se2, listeners3);
assertEquals("The service should be hidden from these listeners", 0, listeners3.size());
assertEquals("Proxy should have been created for this client", 1, gpc.proxyMap.size());
assertEquals(new Long(service2ID), gpc.proxyMap.keySet().iterator().next());
// Update the service, now an additional client is interested
// Will change the properties of sref
props2.put("a", "c");
Map<BundleContext, Collection<ListenerInfo>> listeners4 = new HashMap<>();
BundleContext client2BC = mockBundleContext(11);
listeners4.put(client2BC, Collections.emptyList());
listeners4.put(client1BC, Collections.emptyList());
geh.event(new ServiceEvent(ServiceEvent.MODIFIED, sref2), listeners4);
assertEquals("The service should be hidden from these listeners", 0, listeners4.size());
assertEquals("There should not be an additional proxy for client 2", 1, gpc.proxyMap.size());
assertNotNull(gpc.proxyMap.get(service2ID));
long service3ID = 1L;
Dictionary<String, Object> props3 = new Hashtable<>();
props3.put(Constants.SERVICE_ID, service3ID);
props3.put("foo", "bar");
ServiceReference<?> sref3 = mockServiceReference(props3);
// An event for a new service
Map<BundleContext, Collection<ListenerInfo>> listeners5 = new HashMap<>();
listeners5.put(client1BC, Collections.emptyList());
// Should be ignored
listeners5.put(client1BC, Collections.emptyList());
geh.event(new ServiceEvent(ServiceEvent.REGISTERED, sref3), listeners5);
assertEquals("There should be an additional procy for client1 to the new service", 2, gpc.proxyMap.size());
assertEquals("The service should be hidden from these listeners", 0, listeners5.size());
assertNotNull(gpc.proxyMap.get(service2ID));
assertNotNull(gpc.proxyMap.get(service3ID));
}
use of org.osgi.framework.ServiceEvent in project sling by apache.
the class SlingAuthenticatorServiceListenerTest method testModifyRegistration.
@Test
public void testModifyRegistration() {
final PathBasedHolderCache<AuthenticationRequirementHolder> cache = new PathBasedHolderCache<AuthenticationRequirementHolder>();
final BundleContext context = mock(BundleContext.class);
final SlingAuthenticatorServiceListener listener = SlingAuthenticatorServiceListener.createListener(context, cache);
final ServiceReference<?> ref1 = createServiceReference(new String[] { "/path1", "/path2", "/path3" });
listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref1));
assertPaths(cache, new String[] { "/path1", "/path2", "/path3" }, new ServiceReference<?>[] { ref1, ref1, ref1 });
when(ref1.getProperty(AuthConstants.AUTH_REQUIREMENTS)).thenReturn(new String[] { "/path1", "/path4", "/path5" });
assertPaths(cache, new String[] { "/path1", "/path2", "/path3" }, new ServiceReference<?>[] { ref1, ref1, ref1 });
listener.serviceChanged(new ServiceEvent(ServiceEvent.MODIFIED, ref1));
assertPaths(cache, new String[] { "/path1", "/path4", "/path5" }, new ServiceReference<?>[] { ref1, ref1, ref1 });
listener.serviceChanged(new ServiceEvent(ServiceEvent.MODIFIED_ENDMATCH, ref1));
assertTrue(cache.getHolders().isEmpty());
}
use of org.osgi.framework.ServiceEvent in project sling by apache.
the class SlingAuthenticatorServiceListenerTest method testAddRemoveRegistrations.
@Test
public void testAddRemoveRegistrations() {
final PathBasedHolderCache<AuthenticationRequirementHolder> cache = new PathBasedHolderCache<AuthenticationRequirementHolder>();
final BundleContext context = mock(BundleContext.class);
final SlingAuthenticatorServiceListener listener = SlingAuthenticatorServiceListener.createListener(context, cache);
final ServiceReference<?> ref1 = createServiceReference(new String[] { "/path1" });
listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref1));
final ServiceReference<?> ref2 = createServiceReference(new String[] { "/path2", "/path3" });
listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref2));
final ServiceReference<?> ref3 = createServiceReference(new String[] { "/path4", "/path5" });
listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref3));
assertPaths(cache, new String[] { "/path1", "/path2", "/path3", "/path4", "/path5" }, new ServiceReference<?>[] { ref1, ref2, ref2, ref3, ref3 });
listener.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, ref2));
assertPaths(cache, new String[] { "/path1", "/path4", "/path5" }, new ServiceReference<?>[] { ref1, ref3, ref3 });
listener.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, ref1));
assertPaths(cache, new String[] { "/path4", "/path5" }, new ServiceReference<?>[] { ref3, ref3 });
listener.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, ref3));
assertTrue(cache.getHolders().isEmpty());
}
use of org.osgi.framework.ServiceEvent in project sling by apache.
the class ServiceUserMappedBundleFilterTest method testEvent.
@Test
public void testEvent() {
Map<BundleContext, Collection<ListenerHook.ListenerInfo>> map = new HashMap<BundleContext, Collection<ListenerHook.ListenerInfo>>();
map.put(bundleContext1, new ArrayList<ListenerHook.ListenerInfo>());
map.put(bundleContext2, new ArrayList<ListenerHook.ListenerInfo>());
ServiceEvent serviceEvent = mock(ServiceEvent.class);
ServiceReference serviceReference = mock(ServiceReference.class);
when(serviceEvent.getServiceReference()).thenReturn(serviceReference);
when(serviceReference.getProperty(Constants.OBJECTCLASS)).thenReturn(new String[] { ServiceUserMappedImpl.SERVICEUSERMAPPED });
when(serviceReference.getProperty(Mapping.SERVICENAME)).thenReturn(BUNDLE1);
EventListenerHook eventListenerHook = new ServiceUserMappedBundleFilter();
eventListenerHook.event(serviceEvent, map);
TestCase.assertEquals(1, map.size());
TestCase.assertTrue(map.containsKey(bundleContext1));
}
Aggregations