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;
}
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;
}
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;
}
}
Aggregations