@SuppressWarnings("unchecked") @Test public void testEventHookProxyEvents() 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("(service.id=*)"); GuardingEventHook geh = new GuardingEventHook(hookBC, gpc, serviceFilter); BundleContext client1BC = mockBundleContext(123L); Dictionary<String, Object> props = new Hashtable<>(); props.put(Constants.SERVICE_ID, 13L); props.put(GuardProxyCatalog.PROXY_SERVICE_KEY, Boolean.TRUE); ServiceReference<?> sref = mockServiceReference(props); 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 changes expected for the proxy map.", 0, gpc.proxyMap.size()); assertEquals("The event should be delivered to the client", 1, listeners.size()); Map<BundleContext, Collection<ListenerInfo>> listeners2 = new HashMap<>(); listeners2.put(mockBundleContext(51L), Collections.emptyList()); geh.event(new ServiceEvent(ServiceEvent.REGISTERED, sref), listeners2); assertEquals("No changes expected for the proxy map.", 0, gpc.proxyMap.size()); assertEquals("The event should be delivered to the client", 1, listeners2.size()); }
@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);  }