use of org.osgi.service.cm.Configuration 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;
}
use of org.osgi.service.cm.Configuration in project karaf by apache.
the class FeatureConfigInstaller method installFeatureConfigs.
public void installFeatureConfigs(Feature feature) throws IOException, InvalidSyntaxException {
for (ConfigInfo config : feature.getConfigurations()) {
TypedProperties props = new TypedProperties();
// trim lines
String val = config.getValue();
if (config.isExternal()) {
props.load(new URL(val));
} else {
props.load(new StringReader(val));
}
String[] pid = parsePid(config.getName());
Configuration cfg = findExistingConfiguration(configAdmin, pid[0], pid[1]);
if (cfg == null) {
Dictionary<String, Object> cfgProps = convertToDict(props);
cfg = createConfiguration(configAdmin, pid[0], pid[1]);
String key = createConfigurationKey(pid[0], pid[1]);
cfgProps.put(CONFIG_KEY, key);
cfg.update(cfgProps);
try {
updateStorage(pid[0], pid[1], props, false);
} catch (Exception e) {
LOGGER.warn("Can't update cfg file", e);
}
} else if (config.isAppend()) {
boolean update = false;
Dictionary<String, Object> properties = cfg.getProperties();
for (String key : props.keySet()) {
if (properties.get(key) == null) {
properties.put(key, props.get(key));
update = true;
}
}
if (update) {
cfg.update(properties);
try {
updateStorage(pid[0], pid[1], props, true);
} catch (Exception e) {
LOGGER.warn("Can't update cfg file", e);
}
}
}
}
for (ConfigFileInfo configFile : feature.getConfigurationFiles()) {
installConfigurationFile(configFile.getLocation(), configFile.getFinalname(), configFile.isOverride());
}
}
use of org.osgi.service.cm.Configuration in project karaf by apache.
the class FeaturesServiceImpl method getMavenConfig.
private Dictionary<String, String> getMavenConfig() throws IOException {
Hashtable<String, String> props = new Hashtable<>();
if (configurationAdmin != null) {
Configuration config = configurationAdmin.getConfiguration("org.ops4j.pax.url.mvn", null);
if (config != null) {
Dictionary<String, Object> cfg = config.getProperties();
if (cfg != null) {
for (Enumeration<String> e = cfg.keys(); e.hasMoreElements(); ) {
String key = e.nextElement();
Object val = cfg.get(key);
if (key != null) {
props.put(key, val.toString());
}
}
}
}
}
return props;
}
use of org.osgi.service.cm.Configuration in project karaf by apache.
the class LogServiceImpl method setLevel.
public void setLevel(String logger, String level) {
// make sure both uppercase and lowercase levels are supported
level = level.toUpperCase();
// check if the level is valid
Level lvl = Level.valueOf(level);
// Default logger
if (logger == null) {
logger = LogServiceInternal.ROOT_LOGGER;
}
// Verify
if (lvl == Level.DEFAULT && LogServiceInternal.ROOT_LOGGER.equals(logger)) {
throw new IllegalStateException("Can not unset the ROOT logger");
}
// Get config
Configuration cfg = getConfiguration();
Dictionary<String, Object> props = cfg.getProperties();
// Update
getDelegate(props).setLevel(logger, level);
// Save
try {
cfg.update(props);
} catch (IOException e) {
throw new RuntimeException("Error writing log config to config admin", e);
}
}
use of org.osgi.service.cm.Configuration in project karaf by apache.
the class SetLogLevelTest method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
properties = new Hashtable<String, String>();
properties.put(ROOT_LOGGER, "info");
final Configuration configuration = EasyMock.createMock(Configuration.class);
EasyMock.expect(configuration.getProperties()).andReturn(properties);
configuration.update(properties);
ConfigurationAdmin configAdmin = EasyMock.createMock(ConfigurationAdmin.class);
EasyMock.expect(configAdmin.getConfiguration(LogServiceImpl.CONFIGURATION_PID, null)).andReturn(configuration);
logService = new LogServiceImpl(configAdmin, new LruList(100));
logMBean = new LogMBeanImpl(logService);
EasyMock.replay(configAdmin);
EasyMock.replay(configuration);
}
Aggregations