diff options
author | vempo <vitaliy.emporopulo@amdocs.com> | 2018-10-31 10:27:59 +0200 |
---|---|---|
committer | Avi Gaffa <avi.gaffa@amdocs.com> | 2018-10-31 12:06:24 +0000 |
commit | 461e964344d01e245464980b6ace12e4b28569e6 (patch) | |
tree | ae9ddafa9e3437aec8a7cf56b37f79fae924f7d8 /common/onap-common-configuration-management/onap-configuration-management-core/src/main | |
parent | eba6fee58790a1ffece980de44100b21764051ca (diff) |
Removed JMX, other unused code from configuration
Removed code duplicates, stabilized test execution via
Maven, re-aranged code, fixed spelling.
Change-Id: I41fc303ea0a8c7d78d89a12bb20850de51cb8c52
Issue-ID: SDC-1867
Signed-off-by: vempo <vitaliy.emporopulo@amdocs.com>
Diffstat (limited to 'common/onap-common-configuration-management/onap-configuration-management-core/src/main')
5 files changed, 227 insertions, 285 deletions
diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java index ee1d7b10aa..bdba27a420 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java @@ -38,10 +38,9 @@ import java.util.Collections; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Queue; import java.util.Set; @@ -55,7 +54,6 @@ import java.util.concurrent.TransferQueue; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; -import java.util.stream.Stream; import net.sf.corn.cps.CPScanner; import net.sf.corn.cps.ResourceFilter; import org.apache.commons.configuration2.CompositeConfiguration; @@ -64,6 +62,7 @@ import org.apache.commons.configuration2.FileBasedConfiguration; import org.apache.commons.configuration2.PropertiesConfiguration; import org.apache.commons.configuration2.XMLConfiguration; import org.apache.commons.configuration2.builder.BasicConfigurationBuilder; +import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder; import org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder; import org.apache.commons.configuration2.builder.fluent.Configurations; import org.apache.commons.configuration2.builder.fluent.Parameters; @@ -72,7 +71,6 @@ import org.apache.commons.configuration2.ex.ConfigurationException; import org.apache.commons.io.IOUtils; import org.onap.config.api.Config; import org.onap.config.api.ConfigurationManager; -import org.onap.config.impl.ConfigurationRepository; import org.onap.config.impl.YamlConfiguration; import org.onap.config.type.ConfigurationMode; import org.onap.config.type.ConfigurationType; @@ -82,7 +80,9 @@ import org.slf4j.LoggerFactory; public class ConfigurationUtils { private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationUtils.class); + private static final String CONFIGURATION_TYPE_NOT_SUPPORTED = "Configuration type not supported:"; + private static final Map<Class, Class> ARRAY_CLASS_MAP; static { @@ -104,6 +104,7 @@ public class ConfigurationUtils { } public static Collection<File> getAllFiles(File file, boolean recursive, boolean onlyDirectory) { + ArrayList<File> collection = new ArrayList<>(); if (file.isDirectory() && file.exists()) { File[] files = file.listFiles(); @@ -113,7 +114,7 @@ public class ConfigurationUtils { } else if (innerFile.isDirectory()) { collection.add(innerFile); if (recursive) { - collection.addAll(getAllFiles(innerFile, recursive, onlyDirectory)); + collection.addAll(getAllFiles(innerFile, true, onlyDirectory)); } } } @@ -122,12 +123,17 @@ public class ConfigurationUtils { } public static String getCommaSeparatedList(String[] list) { - return getCommaSeparatedList(list == null ? Collections.emptyList() : Arrays.asList(list)); + return (list == null) || (list.length == 0) ? "" : getCommaSeparatedList(Arrays.asList(list)); } - public static String getCommaSeparatedList(List list) { - return ((Stream<String>) list.stream().filter(o -> o != null && !o.toString().trim().isEmpty()) - .map(o -> o.toString().trim())).collect(Collectors.joining(",")); + public static String getCommaSeparatedList(List<?> list) { + + if ((list == null) || list.isEmpty()) { + return ""; + } + + return list.stream().filter(o -> o != null && !o.toString().trim().isEmpty()) + .map(o -> o.toString().trim()).collect(Collectors.joining(",")); } public static boolean isConfig(URL url) { @@ -150,18 +156,59 @@ public class ConfigurationUtils { return file != null && file.exists() && isConfig(file.getName()); } - private static Optional<String> getNamespace(Configuration config) { + private static Optional<String> readNamespace(Configuration config) { return ofNullable(config).flatMap(configuration -> ofNullable(configuration.getString(Constants.NAMESPACE_KEY))) .map(String::toUpperCase); } + private static Optional<String> readMergeStrategy(Configuration config) { + return ofNullable(config).flatMap(configuration -> ofNullable(configuration.getString(Constants.MODE_KEY))) + .map(String::toUpperCase); + } + + public static ConfigurationMode getMergeStrategy(File file) { + Optional<ConfigurationMode> configurationMode = + getConfiguration(file).flatMap(ConfigurationUtils::readMergeStrategy) + .flatMap(ConfigurationUtils::convertConfigurationMode); + return configurationMode.orElseGet(() -> getMergeStrategy(file.getName().toUpperCase())); + } + public static ConfigurationMode getMergeStrategy(URL url) { Optional<ConfigurationMode> configurationMode = - getConfiguration(url).flatMap(ConfigurationUtils::getMergeStrategy) + getConfiguration(url).flatMap(ConfigurationUtils::readMergeStrategy) .flatMap(ConfigurationUtils::convertConfigurationMode); return configurationMode.orElseGet(() -> getMergeStrategy(url.getFile().toUpperCase())); } + public static ConfigurationMode getMergeStrategy(String file) { + + file = file.toUpperCase().substring(file.lastIndexOf('!') + 1); + file = file.substring(file.lastIndexOf('/') + 1); + + Pattern pattern = Pattern.compile( + "CONFIG(-\\w*){0,1}(-" + "(" + ConfigurationMode.OVERRIDE + "|" + ConfigurationMode.MERGE + "|" + + ConfigurationMode.UNION + ")){0,1}" + "\\.(" + ConfigurationType.PROPERTIES.name() + "|" + + ConfigurationType.XML.name() + "|" + ConfigurationType.JSON.name() + "|" + + ConfigurationType.YAML.name() + ")$"); + Matcher matcher = pattern.matcher(file); + boolean b1 = matcher.matches(); + if (b1) { + for (int i = 1; i <= matcher.groupCount(); i++) { + String modeName = matcher.group(i); + if (modeName != null) { + modeName = modeName.substring(1); + } + try { + return Enum.valueOf(ConfigurationMode.class, modeName); + } catch (Exception exception) { + //do nothing + } + } + } + + return null; + } + public static Optional<FileBasedConfiguration> getConfiguration(URL url) { try { @@ -195,34 +242,14 @@ public class ConfigurationUtils { } } - public static ConfigurationMode getMergeStrategy(String file) { - file = file.toUpperCase().substring(file.lastIndexOf('!') + 1); - file = file.substring(file.lastIndexOf('/') + 1); - Pattern pattern = Pattern.compile( - "CONFIG(-\\w*){0,1}(-" + "(" + ConfigurationMode.OVERRIDE + "|" + ConfigurationMode.MERGE + "|" - + ConfigurationMode.UNION + ")){0,1}" + "\\.(" + ConfigurationType.PROPERTIES.name() + "|" - + ConfigurationType.XML.name() + "|" + ConfigurationType.JSON.name() + "|" - + ConfigurationType.YAML.name() + ")$"); - Matcher matcher = pattern.matcher(file); - boolean b1 = matcher.matches(); - if (b1) { - for (int i = 1; i <= matcher.groupCount(); i++) { - String modeName = matcher.group(i); - if (modeName != null) { - modeName = modeName.substring(1); - } - try { - return Enum.valueOf(ConfigurationMode.class, modeName); - } catch (Exception exception) { - //do nothing - } - } - } - - return null; + public static ConfigurationType getConfigType(File file) { + Objects.requireNonNull(file, "File cannot be null"); + return Enum.valueOf(ConfigurationType.class, + file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf('.') + 1).toUpperCase()); } public static ConfigurationType getConfigType(URL url) { + Objects.requireNonNull(url, "URL cannot be null"); return Enum.valueOf(ConfigurationType.class, url.getFile().substring(url.getFile().lastIndexOf('.') + 1).toUpperCase()); } @@ -237,23 +264,6 @@ public class ConfigurationUtils { return Optional.ofNullable(configurationMode); } - private static Optional<String> getMergeStrategy(Configuration config) { - return ofNullable(config).flatMap(configuration -> ofNullable(configuration.getString(Constants.MODE_KEY))) - .map(String::toUpperCase); - } - - public static ConfigurationMode getMergeStrategy(File file) { - Optional<ConfigurationMode> configurationMode = - getConfiguration(file).flatMap(ConfigurationUtils::getMergeStrategy) - .flatMap(ConfigurationUtils::convertConfigurationMode); - return configurationMode.orElseGet(() -> getMergeStrategy(file.getName().toUpperCase())); - } - - public static ConfigurationType getConfigType(File file) { - return Enum.valueOf(ConfigurationType.class, - file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf('.') + 1).toUpperCase()); - } - public static Class getCollectionGenericType(Field field) { Type type = field.getGenericType(); @@ -266,7 +276,7 @@ public class ConfigurationUtils { if (isWrapperClass(clazz)) { return clazz; } else { - throw new RuntimeException("Collection of type " + clazz.getName() + " not supported."); + throw new IllegalArgumentException("Collection of type " + clazz.getName() + " not supported."); } } } @@ -275,9 +285,8 @@ public class ConfigurationUtils { } public static boolean isWrapperClass(Class clazz) { - return clazz == String.class || clazz == Boolean.class || clazz == Character.class || Number.class - .isAssignableFrom( - clazz); + return clazz == String.class || clazz == Boolean.class || clazz == Character.class + || Number.class.isAssignableFrom(clazz); } public static Class getArrayClass(Class clazz) { @@ -288,16 +297,25 @@ public class ConfigurationUtils { return CPScanner.scanResources(new ResourceFilter()); } + public static BasicConfigurationBuilder<FileBasedConfiguration> getConfigurationBuilder(File file) { + FileBasedConfigurationBuilder<FileBasedConfiguration> builder; + ConfigurationType configType = ConfigurationUtils.getConfigType(file); + builder = getFileBasedConfigurationBuilder(configType); + builder.configure(new Parameters().fileBased().setFile(file) + .setListDelimiterHandler(new DefaultListDelimiterHandler(','))); + return builder; + } + public static BasicConfigurationBuilder<FileBasedConfiguration> getConfigurationBuilder(URL url) { ConfigurationType configType = ConfigurationUtils.getConfigType(url); ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> builder = - getFileBasedConfigurationReloadingFileBasedConfigurationBuilder(configType); + getFileBasedConfigurationBuilder(configType); builder.configure( new Parameters().fileBased().setURL(url).setListDelimiterHandler(new DefaultListDelimiterHandler(','))); return builder; } - private static ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> getFileBasedConfigurationReloadingFileBasedConfigurationBuilder( + private static ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> getFileBasedConfigurationBuilder( ConfigurationType configType) { ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> builder; @@ -320,17 +338,6 @@ public class ConfigurationUtils { return builder; } - public static BasicConfigurationBuilder<FileBasedConfiguration> getConfigurationBuilder(File file, - boolean autoSave) { - ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> builder; - ConfigurationType configType = ConfigurationUtils.getConfigType(file); - builder = getFileBasedConfigurationReloadingFileBasedConfigurationBuilder(configType); - builder.configure(new Parameters().fileBased().setFile(file) - .setListDelimiterHandler(new DefaultListDelimiterHandler(','))); - builder.setAutoSave(autoSave); - return builder; - } - public static <T> T read(Configuration config, Class<T> clazz, String keyPrefix) throws Exception { Config confAnnotation = clazz.getAnnotation(Config.class); if (confAnnotation != null) { @@ -535,6 +542,11 @@ public class ConfigurationUtils { ConfigurationUtils.getNamespace(file).split(Constants.TENANT_NAMESPACE_SEPARATOR)); } + public static String getConfigurationRepositoryKey(URL url) { + return getConfigurationRepositoryKey( + ConfigurationUtils.getNamespace(url).split(Constants.TENANT_NAMESPACE_SEPARATOR)); + } + public static String getConfigurationRepositoryKey(String[] array) { Deque<String> stack = new ArrayDeque<>(); stack.push(Constants.DEFAULT_TENANT); @@ -547,7 +559,7 @@ public class ConfigurationUtils { public static String getNamespace(File file) { Optional<String> namespace = - getConfiguration(file).flatMap(ConfigurationUtils::getNamespace).map(String::toUpperCase); + getConfiguration(file).flatMap(ConfigurationUtils::readNamespace).map(String::toUpperCase); return namespace.orElseGet(() -> getNamespace(file.getName().toUpperCase())); } @@ -577,78 +589,30 @@ public class ConfigurationUtils { return null; } - public static String getConfigurationRepositoryKey(URL url) { - return getConfigurationRepositoryKey( - ConfigurationUtils.getNamespace(url).split(Constants.TENANT_NAMESPACE_SEPARATOR)); - } - public static String getNamespace(URL url) { Optional<String> namespace = - getConfiguration(url).flatMap(ConfigurationUtils::getNamespace).map(String::toUpperCase); + getConfiguration(url).flatMap(ConfigurationUtils::readNamespace).map(String::toUpperCase); return namespace.orElseGet(() -> getNamespace(url.getFile().toUpperCase())); } - public static LinkedHashMap toMap(Configuration config) { - Iterator<String> iterator = config.getKeys(); - LinkedHashMap<String, String> map = new LinkedHashMap<>(); - while (iterator.hasNext()) { - String key = iterator.next(); - if (!(key.equals(Constants.MODE_KEY) || key.equals(Constants.NAMESPACE_KEY) || key.equals( - Constants.LOAD_ORDER_KEY))) { - map.put(key, config.getProperty(key).toString()); - } - } - - return map; - } - - public static Map diff(LinkedHashMap orig, LinkedHashMap latest) { - orig = new LinkedHashMap<>(orig); - latest = new LinkedHashMap<>(latest); - List<String> set = new ArrayList(orig.keySet()); - for (String key : set) { - if (latest.remove(key, orig.get(key))) { - orig.remove(key); - } - } - Set<String> keys = latest.keySet(); - for (String key : keys) { - orig.remove(key); - } - set = new ArrayList(orig.keySet()); - for (String key : set) { - latest.put(key, ""); - } - return new HashMap<>(latest); - } - - public static boolean isArray(String tenant, String namespace, String key, int processingHints) throws Exception { - Object obj = ConfigurationUtils - .getProperty(ConfigurationRepository.lookup().getConfigurationFor(tenant, namespace), key, - processingHints); - return (obj != null) && ConfigurationUtils.isCollection(obj.toString()); - } - public static Object getProperty(Configuration config, String key, int processingHints) { - if (!isDirectLookup(processingHints)) { - - if (config instanceof CompositeConfiguration) { + if (!isDirectLookup(processingHints) && (config instanceof CompositeConfiguration)) { - CompositeConfiguration conf = (CompositeConfiguration) config; - for (int i = 0; i < conf.getNumberOfConfigurations(); i++) { + CompositeConfiguration conf = (CompositeConfiguration) config; + for (int i = 0; i < conf.getNumberOfConfigurations(); i++) { - if (isNodeSpecific(processingHints)) { - Object obj = conf.getConfiguration(i).getProperty(key); - if (obj != null) { - return obj; - } + if (isNodeSpecific(processingHints)) { + Object obj = conf.getConfiguration(i).getProperty(key); + if (obj != null) { + return obj; } } } } + return config.getProperty(key); } @@ -695,6 +659,6 @@ public class ConfigurationUtils { } public static boolean isBlank(String value) { - return value == null || value.trim().length() == 0; + return value == null || value.trim().isEmpty(); } } diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/AggregateConfiguration.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/AggregateConfiguration.java index e81d82bf7d..07c81e5f5b 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/AggregateConfiguration.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/AggregateConfiguration.java @@ -19,14 +19,17 @@ package org.onap.config.impl; import static org.onap.config.Constants.LOAD_ORDER_KEY; import java.io.File; +import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import org.apache.commons.configuration2.CombinedConfiguration; import org.apache.commons.configuration2.Configuration; +import org.apache.commons.configuration2.ex.ConfigurationException; import org.apache.commons.configuration2.tree.MergeCombiner; import org.apache.commons.configuration2.tree.OverrideCombiner; import org.apache.commons.configuration2.tree.UnionCombiner; @@ -40,61 +43,61 @@ public final class AggregateConfiguration { private final Map<String, Configuration> mergeConfig = Collections.synchronizedMap(new HashMap<>()); private final Map<String, Configuration> overrideConfig = Collections.synchronizedMap(new LinkedHashMap<>()); - public void addConfig(File file) throws Exception { - addConfig(file.getAbsolutePath().toUpperCase(), ConfigurationUtils.getMergeStrategy(file), - ConfigurationUtils.getConfigurationBuilder(file, false).getConfiguration()); + public void addConfig(File file) throws ConfigurationException { + addConfig(fileToUrl(file), ConfigurationUtils.getMergeStrategy(file), + ConfigurationUtils.getConfigurationBuilder(file).getConfiguration()); } - private void addConfig(String path, ConfigurationMode configMode, Configuration config) { + public void addConfig(URL url) throws ConfigurationException { + addConfig(url, ConfigurationUtils.getMergeStrategy(url), + ConfigurationUtils.getConfigurationBuilder(url).getConfiguration()); + } + + private void addConfig(URL url, ConfigurationMode configMode, Configuration config) { + + String normalizedUrl = normalize(url); if (configMode != null) { switch (configMode) { case MERGE: - mergeConfig.put(path, config); + mergeConfig.put(normalizedUrl, config); break; case OVERRIDE: - overrideConfig.put(path, config); + overrideConfig.put(normalizedUrl, config); break; case UNION: - unionConfig.put(path, config); + unionConfig.put(normalizedUrl, config); break; default: } } else { - rootConfig.put(path, config); + rootConfig.put(normalizedUrl, config); } } - public void addConfig(URL url) throws Exception { - addConfig(url.getFile().toUpperCase(), ConfigurationUtils.getMergeStrategy(url), - ConfigurationUtils.getConfigurationBuilder(url).getConfiguration()); + private String normalize(URL url) { + // what about Linux where paths are case sensitive? + return url.toString().toUpperCase(); } - public void removeConfig(File file) { - String key = file.getAbsolutePath().toUpperCase(); - if (rootConfig.containsKey(key)) { - rootConfig.remove(key); - } else if (mergeConfig.containsKey(key)) { - mergeConfig.remove(key); - } else if (unionConfig.containsKey(key)) { - unionConfig.remove(key); - } else if (overrideConfig.containsKey(key)) { - overrideConfig.remove(key); - } - } + private URL fileToUrl(File file) { - public boolean containsConfig(File file) { - String key = file.getAbsolutePath().toUpperCase(); - return rootConfig.containsKey(key) || mergeConfig.containsKey(key) || unionConfig.containsKey(key) - || overrideConfig.containsKey(key); + try { + return file.getAbsoluteFile().toURI().toURL(); + } catch (MalformedURLException e) { + throw new IllegalStateException("URL produced by JDK and is not expected to be malformed. File: " + + file.getAbsoluteFile()); + } } public Configuration getFinalConfiguration() { + CombinedConfiguration ccRoot = new CombinedConfiguration(new MergeCombiner()); ArrayList<Configuration> tempList = new ArrayList<>(rootConfig.values()); tempList.sort(this::sortForMerge); for (Configuration conf : tempList) { ccRoot.addConfiguration(conf); } + CombinedConfiguration ccMergeRoot = new CombinedConfiguration(new MergeCombiner()); ccMergeRoot.addConfiguration(ccRoot); tempList = new ArrayList<>(mergeConfig.values()); @@ -102,11 +105,13 @@ public final class AggregateConfiguration { for (Configuration conf : tempList) { ccMergeRoot.addConfiguration(conf); } + CombinedConfiguration ccUnionRoot = new CombinedConfiguration(new UnionCombiner()); ccUnionRoot.addConfiguration(ccMergeRoot); for (Configuration conf : unionConfig.values()) { ccUnionRoot.addConfiguration(conf); } + ArrayList<Configuration> tempOverrideConfigs = new ArrayList<>(overrideConfig.values()); Collections.reverse(tempOverrideConfigs); tempOverrideConfigs.sort(this::sortForOverride); @@ -114,31 +119,32 @@ public final class AggregateConfiguration { for (Configuration conf : tempOverrideConfigs) { ccOverrideRoot.addConfiguration(conf); } + ccOverrideRoot.addConfiguration(ccUnionRoot); return ccOverrideRoot; } private int sortForOverride(Configuration conf1, Configuration conf2) { - String order1 = conf1.getString(LOAD_ORDER_KEY); - String order2 = conf2.getString(LOAD_ORDER_KEY); - if (ConfigurationUtils.isBlank(order1) || !order1.trim().matches("\\d+")) { - order1 = "0"; - } - if (ConfigurationUtils.isBlank(order2) || !order2.trim().matches("\\d+")) { - order2 = "0"; - } - return Integer.parseInt(order2.trim()) - Integer.parseInt(order1.trim()); + return sort(conf1, conf2, (o1, o2) -> o2 - o1); } private int sortForMerge(Configuration conf1, Configuration conf2) { - String order1 = conf1.getString(LOAD_ORDER_KEY); - String order2 = conf2.getString(LOAD_ORDER_KEY); - if (ConfigurationUtils.isBlank(order1) || !order1.trim().matches("\\d+")) { - order1 = "0"; - } - if (ConfigurationUtils.isBlank(order2) || !order2.trim().matches("\\d+")) { - order2 = "0"; + return sort(conf1, conf2, (o1, o2) -> o1 - o2); + } + + private int sort(Configuration conf1, Configuration conf2, Comparator<Integer> comparator) { + int order1 = readLoadOrder(conf1); + int order2 = readLoadOrder(conf2); + return comparator.compare(order1, order2); + } + + private int readLoadOrder(Configuration conf) { + + String order = conf.getString(LOAD_ORDER_KEY); + if (ConfigurationUtils.isBlank(order) || !order.trim().matches("\\d+")) { + return 0; } - return Integer.parseInt(order1.trim()) - Integer.parseInt(order2.trim()); + + return Integer.parseInt(order.trim()); } } diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/CliConfigurationImpl.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/CliConfigurationImpl.java index 77d0c11c9f..03b2075bac 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/CliConfigurationImpl.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/CliConfigurationImpl.java @@ -19,24 +19,19 @@ package org.onap.config.impl; import static org.onap.config.Constants.DEFAULT_NAMESPACE; import static org.onap.config.Constants.DEFAULT_TENANT; import static org.onap.config.Constants.LOAD_ORDER_KEY; -import static org.onap.config.Constants.MBEAN_NAME; import static org.onap.config.Constants.MODE_KEY; import static org.onap.config.Constants.NAMESPACE_KEY; -import java.lang.management.ManagementFactory; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Set; -import javax.management.MBeanServer; -import javax.management.ObjectName; -import javax.management.StandardMBean; import org.onap.config.ConfigurationUtils; import org.onap.config.api.ConfigurationManager; import org.onap.config.api.Hint; @@ -44,13 +39,10 @@ import org.onap.config.type.ConfigurationQuery; public final class CliConfigurationImpl extends ConfigurationImpl implements ConfigurationManager { + private static final List<String> KEYS_TO_FILTER = Arrays.asList(NAMESPACE_KEY, MODE_KEY, LOAD_ORDER_KEY); + public CliConfigurationImpl() throws Exception { - MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - ObjectName name = new ObjectName(MBEAN_NAME); - if (mbs.isRegistered(name)) { - mbs.unregisterMBean(name); - } - mbs.registerMBean(new StandardMBean(this, ConfigurationManager.class), name); + super(); } public String getConfigurationValue(Map<String, Object> input) { @@ -58,40 +50,51 @@ public final class CliConfigurationImpl extends ConfigurationImpl implements Con } private String getConfigurationValue(ConfigurationQuery queryData) { + try { + + Hint[] hints = getHints(queryData); + + String[] value; if (queryData.isFallback()) { - return ConfigurationUtils.getCommaSeparatedList( - get(queryData.getTenant(), queryData.getNamespace(), queryData.getKey(), String[].class, - queryData.isLatest() ? Hint.LATEST_LOOKUP : Hint.DEFAULT, - queryData.isExternalLookup() ? Hint.EXTERNAL_LOOKUP : Hint.DEFAULT, - queryData.isNodeSpecific() ? Hint.NODE_SPECIFIC : Hint.DEFAULT)); + value = get(queryData.getTenant(), queryData.getNamespace(), queryData.getKey(), String[].class, hints); } else { - String[] list = - getInternal(queryData.getTenant(), queryData.getNamespace(), queryData.getKey(), String[].class, - queryData.isLatest() ? Hint.LATEST_LOOKUP : Hint.DEFAULT, - queryData.isExternalLookup() ? Hint.EXTERNAL_LOOKUP : Hint.DEFAULT, - queryData.isNodeSpecific() ? Hint.NODE_SPECIFIC : Hint.DEFAULT); - return ConfigurationUtils - .getCommaSeparatedList(list == null ? Collections.emptyList() : Arrays.asList(list)); + value = getInternal(queryData.getTenant(), queryData.getNamespace(), queryData.getKey(), String[].class, + hints); } + + return ConfigurationUtils.getCommaSeparatedList(value); + } catch (Exception exception) { exception.printStackTrace(); } + return null; } + private Hint[] getHints(ConfigurationQuery query) { + List<Hint> hints = new ArrayList<>(Hint.values().length); + hints.add(query.isLatest() ? Hint.LATEST_LOOKUP : Hint.DEFAULT); + hints.add(query.isExternalLookup() ? Hint.EXTERNAL_LOOKUP : Hint.DEFAULT); + hints.add(query.isNodeSpecific() ? Hint.NODE_SPECIFIC : Hint.DEFAULT); + return hints.toArray(new Hint[0]); + } + private Object getInput(Map<String, Object> input) { Object toReturn = null; try { + toReturn = Class.forName(input.get("ImplClass").toString()).newInstance(); + Method[] methods = toReturn.getClass().getMethods(); for (Method method : methods) { if (input.containsKey(method.getName())) { method.invoke(toReturn, input.get(method.getName())); } } + } catch (Exception exception) { exception.printStackTrace(); } @@ -104,16 +107,21 @@ public final class CliConfigurationImpl extends ConfigurationImpl implements Con } private Map<String, String> listConfiguration(ConfigurationQuery query) { + Map<String, String> map = new HashMap<>(); + try { + Collection<String> keys = getKeys(query.getTenant(), query.getNamespace()); for (String key : keys) { map.put(key, getConfigurationValue(query.key(key))); } + } catch (Exception exception) { exception.printStackTrace(); return null; } + return map; } @@ -122,13 +130,16 @@ public final class CliConfigurationImpl extends ConfigurationImpl implements Con ArrayList<String> keys = new ArrayList<>(); try { + Iterator<String> iter = ConfigurationRepository.lookup().getConfigurationFor(tenant, namespace).getKeys(); while (iter.hasNext()) { + String key = iter.next(); - if (!(key.equals(NAMESPACE_KEY) || key.equals(MODE_KEY) || key.equals(LOAD_ORDER_KEY))) { + if (!KEYS_TO_FILTER.contains(key)) { keys.add(key); } } + } catch (Exception exception) { //do nothing } diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationImpl.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationImpl.java index 9a93801144..8f8a39d82c 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationImpl.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationImpl.java @@ -16,6 +16,8 @@ package org.onap.config.impl; +import static org.onap.config.ConfigurationUtils.isBlank; + import java.io.File; import java.lang.reflect.Constructor; import java.lang.reflect.Field; @@ -40,8 +42,6 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { private static final String KEY_CANNOT_BE_NULL = "Key can't be null."; - private static final ThreadLocal<String> TENANT = ThreadLocal.withInitial(() -> Constants.DEFAULT_TENANT); - private static final Object LOCK = new Object(); private static boolean instantiated = false; @@ -55,8 +55,8 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { private void init() throws Exception { - if (instantiated || !CliConfigurationImpl.class.isAssignableFrom(this.getClass())) { - throw new RuntimeException("Illegal access to configuration."); + if (instantiated) { + return; } Map<String, AggregateConfiguration> moduleConfigStore = new HashMap<>(); @@ -76,7 +76,7 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { } } String configLocation = System.getProperty("config.location"); - if (configLocation != null && configLocation.trim().length() > 0) { + if (!isBlank(configLocation)) { File root = new File(configLocation); Collection<File> filesystemResources = ConfigurationUtils.getAllFiles(root, true, false); Predicate<File> filePredicate = ConfigurationUtils::isConfig; @@ -95,7 +95,7 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { } } String tenantConfigLocation = System.getProperty("tenant.config.location"); - if (tenantConfigLocation != null && tenantConfigLocation.trim().length() > 0) { + if (!isBlank(tenantConfigLocation)) { File root = new File(tenantConfigLocation); Collection<File> tenantsRoot = ConfigurationUtils.getAllFiles(root, false, true); Collection<File> filesystemResources = ConfigurationUtils.getAllFiles(root, true, false); @@ -122,7 +122,7 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { populateFinalConfigurationIncrementally(moduleConfigStore); String nodeConfigLocation = System.getProperty("node.config.location"); - if (nodeConfigLocation != null && nodeConfigLocation.trim().length() > 0) { + if (!isBlank(nodeConfigLocation)) { File root = new File(nodeConfigLocation); Collection<File> filesystemResources = ConfigurationUtils.getAllFiles(root, true, false); Predicate<File> filePredicate = ConfigurationUtils::isConfig; @@ -201,16 +201,8 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { @Override public <T> Map<String, T> populateMap(String tenantId, String namespace, String key, Class<T> clazz) { - if (tenantId == null || tenantId.trim().length() == 0) { - tenantId = TENANT.get(); - } else { - tenantId = tenantId.toUpperCase(); - } - if (namespace == null || namespace.trim().length() == 0) { - namespace = Constants.DEFAULT_NAMESPACE; - } else { - namespace = namespace.toUpperCase(); - } + tenantId = calculateTenant(tenantId); + namespace = calculateNamespace(namespace); Map<String, T> map = new HashMap<>(); Iterator<String> keys; try { @@ -234,21 +226,14 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { @Override public Map generateMap(String tenantId, String namespace, String key) { - if (tenantId == null || tenantId.trim().length() == 0) { - tenantId = TENANT.get(); - } else { - tenantId = tenantId.toUpperCase(); - } - if (namespace == null || namespace.trim().length() == 0) { - namespace = Constants.DEFAULT_NAMESPACE; - } else { - namespace = namespace.toUpperCase(); - } + tenantId = calculateTenant(tenantId); + namespace = calculateNamespace(namespace); + Map map; Map parentMap = new HashMap<>(); Iterator<String> keys; try { - if (key == null || key.trim().length() == 0) { + if (isBlank(key)) { keys = ConfigurationRepository.lookup().getConfigurationFor(tenantId, namespace).getKeys(); } else { keys = ConfigurationRepository.lookup().getConfigurationFor(tenantId, namespace).getKeys(key); @@ -257,11 +242,11 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { map = parentMap; String k = keys.next(); - if (key != null && key.trim().length() != 0 && !k.startsWith(key + ".")) { + if (!isBlank(key) && !k.startsWith(key + ".")) { continue; } String value = getAsString(tenantId, namespace, k); - if (key != null && key.trim().length() != 0 && k.startsWith(key + ".")) { + if (!isBlank(key) && k.startsWith(key + ".")) { k = k.substring(key.trim().length() + 1); } @@ -292,22 +277,17 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { } } - if (tenant == null || tenant.trim().length() == 0) { - tenant = TENANT.get(); - } else { - tenant = tenant.toUpperCase(); - } - if (namespace == null || namespace.trim().length() == 0) { - namespace = Constants.DEFAULT_NAMESPACE; - } else { - namespace = namespace.toUpperCase(); - } - if ((key == null || key.trim().length() == 0) && !clazz.isAnnotationPresent(Config.class)) { + tenant = calculateTenant(tenant); + namespace = calculateNamespace(namespace); + + if (isBlank(key) && !clazz.isAnnotationPresent(Config.class)) { throw new IllegalArgumentException(KEY_CANNOT_BE_NULL); } + if (clazz == null) { throw new IllegalArgumentException("clazz is null."); } + if (clazz.isPrimitive()) { clazz = getWrapperClass(clazz); } @@ -351,8 +331,7 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { return null; } } else if (clazz.isAnnotationPresent(Config.class)) { - return read(tenant, namespace, clazz, (key == null || key.trim().length() == 0) ? "" : (key + "."), - hints); + return read(tenant, namespace, clazz, isBlank(key) ? "" : (key + "."), hints); } else { throw new IllegalArgumentException( "Only primitive classes, wrapper classes, corresponding array classes and any " @@ -364,6 +343,24 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { return null; } + private static String calculateNamespace(String namespace) { + + if (isBlank(namespace)) { + return Constants.DEFAULT_NAMESPACE; + } + + return namespace.toUpperCase(); + } + + private static String calculateTenant(String tenant) { + + if (isBlank(tenant)) { + return Constants.DEFAULT_TENANT; + } + + return tenant.toUpperCase(); + } + private <T> T read(String tenant, String namespace, Class<T> clazz, String keyPrefix, Hint... hints) throws Exception { Config confAnnotation = clazz.getAnnotation(Config.class); @@ -448,11 +445,13 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { } private <T> T getValue(Object obj, Class<T> clazz, int processingHint) { + if (obj == null || obj.toString().trim().length() == 0) { return null; } else { obj = obj.toString().trim(); } + if (String.class.equals(clazz)) { if (obj.toString().startsWith("@") && ConfigurationUtils.isExternalLookup(processingHint)) { String contents = ConfigurationUtils.getFileContents( @@ -535,9 +534,9 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { return (T[]) obj; } - private <T> Collection<T> convert(String commaSaperatedValues, Class<T> clazz, int processingHints) { + private <T> Collection<T> convert(String commaSeparatedValues, Class<T> clazz, int processingHints) { ArrayList<T> collection = new ArrayList<>(); - for (String value : commaSaperatedValues.split(",")) { + for (String value : commaSeparatedValues.split(",")) { try { T type1 = getValue(value, clazz, processingHints); if (type1 != null) { diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationRepository.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationRepository.java index 5b950d1af6..5863caef68 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationRepository.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationRepository.java @@ -18,7 +18,6 @@ package org.onap.config.impl; import java.io.File; import java.sql.Timestamp; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashSet; @@ -31,7 +30,6 @@ import org.apache.commons.configuration2.Configuration; import org.apache.commons.configuration2.FileBasedConfiguration; import org.apache.commons.configuration2.builder.BasicConfigurationBuilder; import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder; -import org.apache.commons.configuration2.ex.ConfigurationException; import org.onap.config.ConfigurationUtils; import org.onap.config.Constants; @@ -105,29 +103,16 @@ public final class ConfigurationRepository { } public void populateOverrideConfiguration(String key, File file) { + ConfigurationHolder holder = store.get(key); + if (holder == null) { holder = new ConfigurationHolder(new CombinedConfiguration()); store.put(key, holder); } - holder.addOverrideConfiguration(file.getAbsolutePath(), ConfigurationUtils.getConfigurationBuilder(file, true)); - populateTenantsNamespace(key); - } - public void refreshOverrideConfigurationFor(String key, int index) { - ConfigurationHolder holder = store.get(key); - if (holder != null) { - holder.refreshOverrideConfiguration(index); - } - } - - public void removeOverrideConfiguration(File file) { - for (String s : (Iterable<String>) new ArrayList(store.keySet())) { - ConfigurationHolder holder = store.get(s); - if (holder.containsOverrideConfiguration(file.getAbsolutePath())) { - holder.removeOverrideConfiguration(file.getAbsolutePath()); - } - } + holder.addOverrideConfiguration(file.getAbsolutePath(), ConfigurationUtils.getConfigurationBuilder(file)); + populateTenantsNamespace(key); } private class ConfigurationHolder { @@ -147,20 +132,6 @@ public final class ConfigurationRepository { this.config = builder; } - public void refreshOverrideConfiguration(int index) { - int count = -1; - for (FileBasedConfigurationBuilder overrides : overrideConfiguration.values()) { - try { - if (++count == index) { - overrides.save(); - overrides.resetResult(); - } - } catch (ConfigurationException exception) { - //do nothing - } - } - } - public void addOverrideConfiguration(String path, BasicConfigurationBuilder<FileBasedConfiguration> builder) { overrideConfiguration.put(path.toUpperCase(), (FileBasedConfigurationBuilder) builder); getEffectiveConfiguration(config, overrideConfiguration.values()); @@ -181,15 +152,6 @@ public final class ConfigurationRepository { } } - public void removeOverrideConfiguration(String path) { - overrideConfiguration.remove(path.toUpperCase()); - getEffectiveConfiguration(config, overrideConfiguration.values()); - } - - public boolean containsOverrideConfiguration(String path) { - return overrideConfiguration.containsKey(path.toUpperCase()); - } - public Configuration getConfiguration(String namespace) throws Exception { if (config == null) { |