aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-api
diff options
context:
space:
mode:
authorandre.schmid <andre.schmid@est.tech>2019-10-18 14:58:21 +0100
committerandre.schmid <andre.schmid@est.tech>2019-10-21 15:53:58 +0100
commit105ce0729d5333cc095ef5bd8104a6c5b90cc9f0 (patch)
treef85c45d12fcc2595ad02516ad182ff5f71c9bf0d /openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-api
parentd289108897f3baf5bc51092c4f55309bdd90f8d9 (diff)
Fix factory instance creation instability
Refactor the factory registry and instance creation. Remove unnecessary static code in Factory creation. Centralize the responsibility to read the factory registry configuration and cache the factory instances in a Singleton called FactoryRegistryManager. Clean the base abstract factory that was sharing non factory code, it had all the factory registry code statically, never used by the concrete factory classes. Change-Id: Ie2e9c29013acd36ddaf15383dd6b06432fbdbb66 Issue-ID: SDC-1905 Signed-off-by: andre.schmid <andre.schmid@est.tech>
Diffstat (limited to 'openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-api')
-rw-r--r--openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-api/src/main/java/org/openecomp/core/factory/impl/AbstractFactoryBase.java157
-rw-r--r--openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-api/src/main/java/org/openecomp/core/factory/impl/FactoryManager.java150
2 files changed, 152 insertions, 155 deletions
diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-api/src/main/java/org/openecomp/core/factory/impl/AbstractFactoryBase.java b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-api/src/main/java/org/openecomp/core/factory/impl/AbstractFactoryBase.java
index 9d25a09758..3a117ef063 100644
--- a/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-api/src/main/java/org/openecomp/core/factory/impl/AbstractFactoryBase.java
+++ b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-api/src/main/java/org/openecomp/core/factory/impl/AbstractFactoryBase.java
@@ -16,170 +16,17 @@
package org.openecomp.core.factory.impl;
-import static org.openecomp.core.utilities.CommonMethods.newInstance;
-
-import java.util.Collection;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
-import org.apache.commons.lang3.StringUtils;
-import org.openecomp.sdc.common.errors.CoreException;
-import org.openecomp.sdc.common.errors.ErrorCategory;
-import org.openecomp.sdc.common.errors.ErrorCode;
-
public abstract class AbstractFactoryBase {
- /**
- * Temporary registry of default implementations. The map keeps class names rather then class
- * types to allow unloading of those classes from memory by garbage collector if factory is not
- * actually used.
- */
- private static final Map<String, String> REGISTRY = new ConcurrentHashMap<>();
-
- /**
- * Cached factory instances.
- */
- private static final Map<String, AbstractFactoryBase> FACTORY_MAP = new ConcurrentHashMap<>();
- public static final String E0001 = "E0001";
-
- /**
- * Registers implementor for an abstract factory. The method accepts Java classes rather then
- * class names to ensure type safety at compilation time.
- *
- * @param <I> Java interface type instantiated by abstract factory
- * @param <F> Type specific abstract factory for concrete Java interface
- * @param factory Java class of a type specific abstract factory
- * @param impl Java class of type specific factory implementor
- */
- public static <I, F extends AbstractFactoryBase> void registerFactory(Class<F> factory,
- Class<? extends F> impl) {
- if (factory == null) {
- throw new CoreException(
- new ErrorCode.ErrorCodeBuilder().withId(E0001).withMessage("Mandatory input factory.")
- .withCategory(ErrorCategory.SYSTEM).build());
- }
-
- if (impl == null) {
- throw new CoreException(
- new ErrorCode.ErrorCodeBuilder().withId(E0001).withMessage("Mandatory input impl.")
- .withCategory(ErrorCategory.SYSTEM).build());
- }
- if (FACTORY_MAP.containsKey(factory.getName())) {
- FACTORY_MAP.remove(factory.getName());
- }
- REGISTRY.put(factory.getName(), impl.getName());
- } // registerFactory
-
- // TODO: Remove
- protected static void registerFactory(String factoryName, String implName) {
- REGISTRY.put(factoryName, implName);
- } // registerFactory
-
- /**
- * Unregister factory.
- *
- * @param <F> the type parameter
- * @param factory the factory
- */
- public static <F extends AbstractFactoryBase> void unregisterFactory(Class<F> factory) {
- if (factory == null) {
- throw new CoreException(
- new ErrorCode.ErrorCodeBuilder().withId(E0001).withMessage("Mandatory input factory.")
- .withCategory(ErrorCategory.SYSTEM).build());
- }
-
- FACTORY_MAP.remove(factory.getName());
- }
-
/**
* Instantiates the configured implementation of an abstract factory.
*
- * @param <I> Java interface type instantiated by abstract factory
* @param <F> Type specific abstract factory for concrete Java interface
* @param factoryType Java class of type specific abstract factory
* @return Instance of implementation class
*/
- @SuppressWarnings("unchecked")
- public static <I, F extends AbstractFactoryBase> F getInstance(Class<F> factoryType) {
- if (factoryType == null) {
- throw new CoreException(
- new ErrorCode.ErrorCodeBuilder().withId(E0001)
- .withMessage("Mandatory input factory type.").withCategory(ErrorCategory.SYSTEM)
- .build());
-
- }
- // Pick up factory instance from cache
- F factory = (F) FACTORY_MAP.get(factoryType.getName());
- // Check for the first time access
- if (factory == null) {
- // Synchronize factory instantiation
- synchronized (FACTORY_MAP) {
- // Re-check the factory instance
- factory = (F) FACTORY_MAP.get(factoryType.getName());
- if (factory == null) {
- // Get the implementation class name
- String implName = REGISTRY.get(factoryType.getName());
-
- if (StringUtils.isEmpty(implName)) {
- throw new CoreException(
- new ErrorCode.ErrorCodeBuilder().withId(E0001)
- .withMessage("Mandatory input factory implementation.")
- .withCategory(ErrorCategory.SYSTEM).build());
- }
-
- factory = newInstance(implName, factoryType);
-
- factory.init();
-
- // Cache the instantiated singleton
- FACTORY_MAP.put(factoryType.getName(), factory);
- }
- }
- }
-
- return factory;
-
- } // getInstance
-
-
- /**
- * Is factory registered boolean.
- *
- * @param <F> the type parameter
- * @param factoryType the factory type
- * @return the boolean
- */
- public static <F extends AbstractFactoryBase> boolean isFactoryRegistered(Class<F> factoryType) {
- boolean isFactoryRegistered = false;
- if (factoryType == null) {
- throw new CoreException(
- new ErrorCode.ErrorCodeBuilder().withId(E0001)
- .withMessage("Mandatory input factory type.").withCategory(ErrorCategory.SYSTEM)
- .build());
- }
- // Pick up factory instance from cache
- F factory = (F) FACTORY_MAP.get(factoryType.getName());
- // Check for the first time access
- if (factory != null) {
- isFactoryRegistered = true;
- } else {
- // Get the implementation class name
- String implName = REGISTRY.get(factoryType.getName());
- if (StringUtils.isNotEmpty(implName)) {
- isFactoryRegistered = true;
- }
- }
- return isFactoryRegistered;
- }
-
- /**
- * Stop all.
- */
- public static void stopAll() {
- Collection<AbstractFactoryBase> factorylist = FACTORY_MAP.values();
- for (AbstractFactoryBase factory : factorylist) {
- factory.stop();
- }
+ public static <F extends AbstractFactoryBase> F getInstance(Class<F> factoryType) {
+ return FactoryManager.getInstance().getFactoryInstance(factoryType);
}
protected void init() {
diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-api/src/main/java/org/openecomp/core/factory/impl/FactoryManager.java b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-api/src/main/java/org/openecomp/core/factory/impl/FactoryManager.java
new file mode 100644
index 0000000000..ec2d93f35b
--- /dev/null
+++ b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-api/src/main/java/org/openecomp/core/factory/impl/FactoryManager.java
@@ -0,0 +1,150 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 Nordix Foundation
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.core.factory.impl;
+
+import com.amdocs.zusammen.utils.facade.impl.FactoryConfig;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.commons.lang3.StringUtils;
+import org.openecomp.core.utilities.CommonMethods;
+import org.openecomp.sdc.common.errors.CoreException;
+import org.openecomp.sdc.common.errors.ErrorCategory;
+import org.openecomp.sdc.common.errors.ErrorCode;
+
+public class FactoryManager {
+
+ private static final FactoryManager instance = new FactoryManager();
+ private static final String ERROR_CODE_E0001 = "E0001";
+ /**
+ * Temporary registry of default implementations. The map keeps class names rather then class
+ * types to allow unloading of those classes from memory by garbage collector if factory is not
+ * actually used.
+ */
+ private final Map<String, String> factoryRegistry = new ConcurrentHashMap<>();
+ /**
+ * Cached factory instances.
+ */
+ private final Map<String, AbstractFactoryBase> factoryInstanceMap = new ConcurrentHashMap<>();
+
+
+ private FactoryManager() {
+ initializeFactoryRegistry();
+ }
+
+ public static synchronized FactoryManager getInstance() {
+ return instance;
+ }
+
+ private void initializeFactoryRegistry() {
+ final Map<String, String> factoryMap = FactoryConfig.getFactoriesMap();
+
+ for (final Map.Entry<String, String> entry : factoryMap.entrySet()) {
+ final String abstractClassName = entry.getKey();
+ final String concreteTypeName = entry.getValue();
+
+ if (StringUtils.isEmpty(concreteTypeName)) {
+ throw new CoreException(
+ new ErrorCode.ErrorCodeBuilder().withId("E0003")
+ .withMessage("Missing configuration value:" + concreteTypeName + ".")
+ .withCategory(ErrorCategory.SYSTEM).build());
+
+ }
+ registerFactory(abstractClassName, concreteTypeName);
+ }
+ }
+
+ /**
+ * Instantiates the configured implementation of an abstract factory.
+ *
+ * @param <F> Type specific abstract factory for concrete Java interface
+ * @param factoryType Java class of type specific abstract factory
+ * @return Instance of implementation class
+ */
+ @SuppressWarnings("unchecked")
+ public <F extends AbstractFactoryBase> F getFactoryInstance(Class<F> factoryType) {
+ if (factoryType == null) {
+ throw new CoreException(
+ new ErrorCode.ErrorCodeBuilder().withId(ERROR_CODE_E0001)
+ .withMessage("Mandatory input factory type.").withCategory(ErrorCategory.SYSTEM)
+ .build());
+
+ }
+ final String factoryTypeName = factoryType.getName();
+ // Check if the factory is already cached
+ if (factoryInstanceMap.get(factoryTypeName) == null) {
+ //if not, create a new one and cache it
+ // Get the implementation class name
+ final String implName = factoryRegistry.get(factoryTypeName);
+
+ if (StringUtils.isEmpty(implName)) {
+ throw new CoreException(
+ new ErrorCode.ErrorCodeBuilder().withId(ERROR_CODE_E0001)
+ .withMessage("Mandatory input factory implementation.")
+ .withCategory(ErrorCategory.SYSTEM).build());
+ }
+
+ F factory = CommonMethods.newInstance(implName, factoryType);
+ factory.init();
+ // Cache the instantiated singleton
+ factoryInstanceMap.putIfAbsent(factoryTypeName, factory);
+ }
+
+ return (F) factoryInstanceMap.get(factoryTypeName);
+ }
+
+ public void registerFactory(final String factoryName, final String implName) {
+ factoryRegistry.put(factoryName, implName);
+ }
+
+ /**
+ * Unregister factory and removes the cached instance if any.
+ * @param factoryName the factory name to unregister
+ */
+ public void unregisterFactory(final String factoryName) {
+ final String factoryClass = factoryRegistry.get(factoryName);
+ if (StringUtils.isNotEmpty(factoryClass)) {
+ factoryInstanceMap.remove(factoryClass);
+ factoryRegistry.remove(factoryName);
+ }
+ }
+
+ /**
+ * Removes the cached factory instance if any.
+ *
+ * @param <F> the type parameter
+ * @param factory the factory
+ */
+ public <F extends AbstractFactoryBase> void removeFactoryInstance(Class<F> factory) {
+ if (factory == null) {
+ throw new CoreException(
+ new ErrorCode.ErrorCodeBuilder().withId(ERROR_CODE_E0001).withMessage("Mandatory input factory.")
+ .withCategory(ErrorCategory.SYSTEM).build());
+ }
+
+ factoryInstanceMap.remove(factory.getName());
+ }
+
+ /**
+ * Stop all.
+ */
+ public void stopAll() {
+ factoryInstanceMap.values().forEach(AbstractFactoryBase::stop);
+ }
+}