Search in sources :

Example 16 with PropertiesConfiguration

use of org.apache.commons.configuration2.PropertiesConfiguration in project scheduling by ow2-proactive.

the class NodeCommandLineProperties method loadConfig.

/**
 * loads NodeSource configuration.
 *
 * @return NodeSource configuration
 */
public static Configuration loadConfig() throws ConfigurationException {
    Configuration config;
    File propertiesFile = new File(NodeCommandLineProperties.class.getClassLoader().getResource(PROPERTIES_FILE).getFile());
    PropertiesBuilderParameters propertyParameters = new Parameters().properties();
    propertyParameters.setFile(propertiesFile);
    propertyParameters.setThrowExceptionOnMissing(true);
    propertyParameters.setListDelimiterHandler(DELIMITER);
    FileBasedConfigurationBuilder<PropertiesConfiguration> builder = new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class);
    builder.configure(propertyParameters);
    config = builder.getConfiguration();
    LOGGER.debug("NodeSources configuration loaded");
    return config;
}
Also used : Parameters(org.apache.commons.configuration2.builder.fluent.Parameters) PropertiesBuilderParameters(org.apache.commons.configuration2.builder.fluent.PropertiesBuilderParameters) FileBasedConfigurationBuilder(org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder) PropertiesConfiguration(org.apache.commons.configuration2.PropertiesConfiguration) Configuration(org.apache.commons.configuration2.Configuration) PropertiesBuilderParameters(org.apache.commons.configuration2.builder.fluent.PropertiesBuilderParameters) File(java.io.File) PropertiesConfiguration(org.apache.commons.configuration2.PropertiesConfiguration)

Example 17 with PropertiesConfiguration

use of org.apache.commons.configuration2.PropertiesConfiguration in project data-prep by Talend.

the class PropertiesEncryption method modifyAndSave.

/**
 * Applies the specified function to the specified set of parameters contained in the input file.
 *
 * @param input The specified name of file to encrypt
 * @param mustBeModified the specified set of parameters
 * @param function the specified function to apply to the set of specified parameters
 */
private void modifyAndSave(String input, Set<String> mustBeModified, Function<String, String> function) {
    Path inputFilePath = Paths.get(input);
    if (Files.exists(inputFilePath) && Files.isRegularFile(inputFilePath) && Files.isReadable(inputFilePath)) {
        try {
            Parameters params = new Parameters();
            // 
            FileBasedConfigurationBuilder<PropertiesConfiguration> builder = // 
            new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class).configure(params.fileBased().setFile(// 
            inputFilePath.toFile()));
            PropertiesConfiguration config = builder.getConfiguration();
            mustBeModified.stream().filter(config::containsKey).forEach(key -> config.setProperty(key, function.apply(config.getString(key))));
            builder.save();
        } catch (ConfigurationException e) {
            LOGGER.error("unable to read {} {}", input, e);
        }
    } else {
        LOGGER.debug("No readable file at {}", input);
    }
}
Also used : Path(java.nio.file.Path) Parameters(org.apache.commons.configuration2.builder.fluent.Parameters) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) PropertiesConfiguration(org.apache.commons.configuration2.PropertiesConfiguration)

Example 18 with PropertiesConfiguration

use of org.apache.commons.configuration2.PropertiesConfiguration in project accumulo by apache.

the class ClientConfiguration method loadFromSearchPath.

@SuppressFBWarnings(value = "PATH_TRAVERSAL_IN", justification = "process runs in same security context as user who provided path")
private static ClientConfiguration loadFromSearchPath(List<String> paths) {
    List<Configuration> configs = new LinkedList<>();
    for (String path : paths) {
        File conf = new File(path);
        if (conf.isFile() && conf.canRead()) {
            var config = new PropertiesConfiguration();
            try (var reader = new FileReader(conf, UTF_8)) {
                config.read(reader);
            } catch (ConfigurationException | IOException e) {
                throw new IllegalStateException("Error loading client configuration file " + conf, e);
            }
            configs.add(config);
            log.info("Loaded client configuration file {}", conf);
        }
    }
    // We couldn't find the client configuration anywhere
    if (configs.isEmpty()) {
        log.debug("Found no client.conf in default paths. Using default client configuration values.");
    }
    return new ClientConfiguration(configs);
}
Also used : MapConfiguration(org.apache.commons.configuration2.MapConfiguration) CompositeConfiguration(org.apache.commons.configuration2.CompositeConfiguration) Configuration(org.apache.commons.configuration2.Configuration) PropertiesConfiguration(org.apache.commons.configuration2.PropertiesConfiguration) IOException(java.io.IOException) PropertiesConfiguration(org.apache.commons.configuration2.PropertiesConfiguration) LinkedList(java.util.LinkedList) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) FileReader(java.io.FileReader) File(java.io.File) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 19 with PropertiesConfiguration

