Search in sources :

Example 11 with BundleContext

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;
}
Also used : Bundle(org.osgi.framework.Bundle) BundleContext(org.osgi.framework.BundleContext)

Example 12 with 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;
    }
}
Also used : Bundle(org.osgi.framework.Bundle) Callable(java.util.concurrent.Callable) BundleContext(org.osgi.framework.BundleContext)

Example 13 with BundleContext

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<>())));
}
Also used : Hashtable(java.util.Hashtable) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 14 with BundleContext

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());
}
Also used : Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) ServiceReference(org.osgi.framework.ServiceReference) Filter(org.osgi.framework.Filter) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 15 with BundleContext

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;
}
Also used : ServiceListener(org.osgi.framework.ServiceListener) Configuration(org.osgi.service.cm.Configuration) Bundle(org.osgi.framework.Bundle) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) ServiceReference(org.osgi.framework.ServiceReference) BundleContext(org.osgi.framework.BundleContext)

Aggregations

BundleContext (org.osgi.framework.BundleContext)1052 Test (org.junit.Test)361 Bundle (org.osgi.framework.Bundle)298 ServiceReference (org.osgi.framework.ServiceReference)229 File (java.io.File)119 Hashtable (java.util.Hashtable)114 HashMap (java.util.HashMap)98 ArrayList (java.util.ArrayList)97 IOException (java.io.IOException)75 ServiceRegistration (org.osgi.framework.ServiceRegistration)68 BundleException (org.osgi.framework.BundleException)60 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)59 Equinox (org.eclipse.osgi.launch.Equinox)51 ComponentContext (org.osgi.service.component.ComponentContext)50 Dictionary (java.util.Dictionary)48 Before (org.junit.Before)48 Properties (java.util.Properties)47 LinkedHashMap (java.util.LinkedHashMap)46 URL (java.net.URL)43 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)40