Search in sources :

Example 21 with BundleContext

use of org.osgi.framework.BundleContext in project karaf by apache.

the class GuardProxyCatalogTest method testInvocationBlocking5.

@SuppressWarnings("unchecked")
@Test
public void testInvocationBlocking5() throws Exception {
    Dictionary<String, Object> c1 = new Hashtable<>();
    c1.put(Constants.SERVICE_PID, "foobar");
    c1.put("service.guard", "(objectClass=" + TestServiceAPI.class.getName() + ")");
    c1.put("doit", "a,b");
    BundleContext bc = mockConfigAdminBundleContext(c1);
    final Object proxy = testCreateProxy(bc, new Class[] { TestServiceAPI2.class }, (TestServiceAPI2) String::toUpperCase);
    // Invoke the service with role 'c'.
    Subject subject = new Subject();
    subject.getPrincipals().add(new RolePrincipal("c"));
    Subject.doAs(subject, (PrivilegedAction<Object>) () -> {
        assertEquals("The invocation under role 'c' should be ok, as there are no rules specified " + "for this service at all.", "HELLO", ((TestServiceAPI2) proxy).doit("hello"));
        return null;
    });
}
Also used : Hashtable(java.util.Hashtable) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal) Subject(javax.security.auth.Subject) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 22 with BundleContext

use of org.osgi.framework.BundleContext in project karaf by apache.

the class GuardProxyCatalogTest method testHandleProxificationForHook.

@SuppressWarnings("unchecked")
@Test
public void testHandleProxificationForHook() throws Exception {
    Dictionary<String, Object> config = new Hashtable<>();
    config.put(Constants.SERVICE_PID, GuardProxyCatalog.SERVICE_ACL_PREFIX + "foo");
    config.put(GuardProxyCatalog.SERVICE_GUARD_KEY, "(a>=5)");
    BundleContext bc = mockConfigAdminBundleContext(config);
    GuardProxyCatalog gpc = new GuardProxyCatalog(bc);
    Dictionary<String, Object> props = new Hashtable<>();
    props.put(Constants.SERVICE_ID, 13L);
    props.put("a", "6");
    props.put(GuardProxyCatalog.PROXY_SERVICE_KEY, Boolean.TRUE);
    ServiceReference<?> sref2 = mockServiceReference(props);
    assertFalse("Should not hide an existing proxy for this client", gpc.handleProxificationForHook(sref2));
    assertEquals("No proxy should have been created", 0, gpc.proxyMap.size());
    Dictionary<String, Object> props4 = new Hashtable<>();
    props4.put(Constants.SERVICE_ID, 15L);
    props4.put("a", "7");
    ServiceReference<?> sref4 = mockServiceReference(props4);
    assertTrue("Should hide a service that needs to be proxied", gpc.handleProxificationForHook(sref4));
    assertEquals("Should trigger proxy creation", 1, gpc.proxyMap.size());
}
Also used : Hashtable(java.util.Hashtable) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 23 with BundleContext

use of org.osgi.framework.BundleContext in project karaf by apache.

the class SystemServiceImplTest method testSetName.

@Test
public void testSetName() throws URISyntaxException, IOException {
    SystemServiceImpl system = new SystemServiceImpl();
    BundleContext bundleContext = EasyMock.createMock(BundleContext.class);
    URL propUrl = this.getClass().getClassLoader().getResource("etc/system.properties");
    File propfile = new File(propUrl.toURI());
    EasyMock.expect(bundleContext.getProperty("karaf.etc")).andReturn(propfile.getParentFile().getParent() + "/etc");
    EasyMock.replay(bundleContext);
    system.setBundleContext(bundleContext);
    system.setName(NEW_NAME);
    EasyMock.verify(bundleContext);
    Properties props = new Properties(propfile);
    String nameAfter = props.getProperty("karaf.name");
    Assert.assertEquals(NEW_NAME, nameAfter);
}
Also used : Properties(org.apache.felix.utils.properties.Properties) File(java.io.File) URL(java.net.URL) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 24 with BundleContext

use of org.osgi.framework.BundleContext in project jackrabbit-oak by apache.

