@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testHandleServiceUnregistering() throws Exception { BundleContext clientBC = openStrictMockBundleContext(mockBundle(12345L)); BundleContext client2BC = openStrictMockBundleContext(mockBundle(6L)); EasyMock.replay(clientBC); EasyMock.replay(client2BC); Hashtable<String, Object> props = new Hashtable<>(); long originalServiceID = 12345678901234L; props.put(Constants.SERVICE_ID, Long.valueOf(originalServiceID)); props.put("foo", "bar"); ServiceReference<?> originalRef = mockServiceReference(props); Hashtable<String, Object> props2 = new Hashtable<>(); long anotherServiceID = 5123456789012345L; props2.put(Constants.SERVICE_ID, anotherServiceID); ServiceReference<?> anotherRef = mockServiceReference(props2); GuardProxyCatalog gpc = new GuardProxyCatalog(mockBundleContext()); ServiceRegistration<?> proxyReg = EasyMock.createMock(ServiceRegistration.class); EasyMock.expect(proxyReg.getReference()).andReturn((ServiceReference) mockServiceReference(props)).anyTimes(); proxyReg.unregister(); EasyMock.expectLastCall().once(); EasyMock.replay(proxyReg); ServiceRegistrationHolder srh = new GuardProxyCatalog.ServiceRegistrationHolder(); srh.registration = proxyReg; ServiceRegistration<?> proxy2Reg = EasyMock.createMock(ServiceRegistration.class); EasyMock.replay(proxy2Reg); ServiceRegistrationHolder srh2 = new GuardProxyCatalog.ServiceRegistrationHolder(); srh2.registration = proxy2Reg; gpc.proxyMap.put(originalServiceID, srh); gpc.proxyMap.put(anotherServiceID, srh2); assertEquals("Precondition", 2, gpc.proxyMap.size()); gpc.createProxyQueue.put(new MockCreateProxyRunnable(originalServiceID)); gpc.createProxyQueue.put(new MockCreateProxyRunnable(777)); assertEquals("Precondition", 2, gpc.createProxyQueue.size()); gpc.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, originalRef)); assertEquals("Registered events should be ignored", 2, gpc.proxyMap.size()); assertEquals("Registered events should be ignored", 2, gpc.createProxyQueue.size()); Hashtable<String, Object> proxyProps = new Hashtable<>(props); proxyProps.put(GuardProxyCatalog.PROXY_SERVICE_KEY, Boolean.TRUE); ServiceReference<?> proxyRef = mockServiceReference(proxyProps); gpc.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, proxyRef)); assertEquals("Unregistering the proxy should be ignored by the listener", 2, gpc.proxyMap.size()); assertEquals("Unregistering the proxy should be ignored by the listener", 2, gpc.createProxyQueue.size()); gpc.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, originalRef)); assertEquals("The proxy for this service should have been removed", 1, gpc.proxyMap.size()); assertEquals(anotherRef.getProperty(Constants.SERVICE_ID), gpc.proxyMap.keySet().iterator().next()); assertEquals("The create proxy job for this service should have been removed", 1, gpc.createProxyQueue.size()); assertEquals(777, gpc.createProxyQueue.iterator().next().getOriginalServiceID()); EasyMock.verify(proxyReg); EasyMock.verify(proxy2Reg); }
@Override public void serviceChanged(ServiceEvent event) { ServiceReference<?> sr = event.getServiceReference(); if (event.getType() == ServiceEvent.REGISTERED) { return; } if (isProxy(sr)) { return; } Long orgServiceID = (Long) sr.getProperty(Constants.SERVICE_ID); if (event.getType() == ServiceEvent.UNREGISTERING) { handleOriginalServiceUnregistering(orgServiceID); } if ((event.getType() & (ServiceEvent.MODIFIED | ServiceEvent.MODIFIED_ENDMATCH)) > 0) { handleOriginalServiceModifed(orgServiceID, sr); } }
GuardProxyCatalog implements ServiceListener { @Override public void serviceChanged(ServiceEvent event) { ServiceReference<?> sr = event.getServiceReference(); if (event.getType() == ServiceEvent.REGISTERED) { return; } if (isProxy(sr)) { return; } Long orgServiceID = (Long) sr.getProperty(Constants.SERVICE_ID); if (event.getType() == ServiceEvent.UNREGISTERING) { handleOriginalServiceUnregistering(orgServiceID); } if ((event.getType() & (ServiceEvent.MODIFIED | ServiceEvent.MODIFIED_ENDMATCH)) > 0) { handleOriginalServiceModifed(orgServiceID, sr); } } }
GuardProxyCatalog implements ServiceListener { @Override public void serviceChanged(ServiceEvent event) { ServiceReference<?> sr = event.getServiceReference(); if (event.getType() == ServiceEvent.REGISTERED) { return; } if (isProxy(sr)) { return; } Long orgServiceID = (Long) sr.getProperty(Constants.SERVICE_ID); if (event.getType() == ServiceEvent.UNREGISTERING) { handleOriginalServiceUnregistering(orgServiceID); } if ((event.getType() & (ServiceEvent.MODIFIED | ServiceEvent.MODIFIED_ENDMATCH)) > 0) { handleOriginalServiceModifed(orgServiceID, sr); } } GuardProxyCatalog(BundleContext bc); }
GuardProxyCatalog implements ServiceListener { @Override public void serviceChanged(ServiceEvent event) { ServiceReference<?> sr = event.getServiceReference(); if (event.getType() == ServiceEvent.REGISTERED) { return; } if (isProxy(sr)) { return; } Long orgServiceID = (Long) sr.getProperty(Constants.SERVICE_ID); if (event.getType() == ServiceEvent.UNREGISTERING) { handleOriginalServiceUnregistering(orgServiceID); } if ((event.getType() & (ServiceEvent.MODIFIED | ServiceEvent.MODIFIED_ENDMATCH)) > 0) { handleOriginalServiceModifed(orgServiceID, sr); } } GuardProxyCatalog(BundleContext bc); @Override void serviceChanged(ServiceEvent event); }
GuardProxyCatalog implements ServiceListener { @Override public void serviceChanged(ServiceEvent event) { ServiceReference<?> sr = event.getServiceReference(); if (event.getType() == ServiceEvent.REGISTERED) { return; } if (isProxy(sr)) { return; } Long orgServiceID = (Long) sr.getProperty(Constants.SERVICE_ID); if (event.getType() == ServiceEvent.UNREGISTERING) { handleOriginalServiceUnregistering(orgServiceID); } if ((event.getType() & (ServiceEvent.MODIFIED | ServiceEvent.MODIFIED_ENDMATCH)) > 0) { handleOriginalServiceModifed(orgServiceID, sr); } } GuardProxyCatalog(BundleContext bc); @Override void serviceChanged(ServiceEvent event); static final String KARAF_SECURED_SERVICES_SYSPROP; static final String SERVICE_GUARD_ROLES_PROPERTY; static final String KARAF_SECURED_COMMAND_COMPULSORY_ROLES_PROPERTY; }