Search in sources :

Example 6 with ConfigurationFileException

use of org.codice.ddf.configuration.status.ConfigurationFileException in project ddf by codice.

the class ConfigurationAdminMigration method getConfigurationFiles.

private Collection<ConfigurationFile> getConfigurationFiles() throws IOException {
    Collection<Path> files = listFiles();
    Collection<ConfigurationFile> configurationFiles = new ArrayList<>(files.size());
    for (Path file : files) {
        ConfigurationFile configFile;
        try {
            configFile = configurationFileFactory.createConfigurationFile(file);
            configurationFiles.add(configFile);
        } catch (ConfigurationFileException e) {
            LOGGER.info(e.getMessage(), e);
            moveConfigurationFile(file, failedDirectory);
        }
    }
    return configurationFiles;
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) ConfigurationFileException(org.codice.ddf.configuration.status.ConfigurationFileException)

Example 7 with ConfigurationFileException

use of org.codice.ddf.configuration.status.ConfigurationFileException in project ddf by codice.

the class ConfigurationAdminMigration method notify.

@Override
public void notify(Path file) {
    Path fileInFailedDirectory = failedDirectory.resolve(file.getFileName());
    boolean result = deleteFileFromFailedDirectory(fileInFailedDirectory);
    LOGGER.debug("Deleted file [{}]: {}", fileInFailedDirectory.toString(), result);
    try {
        ConfigurationFile configFile = configurationFileFactory.createConfigurationFile(file);
        configFile.createConfig();
        moveConfigurationFile(file, processedDirectory);
    } catch (ConfigurationFileException | RuntimeException e) {
        moveConfigurationFile(file, failedDirectory);
    }
}
Also used : Path(java.nio.file.Path) ConfigurationFileException(org.codice.ddf.configuration.status.ConfigurationFileException)

Example 8 with ConfigurationFileException

use of org.codice.ddf.configuration.status.ConfigurationFileException in project ddf by codice.

the class ConfigurationAdminMigration method export.

public void export(@NotNull Path exportDirectory) throws MigrationException, IOException {
    notNull(exportDirectory, "exportDirectory cannot be null");
    Path etcDirectory = createEtcDirectory(exportDirectory);
    try {
        Configuration[] configurations = configurationAdmin.listConfigurations(FILTER);
        if (configurations != null) {
            for (Configuration configuration : configurations) {
                Path exportedFilePath = etcDirectory.resolve(configuration.getPid() + configurationFileExtension);
                try {
                    configurationFileFactory.createConfigurationFile(configuration.getProperties()).exportConfig(exportedFilePath.toString());
                } catch (ConfigurationFileException e) {
                    LOGGER.info("Could not create configuration file {} for configuration {}.", exportedFilePath, configuration.getPid());
                    throw new ExportMigrationException(e);
                } catch (IOException e) {
                    LOGGER.info("Could not export configuration {} to {}.", configuration.getPid(), exportedFilePath);
                    throw new ExportMigrationException(e);
                }
            }
        }
    } catch (InvalidSyntaxException e) {
        LOGGER.info("Invalid filter string {}", FILTER, e);
        throw new UnexpectedMigrationException("Export failed", e);
    } catch (IOException e) {
        LOGGER.info("There was an issue retrieving configurations from ConfigurationAdmin: {}", e.getMessage());
        throw new UnexpectedMigrationException("Export failed", e);
    }
}
Also used : Path(java.nio.file.Path) ExportMigrationException(org.codice.ddf.migration.ExportMigrationException) Configuration(org.osgi.service.cm.Configuration) ConfigurationFileException(org.codice.ddf.configuration.status.ConfigurationFileException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) UnexpectedMigrationException(org.codice.ddf.migration.UnexpectedMigrationException)

Example 9 with ConfigurationFileException

use of org.codice.ddf.configuration.status.ConfigurationFileException in project ddf by codice.

the class FelixPersistenceStrategy method read.

@Override
@SuppressWarnings("unchecked")
public Dictionary<String, Object> read(InputStream inputStream) throws ConfigurationFileException, IOException {
    notNull(inputStream, "InputStream cannot be null");
    final StringBuilder filteredOutput = new StringBuilder();
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
    PropertyConverter propertyConverter = createPropertyConverter(filteredOutput);
    reader.lines().forEach(propertyConverter);
    LOGGER.debug("Calling ConfigurationHandler with {}", filteredOutput.toString());
    Dictionary properties;
    try {
        properties = ConfigurationHandler.read(new ByteArrayInputStream(filteredOutput.toString().getBytes(StandardCharsets.UTF_8)));
    } catch (RuntimeException e) {
        LOGGER.info("ConfigurationHandler failed to read configuration from file", e);
        throw new ConfigurationFileException("Failed to read configuration from file", e);
    }
    checkForInvalidProperties(propertyConverter.getPropertyNames(), properties);
    return properties;
}
Also used : Dictionary(java.util.Dictionary) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) ConfigurationFileException(org.codice.ddf.configuration.status.ConfigurationFileException)

Example 10 with ConfigurationFileException

use of org.codice.ddf.configuration.status.ConfigurationFileException in project ddf by codice.

the class ManagedServiceConfigurationFile method createConfig.

@Override
public void createConfig() throws ConfigurationFileException {
    String servicePid = getServicePid();
    try {
        Configuration configuration = configAdmin.getConfiguration(servicePid, null);
        configuration.update(properties);
    } catch (IOException e) {
        String message = String.format("Unable to get or update Configuration for pid [%s].", servicePid);
        LOGGER.info(message, e);
        throw new ConfigurationFileException(message, e);
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) ConfigurationFileException(org.codice.ddf.configuration.status.ConfigurationFileException) IOException(java.io.IOException)

Aggregations

ConfigurationFileException (org.codice.ddf.configuration.status.ConfigurationFileException)16 Test (org.junit.Test)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)9 IOException (java.io.IOException)4 Path (java.nio.file.Path)4 Configuration (org.osgi.service.cm.Configuration)3 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStreamReader (java.io.InputStreamReader)1 StandardCopyOption (java.nio.file.StandardCopyOption)1 ArrayList (java.util.ArrayList)1 Dictionary (java.util.Dictionary)1 PersistenceStrategy (org.codice.ddf.configuration.persistence.PersistenceStrategy)1 ExportMigrationException (org.codice.ddf.migration.ExportMigrationException)1 UnexpectedMigrationException (org.codice.ddf.migration.UnexpectedMigrationException)1 Matchers.anyObject (org.mockito.Matchers.anyObject)1 Matchers.anyString (org.mockito.Matchers.anyString)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1