Search in sources :

Example 96 with Configuration

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

the class TestCatalog method testInPlaceDirectoryMonitor.

@Test
public void testInPlaceDirectoryMonitor() throws Exception {
    final String TMP_PREFIX = "tcdm_";
    Path tmpDir = Files.createTempDirectory(TMP_PREFIX);
    tmpDir.toFile().deleteOnExit();
    Path tmpFile = Files.createTempFile(tmpDir, TMP_PREFIX, "_tmp.xml");
    tmpFile.toFile().deleteOnExit();
    Files.copy(getFileContentAsStream("metacard5.xml"), tmpFile, StandardCopyOption.REPLACE_EXISTING);
    Map<String, Object> cdmProperties = new HashMap<>();
    cdmProperties.putAll(getServiceManager().getMetatypeDefaults("content-core-directorymonitor", "org.codice.ddf.catalog.content.monitor.ContentDirectoryMonitor"));
    cdmProperties.put("monitoredDirectoryPath", tmpDir.toString() + "/");
    cdmProperties.put("processingMechanism", ContentDirectoryMonitor.IN_PLACE);
    Configuration managedService = getServiceManager().createManagedService("org.codice.ddf.catalog.content.monitor.ContentDirectoryMonitor", cdmProperties);
    //assert that the file was ingested
    assertIngestedDirectoryMonitor("SysAdmin", 1);
    //edit the file
    Files.copy(getFileContentAsStream("metacard4.xml"), tmpFile, StandardCopyOption.REPLACE_EXISTING);
    //assert updated
    assertIngestedDirectoryMonitor("Space", 1);
    //rename the file and change
    Path newPath = Paths.get(tmpFile.toAbsolutePath().toString().replace("tmp.xml", "tmp2.xml"));
    Files.move(tmpFile, newPath, StandardCopyOption.REPLACE_EXISTING);
    tmpFile = newPath;
    Files.copy(getFileContentAsStream("metacard5.xml"), tmpFile, StandardCopyOption.REPLACE_EXISTING);
    //assert renamed
    assertIngestedDirectoryMonitor("SysAdmin", 1);
    //delete the file
    tmpFile.toFile().delete();
    //assert deleted
    assertIngestedDirectoryMonitor("SysAdmin", 0);
    getServiceManager().stopManagedService(managedService.getPid());
}
Also used : Path(java.nio.file.Path) XmlPath(com.jayway.restassured.path.xml.XmlPath) HasXPath.hasXPath(org.hamcrest.xml.HasXPath.hasXPath) Configuration(org.osgi.service.cm.Configuration) HashMap(java.util.HashMap) JSONObject(org.json.simple.JSONObject) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) SkipUnstableTest(org.codice.ddf.itests.common.annotations.SkipUnstableTest) Test(org.junit.Test) AbstractIntegrationTest(org.codice.ddf.itests.common.AbstractIntegrationTest)

Example 97 with Configuration

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

the class SourceConfigurationHandler method deleteRegistryConfigurations.

private void deleteRegistryConfigurations(Metacard metacard) throws IOException, InvalidSyntaxException {
    String registryId = RegistryUtility.getRegistryId(metacard);
    if (registryId == null) {
        return;
    }
    Configuration[] configurations = configurationAdmin.listConfigurations(String.format(CONFIGURATION_FILTER, RegistryConstants.CONFIGURATION_REGISTRY_ID_PROPERTY, registryId));
    if (configurations != null && configurations.length > 0) {
        LOGGER.debug("Deleting {} source configurations for registry-id {}", configurations.length, registryId);
        String sourceName = (String) configurations[0].getProperties().get(ID);
        configurations = configurationAdmin.listConfigurations(String.format(CONFIGURATION_FILTER, ID, sourceName));
        for (Configuration configuration : configurations) {
            configuration.delete();
        }
    }
}
Also used : RegistrySourceConfiguration(org.codice.ddf.registry.federationadmin.service.internal.RegistrySourceConfiguration) Configuration(org.osgi.service.cm.Configuration)

Example 98 with Configuration

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

the class SourceConfigurationHandlerTest method testConfigurationUpdateActivateConfigActivePriorityNotAvailable.

