use of org.osgi.framework.ServiceReference in project karaf by apache.
the class GuardingFindHookTest method testFindHook.
@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);
// Ensure that the hook bundle has nothing hidden
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());
// Ensure that the system bundle has nothing hidden
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());
// Ensure that if we ask for the same client again, it will not create another proxy
// Manually empty the queue
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);
// another client should not get another proxy
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());
}
use of org.osgi.framework.ServiceReference in project karaf by apache.
the class GuardingFindHookTest method mockConfigAdminBundleContext.
@SuppressWarnings({ "rawtypes", "unchecked" })
private BundleContext mockConfigAdminBundleContext(Dictionary<String, Object>... configs) throws IOException, InvalidSyntaxException {
Configuration[] configurations = new Configuration[configs.length];
for (int i = 0; i < configs.length; i++) {
Configuration conf = EasyMock.createMock(Configuration.class);
EasyMock.expect(conf.getProperties()).andReturn(configs[i]).anyTimes();
EasyMock.expect(conf.getPid()).andReturn((String) configs[i].get(Constants.SERVICE_PID)).anyTimes();
EasyMock.replay(conf);
configurations[i] = conf;
}
ConfigurationAdmin ca = EasyMock.createMock(ConfigurationAdmin.class);
EasyMock.expect(ca.listConfigurations("(&(service.pid=org.apache.karaf.service.acl.*)(service.guard=*))")).andReturn(configurations).anyTimes();
EasyMock.replay(ca);
final ServiceReference caSR = EasyMock.createMock(ServiceReference.class);
EasyMock.replay(caSR);
Bundle b = EasyMock.createMock(Bundle.class);
EasyMock.expect(b.getBundleId()).andReturn(877342449L).anyTimes();
EasyMock.replay(b);
BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
EasyMock.expect(bc.getBundle()).andReturn(b).anyTimes();
EasyMock.expect(bc.createFilter(EasyMock.isA(String.class))).andAnswer(() -> FrameworkUtil.createFilter((String) EasyMock.getCurrentArguments()[0])).anyTimes();
String cmFilter = "(&(objectClass=" + ConfigurationAdmin.class.getName() + ")" + "(!(" + GuardProxyCatalog.PROXY_SERVICE_KEY + "=*)))";
bc.addServiceListener(EasyMock.isA(ServiceListener.class), EasyMock.eq(cmFilter));
EasyMock.expectLastCall().anyTimes();
EasyMock.expect(bc.getServiceReferences(EasyMock.anyObject(String.class), EasyMock.eq(cmFilter))).andReturn(new ServiceReference<?>[] { caSR }).anyTimes();
EasyMock.expect(bc.getService(caSR)).andReturn(ca).anyTimes();
EasyMock.replay(bc);
return bc;
}
use of org.osgi.framework.ServiceReference in project karaf by apache.
the class GuardProxyCatalogTest method testAssignRoles3.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testAssignRoles3() throws Exception {
abstract class MyAbstractClass implements TestServiceAPI, TestServiceAPI2 {
}
Dictionary<String, Object> config = new Hashtable<>();
config.put(Constants.SERVICE_PID, "foobar");
config.put("service.guard", "(objectClass=" + TestServiceAPI2.class.getName() + ")");
config.put("doit", "X");
BundleContext bc = mockConfigAdminBundleContext(config);
Map<ServiceReference, Object> serviceMap = new HashMap<>();
testCreateProxy(bc, new Class[] { TestServiceAPI.class, TestServiceAPI2.class }, new MyAbstractClass() {
@Override
public String doit() {
return null;
}
@Override
public String doit(String s) {
return null;
}
}, serviceMap);
assertEquals(1, serviceMap.size());
assertEquals(Collections.singleton("X"), serviceMap.keySet().iterator().next().getProperty(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY));
}
use of org.osgi.framework.ServiceReference in project karaf by apache.
the class EncryptablePropertyPlaceholderTest method getOsgiService.
protected <T> T getOsgiService(Class<T> type, String filter, long timeout) {
ServiceTracker tracker = null;
try {
String flt;
if (filter != null) {
if (filter.startsWith("(")) {
flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")" + filter + ")";
} else {
flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")(" + filter + "))";
}
} else {
flt = "(" + Constants.OBJECTCLASS + "=" + type.getName() + ")";
}
Filter osgiFilter = FrameworkUtil.createFilter(flt);
tracker = new ServiceTracker(bundleContext, osgiFilter, null);
tracker.open(true);
// Note that the tracker is not closed to keep the reference
// This is buggy, as the service reference may change i think
Object svc = type.cast(tracker.waitForService(timeout));
if (svc == null) {
Dictionary dic = bundleContext.getBundle().getHeaders();
System.err.println("Test bundle headers: " + explode(dic));
for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, null))) {
System.err.println("ServiceReference: " + ref);
}
for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, flt))) {
System.err.println("Filtered ServiceReference: " + ref);
}
throw new RuntimeException("Gave up waiting for service " + flt);
}
return type.cast(svc);
} catch (InvalidSyntaxException e) {
throw new IllegalArgumentException("Invalid filter", e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
use of org.osgi.framework.ServiceReference in project karaf by apache.
the class Activator method start.
@Override
public void start(final BundleContext context) throws Exception {
ProxyLoginModule.init(context.getBundle(0).getBundleContext());
final OsgiKeystoreManager keystoreManager = new OsgiKeystoreManager();
keystoreInstanceServiceTracker = new ServiceTracker<>(context, KeystoreInstance.class, new ServiceTrackerCustomizer<KeystoreInstance, KeystoreInstance>() {
@Override
public KeystoreInstance addingService(ServiceReference<KeystoreInstance> reference) {
KeystoreInstance service = context.getService(reference);
keystoreManager.register(service, null);
return service;
}
@Override
public void modifiedService(ServiceReference<KeystoreInstance> reference, KeystoreInstance service) {
}
@Override
public void removedService(ServiceReference<KeystoreInstance> reference, KeystoreInstance service) {
keystoreManager.unregister(service, null);
context.ungetService(reference);
}
});
keystoreInstanceServiceTracker.open();
osgiConfiguration = new OsgiConfiguration();
osgiConfiguration.init();
jaasRealmServiceTracker = new ServiceTracker<>(context, JaasRealm.class, new ServiceTrackerCustomizer<JaasRealm, JaasRealm>() {
@Override
public JaasRealm addingService(ServiceReference<JaasRealm> reference) {
JaasRealm service = context.getService(reference);
osgiConfiguration.register(service, null);
return service;
}
@Override
public void modifiedService(ServiceReference<JaasRealm> reference, JaasRealm service) {
}
@Override
public void removedService(ServiceReference<JaasRealm> reference, JaasRealm service) {
osgiConfiguration.unregister(service, null);
}
});
jaasRealmServiceTracker.open();
registration = context.registerService(KeystoreManager.class, keystoreManager, null);
}
Aggregations