diff options
author | vasraz <vasyl.razinkov@est.tech> | 2021-03-27 22:58:29 +0000 |
---|---|---|
committer | Christophe Closset <christophe.closset@intl.att.com> | 2021-03-29 07:15:14 +0000 |
commit | 04c236567737c965545f64c9542a7d75ed6f9046 (patch) | |
tree | 8d2a3ea1bcb80b8d66603bb31a8b6877a98d84f8 /common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl | |
parent | be66acfea37b8d96cbb1b1e14592de57ec3bc4ae (diff) |
Reformat common
Signed-off-by: Vasyl Razinkov <vasyl.razinkov@est.tech>
Issue-ID: SDC-3449
Change-Id: Ifa1412b8a614f7cdb4c52f05d811ab5e9cf20d83
Diffstat (limited to 'common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl')
7 files changed, 146 insertions, 252 deletions
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 07c81e5f5b..0621561dcc 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 @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.onap.config.impl; import static org.onap.config.Constants.LOAD_ORDER_KEY; @@ -44,17 +43,14 @@ public final class AggregateConfiguration { private final Map<String, Configuration> overrideConfig = Collections.synchronizedMap(new LinkedHashMap<>()); public void addConfig(File file) throws ConfigurationException { - addConfig(fileToUrl(file), ConfigurationUtils.getMergeStrategy(file), - ConfigurationUtils.getConfigurationBuilder(file).getConfiguration()); + addConfig(fileToUrl(file), ConfigurationUtils.getMergeStrategy(file), ConfigurationUtils.getConfigurationBuilder(file).getConfiguration()); } public void addConfig(URL url) throws ConfigurationException { - addConfig(url, ConfigurationUtils.getMergeStrategy(url), - ConfigurationUtils.getConfigurationBuilder(url).getConfiguration()); + 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) { @@ -76,28 +72,24 @@ public final class AggregateConfiguration { private String normalize(URL url) { // what about Linux where paths are case sensitive? - return url.toString().toUpperCase(); + return url.toString().toUpperCase(); } private URL fileToUrl(File file) { - try { - return file.getAbsoluteFile().toURI().toURL(); + 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()); + 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()); @@ -105,13 +97,11 @@ 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); @@ -119,7 +109,6 @@ public final class AggregateConfiguration { for (Configuration conf : tempOverrideConfigs) { ccOverrideRoot.addConfiguration(conf); } - ccOverrideRoot.addConfiguration(ccUnionRoot); return ccOverrideRoot; } @@ -139,12 +128,10 @@ public final class AggregateConfiguration { } 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(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 f919e234f0..3ddf0a9079 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 @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.onap.config.impl; import static org.onap.config.Constants.DEFAULT_NAMESPACE; @@ -59,8 +58,7 @@ public final class CliConfigurationImpl extends ConfigurationImpl implements Con if (queryData.isFallback()) { value = get(queryData.getTenant(), queryData.getNamespace(), queryData.getKey(), String[].class, hints); } else { - value = getInternal(queryData.getTenant(), queryData.getNamespace(), queryData.getKey(), String[].class, - hints); + value = getInternal(queryData.getTenant(), queryData.getNamespace(), queryData.getKey(), String[].class, hints); } return ConfigurationUtils.getCommaSeparatedList(value); } catch (Exception exception) { @@ -130,11 +128,7 @@ public final class CliConfigurationImpl extends ConfigurationImpl implements Con } } } catch (Exception exception) { - LOGGER.warn("Error occurred while searching for in-memory keys for namespace: '{}' and tenant: '{}'", - namespace, - tenant, - exception - ); + LOGGER.warn("Error occurred while searching for in-memory keys for namespace: '{}' and tenant: '{}'", namespace, tenant, exception); } return keys; } diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationFilter.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationFilter.java index 06981e4b3b..ea48d3fe01 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationFilter.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationFilter.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.onap.config.impl; import java.io.IOException; @@ -27,7 +26,6 @@ import javax.servlet.annotation.WebFilter; import org.onap.config.Constants; import org.onap.config.api.Configuration; - @WebFilter("/") public class ConfigurationFilter implements Filter { @@ -37,8 +35,8 @@ public class ConfigurationFilter implements Filter { } @Override - public void doFilter(ServletRequest paramServletRequest, ServletResponse paramServletResponse, - FilterChain paramFilterChain) throws IOException, ServletException { + public void doFilter(ServletRequest paramServletRequest, ServletResponse paramServletResponse, FilterChain paramFilterChain) + throws IOException, ServletException { Configuration.TENANT.set(Constants.DEFAULT_TENANT); try { paramFilterChain.doFilter(paramServletRequest, paramServletResponse); @@ -51,5 +49,4 @@ public class ConfigurationFilter implements Filter { public void destroy() { //Use the default behavior } - } 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 5d49fa970f..964b8d1a2d 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 @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.onap.config.impl; import static org.onap.config.ConfigurationUtils.isBlank; @@ -36,7 +35,6 @@ import java.util.Set; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Predicate; import java.util.stream.Collectors; - import org.apache.commons.configuration2.ex.ConfigurationException; import org.onap.config.ConfigurationUtils; import org.onap.config.Constants; @@ -49,115 +47,18 @@ import org.slf4j.LoggerFactory; public class ConfigurationImpl implements org.onap.config.api.Configuration { private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationImpl.class); - private static final String KEY_CANNOT_BE_NULL = "Key can't be null."; private static final NonConfigResource NON_CONFIG_RESOURCE = new NonConfigResource(); private static final Map<String, AggregateConfiguration> MODULE_CONFIG_STORE = new HashMap<>(); static { - if (!loadClassPathConfigurationsAndResources() - || !loadAdditionalConfigurationsAndResources() - || !loadTenantConfigurations()) { + if (!loadClassPathConfigurationsAndResources() || !loadAdditionalConfigurationsAndResources() || !loadTenantConfigurations()) { throw new IllegalStateException("Failed to initialize configuration"); } populateFinalConfigurationIncrementally(MODULE_CONFIG_STORE); loadNodeSpecificConfigurations(); } - @Override - public <T> T get(String tenant, String namespace, String key, Class<T> clazz, Hint... hints) { - String[] tenantNamespaceArray; - if (tenant == null && namespace != null) { - tenantNamespaceArray = namespace.split(Constants.TENANT_NAMESPACE_SEPARATOR); - if (tenantNamespaceArray.length > 1) { - tenant = tenantNamespaceArray[0]; - namespace = tenantNamespaceArray[1]; - } - } - - tenant = ConfigurationRepository.lookup().isValidTenant(tenant) ? tenant.toUpperCase() : Constants.DEFAULT_TENANT; - namespace = ConfigurationRepository.lookup().isValidNamespace(namespace) ? namespace.toUpperCase() : Constants.DEFAULT_NAMESPACE; - hints = hints == null || hints.length == 0 ? new Hint[]{Hint.EXTERNAL_LOOKUP, Hint.NODE_SPECIFIC} : hints; - T returnValue; - returnValue = getInternal(tenant, namespace, key, clazz, hints); - if ((returnValue == null || ConfigurationUtils.isZeroLengthArray(clazz, returnValue)) - && !Constants.DEFAULT_TENANT.equals(tenant)) { - returnValue = getInternal(Constants.DEFAULT_TENANT, namespace, key, clazz, hints); - } - if ((returnValue == null || ConfigurationUtils.isZeroLengthArray(clazz, returnValue)) - && !Constants.DEFAULT_NAMESPACE.equals(namespace)) { - returnValue = getInternal(tenant, Constants.DEFAULT_NAMESPACE, key, clazz, hints); - } - if ((returnValue == null || ConfigurationUtils.isZeroLengthArray(clazz, returnValue)) - && !Constants.DEFAULT_NAMESPACE.equals(namespace) && !Constants.DEFAULT_TENANT.equals(tenant)) { - returnValue = getInternal(Constants.DEFAULT_TENANT, Constants.DEFAULT_NAMESPACE, key, clazz, hints); - } - if (returnValue == null && ConfigurationUtils.isAPrimitive(clazz)) { - returnValue = (T) ConfigurationUtils.getDefaultFor(clazz); - } - return returnValue; - } - - @Override - public <T> Map<String, T> populateMap(String tenantId, String namespace, String key, Class<T> clazz) { - final String calculatedTenantId = calculateTenant(tenantId); - final String calculatedNamespace = calculateNamespace(namespace); - Map<String, T> map = new HashMap<>(); - Iterator<String> keys; - try { - keys = ConfigurationRepository.lookup().getConfigurationFor(calculatedTenantId, calculatedNamespace).getKeys(key); - keys.forEachRemaining(k -> { - if (k.startsWith(key + ".")) { - k = k.substring(key.length() + 1); - String subkey = k.substring(0, k.indexOf('.')); - if (!map.containsKey(subkey)) { - map.put(subkey, get(calculatedTenantId, calculatedNamespace, key + "." + subkey, clazz)); - } - } - }); - } catch (Exception e) { - LOGGER.warn( - "Couldn't populate map fot tenant: {}, namespace: {}, key: {}, type: {}", - tenantId, - namespace, - key, - clazz.getSimpleName(), - e - ); - } - return map; - } - - @Override - public Map<Object, Object> generateMap(String tenantId, String namespace, String key) { - final String calculatedTenantId = calculateTenant(tenantId); - final String calculatedNamespace = calculateNamespace(namespace); - Map<Object, Object> parentMap = new HashMap<>(); - Iterator<String> configKeys; - try { - if (isBlank(key)) { - configKeys = ConfigurationRepository.lookup().getConfigurationFor(calculatedTenantId, calculatedNamespace).getKeys(); - } else { - configKeys = ConfigurationRepository.lookup().getConfigurationFor(calculatedTenantId, calculatedNamespace).getKeys(key); - } - configKeys.forEachRemaining(subKey -> { - if (!isBlank(key) && !subKey.startsWith(key + ".")) { - configKeys.remove(); - } - parseConfigSubKeys(subKey, key, calculatedTenantId, calculatedNamespace, parentMap); - }); - } catch (Exception e) { - LOGGER.warn( - "Couldn't generate map fot tenant: {}, namespace: {}, key: {}", - tenantId, - namespace, - key, - e - ); - } - return parentMap; - } - private static boolean loadClassPathConfigurationsAndResources() { List<URL> classpathResources = ConfigurationUtils.getAllClassPathResources(); Predicate<URL> predicate = ConfigurationUtils::isConfig; @@ -165,11 +66,8 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { List<URL> configResources = resources.get(true); List<URL> nonConfigResources = resources.get(false); AtomicReference<Boolean> successFlagHolder = new AtomicReference<>(true); - configResources.forEach(url -> successFlagHolder.set(setUpdateModuleConfigStore(url))); - nonConfigResources.forEach(NON_CONFIG_RESOURCE::add); - return successFlagHolder.get(); } @@ -183,9 +81,7 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { Map<Boolean, List<File>> resources = filesystemResources.stream().collect(Collectors.partitioningBy(filePredicate)); List<File> configResources = resources.get(true); List<File> nonConfigResources = resources.get(false); - configResources.forEach(file -> successFlagHolder.set(setUpdateModuleConfigStore(file))); - nonConfigResources.forEach(NON_CONFIG_RESOURCE::add); } return successFlagHolder.get(); @@ -198,20 +94,17 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { File root = new File(tenantConfigLocation); Collection<File> tenantsRoot = ConfigurationUtils.getAllFiles(root, false, true); Collection<File> filesystemResources = ConfigurationUtils.getAllFiles(root, true, false); - Map<Boolean, List<File>> resources = filesystemResources.stream().collect(Collectors.partitioningBy(ConfigurationUtils::isConfig)); Collection<File> tenantResources = resources.get(true); - tenantResources.forEach(configFile -> { AtomicReference<String> moduleNameHolder = new AtomicReference<>(ConfigurationUtils.getNamespace(configFile)); Predicate<File> startsWithRootPredicate = tenantRoot -> configFile.getAbsolutePath().startsWith(tenantRoot.getAbsolutePath()); - Collection<File> matchesTenantRoot = tenantsRoot.stream().filter(startsWithRootPredicate).collect(Collectors.toCollection(ArrayList::new)); + Collection<File> matchesTenantRoot = tenantsRoot.stream().filter(startsWithRootPredicate) + .collect(Collectors.toCollection(ArrayList::new)); AtomicReference<String[]> altResource = new AtomicReference<>(); matchesTenantRoot.forEach(file -> altResource.set( - (file.getName().toUpperCase() + Constants.TENANT_NAMESPACE_SEPARATOR + moduleNameHolder.get()) - .split(Constants.TENANT_NAMESPACE_SEPARATOR) - ) - ); + (file.getName().toUpperCase() + Constants.TENANT_NAMESPACE_SEPARATOR + moduleNameHolder.get()) + .split(Constants.TENANT_NAMESPACE_SEPARATOR))); successFlagHolder.set(setUpdateModuleConfigStore(configFile, altResource.get())); }); } @@ -223,25 +116,20 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { if (!isBlank(nodeConfigLocation)) { File root = new File(nodeConfigLocation); Collection<File> filesystemResources = ConfigurationUtils.getAllFiles(root, true, false); - filesystemResources.stream().filter(ConfigurationUtils::isConfig).forEach( - file -> ConfigurationRepository.lookup().populateOverrideConfiguration( - ConfigurationUtils.getConfigurationRepositoryKey( - ConfigurationUtils.getNamespace(file).split(Constants.TENANT_NAMESPACE_SEPARATOR)), file) - ); + filesystemResources.stream().filter(ConfigurationUtils::isConfig).forEach(file -> ConfigurationRepository.lookup() + .populateOverrideConfiguration(ConfigurationUtils + .getConfigurationRepositoryKey(ConfigurationUtils.getNamespace(file).split(Constants.TENANT_NAMESPACE_SEPARATOR)), file)); } } private static void populateFinalConfigurationIncrementally(Map<String, AggregateConfiguration> configs) { if (configs.get(Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMITER + Constants.DB_NAMESPACE) != null) { - ConfigurationRepository.lookup().populateConfiguration( - Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMITER + Constants.DB_NAMESPACE, - configs.remove(Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMITER + Constants.DB_NAMESPACE) - .getFinalConfiguration()); + ConfigurationRepository.lookup() + .populateConfiguration(Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMITER + Constants.DB_NAMESPACE, + configs.remove(Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMITER + Constants.DB_NAMESPACE).getFinalConfiguration()); } Set<String> modules = configs.keySet(); - modules.forEach( - m -> ConfigurationRepository.lookup().populateConfiguration(m, configs.get(m).getFinalConfiguration()) - ); + modules.forEach(m -> ConfigurationRepository.lookup().populateConfiguration(m, configs.get(m).getFinalConfiguration())); } private static <T> boolean setUpdateModuleConfigStore(T resource, String... namedResources) { @@ -292,6 +180,98 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { } } + 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(); + } + + @Override + public <T> T get(String tenant, String namespace, String key, Class<T> clazz, Hint... hints) { + String[] tenantNamespaceArray; + if (tenant == null && namespace != null) { + tenantNamespaceArray = namespace.split(Constants.TENANT_NAMESPACE_SEPARATOR); + if (tenantNamespaceArray.length > 1) { + tenant = tenantNamespaceArray[0]; + namespace = tenantNamespaceArray[1]; + } + } + tenant = ConfigurationRepository.lookup().isValidTenant(tenant) ? tenant.toUpperCase() : Constants.DEFAULT_TENANT; + namespace = ConfigurationRepository.lookup().isValidNamespace(namespace) ? namespace.toUpperCase() : Constants.DEFAULT_NAMESPACE; + hints = hints == null || hints.length == 0 ? new Hint[]{Hint.EXTERNAL_LOOKUP, Hint.NODE_SPECIFIC} : hints; + T returnValue; + returnValue = getInternal(tenant, namespace, key, clazz, hints); + if ((returnValue == null || ConfigurationUtils.isZeroLengthArray(clazz, returnValue)) && !Constants.DEFAULT_TENANT.equals(tenant)) { + returnValue = getInternal(Constants.DEFAULT_TENANT, namespace, key, clazz, hints); + } + if ((returnValue == null || ConfigurationUtils.isZeroLengthArray(clazz, returnValue)) && !Constants.DEFAULT_NAMESPACE.equals(namespace)) { + returnValue = getInternal(tenant, Constants.DEFAULT_NAMESPACE, key, clazz, hints); + } + if ((returnValue == null || ConfigurationUtils.isZeroLengthArray(clazz, returnValue)) && !Constants.DEFAULT_NAMESPACE.equals(namespace) + && !Constants.DEFAULT_TENANT.equals(tenant)) { + returnValue = getInternal(Constants.DEFAULT_TENANT, Constants.DEFAULT_NAMESPACE, key, clazz, hints); + } + if (returnValue == null && ConfigurationUtils.isAPrimitive(clazz)) { + returnValue = (T) ConfigurationUtils.getDefaultFor(clazz); + } + return returnValue; + } + + @Override + public <T> Map<String, T> populateMap(String tenantId, String namespace, String key, Class<T> clazz) { + final String calculatedTenantId = calculateTenant(tenantId); + final String calculatedNamespace = calculateNamespace(namespace); + Map<String, T> map = new HashMap<>(); + Iterator<String> keys; + try { + keys = ConfigurationRepository.lookup().getConfigurationFor(calculatedTenantId, calculatedNamespace).getKeys(key); + keys.forEachRemaining(k -> { + if (k.startsWith(key + ".")) { + k = k.substring(key.length() + 1); + String subkey = k.substring(0, k.indexOf('.')); + if (!map.containsKey(subkey)) { + map.put(subkey, get(calculatedTenantId, calculatedNamespace, key + "." + subkey, clazz)); + } + } + }); + } catch (Exception e) { + LOGGER.warn("Couldn't populate map fot tenant: {}, namespace: {}, key: {}, type: {}", tenantId, namespace, key, clazz.getSimpleName(), e); + } + return map; + } + + @Override + public Map<Object, Object> generateMap(String tenantId, String namespace, String key) { + final String calculatedTenantId = calculateTenant(tenantId); + final String calculatedNamespace = calculateNamespace(namespace); + Map<Object, Object> parentMap = new HashMap<>(); + Iterator<String> configKeys; + try { + if (isBlank(key)) { + configKeys = ConfigurationRepository.lookup().getConfigurationFor(calculatedTenantId, calculatedNamespace).getKeys(); + } else { + configKeys = ConfigurationRepository.lookup().getConfigurationFor(calculatedTenantId, calculatedNamespace).getKeys(key); + } + configKeys.forEachRemaining(subKey -> { + if (!isBlank(key) && !subKey.startsWith(key + ".")) { + configKeys.remove(); + } + parseConfigSubKeys(subKey, key, calculatedTenantId, calculatedNamespace, parentMap); + }); + } catch (Exception e) { + LOGGER.warn("Couldn't generate map fot tenant: {}, namespace: {}, key: {}", tenantId, namespace, key, e); + } + return parentMap; + } + private void parseConfigSubKeys(String subKey, String keyPrefix, String tenantId, String namespace, Map<Object, Object> targetMap) { String value = getAsString(tenantId, namespace, subKey); if (!isBlank(keyPrefix) && subKey.startsWith(keyPrefix + ".")) { @@ -318,18 +298,14 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { processingHints = processingHints | hint.value(); } } - 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 (ConfigurationUtils.isAPrimitive(clazz)) { clazz = getWrapperClass(clazz); } @@ -341,43 +317,31 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { } else if (clazz.isAnnotationPresent(Config.class)) { return getAnnotatedTypeValue(tenant, namespace, clazz, isBlank(key) ? "" : (key + "."), hints); } else { - throw new IllegalArgumentException( - "Only primitive classes, wrapper classes, corresponding array classes and any " - + "class decorated with @org.openecomp.config.api.Config are allowed as argument."); + throw new IllegalArgumentException("Only primitive classes, wrapper classes, corresponding array classes and any " + + "class decorated with @org.openecomp.config.api.Config are allowed as argument."); } } catch (Exception exception) { - LOGGER.warn( - "Failed to get internal value fot tenant: {}, namespace: {}, key: {}, type: {}", - tenant, - namespace, - key, - clazz.getSimpleName(), - exception - ); + LOGGER + .warn("Failed to get internal value fot tenant: {}, namespace: {}, key: {}, type: {}", tenant, namespace, key, clazz.getSimpleName(), + exception); } return null; } private <T> T getWrapperTypeValue(String tenant, String namespace, String key, Class<T> clazz, int processingHints) throws Exception { - Object obj = ConfigurationUtils.getProperty( - ConfigurationRepository.lookup().getConfigurationFor(tenant, namespace), key, processingHints); + Object obj = ConfigurationUtils.getProperty(ConfigurationRepository.lookup().getConfigurationFor(tenant, namespace), key, processingHints); if (obj != null) { if (ConfigurationUtils.isCollection(obj.toString())) { obj = ConfigurationUtils.getCollectionString(obj.toString()); } - String value = ConfigurationUtils.processVariablesIfPresent( - tenant, - namespace, - obj.toString().split(",")[0] - ); + String value = ConfigurationUtils.processVariablesIfPresent(tenant, namespace, obj.toString().split(",")[0]); return (T) getTypeValue(value, clazz, processingHints); } return null; } private <T> T getArrayTypeValue(String tenant, String namespace, String key, Class<T> clazz, int processingHints) throws Exception { - Object obj = ConfigurationUtils.getProperty( - ConfigurationRepository.lookup().getConfigurationFor(tenant, namespace), key, processingHints); + Object obj = ConfigurationUtils.getProperty(ConfigurationRepository.lookup().getConfigurationFor(tenant, namespace), key, processingHints); if (obj != null) { Class componentType = clazz.getComponentType(); if (ConfigurationUtils.isAPrimitivesArray(clazz)) { @@ -388,11 +352,8 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { for (String itemValue : collString.split(",")) { tempCollection.add(ConfigurationUtils.processVariablesIfPresent(tenant, namespace, itemValue)); } - Collection<T> collection = convert( - ConfigurationUtils.getCollectionString(Arrays.toString(tempCollection.toArray())), - (Class<T>) componentType, - processingHints - ); + Collection<T> collection = convert(ConfigurationUtils.getCollectionString(Arrays.toString(tempCollection.toArray())), + (Class<T>) componentType, processingHints); if (ConfigurationUtils.isAPrimitivesArray(clazz)) { return (T) ConfigurationUtils.getPrimitiveArray(collection, componentType); } else { @@ -403,27 +364,8 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { } } - 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 getAnnotatedTypeValue(String tenant, String namespace, Class<T> clazz, String keyPrefix, Hint... hints) - throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { - + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { Config confAnnotation = clazz.getAnnotation(Config.class); if (confAnnotation != null && confAnnotation.key().length() > 0 && !keyPrefix.endsWith(".")) { keyPrefix += (confAnnotation.key() + "."); @@ -438,8 +380,7 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { if (fieldConfAnnotation == null) { continue; } - if (ConfigurationUtils.isAPrimitiveOrWrapper(fieldType) || - ConfigurationUtils.isAPrimitivesOrWrappersArray(fieldType)) { + if (ConfigurationUtils.isAPrimitiveOrWrapper(fieldType) || ConfigurationUtils.isAPrimitivesOrWrappersArray(fieldType)) { setPrimitiveField(field, objToReturn, tenant, namespace, keyPrefix, hints); } if (ConfigurationUtils.isACollection(fieldType)) { @@ -453,25 +394,23 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { } private void setPrimitiveField(Field field, Object objToReturn, String tenant, String namespace, String keyPrefix, Hint[] hints) - throws IllegalAccessException { + throws IllegalAccessException { String fieldConfAnnotationKey = field.getAnnotation(Config.class).key(); Class<?> fieldType = field.getType(); field.set(objToReturn, get(tenant, namespace, keyPrefix + fieldConfAnnotationKey, fieldType, hints)); } - private void setMapField(Field field, Object objToReturn, String tenant, String namespace, String keyPrefix) - throws IllegalAccessException { + private void setMapField(Field field, Object objToReturn, String tenant, String namespace, String keyPrefix) throws IllegalAccessException { String fieldConfAnnotationKey = field.getAnnotation(Config.class).key(); field.set(objToReturn, generateMap(tenant, namespace, keyPrefix + fieldConfAnnotationKey)); } private void setCollectionField(Field field, Object objToReturn, String tenant, String namespace, String keyPrefix, Hint[] hints) - throws IllegalAccessException, InvocationTargetException, InstantiationException { + throws IllegalAccessException, InvocationTargetException, InstantiationException { String fieldConfAnnotationKey = field.getAnnotation(Config.class).key(); Class<?> fieldType = field.getType(); Object obj = get(tenant, namespace, keyPrefix + fieldConfAnnotationKey, - ConfigurationUtils.getArrayClass(ConfigurationUtils.getCollectionGenericType(field)), - hints); + ConfigurationUtils.getArrayClass(ConfigurationUtils.getCollectionGenericType(field)), hints); if (obj == null) { return; } @@ -480,20 +419,16 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { if (fieldType.isInterface()) { clazzToInstantiate = ConfigurationUtils.getConcreteCollection(fieldType).getClass(); } else if (Modifier.isAbstract(fieldType.getModifiers())) { - clazzToInstantiate = - ConfigurationUtils.getCompatibleCollectionForAbstractDef(fieldType) - .getClass(); + clazzToInstantiate = ConfigurationUtils.getCompatibleCollectionForAbstractDef(fieldType).getClass(); } else { clazzToInstantiate = fieldType; } Constructor construct = getConstructorWithArguments(clazzToInstantiate, Collection.class); - if (construct != null) { construct.setAccessible(true); field.set(objToReturn, construct.newInstance(list)); } else { - construct = getConstructorWithArguments(clazzToInstantiate, Integer.class, - Boolean.class, Collection.class); + construct = getConstructorWithArguments(clazzToInstantiate, Integer.class, Boolean.class, Collection.class); if (construct != null) { construct.setAccessible(true); field.set(objToReturn, construct.newInstance(list.size(), true, list)); @@ -555,8 +490,7 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { private <T> T getValueForStringType(Object obj, int processingHint) { if (obj.toString().startsWith("@") && ConfigurationUtils.isExternalLookup(processingHint)) { String subString = obj.toString().substring(1).trim(); - String contents = ConfigurationUtils.getFileContents( - NON_CONFIG_RESOURCE.locate(subString)); + String contents = ConfigurationUtils.getFileContents(NON_CONFIG_RESOURCE.locate(subString)); if (contents == null) { contents = ConfigurationUtils.getFileContents(subString); } 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 7b2e267a14..9be4c262b8 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 @@ -14,7 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.onap.config.impl; import java.io.File; @@ -40,26 +39,20 @@ public final class ConfigurationRepository { private static final Logger logger = LoggerFactory.getLogger(ConfigurationRepository.class); private static final ConfigurationRepository REPO = new ConfigurationRepository(); - private final Set<String> tenants = Collections.synchronizedSet(new HashSet<>()); - private final Set<String> namespaces = Collections.synchronizedSet(new HashSet<>()); - - private final Map<String, ConfigurationHolder> store = Collections.synchronizedMap( - - new LinkedHashMap<String, ConfigurationHolder>(16, 0.75f, true) { - - @Override - protected boolean removeEldestEntry(Map.Entry eldest) { - try { - return size() > getConfigurationFor(Constants.DEFAULT_TENANT, Constants.DB_NAMESPACE) - .getInt("config.size.max"); - } catch (Exception exception) { - logger.info("ConfigurationException", exception); - return false; - } + private final Map<String, ConfigurationHolder> store = Collections + .synchronizedMap(new LinkedHashMap<String, ConfigurationHolder>(16, 0.75f, true) { + @Override + protected boolean removeEldestEntry(Map.Entry eldest) { + try { + return size() > getConfigurationFor(Constants.DEFAULT_TENANT, Constants.DB_NAMESPACE).getInt("config.size.max"); + } catch (Exception exception) { + logger.info("ConfigurationException", exception); + return false; } - }); + } + }); private ConfigurationRepository() { tenants.add(Constants.DEFAULT_TENANT); @@ -106,22 +99,18 @@ 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)); populateTenantsNamespace(key); } private class ConfigurationHolder { - private final Map<String, FileBasedConfigurationBuilder<FileBasedConfiguration>> overrideConfiguration = - new LinkedHashMap<>(); + private final Map<String, FileBasedConfigurationBuilder<FileBasedConfiguration>> overrideConfiguration = new LinkedHashMap<>(); private BasicConfigurationBuilder<Configuration> builder; private Timestamp lastConfigurationBuildTime; private Configuration config; @@ -160,17 +149,13 @@ public final class ConfigurationRepository { if (config == null) { config = builder.getConfiguration(); lastConfigurationBuildTime = new Timestamp(System.currentTimeMillis()); - } else if (lastConfigurationBuildTime != null - && System.currentTimeMillis() - lastConfigurationBuildTime.getTime() - > getConfigurationFor(Constants.DEFAULT_TENANT, Constants.DB_NAMESPACE) - .getInt("config.refresh.interval")) { + } else if (lastConfigurationBuildTime != null && System.currentTimeMillis() - lastConfigurationBuildTime.getTime() > getConfigurationFor( + Constants.DEFAULT_TENANT, Constants.DB_NAMESPACE).getInt("config.refresh.interval")) { lastConfigurationBuildTime = new Timestamp(System.currentTimeMillis()); } - if (composite == null && overrideConfiguration.size() != 0) { composite = getEffectiveConfiguration(config, overrideConfiguration.values()); } - return overrideConfiguration.size() == 0 ? config : composite; } } diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ContextListener.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ContextListener.java index 520852ee1f..9f9706f622 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ContextListener.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ContextListener.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.onap.config.impl; import static org.onap.config.Constants.MBEAN_NAME; diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/YamlConfiguration.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/YamlConfiguration.java index bdaf21915e..d7b8df1487 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/YamlConfiguration.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/YamlConfiguration.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.onap.config.impl; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; @@ -30,5 +29,4 @@ public class YamlConfiguration extends JacksonConfiguration { public YamlConfiguration() { super(new YAMLFactory()); } - } |