use of org.apache.commons.configuration.Configuration in project whirr by apache.
the class ClusterSpecCommand method getClusterSpec.
protected ClusterSpec getClusterSpec(OptionSet optionSet) throws ConfigurationException {
Configuration optionsConfig = new PropertiesConfiguration();
for (Map.Entry<Property, OptionSpec> entry : optionSpecs.entrySet()) {
Property property = entry.getKey();
OptionSpec option = entry.getValue();
if (property.hasMultipleArguments()) {
optionsConfig.setProperty(property.getConfigName(), optionSet.valuesOf(option));
} else {
optionsConfig.setProperty(property.getConfigName(), optionSet.valueOf(option));
}
}
CompositeConfiguration config = new CompositeConfiguration();
config.addConfiguration(optionsConfig);
if (optionSet.has(configOption)) {
Configuration defaults = new PropertiesConfiguration(optionSet.valueOf(configOption));
config.addConfiguration(defaults);
}
for (Property required : EnumSet.of(SERVICE_NAME, CLUSTER_NAME, IDENTITY)) {
if (config.getString(required.getConfigName()) == null) {
throw new IllegalArgumentException(String.format("Option '%s' not set.", required.getSimpleName()));
}
}
return ClusterSpec.fromConfiguration(config);
}
use of org.apache.commons.configuration.Configuration 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.Configuration 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.Configuration in project archaius by Netflix.
the class ConcurrentCompositeConfiguration method removeConfiguration.
/**
* Removes the configuration with the specified name.
*
* @param name the name of the configuration to be removed
* @return the removed configuration (<b>null</b> if this configuration
* was not found)
*/
public Configuration removeConfiguration(String name) {
Configuration conf = getConfiguration(name);
if (conf != null && !conf.equals(containerConfiguration)) {
configList.remove(conf);
namedConfigurations.remove(name);
} else if (conf != null && conf.equals(containerConfiguration)) {
throw new IllegalArgumentException("Can't remove container configuration");
}
return conf;
}
use of org.apache.commons.configuration.Configuration in project archaius by Netflix.
the class ConcurrentCompositeConfiguration method getList.
/**
* Get a List of objects associated with the given configuration key.
* If the key doesn't map to an existing object, the default value
* is returned.
*
* @param key The configuration key.
* @param defaultValue The default value.
* @return The associated List of value.
*
*/
@Override
public List getList(String key, List defaultValue) {
List<Object> list = new ArrayList<Object>();
// add all elements from the first configuration containing the requested key
Iterator<AbstractConfiguration> it = configList.iterator();
if (overrideProperties.containsKey(key)) {
appendListProperty(list, overrideProperties, key);
}
while (it.hasNext() && list.isEmpty()) {
Configuration config = it.next();
if ((config != containerConfiguration || containerConfigurationChanged) && config.containsKey(key)) {
appendListProperty(list, config, key);
}
}
// add all elements from the in memory configuration
if (list.isEmpty()) {
appendListProperty(list, containerConfiguration, key);
}
if (list.isEmpty()) {
return defaultValue;
}
ListIterator<Object> lit = list.listIterator();
while (lit.hasNext()) {
lit.set(interpolate(lit.next()));
}
return list;
}
Aggregations