use of org.apache.commons.configuration2.PropertiesConfiguration in project hadoop by apache.

the class MetricsConfig method toString.

static String toString(Configuration c) {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    try {
        PrintWriter pw = new PrintWriter(buffer, false);
        PropertiesConfiguration tmp = new PropertiesConfiguration();
        tmp.copy(c);
        tmp.write(pw);
        return buffer.toString("UTF-8");
    } catch (Exception e) {
        throw new MetricsConfigException(e);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) PropertiesConfiguration(org.apache.commons.configuration2.PropertiesConfiguration) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) PrintWriter(java.io.PrintWriter)

Example 20 with PropertiesConfiguration

use of org.apache.commons.configuration2.PropertiesConfiguration in project hadoop by apache.

the class MetricsConfig method loadFirst.

/**
   * Load configuration from a list of files until the first successful load
   * @param conf  the configuration object
   * @param files the list of filenames to try
   * @return  the configuration object
   */
static MetricsConfig loadFirst(String prefix, String... fileNames) {
    for (String fname : fileNames) {
        try {
            Configuration cf = new Configurations().propertiesBuilder(fname).configure(new Parameters().properties().setFileName(fname).setListDelimiterHandler(new DefaultListDelimiterHandler(','))).getConfiguration().interpolatedConfiguration();
            LOG.info("loaded properties from " + fname);
            LOG.debug(toString(cf));
            MetricsConfig mc = new MetricsConfig(cf, prefix);
            LOG.debug(mc);
            return mc;
        } catch (ConfigurationException e) {
            // Commons Configuration defines the message text when file not found
            if (e.getMessage().startsWith("Could not locate")) {
                continue;
            }
            throw new MetricsConfigException(e);
        }
    }
    LOG.warn("Cannot locate configuration: tried " + Joiner.on(",").join(fileNames));
    // default to an empty configuration
    return new MetricsConfig(new PropertiesConfiguration(), prefix);
}
Also used : DefaultListDelimiterHandler(org.apache.commons.configuration2.convert.DefaultListDelimiterHandler) Parameters(org.apache.commons.configuration2.builder.fluent.Parameters) SubsetConfiguration(org.apache.commons.configuration2.SubsetConfiguration) Configuration(org.apache.commons.configuration2.Configuration) PropertiesConfiguration(org.apache.commons.configuration2.PropertiesConfiguration) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) Configurations(org.apache.commons.configuration2.builder.fluent.Configurations) PropertiesConfiguration(org.apache.commons.configuration2.PropertiesConfiguration)

Aggregations

PropertiesConfiguration (org.apache.commons.configuration2.PropertiesConfiguration)28 ConfigurationException (org.apache.commons.configuration2.ex.ConfigurationException)10 File (java.io.File)7 IOException (java.io.IOException)6 Path (java.nio.file.Path)6 Parameters (org.apache.commons.configuration2.builder.fluent.Parameters)5 Configuration (org.apache.commons.configuration2.Configuration)4 Configurations (org.apache.commons.configuration2.builder.fluent.Configurations)4 FileReader (java.io.FileReader)3 FileBasedConfigurationBuilder (org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 StringWriter (java.io.StringWriter)2 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)2 BaseConfiguration (org.apache.commons.configuration2.BaseConfiguration)2 PropertiesBuilderParameters (org.apache.commons.configuration2.builder.fluent.PropertiesBuilderParameters)2 DefaultListDelimiterHandler (org.apache.commons.configuration2.convert.DefaultListDelimiterHandler)2 Preconditions (com.google.common.base.Preconditions)1 BufferedReader (java.io.BufferedReader)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1