Search in sources :

Example 11 with FileBasedConfigurationBuilder

use of org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder 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 12 with FileBasedConfigurationBuilder

use of org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder in project sponge by softelnet.

the class DefaultConfigurationManager method createXmlConfiguration.

protected Pair<XMLConfiguration, URL> createXmlConfiguration(String fileName) {
    List<Lookup> lookups = Arrays.asList(new SystemPropertiesLookup(), new HomeLookup(), new ConfigLookup());
    Parameters params = new Parameters();
    FallbackBasePathLocationStrategy locationStrategy = new FallbackBasePathLocationStrategy(FileLocatorUtils.DEFAULT_LOCATION_STRATEGY, home);
    FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<>(XMLConfiguration.class).configure(params.xml().setDefaultLookups(lookups).setLocationStrategy(locationStrategy).setFileName(fileName).setSchemaValidation(true).setEntityResolver(new ResourceSchemaResolver()));
    try {
        XMLConfiguration xmlConfiguration = builder.getConfiguration();
        return new ImmutablePair<>(xmlConfiguration, locationStrategy.getLocatedUrl());
    } catch (ConfigurationException e) {
        throw new ConfigException("Error reading configuration file " + fileName, e);
    }
}
Also used : Parameters(org.apache.commons.configuration2.builder.fluent.Parameters) ConfigException(org.openksavi.sponge.config.ConfigException) XMLConfiguration(org.apache.commons.configuration2.XMLConfiguration) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) FallbackBasePathLocationStrategy(org.openksavi.sponge.core.config.FallbackBasePathLocationStrategy) SystemPropertiesLookup(org.apache.commons.configuration2.interpol.SystemPropertiesLookup) Lookup(org.apache.commons.configuration2.interpol.Lookup) SystemPropertiesLookup(org.apache.commons.configuration2.interpol.SystemPropertiesLookup)

Example 13 with FileBasedConfigurationBuilder

use of org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder in project midpoint by Evolveum.

the class StartupConfiguration method createXmlConfiguration.

private void createXmlConfiguration(String filename) throws ConfigurationException {
    Map<String, Lookup> lookups = new HashMap<>(ConfigurationInterpolator.getDefaultPrefixLookups());
    lookups.put(RandomLookup.PREFIX, new RandomLookup());
    lookups.put(HostnameLookup.PREFIX, new HostnameLookup());
    FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<>(XMLConfiguration.class).configure(new Parameters().xml().setFileName(filename).setPrefixLookups(lookups));
    /*
        On debug level this shows stacktrace for:
        DEBUG org.apache.commons.beanutils.FluentPropertyBeanIntrospector - Exception is:
        java.beans.IntrospectionException: bad write method arg count:
        public final void org.apache.commons.configuration2.AbstractConfiguration.setProperty
        This is reportedly beanutils over-strictness issue but is nowhere close to be fixed.
        Jira for commons-configuration can be also found, but they rely on beanutils fix.
        */
    config = builder.getConfiguration();
    config.addProperty(MIDPOINT_HOME_PROPERTY, midPointHomePath);
    applyEnvironmentProperties();
    resolveFileReferences();
}
Also used : XMLConfiguration(org.apache.commons.configuration2.XMLConfiguration) Parameters(org.apache.commons.configuration2.builder.fluent.Parameters) HostnameLookup(com.evolveum.midpoint.init.interpol.HostnameLookup) RandomLookup(com.evolveum.midpoint.init.interpol.RandomLookup) Lookup(org.apache.commons.configuration2.interpol.Lookup) HostnameLookup(com.evolveum.midpoint.init.interpol.HostnameLookup) RandomLookup(com.evolveum.midpoint.init.interpol.RandomLookup)

Example 14 with FileBasedConfigurationBuilder

use of org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder in project zeppelin by apache.

the class ZeppelinConfiguration method loadXMLConfig.

private void loadXMLConfig(@Nullable String filename) throws ConfigurationException {
    if (StringUtils.isBlank(filename)) {
        filename = ZEPPELIN_SITE_XML;
    }
    List<FileLocationStrategy> subs = Arrays.asList(new ZeppelinLocationStrategy(), new ClasspathLocationStrategy());
    FileLocationStrategy strategy = new CombinedLocationStrategy(subs);
    Parameters params = new Parameters();
    FileBasedConfigurationBuilder<XMLConfiguration> xmlbuilder = new FileBasedConfigurationBuilder<XMLConfiguration>(XMLConfiguration.class).configure(params.xml().setLocationStrategy(strategy).setFileName(filename).setBasePath(File.separator + "conf" + File.separator));
    XMLConfiguration xmlConfig = xmlbuilder.getConfiguration();
    List<ImmutableNode> nodes = xmlConfig.getNodeModel().getRootNode().getChildren();
    if (nodes != null && !nodes.isEmpty()) {
        for (ImmutableNode p : nodes) {
            String name = String.valueOf(p.getChildren("name").get(0).getValue());
            String value = String.valueOf(p.getChildren("value").get(0).getValue());
            if (StringUtils.isNotBlank(name) && StringUtils.isNotBlank(value)) {
                setProperty(name, value);
            }
        }
    }
}
Also used : FileLocationStrategy(org.apache.commons.configuration2.io.FileLocationStrategy) XMLConfiguration(org.apache.commons.configuration2.XMLConfiguration) Parameters(org.apache.commons.configuration2.builder.fluent.Parameters) ImmutableNode(org.apache.commons.configuration2.tree.ImmutableNode) CombinedLocationStrategy(org.apache.commons.configuration2.io.CombinedLocationStrategy) ClasspathLocationStrategy(org.apache.commons.configuration2.io.ClasspathLocationStrategy)

Aggregations

Parameters (org.apache.commons.configuration2.builder.fluent.Parameters)11 PropertiesConfiguration (org.apache.commons.configuration2.PropertiesConfiguration)8 XMLConfiguration (org.apache.commons.configuration2.XMLConfiguration)5 ConfigurationException (org.apache.commons.configuration2.ex.ConfigurationException)5 File (java.io.File)4 FileBasedConfigurationBuilder (org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder)4 Configurations (org.apache.commons.configuration2.builder.fluent.Configurations)3 IOException (java.io.IOException)2 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)2 Configuration (org.apache.commons.configuration2.Configuration)2 FileBasedConfiguration (org.apache.commons.configuration2.FileBasedConfiguration)2 PropertiesBuilderParameters (org.apache.commons.configuration2.builder.fluent.PropertiesBuilderParameters)2 DefaultListDelimiterHandler (org.apache.commons.configuration2.convert.DefaultListDelimiterHandler)2 Lookup (org.apache.commons.configuration2.interpol.Lookup)2 HostnameLookup (com.evolveum.midpoint.init.interpol.HostnameLookup)1 RandomLookup (com.evolveum.midpoint.init.interpol.RandomLookup)1 URL (java.net.URL)1 Path (java.nio.file.Path)1 SneakyThrows (lombok.SneakyThrows)1 HashProcessor (org.apache.activemq.artemis.utils.HashProcessor)1