Search in sources :

Example 76 with Configuration

use of org.osgi.service.cm.Configuration in project ddf by codice.

the class ConfigurationAdmin method getConfigurations.

/**
     * @see ConfigurationAdminMBean#getConfigurations(java.lang.String)
     */
public String[][] getConfigurations(String filter) throws IOException {
    if (filter == null || filter.length() < 1) {
        throw new IOException("Argument filter cannot be null or empty");
    }
    List<String[]> result = new ArrayList<>();
    Configuration[] configurations;
    try {
        configurations = configurationAdmin.listConfigurations(filter);
    } catch (InvalidSyntaxException e) {
        throw new IOException("Invalid filter [" + filter + "] : " + e);
    }
    if (configurations != null) {
        for (Configuration config : configurations) {
            if (isPermittedToViewService(config.getPid())) {
                result.add(new String[] { config.getPid(), config.getBundleLocation() });
            }
        }
    }
    return result.toArray(new String[result.size()][]);
}
Also used : Configuration(org.osgi.service.cm.Configuration) ArrayList(java.util.ArrayList) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException)

Example 77 with Configuration

use of org.osgi.service.cm.Configuration in project ddf by codice.

the class ConfigurationAdmin method updateForLocation.

/**
     * @see ConfigurationAdminMBean#updateForLocation(java.lang.String, java.lang.String, java.util.Map)
     */
public void updateForLocation(final String pid, String location, Map<String, Object> configurationTable) throws IOException {
    if (pid == null || pid.length() < 1) {
        throw loggedException("Argument pid cannot be null or empty");
    }
    if (configurationTable == null) {
        throw loggedException("Argument configurationTable cannot be null");
    }
    Configuration config = configurationAdmin.getConfiguration(pid, location);
    if (isPermittedToViewService(config.getPid())) {
        final List<Map<String, Object>> metatype = findMetatypeForConfig(config);
        List<Map.Entry<String, Object>> configEntries = new ArrayList<>();
        CollectionUtils.addAll(configEntries, configurationTable.entrySet().iterator());
        if (metatype == null) {
            throw loggedException("Could not find metatype for " + pid);
        }
        // now we have to filter each property based on its cardinality
        CollectionUtils.transform(configEntries, new CardinalityTransformer(metatype, pid));
        Dictionary<String, Object> newConfigProperties = new Hashtable<>();
        // "password", do not update the password.
        for (Map.Entry<String, Object> configEntry : configEntries) {
            String configEntryKey = configEntry.getKey();
            Object configEntryValue = configEntry.getValue();
            if (configEntryValue.equals("password")) {
                for (Map<String, Object> metatypeProperties : metatype) {
                    if (metatypeProperties.get("id").equals(configEntry.getKey()) && AttributeDefinition.PASSWORD == (Integer) metatypeProperties.get("type")) {
                        Dictionary<String, Object> configProperties = config.getProperties();
                        if (configProperties != null) {
                            configEntryValue = configProperties.get(configEntryKey);
                        }
                        break;
                    }
                }
            }
            newConfigProperties.put(configEntryKey, configEntryValue);
        }
        config.update(newConfigProperties);
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) HashMap(java.util.HashMap) Map(java.util.Map)

Example 78 with Configuration

use of org.osgi.service.cm.Configuration in project ddf by codice.

the class ConfigurationAdmin method createFactoryConfigurationForLocation.

/**
     * @see ConfigurationAdminMBean#createFactoryConfigurationForLocation(java.lang.String, java.lang.String)
     */
public String createFactoryConfigurationForLocation(String factoryPid, String location) throws IOException {
    if (StringUtils.isBlank(factoryPid)) {
        throw new IOException("Argument factoryPid cannot be null or empty");
    }
    if (isPermittedToViewService(factoryPid)) {
        Configuration config = configurationAdmin.createFactoryConfiguration(factoryPid);
        config.setBundleLocation(location);
        return config.getPid();
    }
    return null;
}
Also used : Configuration(org.osgi.service.cm.Configuration) IOException(java.io.IOException)

Example 79 with Configuration

use of org.osgi.service.cm.Configuration in project ddf by codice.

the class ConfigurationAdminExtTest method testGetConfigurationIOException.

/**
     * Tests the {@link ConfigurationAdminExt#getConfiguration(String)} method
     * for the case where configurationAdmin.listConfigurations(..) throws an IOException
     *
     * @throws Exception
     */
@Test
public void testGetConfigurationIOException() throws Exception {
    doThrow(new IOException()).when(testConfigAdmin).listConfigurations(anyString());
    Configuration result = configurationAdminExt.getConfiguration(TEST_PID);
    assertThat("Should handle the exception gracefully and return null.", result, is(nullValue()));
}
Also used : Configuration(org.osgi.service.cm.Configuration) IOException(java.io.IOException) Test(org.junit.Test)

Example 80 with Configuration

use of org.osgi.service.cm.Configuration in project ddf by codice.

the class ConfigurationAdmin method deleteForLocation.

/**
     * @see ConfigurationAdminMBean#deleteForLocation(java.lang.String, java.lang.String)
     */
public void deleteForLocation(String pid, String location) throws IOException {
    if (pid == null || pid.length() < 1) {
        throw new IOException("Argument pid cannot be null or empty");
    }
    if (isPermittedToViewService(pid)) {
        Configuration config = configurationAdmin.getConfiguration(pid, location);
        config.delete();
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) IOException(java.io.IOException)

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