the class NodeStateSolrServersObserverService method activate.

@Activate
protected void activate(ComponentContext componentContext) throws Exception {
    boolean enabled = PropertiesUtil.toBoolean(componentContext.getProperties().get(ENABLED), false);
    if (enabled) {
        BundleContext bundleContext = componentContext.getBundleContext();
        Whiteboard whiteboard = new OsgiWhiteboard(bundleContext);
        executor = new WhiteboardExecutor();
        executor.start(whiteboard);
        backgroundObserver = new BackgroundObserver(nodeStateSolrServersObserver, executor, 5);
        regs.add(bundleContext.registerService(Observer.class.getName(), backgroundObserver, null));
    }
}
Also used : BackgroundObserver(org.apache.jackrabbit.oak.spi.commit.BackgroundObserver) OsgiWhiteboard(org.apache.jackrabbit.oak.osgi.OsgiWhiteboard) WhiteboardExecutor(org.apache.jackrabbit.oak.spi.whiteboard.WhiteboardExecutor) OsgiWhiteboard(org.apache.jackrabbit.oak.osgi.OsgiWhiteboard) Whiteboard(org.apache.jackrabbit.oak.spi.whiteboard.Whiteboard) BundleContext(org.osgi.framework.BundleContext) Activate(org.apache.felix.scr.annotations.Activate)

Example 25 with BundleContext

use of org.osgi.framework.BundleContext in project karaf by apache.

the class Activator method start.

@Override
public void start(final BundleContext context) throws Exception {
    registrations = new ArrayList<>();
    registrations.add(context.registerService(DumpProvider.class, new LogDumpProvider(context), null));
    featuresServiceTracker = new SingleServiceTracker<>(context, FeaturesService.class, (oldFs, newFs) -> {
        if (featuresProviderRegistration != null) {
            featuresProviderRegistration.unregister();
            featuresProviderRegistration = null;
        }
        if (newFs != null) {
            featuresProviderRegistration = context.registerService(DumpProvider.class, new FeaturesDumpProvider(newFs), null);
        }
    });
    featuresServiceTracker.open();
    final DiagnosticDumpMBeanImpl diagnostic = new DiagnosticDumpMBeanImpl();
    diagnostic.setBundleContext(context);
    Hashtable<String, Object> props = new Hashtable<>();
    props.put("jmx.objectname", "org.apache.karaf:type=diagnostic,name=" + System.getProperty("karaf.name"));
    mbeanRegistration = context.registerService(getInterfaceNames(diagnostic), diagnostic, props);
}
Also used : BundleActivator(org.osgi.framework.BundleActivator) DiagnosticDumpMBeanImpl(org.apache.karaf.diagnostic.management.internal.DiagnosticDumpMBeanImpl) DumpProvider(org.apache.karaf.diagnostic.core.DumpProvider) FeaturesService(org.apache.karaf.features.FeaturesService) BundleContext(org.osgi.framework.BundleContext) ArrayList(java.util.ArrayList) LogDumpProvider(org.apache.karaf.diagnostic.common.LogDumpProvider) FeaturesDumpProvider(org.apache.karaf.diagnostic.common.FeaturesDumpProvider) List(java.util.List) SingleServiceTracker(org.apache.karaf.util.tracker.SingleServiceTracker) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Hashtable(java.util.Hashtable) ServiceRegistration(org.osgi.framework.ServiceRegistration) LogDumpProvider(org.apache.karaf.diagnostic.common.LogDumpProvider) Hashtable(java.util.Hashtable) FeaturesService(org.apache.karaf.features.FeaturesService) DiagnosticDumpMBeanImpl(org.apache.karaf.diagnostic.management.internal.DiagnosticDumpMBeanImpl) DumpProvider(org.apache.karaf.diagnostic.core.DumpProvider) LogDumpProvider(org.apache.karaf.diagnostic.common.LogDumpProvider) FeaturesDumpProvider(org.apache.karaf.diagnostic.common.FeaturesDumpProvider) FeaturesDumpProvider(org.apache.karaf.diagnostic.common.FeaturesDumpProvider)

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