Search in sources :

Example 1 with CombinedConfiguration

use of org.apache.commons.configuration.CombinedConfiguration in project archaius by Netflix.

the class OverridingPropertiesConfiguration method convertToConcurrentCompositeConfiguration.

/**
     * Convert CombinedConfiguration into {@link ConcurrentCompositeConfiguration} as the later has better performance
     * and thread safety. 
     * 
     * @param config Configuration to be converted
     */
public static ConcurrentCompositeConfiguration convertToConcurrentCompositeConfiguration(CombinedConfiguration config) {
    ConcurrentCompositeConfiguration root = new ConcurrentCompositeConfiguration();
    IdentityHashMap<Configuration, String> reverseMap = new IdentityHashMap<Configuration, String>();
    for (String name : (Set<String>) config.getConfigurationNames()) {
        Configuration child = config.getConfiguration(name);
        reverseMap.put(child, name);
    }
    for (int i = 0; i < config.getNumberOfConfigurations(); i++) {
        Configuration child = config.getConfiguration(i);
        String name = reverseMap.get(child);
        if (child instanceof CombinedConfiguration) {
            CombinedConfiguration combinedConf = (CombinedConfiguration) child;
            ConcurrentCompositeConfiguration newConf = convertToConcurrentCompositeConfiguration(combinedConf);
            root.addConfiguration(newConf, name);
        } else {
            Configuration conf = new ConcurrentMapConfiguration(child);
            root.addConfiguration((AbstractConfiguration) conf, name);
        }
    }
    return root;
}
Also used : Set(java.util.Set) Configuration(org.apache.commons.configuration.Configuration) ConcurrentMapConfiguration(com.netflix.config.ConcurrentMapConfiguration) AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) ConcurrentCompositeConfiguration(com.netflix.config.ConcurrentCompositeConfiguration) CombinedConfiguration(org.apache.commons.configuration.CombinedConfiguration) ConcurrentMapConfiguration(com.netflix.config.ConcurrentMapConfiguration) IdentityHashMap(java.util.IdentityHashMap) CombinedConfiguration(org.apache.commons.configuration.CombinedConfiguration) ConcurrentCompositeConfiguration(com.netflix.config.ConcurrentCompositeConfiguration)

Example 2 with CombinedConfiguration

use of org.apache.commons.configuration.CombinedConfiguration in project archaius by Netflix.

the class OverridingPropertiesConfiguration method getAllNamedConfiguration.

/**
     * Gets all named sub-configuration from a configuration in a map. This method examines each sub-configuration
     * which is an instance of {@link ConcurrentCompositeConfiguration} or CombinedConfiguration and extract the 
     * named configurations out of them.
     *  
     * @param conf Configuration to get all the named sub-configurations
     * @return map where key is the name of the sub-configuration and value is the sub-configuration
     */
public static Map<String, Configuration> getAllNamedConfiguration(Configuration conf) {
    List<Configuration> toProcess = new ArrayList<Configuration>();
    Map<String, Configuration> map = new HashMap<String, Configuration>();
    toProcess.add(conf);
    while (!toProcess.isEmpty()) {
        Configuration current = toProcess.remove(0);
        if (current instanceof ConcurrentCompositeConfiguration) {
            ConcurrentCompositeConfiguration composite = (ConcurrentCompositeConfiguration) current;
            for (String name : composite.getConfigurationNames()) {
                map.put(name, composite.getConfiguration(name));
            }
            for (Configuration c : composite.getConfigurations()) {
                toProcess.add(c);
            }
        } else if (current instanceof CombinedConfiguration) {
            CombinedConfiguration combined = (CombinedConfiguration) current;
            for (String name : (Set<String>) combined.getConfigurationNames()) {
                map.put(name, combined.getConfiguration(name));
            }
            for (int i = 0; i < combined.getNumberOfConfigurations(); i++) {
                toProcess.add(combined.getConfiguration(i));
            }
        }
    }
    return map;
}
Also used : Configuration(org.apache.commons.configuration.Configuration) ConcurrentMapConfiguration(com.netflix.config.ConcurrentMapConfiguration) AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) ConcurrentCompositeConfiguration(com.netflix.config.ConcurrentCompositeConfiguration) CombinedConfiguration(org.apache.commons.configuration.CombinedConfiguration) HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) ArrayList(java.util.ArrayList) CombinedConfiguration(org.apache.commons.configuration.CombinedConfiguration) ConcurrentCompositeConfiguration(com.netflix.config.ConcurrentCompositeConfiguration)

Example 3 with CombinedConfiguration

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

the class MultiConfigurationBuilder method getConfiguration.

@Override
public Configuration getConfiguration() throws ConfigurationException {
    List<Configuration> 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()) {
                XMLConfiguration config = new XMLConfiguration();
                config.load(resource.getInputStream());
                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((AbstractConfiguration) config);
        }
        return combinedConfig;
    } else if (configs.size() == 1) {
        return configs.get(0);
    } else {
        return null;
    }
}
Also used : XMLConfiguration(org.apache.commons.configuration.XMLConfiguration) Configuration(org.apache.commons.configuration.Configuration) AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) XMLConfiguration(org.apache.commons.configuration.XMLConfiguration) CombinedConfiguration(org.apache.commons.configuration.CombinedConfiguration) ConfigurationException(org.apache.commons.configuration.ConfigurationException) ArrayList(java.util.ArrayList) Resource(org.springframework.core.io.Resource) CombinedConfiguration(org.apache.commons.configuration.CombinedConfiguration) ConfigurationException(org.apache.commons.configuration.ConfigurationException) OverrideCombiner(org.apache.commons.configuration.tree.OverrideCombiner)

Aggregations

AbstractConfiguration (org.apache.commons.configuration.AbstractConfiguration)3 CombinedConfiguration (org.apache.commons.configuration.CombinedConfiguration)3 Configuration (org.apache.commons.configuration.Configuration)3 ConcurrentCompositeConfiguration (com.netflix.config.ConcurrentCompositeConfiguration)2 ConcurrentMapConfiguration (com.netflix.config.ConcurrentMapConfiguration)2 ArrayList (java.util.ArrayList)2 IdentityHashMap (java.util.IdentityHashMap)2 PropertiesConfiguration (org.apache.commons.configuration.PropertiesConfiguration)2 HashMap (java.util.HashMap)1 Set (java.util.Set)1 ConfigurationException (org.apache.commons.configuration.ConfigurationException)1 XMLConfiguration (org.apache.commons.configuration.XMLConfiguration)1 OverrideCombiner (org.apache.commons.configuration.tree.OverrideCombiner)1 Resource (org.springframework.core.io.Resource)1