@Test
public void testConfigurationUpdateActivateConfigActivePriorityNotAvailable() throws Exception {
    ArgumentCaptor<Dictionary> captor = ArgumentCaptor.forClass(Dictionary.class);
    mcard.setAttribute(RegistryObjectMetacardType.REGISTRY_IDENTITY_NODE, null);
    when(configAdmin.listConfigurations(anyString())).thenReturn(new Configuration[] { config });
    Hashtable<String, Object> props = new Hashtable<>();
    props.put("id", "TestRegNode");
    props.put("origConfig", "origConfigValue");
    props.put(RegistryConstants.CONFIGURATION_REGISTRY_ID_PROPERTY, "urn:uuid:2014ca7f59ac46f495e32b4a67a51276");
    props.put("bindingType", "Some_Other_Binding_Type");
    Configuration newDisabledConfig = mock(Configuration.class);
    when(newDisabledConfig.getFactoryPid()).thenReturn("Some_Other_Source_disabled");
    when(configAdmin.createFactoryConfiguration("Some_Other_Source_disabled", null)).thenReturn(newDisabledConfig);
    doReturn(props).when(newDisabledConfig).getProperties();
    Configuration newActiveConfig = mock(Configuration.class);
    when(configAdmin.createFactoryConfiguration("Csw_Federated_Source", null)).thenReturn(newActiveConfig);
    doReturn("Csw_Federated_Source").when(newActiveConfig).getFactoryPid();
    when(config.getProperties()).thenReturn(props);
    when(config.getFactoryPid()).thenReturn("Some_Other_Source");
    List<String> priority = new ArrayList<>();
    priority.add("Top_Priority_Source");
    priority.add("CSW_2.0.2");
    List<String> bindings = new ArrayList<>();
    bindings.add("Top_Priority_Source=Some_Other_Source");
    bindings.add("CSW_2.0.2=Csw_Federated_Source");
    sch.setBindingTypeFactoryPid(bindings);
    sch.setSourceActivationPriorityOrder(priority);
    sch.setPreserveActiveConfigurations(false);
    sch.setActivateConfigurations(true);
    setupSerialExecutor();
    sch.handleEvent(updateEvent);
    verify(config, times(1)).delete();
    verify(configAdmin, times(1)).createFactoryConfiguration("Some_Other_Source_disabled", null);
    verify(configAdmin, times(1)).createFactoryConfiguration("Csw_Federated_Source", null);
    verify(newActiveConfig).update(captor.capture());
    List<Dictionary> values = captor.getAllValues();
    assertThat(values.size(), equalTo(1));
    Dictionary passedValues = values.get(0);
    assertThat(passedValues.get("attId"), equalTo("attValue"));
    assertCswProperties(passedValues);
}
Also used : Dictionary(java.util.Dictionary) Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 99 with Configuration

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

the class SourceConfigurationHandler method updateRegistryConfigurations.

/**
     * Finds all source configurations associated with the registry metacard creates/updates them
     * with the information in the metacards service bindings. This method will enable/disable
     * configurations if the right conditions are met but will never delete a configuration other
     * than for switching a configuration from enabled to disabled or vice-versa
     *
     * @param metacard       Registry metacard with new service binding info
     * @param activationHint Flag indicating if the created/updated configuration should be activated.
     *                       Configuration activation is not determined solely on this field but in
     *                       conjunction with {@see setActivateConfigurations} and {@see setPreserveActiveConfigurations}.
     * @throws IOException
     * @throws InvalidSyntaxException
     * @throws ParserException
     */
