use of org.osgi.service.cm.ConfigurationAdmin in project karaf by apache.
the class KarafMBeanServerGuardTest method testCanInvokeMBean2.
public void testCanInvokeMBean2() throws Exception {
final ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
MBeanParameterInfo[] sig = new MBeanParameterInfo[] { new MBeanParameterInfo("arg1", "java.lang.String", "") };
MBeanOperationInfo op = new MBeanOperationInfo("doit", "", sig, "int", MBeanOperationInfo.INFO);
MBeanInfo info = EasyMock.createMock(MBeanInfo.class);
EasyMock.expect(info.getOperations()).andReturn(new MBeanOperationInfo[] { op }).anyTimes();
EasyMock.expect(info.getAttributes()).andReturn(new MBeanAttributeInfo[] {}).anyTimes();
EasyMock.replay(info);
final MBeanServer mbs = EasyMock.createMock(MBeanServer.class);
EasyMock.expect(mbs.getMBeanInfo(on)).andReturn(info).anyTimes();
EasyMock.replay(mbs);
Dictionary<String, Object> configuration = new Hashtable<>();
configuration.put("doit(java.lang.String)[/11/]", "admin");
configuration.put("doit(java.lang.String)", "admin");
configuration.put("doit(java.lang.String,java.lang.String)", "admin");
configuration.put("doit(int)[\"12\"]", "admin");
configuration.put("doit", "admin");
configuration.put("do*", "admin");
ConfigurationAdmin ca = getMockConfigAdmin(configuration);
final KarafMBeanServerGuard guard = new KarafMBeanServerGuard();
guard.setConfigAdmin(ca);
Subject subject = loginWithTestRoles("viewer");
Subject.doAs(subject, (PrivilegedAction<Void>) () -> {
try {
assertFalse(guard.canInvoke(mbs, on));
return null;
} catch (Throwable th) {
throw new RuntimeException(th);
}
});
}
use of org.osgi.service.cm.ConfigurationAdmin in project karaf by apache.
the class KarafMBeanServerGuardTest method testCanInvokeMBeanGetter2.
public void testCanInvokeMBeanGetter2() throws Exception {
final ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
MBeanAttributeInfo attr = new MBeanAttributeInfo("a1", "boolean", "", true, false, false);
MBeanInfo info = EasyMock.createMock(MBeanInfo.class);
EasyMock.expect(info.getOperations()).andReturn(new MBeanOperationInfo[] {}).anyTimes();
EasyMock.expect(info.getAttributes()).andReturn(new MBeanAttributeInfo[] { attr }).anyTimes();
EasyMock.replay(info);
final MBeanServer mbs = EasyMock.createMock(MBeanServer.class);
EasyMock.expect(mbs.getMBeanInfo(on)).andReturn(info).anyTimes();
EasyMock.replay(mbs);
Dictionary<String, Object> configuration = new Hashtable<>();
configuration.put("get*", "admin");
configuration.put("is*", "viewer");
configuration.put("*", "admin");
ConfigurationAdmin ca = getMockConfigAdmin(configuration);
final KarafMBeanServerGuard guard = new KarafMBeanServerGuard();
guard.setConfigAdmin(ca);
Subject subject = loginWithTestRoles("viewer");
Subject.doAs(subject, (PrivilegedAction<Void>) () -> {
try {
assertFalse(guard.canInvoke(mbs, on));
return null;
} catch (Throwable th) {
throw new RuntimeException(th);
}
});
}
use of org.osgi.service.cm.ConfigurationAdmin in project karaf by apache.
the class KarafMBeanServerGuardTest method testCanInvokeMBeanSetter2.
public void testCanInvokeMBeanSetter2() throws Exception {
final ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
MBeanAttributeInfo attr = new MBeanAttributeInfo("A2", "java.lang.String", "", true, true, false);
MBeanInfo info = EasyMock.createMock(MBeanInfo.class);
EasyMock.expect(info.getOperations()).andReturn(new MBeanOperationInfo[] {}).anyTimes();
EasyMock.expect(info.getAttributes()).andReturn(new MBeanAttributeInfo[] { attr }).anyTimes();
EasyMock.replay(info);
final MBeanServer mbs = EasyMock.createMock(MBeanServer.class);
EasyMock.expect(mbs.getMBeanInfo(on)).andReturn(info).anyTimes();
EasyMock.replay(mbs);
Dictionary<String, Object> configuration = new Hashtable<>();
configuration.put("get*", "admin");
configuration.put("setA2", "admin");
configuration.put("*", "admin");
ConfigurationAdmin ca = getMockConfigAdmin(configuration);
final KarafMBeanServerGuard guard = new KarafMBeanServerGuard();
guard.setConfigAdmin(ca);
Subject subject = loginWithTestRoles("viewer");
Subject.doAs(subject, (PrivilegedAction<Void>) () -> {
try {
assertFalse(guard.canInvoke(mbs, on));
return null;
} catch (Throwable th) {
throw new RuntimeException(th);
}
});
}
use of org.osgi.service.cm.ConfigurationAdmin in project sling by apache.
the class SlingLogPanel method configureLogger.
/**
* Configures the logger with the given pid. If the pid is empty a new logger configuration is created.
*
* @param pid configuration pid of the logger
* @param logLevel the log level to set
* @param loggers list of logger categories to set
* @param logFile log file (relative path is ok)
* @param additive logger additivity
* @throws IOException when an existing configuration couldn't be updated or a configuration couldn't be created.
* @throws ConfigurationException when mandatory parameters where not specified
*/
private void configureLogger(final String pid, final String logLevel, final String[] loggers, final String logFile, boolean additive) throws IOException, ConfigurationException {
// try to get the configadmin service reference
ServiceReference sr = this.bundleContext.getServiceReference(ConfigurationAdmin.class.getName());
if (sr != null) {
try {
if (logLevel == null) {
throw new ConfigurationException(LogConfigManager.LOG_LEVEL, "Log level has to be specified.");
}
if (loggers == null) {
throw new ConfigurationException(LogConfigManager.LOG_LOGGERS, "Logger categories have to be specified.");
}
if (logFile == null) {
throw new ConfigurationException(LogConfigManager.LOG_FILE, "LogFile name has to be specified.");
}
// try to get the configadmin
final ConfigurationAdmin configAdmin = (ConfigurationAdmin) this.bundleContext.getService(sr);
if (configAdmin != null) {
Configuration config;
if (pid == null || pid.length() == 0) {
config = configAdmin.createFactoryConfiguration(LogConfigManager.FACTORY_PID_CONFIGS);
} else {
config = configAdmin.getConfiguration(pid);
}
if (config != null) {
Dictionary<String, Object> dict = new Hashtable<String, Object>();
dict.put(LogConfigManager.LOG_LEVEL, logLevel.toLowerCase());
dict.put(LogConfigManager.LOG_LOGGERS, loggers);
dict.put(LogConfigManager.LOG_FILE, logFile);
if (additive) {
dict.put(LogConfigManager.LOG_ADDITIV, "true");
} else {
dict.put(LogConfigManager.LOG_ADDITIV, "false");
}
config.update(dict);
}
}
} finally {
// release the configadmin reference
this.bundleContext.ungetService(sr);
}
}
}
use of org.osgi.service.cm.ConfigurationAdmin in project aries by apache.
the class DSLTest method testConfiguration.
@Test
public void testConfiguration() throws IOException, InterruptedException {
ServiceReference<ConfigurationAdmin> serviceReference = bundleContext.getServiceReference(ConfigurationAdmin.class);
ConfigurationAdmin configurationAdmin = bundleContext.getService(serviceReference);
AtomicReference<Dictionary<?, ?>> atomicReference = new AtomicReference<>(null);
Configuration configuration = null;
CountDownLatch countDownLatch = new CountDownLatch(1);
try (OSGiResult<Dictionary<String, ?>> result = configuration("test.configuration").run(bundleContext, x -> {
atomicReference.set(x);
countDownLatch.countDown();
})) {
assertNull(atomicReference.get());
configuration = configurationAdmin.getConfiguration("test.configuration");
configuration.update(new Hashtable<>());
countDownLatch.await(10, TimeUnit.SECONDS);
assertNotNull(atomicReference.get());
} finally {
bundleContext.ungetService(serviceReference);
if (configuration != null) {
configuration.delete();
}
}
}
Aggregations