Search in sources :

Example 36 with Configuration

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;
}
Also used : ServiceListener(org.osgi.framework.ServiceListener) Configuration(org.osgi.service.cm.Configuration) Bundle(org.osgi.framework.Bundle) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) ServiceReference(org.osgi.framework.ServiceReference) BundleContext(org.osgi.framework.BundleContext)

Example 37 with Configuration

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());
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) StringReader(java.io.StringReader) ConfigFileInfo(org.apache.karaf.features.ConfigFileInfo) ConfigInfo(org.apache.karaf.features.ConfigInfo) TypedProperties(org.apache.felix.utils.properties.TypedProperties) URL(java.net.URL) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 38 with Configuration

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;
}
Also used : Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable)

Example 39 with Configuration

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);
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) Level(org.apache.karaf.log.core.Level) IOException(java.io.IOException)

Example 40 with Configuration

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);
}
Also used : Configuration(org.osgi.service.cm.Configuration) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Aggregations

Configuration (org.osgi.service.cm.Configuration)226 Test (org.junit.Test)85 Hashtable (java.util.Hashtable)75 IOException (java.io.IOException)55 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)49 Dictionary (java.util.Dictionary)36 ArrayList (java.util.ArrayList)19 HashMap (java.util.HashMap)19 ServiceReference (org.osgi.framework.ServiceReference)19 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)18 Matchers.anyString (org.mockito.Matchers.anyString)16 BundleContext (org.osgi.framework.BundleContext)15 RegistrySourceConfiguration (org.codice.ddf.registry.federationadmin.service.internal.RegistrySourceConfiguration)11 Map (java.util.Map)10 Bundle (org.osgi.framework.Bundle)10 File (java.io.File)9 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)9 Mockito.anyString (org.mockito.Mockito.anyString)9 AbstractIntegrationTest (org.codice.ddf.itests.common.AbstractIntegrationTest)8 SkipUnstableTest (org.codice.ddf.itests.common.annotations.SkipUnstableTest)7