private synchronized void updateRegistryConfigurations(Metacard metacard, boolean activationHint) throws IOException, InvalidSyntaxException, ParserException {
    if (RegistryUtility.isIdentityNode(metacard)) {
        return;
    }
    boolean autoActivateConfigurations = activateConfigurations && (activationHint || !preserveActiveConfigurations);
    List<ServiceBindingType> bindingTypes = registryTypeHelper.getBindingTypes(metacardMarshaller.getRegistryPackageFromMetacard(metacard));
    String registryId = RegistryUtility.getRegistryId(metacard);
    String configId = getDeconflictedConfigId(metacard.getTitle(), registryId);
    Map<String, Configuration> fpidToConfigurationMap = getCurrentConfigurations(registryId);
    String bindingTypeToActivate = "";
    if (autoActivateConfigurations) {
        bindingTypeToActivate = getBindingTypeToActivate(bindingTypes);
        if (StringUtils.isNotEmpty(bindingTypeToActivate)) {
            String fPidToActivate = bindingTypeToFactoryPidMap.get(bindingTypeToActivate);
            activateDeactivateExistingConfiguration(fPidToActivate, fpidToConfigurationMap, bindingTypeToActivate);
        }
    }
    for (ServiceBindingType bindingType : bindingTypes) {
        Map<String, Object> slotMap = this.getServiceBindingProperties(bindingType);
        String factoryPidMask = (String) slotMap.get(BINDING_TYPE);
        if (factoryPidMask == null) {
            continue;
        }
        String factoryPid = bindingTypeToFactoryPidMap.get(factoryPidMask);
        if (StringUtils.isBlank(factoryPid)) {
            continue;
        }
        Configuration curConfig = findOrCreateConfig(factoryPid, fpidToConfigurationMap, factoryPidMask, (autoActivateConfigurations && factoryPidMask.equals(bindingTypeToActivate)));
        Hashtable<String, Object> serviceConfigurationProperties = new Hashtable<>();
        if (fpidToConfigurationMap.containsKey(curConfig.getFactoryPid().concat(getConfigStringProperty(curConfig, BINDING_TYPE)))) {
            serviceConfigurationProperties.putAll(getConfigurationsFromDictionary(curConfig.getProperties()));
        } else {
            serviceConfigurationProperties.putAll(getMetatypeDefaults(factoryPid));
        }
        serviceConfigurationProperties.putAll(slotMap);
        serviceConfigurationProperties.put(ID, configId);
        serviceConfigurationProperties.put(SHORTNAME, configId);
        serviceConfigurationProperties.put(RegistryConstants.CONFIGURATION_REGISTRY_ID_PROPERTY, registryId);
        curConfig.update(serviceConfigurationProperties);
        LOGGER.debug("Updating source configuration {} with registry-id {}", configId, registryId);
        fpidToConfigurationMap.remove(curConfig.getFactoryPid().concat(factoryPidMask));
    }
    //if a binding was removed the configuration could still exist, If the registry
    //entry gets renamed the non-binding configs will not get renamed appropriately so we
    //go through them here and make sure the name is set correctly.
    fpidToConfigurationMap.values().stream().forEach(config -> {
        try {
            Dictionary<String, Object> properties = config.getProperties();
            properties.put(ID, configId);
            properties.put(SHORTNAME, configId);
            config.update(properties);
        } catch (IOException e) {
            LOGGER.debug("Could not remove configuration for {}:{}", config.getProperties().get(ID), config.getFactoryPid());
        }
    });
}
Also used : ServiceBindingType(oasis.names.tc.ebxml_regrep.xsd.rim._3.ServiceBindingType) RegistrySourceConfiguration(org.codice.ddf.registry.federationadmin.service.internal.RegistrySourceConfiguration) Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) IOException(java.io.IOException)

Example 100 with Configuration

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

the class SourceConfigurationHandler method toggleConfiguration.

/**
     * Toggles a configuration between enabled and disabled.
     *
     * @param config The configuration to enable/disable
     * @return The new enabled/disabled configuration
     * @throws IOException
     */
private Configuration toggleConfiguration(Configuration config) throws IOException {
    String newFpid;
    if (config.getFactoryPid().contains(DISABLED_CONFIGURATION_SUFFIX)) {
        newFpid = config.getFactoryPid().replace(DISABLED_CONFIGURATION_SUFFIX, "");
    } else {
        newFpid = config.getFactoryPid().concat(DISABLED_CONFIGURATION_SUFFIX);
    }
    Dictionary<String, Object> properties = config.getProperties();
    config.delete();
    Configuration newConfig = configurationAdmin.createFactoryConfiguration(newFpid, null);
    newConfig.update(properties);
    return newConfig;
}
Also used : RegistrySourceConfiguration(org.codice.ddf.registry.federationadmin.service.internal.RegistrySourceConfiguration) Configuration(org.osgi.service.cm.Configuration)

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