From 461e964344d01e245464980b6ace12e4b28569e6 Mon Sep 17 00:00:00 2001 From: vempo Date: Wed, 31 Oct 2018 10:27:59 +0200 Subject: 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 --- .../org/onap/config/impl/ConfigurationImpl.java | 89 +++++++++++----------- 1 file changed, 44 insertions(+), 45 deletions(-) (limited to 'common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationImpl.java') 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 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 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 filesystemResources = ConfigurationUtils.getAllFiles(root, true, false); Predicate 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 tenantsRoot = ConfigurationUtils.getAllFiles(root, false, true); Collection 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 filesystemResources = ConfigurationUtils.getAllFiles(root, true, false); Predicate filePredicate = ConfigurationUtils::isConfig; @@ -201,16 +201,8 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { @Override public Map populateMap(String tenantId, String namespace, String key, Class 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 map = new HashMap<>(); Iterator 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 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 read(String tenant, String namespace, Class 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 getValue(Object obj, Class 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 Collection convert(String commaSaperatedValues, Class clazz, int processingHints) { + private Collection convert(String commaSeparatedValues, Class clazz, int processingHints) { ArrayList collection = new ArrayList<>(); - for (String value : commaSaperatedValues.split(",")) { + for (String value : commaSeparatedValues.split(",")) { try { T type1 = getValue(value, clazz, processingHints); if (type1 != null) { -- cgit 1.2.3-korg