@SuppressWarnings("unchecked") @Test public void testFindHook() throws Exception { Dictionary<String, Object> config = new Hashtable<>(); config.put("service.guard", "(|(moo=foo)(foo=*))"); BundleContext hookBC = mockConfigAdminBundleContext(config); GuardProxyCatalog gpc = new GuardProxyCatalog(hookBC); Filter serviceFilter = FrameworkUtil.createFilter("(foo=*)"); GuardingFindHook gfh = new GuardingFindHook(hookBC, gpc, serviceFilter); BundleContext clientBC = mockBundleContext(31L); Dictionary<String, Object> props = new Hashtable<>(); props.put(Constants.SERVICE_ID, 16L); props.put("moo", "foo"); ServiceReference<?> sref = mockServiceReference(props); Collection<ServiceReference<?>> refs = new ArrayList<>(); refs.add(sref); assertEquals("Precondition", 0, gpc.proxyMap.size()); gfh.find(clientBC, null, null, true, refs); assertEquals("The service doesn't match the filter so should have no effect", 0, gpc.proxyMap.size()); assertEquals("The service doesn't match the filter so should be presented to the client", Collections.singletonList(sref), refs); long service2ID = 17L; Dictionary<String, Object> props2 = new Hashtable<>(); props2.put(Constants.SERVICE_ID, service2ID); props2.put("foo", new Object()); ServiceReference<?> sref2 = mockServiceReference(props2); Collection<ServiceReference<?>> refs2 = new ArrayList<>(); refs2.add(sref2); gfh.find(clientBC, null, null, true, refs2); assertEquals("The service should be hidden from the client", 0, refs2.size()); assertEquals("The service should have caused a proxy creation", 1, gpc.proxyMap.size()); assertEquals("A proxy creation job should have been created", 1, gpc.createProxyQueue.size()); assertEquals(sref2.getProperty(Constants.SERVICE_ID), gpc.proxyMap.keySet().iterator().next()); Collection<ServiceReference<?>> refs3 = new ArrayList<>(); refs3.add(sref2); gfh.find(hookBC, null, null, true, refs3); assertEquals("The service should not be hidden from the hook bundle", Collections.singletonList(sref2), refs3); assertEquals("No proxy creation caused in this case", 1, gpc.proxyMap.size()); assertEquals("No change expected", sref2.getProperty(Constants.SERVICE_ID), gpc.proxyMap.keySet().iterator().next()); gfh.find(mockBundleContext(0L), null, null, true, refs3); assertEquals("The service should not be hidden from the framework bundle", Collections.singletonList(sref2), refs3); assertEquals("No proxy creation caused in this case", 1, gpc.proxyMap.size()); assertEquals("No change expected", sref2.getProperty(Constants.SERVICE_ID), gpc.proxyMap.keySet().iterator().next()); gpc.createProxyQueue.clear(); gfh.find(clientBC, null, null, true, refs3); assertEquals("The service should be hidden from the client", 0, refs3.size()); assertEquals("There is already a proxy for this client, no need for an additional one", 1, gpc.proxyMap.size()); assertEquals("No additional jobs should have been scheduled", 0, gpc.createProxyQueue.size()); assertEquals("No change expected", sref2.getProperty(Constants.SERVICE_ID), gpc.proxyMap.keySet().iterator().next()); Collection<ServiceReference<?>> refs4 = new ArrayList<>(); refs4.add(sref2); BundleContext client2BC = mockBundleContext(32768L); gfh.find(client2BC, null, null, true, refs4); assertEquals("The service should be hidden for this new client", 0, refs4.size()); assertEquals("No proxy creation job should have been created", 0, gpc.createProxyQueue.size()); assertEquals("No proxy creation caused in this case", 1, gpc.proxyMap.size()); assertEquals("No change expected", sref2.getProperty(Constants.SERVICE_ID), gpc.proxyMap.keySet().iterator().next()); }
public void find(BundleContext bundleContext, String name, String filter, boolean allServices, Collection<ServiceReference<?>> references) { if (servicesFilter == null) { return; } if (this.bundleContext.equals(bundleContext) || bundleContext.getBundle().getBundleId() == 0) { return; } for (Iterator<ServiceReference<?>> i = references.iterator(); i.hasNext(); ) { ServiceReference<?> sr = i.next(); if (!servicesFilter.match(sr)) { continue; } if (guardProxyCatalog.handleProxificationForHook(sr)) { i.remove(); } } }
GuardingFindHook implements FindHook { public void find(BundleContext bundleContext, String name, String filter, boolean allServices, Collection<ServiceReference<?>> references) { if (servicesFilter == null) { return; } if (this.bundleContext.equals(bundleContext) || bundleContext.getBundle().getBundleId() == 0) { return; } for (Iterator<ServiceReference<?>> i = references.iterator(); i.hasNext(); ) { ServiceReference<?> sr = i.next(); if (!servicesFilter.match(sr)) { continue; } if (guardProxyCatalog.handleProxificationForHook(sr)) { i.remove(); } } } }
GuardingFindHook implements FindHook { public void find(BundleContext bundleContext, String name, String filter, boolean allServices, Collection<ServiceReference<?>> references) { if (servicesFilter == null) { return; } if (this.bundleContext.equals(bundleContext) || bundleContext.getBundle().getBundleId() == 0) { return; } for (Iterator<ServiceReference<?>> i = references.iterator(); i.hasNext(); ) { ServiceReference<?> sr = i.next(); if (!servicesFilter.match(sr)) { continue; } if (guardProxyCatalog.handleProxificationForHook(sr)) { i.remove(); } } } GuardingFindHook(BundleContext bundleContext, GuardProxyCatalog guardProxyCatalog, Filter securedServicesFilter); }
GuardingFindHook implements FindHook { public void find(BundleContext bundleContext, String name, String filter, boolean allServices, Collection<ServiceReference<?>> references) { if (servicesFilter == null) { return; } if (this.bundleContext.equals(bundleContext) || bundleContext.getBundle().getBundleId() == 0) { return; } for (Iterator<ServiceReference<?>> i = references.iterator(); i.hasNext(); ) { ServiceReference<?> sr = i.next(); if (!servicesFilter.match(sr)) { continue; } if (guardProxyCatalog.handleProxificationForHook(sr)) { i.remove(); } } } GuardingFindHook(BundleContext bundleContext, GuardProxyCatalog guardProxyCatalog, Filter securedServicesFilter); void find(BundleContext bundleContext, String name, String filter, boolean allServices, Collection<ServiceReference<?>> references); }
GuardingFindHook implements FindHook { public void find(BundleContext bundleContext, String name, String filter, boolean allServices, Collection<ServiceReference<?>> references) { if (servicesFilter == null) { return; } if (this.bundleContext.equals(bundleContext) || bundleContext.getBundle().getBundleId() == 0) { return; } for (Iterator<ServiceReference<?>> i = references.iterator(); i.hasNext(); ) { ServiceReference<?> sr = i.next(); if (!servicesFilter.match(sr)) { continue; } if (guardProxyCatalog.handleProxificationForHook(sr)) { i.remove(); } } } GuardingFindHook(BundleContext bundleContext, GuardProxyCatalog guardProxyCatalog, Filter securedServicesFilter); void find(BundleContext bundleContext, String name, String filter, boolean allServices, Collection<ServiceReference<?>> references);  }