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;
});
}
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());
}
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);
}
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));
}
}
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);
}
Aggregations