@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()); 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); 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()); 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(Long.valueOf(service2ID), gpc.proxyMap.keySet().iterator().next()); 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); Map<BundleContext, Collection<ListenerInfo>> listeners5 = new HashMap<>(); listeners5.put(client1BC, Collections.emptyList()); 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)); }
@Override public void event(ServiceEvent event, Map<BundleContext, Collection<ListenerHook.ListenerInfo>> listeners) { if (servicesFilter == null) { return; } ServiceReference<?> sr = event.getServiceReference(); if (!servicesFilter.match(sr)) { return; } boolean proxificationDone = false; BundleContext system = bundleContext.getBundle(0).getBundleContext(); for (Iterator<BundleContext> i = listeners.keySet().iterator(); i.hasNext(); ) { BundleContext bc = i.next(); if (bc == bundleContext || bc == system) { continue; } if (proxificationDone || (proxificationDone = guardProxyCatalog.handleProxificationForHook(sr))) { i.remove(); } } }
GuardingEventHook implements EventListenerHook { @Override public void event(ServiceEvent event, Map<BundleContext, Collection<ListenerHook.ListenerInfo>> listeners) { if (servicesFilter == null) { return; } ServiceReference<?> sr = event.getServiceReference(); if (!servicesFilter.match(sr)) { return; } boolean proxificationDone = false; BundleContext system = bundleContext.getBundle(0).getBundleContext(); for (Iterator<BundleContext> i = listeners.keySet().iterator(); i.hasNext(); ) { BundleContext bc = i.next(); if (bc == bundleContext || bc == system) { continue; } if (proxificationDone || (proxificationDone = guardProxyCatalog.handleProxificationForHook(sr))) { i.remove(); } } } }
GuardingEventHook implements EventListenerHook { @Override public void event(ServiceEvent event, Map<BundleContext, Collection<ListenerHook.ListenerInfo>> listeners) { if (servicesFilter == null) { return; } ServiceReference<?> sr = event.getServiceReference(); if (!servicesFilter.match(sr)) { return; } boolean proxificationDone = false; BundleContext system = bundleContext.getBundle(0).getBundleContext(); for (Iterator<BundleContext> i = listeners.keySet().iterator(); i.hasNext(); ) { BundleContext bc = i.next(); if (bc == bundleContext || bc == system) { continue; } if (proxificationDone || (proxificationDone = guardProxyCatalog.handleProxificationForHook(sr))) { i.remove(); } } } GuardingEventHook(BundleContext bundleContext, GuardProxyCatalog guardProxyCatalog, Filter securedServicesFilter); }
GuardingEventHook implements EventListenerHook { @Override public void event(ServiceEvent event, Map<BundleContext, Collection<ListenerHook.ListenerInfo>> listeners) { if (servicesFilter == null) { return; } ServiceReference<?> sr = event.getServiceReference(); if (!servicesFilter.match(sr)) { return; } boolean proxificationDone = false; BundleContext system = bundleContext.getBundle(0).getBundleContext(); for (Iterator<BundleContext> i = listeners.keySet().iterator(); i.hasNext(); ) { BundleContext bc = i.next(); if (bc == bundleContext || bc == system) { continue; } if (proxificationDone || (proxificationDone = guardProxyCatalog.handleProxificationForHook(sr))) { i.remove(); } } } GuardingEventHook(BundleContext bundleContext, GuardProxyCatalog guardProxyCatalog, Filter securedServicesFilter); @Override void event(ServiceEvent event, Map<BundleContext, Collection<ListenerHook.ListenerInfo>> listeners); }
GuardingEventHook implements EventListenerHook { @Override public void event(ServiceEvent event, Map<BundleContext, Collection<ListenerHook.ListenerInfo>> listeners) { if (servicesFilter == null) { return; } ServiceReference<?> sr = event.getServiceReference(); if (!servicesFilter.match(sr)) { return; } boolean proxificationDone = false; BundleContext system = bundleContext.getBundle(0).getBundleContext(); for (Iterator<BundleContext> i = listeners.keySet().iterator(); i.hasNext(); ) { BundleContext bc = i.next(); if (bc == bundleContext || bc == system) { continue; } if (proxificationDone || (proxificationDone = guardProxyCatalog.handleProxificationForHook(sr))) { i.remove(); } } } GuardingEventHook(BundleContext bundleContext, GuardProxyCatalog guardProxyCatalog, Filter securedServicesFilter); @Override void event(ServiceEvent event, Map<BundleContext, Collection<ListenerHook.ListenerInfo>> listeners);  }