use of org.osgi.service.cm.ConfigurationAdmin in project karaf by apache.
the class Activator method doStart.
protected void doStart() throws Exception {
ConfigurationAdmin configurationAdmin = getTrackedService(ConfigurationAdmin.class);
if (configurationAdmin == null) {
return;
}
ConfigRepository configRepository = new ConfigRepositoryImpl(configurationAdmin, new File(System.getProperty("karaf.etc")));
register(ConfigRepository.class, configRepository);
ConfigMBeanImpl configMBean = new ConfigMBeanImpl();
configMBean.setConfigRepo(configRepository);
registerMBean(configMBean, "type=config");
}
use of org.osgi.service.cm.ConfigurationAdmin in project karaf by apache.
the class GuardingEventHookTest method mockConfigAdminBundleContext.
@SuppressWarnings({ "rawtypes", "unchecked" })
private BundleContext mockConfigAdminBundleContext(BundleContext frameworkContext, 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 sb = EasyMock.createMock(Bundle.class);
EasyMock.expect(sb.getBundleId()).andReturn(0L).anyTimes();
EasyMock.expect(sb.getBundleContext()).andReturn(frameworkContext).anyTimes();
EasyMock.replay(sb);
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.getBundle(0L)).andReturn(sb).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.service.cm.ConfigurationAdmin in project karaf by apache.
the class GuardProxyCatalogTest method testProxyCreationThread.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testProxyCreationThread() throws Exception {
ProxyManager proxyManager = getProxyManager();
ConfigurationAdmin ca = EasyMock.createMock(ConfigurationAdmin.class);
EasyMock.expect(ca.listConfigurations(EasyMock.anyObject(String.class))).andReturn(null).anyTimes();
EasyMock.replay(ca);
ServiceReference pmSref = EasyMock.createMock(ServiceReference.class);
EasyMock.replay(pmSref);
ServiceReference pmSref2 = EasyMock.createMock(ServiceReference.class);
EasyMock.replay(pmSref2);
ServiceReference cmSref = EasyMock.createMock(ServiceReference.class);
EasyMock.replay(cmSref);
Bundle b = EasyMock.createMock(Bundle.class);
EasyMock.expect(b.getBundleId()).andReturn(23992734L).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();
final ServiceListener[] pmListenerHolder = new ServiceListener[1];
String pmFilter = "(&(objectClass=" + ProxyManager.class.getName() + ")" + "(!(" + GuardProxyCatalog.PROXY_SERVICE_KEY + "=*)))";
bc.addServiceListener(EasyMock.isA(ServiceListener.class), EasyMock.eq(pmFilter));
EasyMock.expectLastCall().andAnswer(() -> {
pmListenerHolder[0] = (ServiceListener) EasyMock.getCurrentArguments()[0];
return null;
}).anyTimes();
EasyMock.expect(bc.getServiceReferences(EasyMock.anyObject(String.class), EasyMock.contains(ConfigurationAdmin.class.getName()))).andReturn(new ServiceReference[] { cmSref }).anyTimes();
EasyMock.expect(bc.getService(pmSref)).andReturn(proxyManager).anyTimes();
EasyMock.expect(bc.getService(pmSref2)).andReturn(proxyManager).anyTimes();
EasyMock.expect(bc.getService(cmSref)).andReturn(ca).anyTimes();
EasyMock.replay(bc);
// This should put a ServiceListener in the pmListenerHolder, the ServiceTracker does that
GuardProxyCatalog gpc = new GuardProxyCatalog(bc);
// The service being proxied has these properties
final Hashtable<String, Object> serviceProps = new Hashtable<>();
serviceProps.put(Constants.OBJECTCLASS, new String[] { TestServiceAPI.class.getName() });
serviceProps.put(Constants.SERVICE_ID, 162L);
final Map<ServiceReference<?>, Object> serviceMap = new HashMap<>();
// The mock bundle context for the bundle providing the service is set up here
BundleContext providerBC = EasyMock.createMock(BundleContext.class);
// These are the expected service properties of the proxy registration. Note the proxy marker...
final Hashtable<String, Object> expectedProxyProps = new Hashtable<>(serviceProps);
expectedProxyProps.put(GuardProxyCatalog.PROXY_SERVICE_KEY, Boolean.TRUE);
EasyMock.expect(providerBC.registerService(EasyMock.isA(String[].class), EasyMock.anyObject(), EasyMock.isA(Dictionary.class))).andAnswer((IAnswer) () -> {
Dictionary<String, Object> props = (Dictionary<String, Object>) EasyMock.getCurrentArguments()[2];
ServiceRegistration reg = EasyMock.createMock(ServiceRegistration.class);
ServiceReference sr = mockServiceReference(props);
EasyMock.expect(reg.getReference()).andReturn(sr).anyTimes();
reg.unregister();
EasyMock.expectLastCall().once();
EasyMock.replay(reg);
serviceMap.put(sr, EasyMock.getCurrentArguments()[1]);
return reg;
}).once();
EasyMock.expect(providerBC.getService(EasyMock.isA(ServiceReference.class))).andAnswer(() -> serviceMap.get(EasyMock.getCurrentArguments()[0])).anyTimes();
EasyMock.replay(providerBC);
// In some cases the proxy-creating code is looking for a classloader (e.g. when run through
// a coverage tool such as EclEmma). This will satisfy that.
BundleWiring bw = EasyMock.createMock(BundleWiring.class);
EasyMock.expect(bw.getClassLoader()).andReturn(getClass().getClassLoader()).anyTimes();
EasyMock.replay(bw);
// The mock bundle that provides the original service (and also the proxy is registered with this)
Bundle providerBundle = EasyMock.createNiceMock(Bundle.class);
EasyMock.expect(providerBundle.getBundleContext()).andReturn(providerBC).anyTimes();
EasyMock.expect(providerBundle.adapt(BundleWiring.class)).andReturn(bw).anyTimes();
EasyMock.replay(providerBundle);
ServiceReference sr = mockServiceReference(providerBundle, serviceProps);
assertEquals("Precondition", 0, gpc.proxyMap.size());
assertEquals("Precondition", 0, gpc.createProxyQueue.size());
// Create the proxy for the service
gpc.proxyIfNotAlreadyProxied(sr);
assertEquals(1, gpc.proxyMap.size());
assertEquals(1, gpc.createProxyQueue.size());
// The actual proxy creation is done asynchronously.
GuardProxyCatalog.ServiceRegistrationHolder holder = gpc.proxyMap.get(162L);
assertNull("The registration shouldn't have happened yet", holder.registration);
assertEquals(1, gpc.createProxyQueue.size());
Thread[] tarray = new Thread[Thread.activeCount()];
Thread.enumerate(tarray);
for (Thread t : tarray) {
if (t != null) {
assertTrue(!GuardProxyCatalog.PROXY_CREATOR_THREAD_NAME.equals(t.getName()));
}
}
// make the proxy manager appear
pmListenerHolder[0].serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, pmSref));
// give the system some time to send the events...
Thread.sleep(400);
Thread ourThread = null;
Thread[] tarray2 = new Thread[Thread.activeCount()];
Thread.enumerate(tarray2);
for (Thread t : tarray2) {
if (t != null) {
if (t.getName().equals(GuardProxyCatalog.PROXY_CREATOR_THREAD_NAME)) {
ourThread = t;
}
}
}
assertNotNull(ourThread);
assertTrue(ourThread.isDaemon());
assertTrue(ourThread.isAlive());
assertNotNull(holder.registration);
assertEquals(0, gpc.createProxyQueue.size());
int numProxyThreads = 0;
pmListenerHolder[0].serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, pmSref2));
// give the system some time to send the events...
Thread.sleep(300);
Thread[] tarray3 = new Thread[Thread.activeCount()];
Thread.enumerate(tarray3);
for (Thread t : tarray3) {
if (t != null) {
if (t.getName().equals(GuardProxyCatalog.PROXY_CREATOR_THREAD_NAME)) {
numProxyThreads++;
}
}
}
assertEquals("Maximum 1 proxy thread, even if there is more than 1 proxy service", 1, numProxyThreads);
// Clean up thread
pmListenerHolder[0].serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, pmSref));
// Give the system some time to stop the threads...
Thread.sleep(300);
Thread[] tarray4 = new Thread[Thread.activeCount()];
Thread.enumerate(tarray4);
for (Thread t : tarray4) {
if (t != null) {
assertTrue(!GuardProxyCatalog.PROXY_CREATOR_THREAD_NAME.equals(t.getName()));
}
}
}
use of org.osgi.service.cm.ConfigurationAdmin in project karaf by apache.
the class GuardProxyCatalogTest 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;
}
if (configurations.length == 0) {
configurations = null;
}
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.service.cm.ConfigurationAdmin in project karaf by apache.
the class GuardProxyCatalog method getServiceGuardConfigs.
// Ensures that it never returns null
private Configuration[] getServiceGuardConfigs() throws IOException, InvalidSyntaxException {
ConfigurationAdmin ca = null;
try {
ca = configAdminTracker.waitForService(5000);
} catch (InterruptedException e) {
}
if (ca == null) {
throw new IllegalStateException("Role based access for services requires the OSGi Configuration Admin Service to be present");
}
Configuration[] configs = ca.listConfigurations("(&(" + Constants.SERVICE_PID + "=" + SERVICE_ACL_PREFIX + "*)(" + SERVICE_GUARD_KEY + "=*))");
if (configs == null) {
return new Configuration[] {};
}
return configs;
}
Aggregations