Search in sources :

Example 31 with Configuration

use of org.osgi.service.cm.Configuration in project karaf by apache.

the class SecuredCommandConfigTransformer method configurationEvent.

@Override
public void configurationEvent(ConfigurationEvent event) {
    if (!event.getPid().startsWith(PROXY_COMMAND_ACL_PID_PREFIX))
        return;
    try {
        switch(event.getType()) {
            case ConfigurationEvent.CM_DELETED:
                deleteServiceGuardConfig(event.getPid(), event.getPid().substring(PROXY_COMMAND_ACL_PID_PREFIX.length()));
                break;
            case ConfigurationEvent.CM_UPDATED:
                Configuration config = configAdmin.getConfiguration(event.getPid(), null);
                generateServiceGuardConfig(config);
                refreshTheAffectedShellCommandBundle(event, config);
                break;
        }
    } catch (Exception e) {
        LOGGER.error("Problem processing Configuration Event {}", event, e);
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException)

Example 32 with Configuration

use of org.osgi.service.cm.Configuration in project karaf by apache.

the class ConfigRepositoryImpl method updateStorage.

protected void updateStorage(String pid, Dictionary<String, Object> props) throws IOException {
    if (storage != null) {
        Configuration cfg = configAdmin.getConfiguration(pid, null);
        // Initialize cfgFile with default location. Value gets overwritten when the existing configuration references a correct location.
        File cfgFile = new File(storage, pid + ".cfg");
        if (cfg != null) {
            Dictionary<String, Object> oldProps = cfg.getProperties();
            if (oldProps != null && oldProps.get(FILEINSTALL_FILE_NAME) != null) {
                try {
                    cfgFile = getCfgFileFromProperties(oldProps);
                    if (cfgFile == null) {
                        throw new IOException("The configuration value '" + oldProps.get(FILEINSTALL_FILE_NAME) + "' for '" + FILEINSTALL_FILE_NAME + "' does not represent a valid file location.");
                    }
                } catch (URISyntaxException | MalformedURLException e) {
                    throw new IOException(e);
                }
            }
        }
        LOGGER.trace("Update {}", cfgFile.getName());
        // update the cfg file
        Properties properties = new Properties(cfgFile);
        for (Enumeration<String> keys = props.keys(); keys.hasMoreElements(); ) {
            String key = keys.nextElement();
            if (!Constants.SERVICE_PID.equals(key) && !ConfigurationAdmin.SERVICE_FACTORYPID.equals(key) && !FILEINSTALL_FILE_NAME.equals(key)) {
                if (props.get(key) != null) {
                    properties.put(key, props.get(key).toString());
                }
            }
        }
        // remove "removed" properties from the cfg file
        ArrayList<String> propertiesToRemove = new ArrayList<>();
        for (String key : properties.keySet()) {
            if (props.get(key) == null && !Constants.SERVICE_PID.equals(key) && !ConfigurationAdmin.SERVICE_FACTORYPID.equals(key) && !FILEINSTALL_FILE_NAME.equals(key)) {
                propertiesToRemove.add(key);
            }
        }
        for (String key : propertiesToRemove) {
            properties.remove(key);
        }
        // save the cfg file
        storage.mkdirs();
        properties.save();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Configuration(org.osgi.service.cm.Configuration) ArrayList(java.util.ArrayList) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) URISyntaxException(java.net.URISyntaxException) Properties(org.apache.felix.utils.properties.Properties) File(java.io.File)

Example 33 with Configuration

use of org.osgi.service.cm.Configuration in project karaf by apache.

the class ConfigManagedServiceFactoryTest method createNewFactoryConfig.

@Test
public void createNewFactoryConfig() throws Exception {
    executeCommand("config:edit --factory myconfig2\n" + "config:property-set test1 data1\n" + "config:update", new RolePrincipal("manager"));
    Configuration config = configAdmin.listConfigurations("(service.factorypid=myconfig2)")[0];
    assertEquals("data1", config.getProperties().get("test1"));
}
Also used : Configuration(org.osgi.service.cm.Configuration) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal) Test(org.junit.Test)

Example 34 with Configuration

use of org.osgi.service.cm.Configuration in project karaf by apache.

the class JdbcServiceImpl method delete.

@Override
public void delete(String name) throws Exception {
    String filter = String.format("(%s=%s)", DataSourceFactory.JDBC_DATASOURCE_NAME, name);
    Configuration[] configs = configAdmin.listConfigurations(filter);
    for (Configuration config : configs) {
        config.delete();
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration)

Example 35 with Configuration

use of org.osgi.service.cm.Configuration in project karaf by apache.

the class JdbcServiceImpl method create.

@Override
public void create(String name, String driverName, String driverClass, String databaseName, String url, String user, String password, String databaseType) throws Exception {
    if (driverName == null && driverClass == null) {
        throw new IllegalStateException("No driverName or driverClass supplied");
    }
    if (datasources().contains(name)) {
        throw new IllegalArgumentException("There is already a DataSource with the name " + name);
    }
    Dictionary<String, String> properties = new Hashtable<>();
    properties.put(DataSourceFactory.JDBC_DATASOURCE_NAME, name);
    put(properties, DataSourceFactory.OSGI_JDBC_DRIVER_NAME, driverName);
    put(properties, DataSourceFactory.OSGI_JDBC_DRIVER_CLASS, driverClass);
    put(properties, DataSourceFactory.JDBC_DATABASE_NAME, databaseName);
    put(properties, DataSourceFactory.JDBC_URL, url);
    put(properties, DataSourceFactory.JDBC_USER, user);
    put(properties, DataSourceFactory.JDBC_PASSWORD, password);
    put(properties, "dataSourceType", databaseType);
    Configuration config = configAdmin.createFactoryConfiguration("org.ops4j.datasource", null);
    config.update(properties);
}
Also used : Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable)

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