use of org.osgi.framework.BundleContext in project karaf by apache.
the class TestBundleFactory method createBundleContext.
public BundleContext createBundleContext() {
BundleContext bundleContext = createMock(BundleContext.class);
Bundle[] bundles = createBundles();
expect(bundleContext.getProperty("karaf.systemBundlesStartLevel")).andReturn(Integer.toString(50)).anyTimes();
expect(bundleContext.getBundles()).andReturn(bundles).anyTimes();
expect(bundleContext.getBundle(0)).andReturn(null).anyTimes();
expect(bundleContext.getBundle(1)).andReturn(bundles[0]).anyTimes();
expect(bundleContext.getBundle(2)).andReturn(bundles[1]).anyTimes();
replay(bundleContext);
return bundleContext;
}
use of org.osgi.framework.BundleContext in project karaf by apache.
the class InfoAction method getOsgiFramework.
String getOsgiFramework() {
try {
Callable<String> call = new Callable<String>() {
@Override
public String call() throws Exception {
BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext();
Bundle sysBundle = context.getBundle(0);
return sysBundle.getSymbolicName() + "-" + sysBundle.getVersion();
}
};
return call.call();
} catch (Throwable t) {
// We're not in OSGi, just safely return null
return null;
}
}
use of org.osgi.framework.BundleContext in project karaf by apache.
the class GuardProxyCatalogTest method testIsProxy.
@Test
public void testIsProxy() throws Exception {
BundleContext bc = mockBundleContext();
GuardProxyCatalog gpc = new GuardProxyCatalog(bc);
Dictionary<String, Object> props = new Hashtable<>();
props.put(GuardProxyCatalog.PROXY_SERVICE_KEY, Boolean.TRUE);
assertTrue(gpc.isProxy(mockServiceReference(props)));
assertFalse(gpc.isProxy(mockServiceReference(new Hashtable<>())));
}
use of org.osgi.framework.BundleContext 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.BundleContext 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;
}
Aggregations