From 8b7a0405a97cacf765c0e9a1988af98cd91a9f67 Mon Sep 17 00:00:00 2001 From: vempo Date: Sun, 28 Oct 2018 19:04:06 +0200 Subject: 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 --- .../org/onap/config/impl/ConfigurationImpl.java | 24 ++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 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 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 tenant = ThreadLocal.withInitial(() -> Constants.DEFAULT_TENANT); + + private static final ThreadLocal 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 moduleConfigStore = new HashMap<>(); List classpathResources = ConfigurationUtils.getAllClassPathResources(); Predicate 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 Map populateMap(String tenantId, String namespace, String key, Class 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(); } -- cgit 1.2.3-korg