Search in sources :

Example 1 with CombinedConfiguration

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

the class MultiResourceConfigurationBuilder method getConfiguration.

@Override
public HierarchicalConfiguration getConfiguration() throws ConfigurationException {
    List<HierarchicalConfiguration> configs = new ArrayList<>();
    // Last configurations should be loaded and added first so that they have greater priority.
    logger.info("Loading XML configurations in the order in which the properties will be resolved");
    for (int i = configPaths.length - 1; i >= 0; i--) {
        try {
            Resource resource = resourceLoader.getResource(configPaths[i]);
            if (resource.exists()) {
                Map<String, Lookup> prefixLookups = null;
                if (configDecryptor != null) {
                    prefixLookups = Collections.singletonMap(encryptedValuePrefix, new DecryptionLookup(configDecryptor));
                }
                XMLConfiguration config = ConfigUtils.readXmlConfiguration(resource, configListDelimiter, prefixLookups);
                logger.info("XML configuration loaded from " + resource);
                configs.add(config);
            }
        } catch (Exception e) {
            throw new ConfigurationException("Unable to load configuration at " + configPaths[i], e);
        }
    }
    if (configs.size() > 1) {
        CombinedConfiguration combinedConfig = new CombinedConfiguration(new OverrideCombiner());
        for (Configuration config : configs) {
            combinedConfig.addConfiguration(config);
        }
        return combinedConfig;
    } else if (configs.size() == 1) {
        return configs.get(0);
    } else {
        return null;
    }
}
Also used : XMLConfiguration(org.apache.commons.configuration2.XMLConfiguration) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) Configuration(org.apache.commons.configuration2.Configuration) CombinedConfiguration(org.apache.commons.configuration2.CombinedConfiguration) ArrayList(java.util.ArrayList) Resource(org.springframework.core.io.Resource) DecryptionLookup(org.craftercms.commons.config.DecryptionLookup) CombinedConfiguration(org.apache.commons.configuration2.CombinedConfiguration) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) OverrideCombiner(org.apache.commons.configuration2.tree.OverrideCombiner) XMLConfiguration(org.apache.commons.configuration2.XMLConfiguration) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) DecryptionLookup(org.craftercms.commons.config.DecryptionLookup) Lookup(org.apache.commons.configuration2.interpol.Lookup)

Example 2 with CombinedConfiguration

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

the class DefaultConfigurationManager method createRootConfig.

protected CommonsConfiguration createRootConfig() {
    MergeCombiner combiner = new MergeCombiner();
    combiner.addListNode(PluginManagerConstants.CFG_PLUGIN);
    CombinedConfiguration cc = new CombinedConfiguration(combiner);
    // Try to add explicit configuration
    if (configurationFilename != null) {
        logger.info("Loading configuration file {}...", configurationFilename);
        Pair<XMLConfiguration, URL> configurationPair = createXmlConfiguration(configurationFilename);
        cc.addConfiguration(configurationPair.getLeft());
        configurationFileUrl = configurationPair.getRight();
    }
    // Add default configuration
    cc.addConfiguration(createXmlConfiguration(ConfigurationConstants.DEFAULT_CONFIG).getLeft());
    return new CommonsConfiguration(cc);
}
Also used : XMLConfiguration(org.apache.commons.configuration2.XMLConfiguration) MergeCombiner(org.apache.commons.configuration2.tree.MergeCombiner) CombinedConfiguration(org.apache.commons.configuration2.CombinedConfiguration) CommonsConfiguration(org.openksavi.sponge.core.config.CommonsConfiguration) URL(java.net.URL)

Aggregations

CombinedConfiguration (org.apache.commons.configuration2.CombinedConfiguration)2 XMLConfiguration (org.apache.commons.configuration2.XMLConfiguration)2 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Configuration (org.apache.commons.configuration2.Configuration)1 HierarchicalConfiguration (org.apache.commons.configuration2.HierarchicalConfiguration)1 ConfigurationException (org.apache.commons.configuration2.ex.ConfigurationException)1 Lookup (org.apache.commons.configuration2.interpol.Lookup)1 MergeCombiner (org.apache.commons.configuration2.tree.MergeCombiner)1 OverrideCombiner (org.apache.commons.configuration2.tree.OverrideCombiner)1 DecryptionLookup (org.craftercms.commons.config.DecryptionLookup)1 CommonsConfiguration (org.openksavi.sponge.core.config.CommonsConfiguration)1 Resource (org.springframework.core.io.Resource)1