diff options
author | vempo <vitaliy.emporopulo@amdocs.com> | 2018-10-28 19:04:06 +0200 |
---|---|---|
committer | Vitaly Emporopulo <Vitaliy.Emporopulo@amdocs.com> | 2018-10-28 19:05:11 +0000 |
commit | 8b7a0405a97cacf765c0e9a1988af98cd91a9f67 (patch) | |
tree | aa100b988d6e974089609caa27863667353a7860 /common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl | |
parent | 2074ab2e8b4416126542c09205d3ca6646ed6718 (diff) |
Handled not thread-safe fields in configuration
Replaced not thread-safe fields with synchornized versions,
removed duplicate code, deleted class that was accessing DB,
made surefire plugin to pick up all available unit tests
(instead of hand-picked).
Change-Id: Idff3ac333dc87ebfd3ecf50438ba0179556eb9c9
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/java/org/onap/config/impl')
4 files changed, 35 insertions, 57 deletions
diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/AgglomerateConfiguration.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/AgglomerateConfiguration.java deleted file mode 100644 index ed2511d722..0000000000 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/AgglomerateConfiguration.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright © 2016-2018 European Support Limited - * - * 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 org.onap.config.impl; - -import java.util.Collections; -import java.util.Map; -import java.util.WeakHashMap; -import org.apache.commons.configuration2.DatabaseConfiguration; - -public class AgglomerateConfiguration extends DatabaseConfiguration { - - private final Map<String, Object> store = Collections.synchronizedMap(new WeakHashMap<>()); - - public Object getPropertyValue(String key) { - - Object objToReturn = store.get(key); - if (objToReturn == null && !store.containsKey(key)) { - objToReturn = super.getProperty(key); - store.put(key, objToReturn); - } - - return objToReturn; - } - -} 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 32a1902151..e81d82bf7d 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 @@ -35,10 +35,10 @@ import org.onap.config.type.ConfigurationMode; public final class AggregateConfiguration { - private final Map<String, Configuration> rootConfig = new HashMap<>(); - private final Map<String, Configuration> unionConfig = new HashMap<>(); - private final Map<String, Configuration> mergeConfig = new HashMap<>(); - private final Map<String, Configuration> overrideConfig = new LinkedHashMap<>(); + private final Map<String, Configuration> rootConfig = Collections.synchronizedMap(new HashMap<>()); + private final Map<String, Configuration> unionConfig = Collections.synchronizedMap(new HashMap<>()); + 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), 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 af5ae04104..9a93801144 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 @@ -39,14 +39,26 @@ import org.onap.config.api.Hint; 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 ThreadLocal<String> TENANT = ThreadLocal.withInitial(() -> Constants.DEFAULT_TENANT); + + private static final Object LOCK = new Object(); private static boolean instantiated = false; public ConfigurationImpl() throws Exception { + + synchronized (LOCK) { + init(); + } + } + + private void init() throws Exception { + if (instantiated || !CliConfigurationImpl.class.isAssignableFrom(this.getClass())) { throw new RuntimeException("Illegal access to configuration."); } + Map<String, AggregateConfiguration> moduleConfigStore = new HashMap<>(); List<URL> classpathResources = ConfigurationUtils.getAllClassPathResources(); Predicate<URL> predicate = ConfigurationUtils::isConfig; @@ -107,6 +119,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) { @@ -122,6 +135,7 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { } } } + instantiated = true; } @@ -186,8 +200,9 @@ 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(); + tenantId = TENANT.get(); } else { tenantId = tenantId.toUpperCase(); } @@ -218,8 +233,9 @@ 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(); + tenantId = TENANT.get(); } else { tenantId = tenantId.toUpperCase(); } @@ -277,7 +293,7 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { } if (tenant == null || tenant.trim().length() == 0) { - tenant = ConfigurationImpl.tenant.get(); + tenant = TENANT.get(); } else { tenant = tenant.toUpperCase(); } 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 132043d5b5..5b950d1af6 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 @@ -20,6 +20,7 @@ import java.io.File; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Map; @@ -38,10 +39,14 @@ public final class ConfigurationRepository { private static final ConfigurationRepository repo = new ConfigurationRepository(); - private final Set<String> tenants = new HashSet<>(); - private final Set<String> namespaces = new HashSet<>(); - private final LinkedHashMap<String, ConfigurationHolder> store = + 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 { @@ -51,7 +56,8 @@ public final class ConfigurationRepository { return false; } } - }; + }); + private ConfigurationRepository() { tenants.add(Constants.DEFAULT_TENANT); @@ -80,13 +86,8 @@ public final class ConfigurationRepository { } public Configuration getConfigurationFor(String tenant, String namespace) throws Exception { - ConfigurationHolder config; String module = tenant + Constants.KEY_ELEMENTS_DELIMITER + namespace; - config = store.get(module); - if (config == null) { - config = new ConfigurationHolder(new BasicConfigurationBuilder<>(AgglomerateConfiguration.class)); - store.put(module, config); - } + ConfigurationHolder config = store.get(module); return config.getConfiguration(tenant + Constants.KEY_ELEMENTS_DELIMITER + namespace); } |