@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void testServiceFactoryBehaviour() throws Exception { final Map<ServiceReference, Object> serviceMap = new HashMap<>(); TestServiceAPI testService = new TestService(); BundleContext bc = mockConfigAdminBundleContext(); GuardProxyCatalog gpc = new GuardProxyCatalog(bc); long serviceID = 117L; final Hashtable<String, Object> serviceProps = new Hashtable<>(); serviceProps.put(Constants.OBJECTCLASS, new String [] {TestServiceAPI.class.getName()}); serviceProps.put(Constants.SERVICE_ID, serviceID); serviceProps.put("bar", 42L); BundleContext providerBC = EasyMock.createMock(BundleContext.class); EasyMock.expect(providerBC.registerService( EasyMock.isA(String[].class), EasyMock.anyObject(), EasyMock.isA(Dictionary.class))).andAnswer((IAnswer) () -> { Dictionary<String,Object> props = (Dictionary<String, Object>) EasyMock.getCurrentArguments()[2]; ServiceRegistration reg = EasyMock.createMock(ServiceRegistration.class); ServiceReference sr = mockServiceReference(props); EasyMock.expect(reg.getReference()).andReturn(sr).anyTimes(); EasyMock.replay(reg); serviceMap.put(sr, EasyMock.getCurrentArguments()[1]); return reg; }).once(); EasyMock.expect(providerBC.getService(EasyMock.isA(ServiceReference.class))).andAnswer( () -> serviceMap.get(EasyMock.getCurrentArguments()[0])).anyTimes(); EasyMock.replay(providerBC); BundleWiring bw = EasyMock.createMock(BundleWiring.class); EasyMock.expect(bw.getClassLoader()).andReturn(getClass().getClassLoader()).anyTimes(); EasyMock.replay(bw); 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); BundleContext clientBC = EasyMock.createMock(BundleContext.class); EasyMock.expect(clientBC.getService(sr)).andReturn(testService).anyTimes(); EasyMock.replay(clientBC); Bundle clientBundle = EasyMock.createNiceMock(Bundle.class); EasyMock.expect(clientBundle.getBundleContext()).andReturn(clientBC).anyTimes(); EasyMock.replay(clientBundle); gpc.proxyIfNotAlreadyProxied(sr); GuardProxyCatalog.ServiceRegistrationHolder holder = gpc.proxyMap.get(serviceID); GuardProxyCatalog.CreateProxyRunnable runnable = gpc.createProxyQueue.take(); assertEquals(117L, runnable.getOriginalServiceID()); ProxyManager pm = getProxyManager(); runnable.run(pm); ServiceReference<?> proxySR = holder.registration.getReference(); EasyMock.verify(providerBC); ServiceFactory proxyServiceSF = (ServiceFactory) serviceMap.get(proxySR); TestServiceAPI proxyService = (TestServiceAPI) proxyServiceSF.getService(clientBundle, null); assertNotSame("The proxy should not be the same object as the original service", testService, proxyService); assertEquals("Doing it", proxyService.doit()); EasyMock.reset(clientBC); EasyMock.expect(clientBC.ungetService(sr)).andReturn(true).once(); EasyMock.replay(clientBC); proxyServiceSF.ungetService(clientBundle, null, proxyService); EasyMock.verify(clientBC); }
void proxyIfNotAlreadyProxied(final ServiceReference<?> originalRef) { final long orgServiceID = (Long) originalRef.getProperty(Constants.SERVICE_ID); final ServiceRegistrationHolder registrationHolder = new ServiceRegistrationHolder(); ServiceRegistrationHolder previousHolder = proxyMap.putIfAbsent(orgServiceID, registrationHolder); if (previousHolder != null) { return; } LOG.trace("Will create proxy of service {}({})", originalRef.getProperty(Constants.OBJECTCLASS), orgServiceID); CreateProxyRunnable cpr = new CreateProxyRunnable() { @Override public long getOriginalServiceID() { return orgServiceID; } @Override public void run(final ProxyManager pm) throws Exception { String[] objectClassProperty = (String[]) originalRef.getProperty(Constants.OBJECTCLASS); ServiceFactory<Object> sf = new ProxyServiceFactory(pm, originalRef); registrationHolder.registration = originalRef.getBundle().getBundleContext().registerService( objectClassProperty, sf, proxyPropertiesRoles()); Dictionary<String, Object> actualProxyProps = copyProperties(registrationHolder.registration.getReference()); LOG.debug("Created proxy of service {} under {} with properties {}", orgServiceID, actualProxyProps.get(Constants.OBJECTCLASS), actualProxyProps); } private Dictionary<String, Object> proxyPropertiesRoles() throws Exception { Dictionary<String, Object> p = proxyProperties(originalRef); Set<String> roles = getServiceInvocationRoles(originalRef); if (roles != null) { roles.remove(ROLE_WILDCARD); p.put(SERVICE_GUARD_ROLES_PROPERTY, roles); } else { p.remove(SERVICE_GUARD_ROLES_PROPERTY); } return p; } }; try { createProxyQueue.put(cpr); } catch (InterruptedException e) { LOG.warn("Problem scheduling a proxy creator for service {}({})", originalRef.getProperty(Constants.OBJECTCLASS), orgServiceID, e); e.printStackTrace(); } }
GuardProxyCatalog implements ServiceListener { void proxyIfNotAlreadyProxied(final ServiceReference<?> originalRef) { final long orgServiceID = (Long) originalRef.getProperty(Constants.SERVICE_ID); final ServiceRegistrationHolder registrationHolder = new ServiceRegistrationHolder(); ServiceRegistrationHolder previousHolder = proxyMap.putIfAbsent(orgServiceID, registrationHolder); if (previousHolder != null) { return; } LOG.trace("Will create proxy of service {}({})", originalRef.getProperty(Constants.OBJECTCLASS), orgServiceID); CreateProxyRunnable cpr = new CreateProxyRunnable() { @Override public long getOriginalServiceID() { return orgServiceID; } @Override public void run(final ProxyManager pm) throws Exception { String[] objectClassProperty = (String[]) originalRef.getProperty(Constants.OBJECTCLASS); ServiceFactory<Object> sf = new ProxyServiceFactory(pm, originalRef); registrationHolder.registration = originalRef.getBundle().getBundleContext().registerService( objectClassProperty, sf, proxyPropertiesRoles()); Dictionary<String, Object> actualProxyProps = copyProperties(registrationHolder.registration.getReference()); LOG.debug("Created proxy of service {} under {} with properties {}", orgServiceID, actualProxyProps.get(Constants.OBJECTCLASS), actualProxyProps); } private Dictionary<String, Object> proxyPropertiesRoles() throws Exception { Dictionary<String, Object> p = proxyProperties(originalRef); Set<String> roles = getServiceInvocationRoles(originalRef); if (roles != null) { roles.remove(ROLE_WILDCARD); p.put(SERVICE_GUARD_ROLES_PROPERTY, roles); } else { p.remove(SERVICE_GUARD_ROLES_PROPERTY); } return p; } }; try { createProxyQueue.put(cpr); } catch (InterruptedException e) { LOG.warn("Problem scheduling a proxy creator for service {}({})", originalRef.getProperty(Constants.OBJECTCLASS), orgServiceID, e); e.printStackTrace(); } } }
GuardProxyCatalog implements ServiceListener { void proxyIfNotAlreadyProxied(final ServiceReference<?> originalRef) { final long orgServiceID = (Long) originalRef.getProperty(Constants.SERVICE_ID); final ServiceRegistrationHolder registrationHolder = new ServiceRegistrationHolder(); ServiceRegistrationHolder previousHolder = proxyMap.putIfAbsent(orgServiceID, registrationHolder); if (previousHolder != null) { return; } LOG.trace("Will create proxy of service {}({})", originalRef.getProperty(Constants.OBJECTCLASS), orgServiceID); CreateProxyRunnable cpr = new CreateProxyRunnable() { @Override public long getOriginalServiceID() { return orgServiceID; } @Override public void run(final ProxyManager pm) throws Exception { String[] objectClassProperty = (String[]) originalRef.getProperty(Constants.OBJECTCLASS); ServiceFactory<Object> sf = new ProxyServiceFactory(pm, originalRef); registrationHolder.registration = originalRef.getBundle().getBundleContext().registerService( objectClassProperty, sf, proxyPropertiesRoles()); Dictionary<String, Object> actualProxyProps = copyProperties(registrationHolder.registration.getReference()); LOG.debug("Created proxy of service {} under {} with properties {}", orgServiceID, actualProxyProps.get(Constants.OBJECTCLASS), actualProxyProps); } private Dictionary<String, Object> proxyPropertiesRoles() throws Exception { Dictionary<String, Object> p = proxyProperties(originalRef); Set<String> roles = getServiceInvocationRoles(originalRef); if (roles != null) { roles.remove(ROLE_WILDCARD); p.put(SERVICE_GUARD_ROLES_PROPERTY, roles); } else { p.remove(SERVICE_GUARD_ROLES_PROPERTY); } return p; } }; try { createProxyQueue.put(cpr); } catch (InterruptedException e) { LOG.warn("Problem scheduling a proxy creator for service {}({})", originalRef.getProperty(Constants.OBJECTCLASS), orgServiceID, e); e.printStackTrace(); } } GuardProxyCatalog(BundleContext bc); }
GuardProxyCatalog implements ServiceListener { void proxyIfNotAlreadyProxied(final ServiceReference<?> originalRef) { final long orgServiceID = (Long) originalRef.getProperty(Constants.SERVICE_ID); final ServiceRegistrationHolder registrationHolder = new ServiceRegistrationHolder(); ServiceRegistrationHolder previousHolder = proxyMap.putIfAbsent(orgServiceID, registrationHolder); if (previousHolder != null) { return; } LOG.trace("Will create proxy of service {}({})", originalRef.getProperty(Constants.OBJECTCLASS), orgServiceID); CreateProxyRunnable cpr = new CreateProxyRunnable() { @Override public long getOriginalServiceID() { return orgServiceID; } @Override public void run(final ProxyManager pm) throws Exception { String[] objectClassProperty = (String[]) originalRef.getProperty(Constants.OBJECTCLASS); ServiceFactory<Object> sf = new ProxyServiceFactory(pm, originalRef); registrationHolder.registration = originalRef.getBundle().getBundleContext().registerService( objectClassProperty, sf, proxyPropertiesRoles()); Dictionary<String, Object> actualProxyProps = copyProperties(registrationHolder.registration.getReference()); LOG.debug("Created proxy of service {} under {} with properties {}", orgServiceID, actualProxyProps.get(Constants.OBJECTCLASS), actualProxyProps); } private Dictionary<String, Object> proxyPropertiesRoles() throws Exception { Dictionary<String, Object> p = proxyProperties(originalRef); Set<String> roles = getServiceInvocationRoles(originalRef); if (roles != null) { roles.remove(ROLE_WILDCARD); p.put(SERVICE_GUARD_ROLES_PROPERTY, roles); } else { p.remove(SERVICE_GUARD_ROLES_PROPERTY); } return p; } }; try { createProxyQueue.put(cpr); } catch (InterruptedException e) { LOG.warn("Problem scheduling a proxy creator for service {}({})", originalRef.getProperty(Constants.OBJECTCLASS), orgServiceID, e); e.printStackTrace(); } } GuardProxyCatalog(BundleContext bc); @Override void serviceChanged(ServiceEvent event); }
GuardProxyCatalog implements ServiceListener { void proxyIfNotAlreadyProxied(final ServiceReference<?> originalRef) { final long orgServiceID = (Long) originalRef.getProperty(Constants.SERVICE_ID); final ServiceRegistrationHolder registrationHolder = new ServiceRegistrationHolder(); ServiceRegistrationHolder previousHolder = proxyMap.putIfAbsent(orgServiceID, registrationHolder); if (previousHolder != null) { return; } LOG.trace("Will create proxy of service {}({})", originalRef.getProperty(Constants.OBJECTCLASS), orgServiceID); CreateProxyRunnable cpr = new CreateProxyRunnable() { @Override public long getOriginalServiceID() { return orgServiceID; } @Override public void run(final ProxyManager pm) throws Exception { String[] objectClassProperty = (String[]) originalRef.getProperty(Constants.OBJECTCLASS); ServiceFactory<Object> sf = new ProxyServiceFactory(pm, originalRef); registrationHolder.registration = originalRef.getBundle().getBundleContext().registerService( objectClassProperty, sf, proxyPropertiesRoles()); Dictionary<String, Object> actualProxyProps = copyProperties(registrationHolder.registration.getReference()); LOG.debug("Created proxy of service {} under {} with properties {}", orgServiceID, actualProxyProps.get(Constants.OBJECTCLASS), actualProxyProps); } private Dictionary<String, Object> proxyPropertiesRoles() throws Exception { Dictionary<String, Object> p = proxyProperties(originalRef); Set<String> roles = getServiceInvocationRoles(originalRef); if (roles != null) { roles.remove(ROLE_WILDCARD); p.put(SERVICE_GUARD_ROLES_PROPERTY, roles); } else { p.remove(SERVICE_GUARD_ROLES_PROPERTY); } return p; } }; try { createProxyQueue.put(cpr); } catch (InterruptedException e) { LOG.warn("Problem scheduling a proxy creator for service {}({})", originalRef.getProperty(Constants.OBJECTCLASS), orgServiceID, e); e.printStackTrace(); } } 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; }