Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.config.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Map.Entry;
import org.apache.commons.configuration.AbstractConfiguration;
import org.apache.commons.configuration.CombinedConfiguration;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.netflix.config.ConcurrentCompositeConfiguration;
import com.netflix.config.ConcurrentMapConfiguration;
import com.netflix.config.ConfigurationManager;
/**
* Utility class for configuration.
*
* @author awang
*
*/
public class ConfigurationUtils {
private static final Logger logger = LoggerFactory.getLogger(ConfigurationUtils.class);
/**
* 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 reverseMap = new IdentityHashMap();
for (String name: (Set) 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;
}
/**
* 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 getAllNamedConfiguration(Configuration conf) {
List toProcess = new ArrayList();
Map map = new HashMap();
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) combined.getConfigurationNames()) {
map.put(name, combined.getConfiguration(name));
}
for (int i = 0; i < combined.getNumberOfConfigurations(); i++) {
toProcess.add(combined.getConfiguration(i));
}
}
}
return map;
}
/**
* Utility method to obtain Properties given an instance of AbstractConfiguration.
* Returns an empty Properties object if the config has no properties or is null.
* @param config Configuration to get the properties
* @return properties extracted from the configuration
*/
public static Properties getProperties(Configuration config) {
Properties p = new Properties();
if (config != null){
Iterator it = config.getKeys();
while (it.hasNext()){
String key = it.next();
if (key != null) {
Object value = config.getProperty(key);
if (value != null) {
p.put(key, value);
} }
}
}
return p;
}
public static void loadProperties(Properties props, Configuration config) {
for (Entry