Search in sources :

Example 1 with FileBasedConfigurationBuilder

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

the class ConfigUtils method readXmlConfiguration.

public static XMLConfiguration readXmlConfiguration(Resource resource, char listDelimiter, Map<String, Lookup> prefixLookups) throws ConfigurationException {
    Parameters params = new Parameters();
    FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<>(XMLConfiguration.class);
    try {
        XMLBuilderParameters xmlParams = params.xml().setURL(resource.getURL()).setListDelimiterHandler(new DefaultListDelimiterHandler(listDelimiter));
        if (MapUtils.isNotEmpty(prefixLookups)) {
            xmlParams = xmlParams.setPrefixLookups(prefixLookups);
        }
        builder.configure(xmlParams);
    } catch (IOException e) {
        throw new ConfigurationException("Unable to get URL of resource " + resource, e);
    }
    return builder.getConfiguration();
}
Also used : XMLConfiguration(org.apache.commons.configuration2.XMLConfiguration) DefaultListDelimiterHandler(org.apache.commons.configuration2.convert.DefaultListDelimiterHandler) Parameters(org.apache.commons.configuration2.builder.fluent.Parameters) XMLBuilderParameters(org.apache.commons.configuration2.builder.fluent.XMLBuilderParameters) FileBasedConfigurationBuilder(org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder) XMLBuilderParameters(org.apache.commons.configuration2.builder.fluent.XMLBuilderParameters) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) IOException(java.io.IOException)

Example 2 with FileBasedConfigurationBuilder

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

the class CasConfigurationPropertiesEnvironmentManager method savePropertyForStandaloneProfile.

/**
 * Save property for standalone profile.
 *
 * @param pair the pair
 */
@SneakyThrows
public void savePropertyForStandaloneProfile(final Pair<String, String> pair) {
    final File file = getStandaloneProfileConfigurationDirectory();
    final Parameters params = new Parameters();
    final FileBasedConfigurationBuilder<FileBasedConfiguration> builder = new FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class).configure(params.properties().setFile(new File(file, getApplicationName() + ".properties")));
    final Configuration config = builder.getConfiguration();
    config.setProperty(pair.getKey(), pair.getValue());
    builder.save();
}
Also used : Parameters(org.apache.commons.configuration2.builder.fluent.Parameters) Configuration(org.apache.commons.configuration2.Configuration) PropertiesConfiguration(org.apache.commons.configuration2.PropertiesConfiguration) FileBasedConfiguration(org.apache.commons.configuration2.FileBasedConfiguration) FileBasedConfiguration(org.apache.commons.configuration2.FileBasedConfiguration) File(java.io.File) PropertiesConfiguration(org.apache.commons.configuration2.PropertiesConfiguration) SneakyThrows(lombok.SneakyThrows)

Example 3 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 4 with FileBasedConfigurationBuilder

use of org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder in project xwiki-platform by xwiki.

the class XWikiPropertiesConfigurationSource method loadConfiguration.

private Configuration loadConfiguration() {
    // Looking for /etc/xwiki/xwiki.properties first
    File file = new File("/etc/xwiki/" + XWIKI_PROPERTIES_FILE);
    if (file.exists()) {
        try {
            return new Configurations().properties(file);
        } catch (Exception e) {
            // Note: if we cannot read the configuration file for any reason we log a warning but continue since
            // XWiki will use default values for all configurable elements.
            this.logger.warn("Failed to load configuration file [{}]", file, e.getMessage());
        }
    }
    // Register the Commons Properties Configuration, looking for a xwiki.properties file
    // in the XWiki path somewhere.
    URL xwikiPropertiesUrl = null;
    try {
        xwikiPropertiesUrl = this.environment.getResource(XWIKI_PROPERTIES_WARPATH);
        if (xwikiPropertiesUrl != null) {
            FileBasedConfigurationBuilder<PropertiesConfiguration> builder = new FileBasedConfigurationBuilder<PropertiesConfiguration>(PropertiesConfiguration.class).configure(new Parameters().properties().setListDelimiterHandler(new DefaultListDelimiterHandler(',')).setURL(xwikiPropertiesUrl));
            return builder.getConfiguration();
        } else {
            // We use a debug logging level here since we consider it's ok that there's no XWIKI_PROPERTIES_FILE
            // available, in which case default values are used.
            this.logger.debug("No configuration file [{}] found. Using default configuration values.", XWIKI_PROPERTIES_WARPATH);
        }
    } catch (Exception e) {
        // Note: if we cannot read the configuration file for any reason we log a warning but continue since XWiki
        // will use default values for all configurable elements.
        this.logger.warn("Failed to load configuration file [{}]. Using default configuration values. " + "Internal error [{}]", XWIKI_PROPERTIES_WARPATH, e.getMessage());
    }
    // implementation.
    return new BaseConfiguration();
}
Also used : BaseConfiguration(org.apache.commons.configuration2.BaseConfiguration) DefaultListDelimiterHandler(org.apache.commons.configuration2.convert.DefaultListDelimiterHandler) Parameters(org.apache.commons.configuration2.builder.fluent.Parameters) Configurations(org.apache.commons.configuration2.builder.fluent.Configurations) File(java.io.File) PropertiesConfiguration(org.apache.commons.configuration2.PropertiesConfiguration) InitializationException(org.xwiki.component.phase.InitializationException) URL(java.net.URL)

Example 5 with FileBasedConfigurationBuilder

use of org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder in project activemq-artemis by apache.

the class ArtemisTest method checkRole.

private void checkRole(String user, File roleFile, String... roles) throws Exception {
    Configurations configs = new Configurations();
    FileBasedConfigurationBuilder<PropertiesConfiguration> roleBuilder = configs.propertiesBuilder(roleFile);
    PropertiesConfiguration roleConfig = roleBuilder.getConfiguration();
    for (String r : roles) {
        String storedUsers = (String) roleConfig.getProperty(r);
        System.out.println("users in role: " + r + " ; " + storedUsers);
        List<String> userList = StringUtil.splitStringList(storedUsers, ",");
        assertTrue(userList.contains(user));
    }
}
Also used : SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Configurations(org.apache.commons.configuration2.builder.fluent.Configurations) PropertiesConfiguration(org.apache.commons.configuration2.PropertiesConfiguration)

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