From 105ce0729d5333cc095ef5bd8104a6c5b90cc9f0 Mon Sep 17 00:00:00 2001 From: "andre.schmid" Date: Fri, 18 Oct 2019 14:58:21 +0100 Subject: 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 --- .../ComponentDependencyModelManagerFactory.java | 3 +- .../ComponentManagerFactory.java | 3 +- .../CompositionEntityDataManagerFactory.java | 3 +- .../ComputeManagerFactory.java | 3 +- .../DeploymentFlavorManagerFactory.java | 3 +- .../vendorsoftwareproduct/ImageManagerFactory.java | 3 +- .../MonitoringUploadsManagerFactory.java | 3 +- .../NetworkManagerFactory.java | 3 +- .../vendorsoftwareproduct/NicManagerFactory.java | 3 +- ...chestrationTemplateCandidateManagerFactory.java | 3 +- .../ProcessManagerFactory.java | 3 +- .../vendorsoftwareproduct/VspManagerFactory.java | 3 +- .../core/factory/impl/AbstractFactoryBase.java | 157 +-------------------- .../core/factory/impl/FactoryManager.java | 150 ++++++++++++++++++++ .../core/factory/FactoriesConfigImpl.java | 25 ++-- .../org/openecomp/core/factory/FactoryConfig.java | 14 +- .../core/factory/api/AbstractComponentFactory.java | 61 -------- .../openecomp/core/utilities/CommonMethods.java | 11 +- .../exception/NewInstanceRuntimeException.java | 31 ++++ .../factory/HeatToToscaTranslatorFactory.java | 3 +- 20 files changed, 216 insertions(+), 272 deletions(-) create mode 100644 openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-api/src/main/java/org/openecomp/core/factory/impl/FactoryManager.java create mode 100644 openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/exception/NewInstanceRuntimeException.java (limited to 'openecomp-be') diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/ComponentDependencyModelManagerFactory.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/ComponentDependencyModelManagerFactory.java index cbf311fcff..bd5cd45561 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/ComponentDependencyModelManagerFactory.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/ComponentDependencyModelManagerFactory.java @@ -22,12 +22,11 @@ package org.openecomp.sdc.vendorsoftwareproduct; import org.openecomp.core.factory.api.AbstractComponentFactory; -import org.openecomp.core.factory.api.AbstractFactory; public abstract class ComponentDependencyModelManagerFactory extends AbstractComponentFactory { public static ComponentDependencyModelManagerFactory getInstance() { - return AbstractFactory.getInstance(ComponentDependencyModelManagerFactory.class); + return getInstance(ComponentDependencyModelManagerFactory.class); } } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/ComponentManagerFactory.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/ComponentManagerFactory.java index 21a77b3c97..9af238e4e3 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/ComponentManagerFactory.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/ComponentManagerFactory.java @@ -21,11 +21,10 @@ package org.openecomp.sdc.vendorsoftwareproduct; import org.openecomp.core.factory.api.AbstractComponentFactory; -import org.openecomp.core.factory.api.AbstractFactory; public abstract class ComponentManagerFactory extends AbstractComponentFactory { public static ComponentManagerFactory getInstance() { - return AbstractFactory.getInstance(ComponentManagerFactory.class); + return getInstance(ComponentManagerFactory.class); } } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/CompositionEntityDataManagerFactory.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/CompositionEntityDataManagerFactory.java index b8e2fd7b0c..be4f187c15 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/CompositionEntityDataManagerFactory.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/CompositionEntityDataManagerFactory.java @@ -21,12 +21,11 @@ package org.openecomp.sdc.vendorsoftwareproduct; import org.openecomp.core.factory.api.AbstractComponentFactory; -import org.openecomp.core.factory.api.AbstractFactory; public abstract class CompositionEntityDataManagerFactory extends AbstractComponentFactory { public static CompositionEntityDataManagerFactory getInstance() { - return AbstractFactory.getInstance(CompositionEntityDataManagerFactory.class); + return getInstance(CompositionEntityDataManagerFactory.class); } } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/ComputeManagerFactory.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/ComputeManagerFactory.java index e888c4fb24..063bae1237 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/ComputeManagerFactory.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/ComputeManagerFactory.java @@ -21,11 +21,10 @@ package org.openecomp.sdc.vendorsoftwareproduct; import org.openecomp.core.factory.api.AbstractComponentFactory; -import org.openecomp.core.factory.api.AbstractFactory; public abstract class ComputeManagerFactory extends AbstractComponentFactory { public static ComputeManagerFactory getInstance() { - return AbstractFactory.getInstance(ComputeManagerFactory.class); + return getInstance(ComputeManagerFactory.class); } } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/DeploymentFlavorManagerFactory.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/DeploymentFlavorManagerFactory.java index 50a257f62c..97ae62978d 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/DeploymentFlavorManagerFactory.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/DeploymentFlavorManagerFactory.java @@ -22,12 +22,11 @@ package org.openecomp.sdc.vendorsoftwareproduct; import org.openecomp.core.factory.api.AbstractComponentFactory; -import org.openecomp.core.factory.api.AbstractFactory; public abstract class DeploymentFlavorManagerFactory extends AbstractComponentFactory { public static DeploymentFlavorManagerFactory getInstance() { - return AbstractFactory.getInstance(DeploymentFlavorManagerFactory.class); + return getInstance(DeploymentFlavorManagerFactory.class); } } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/ImageManagerFactory.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/ImageManagerFactory.java index 82b782a4fd..2ee014bb28 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/ImageManagerFactory.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/ImageManagerFactory.java @@ -21,12 +21,11 @@ package org.openecomp.sdc.vendorsoftwareproduct; import org.openecomp.core.factory.api.AbstractComponentFactory; -import org.openecomp.core.factory.api.AbstractFactory; public abstract class ImageManagerFactory extends AbstractComponentFactory { public static ImageManagerFactory getInstance() { - return AbstractFactory.getInstance(ImageManagerFactory.class); + return getInstance(ImageManagerFactory.class); } } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/MonitoringUploadsManagerFactory.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/MonitoringUploadsManagerFactory.java index eb4271be16..9850b2e19b 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/MonitoringUploadsManagerFactory.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/MonitoringUploadsManagerFactory.java @@ -21,12 +21,11 @@ package org.openecomp.sdc.vendorsoftwareproduct; import org.openecomp.core.factory.api.AbstractComponentFactory; -import org.openecomp.core.factory.api.AbstractFactory; public abstract class MonitoringUploadsManagerFactory extends AbstractComponentFactory { public static MonitoringUploadsManagerFactory getInstance() { - return AbstractFactory.getInstance(MonitoringUploadsManagerFactory.class); + return getInstance(MonitoringUploadsManagerFactory.class); } } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/NetworkManagerFactory.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/NetworkManagerFactory.java index 943e676727..a0b7465235 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/NetworkManagerFactory.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/NetworkManagerFactory.java @@ -21,11 +21,10 @@ package org.openecomp.sdc.vendorsoftwareproduct; import org.openecomp.core.factory.api.AbstractComponentFactory; -import org.openecomp.core.factory.api.AbstractFactory; public abstract class NetworkManagerFactory extends AbstractComponentFactory { public static NetworkManagerFactory getInstance() { - return AbstractFactory.getInstance(NetworkManagerFactory.class); + return getInstance(NetworkManagerFactory.class); } } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/NicManagerFactory.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/NicManagerFactory.java index 73f7f79996..212619ce87 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/NicManagerFactory.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/NicManagerFactory.java @@ -21,11 +21,10 @@ package org.openecomp.sdc.vendorsoftwareproduct; import org.openecomp.core.factory.api.AbstractComponentFactory; -import org.openecomp.core.factory.api.AbstractFactory; public abstract class NicManagerFactory extends AbstractComponentFactory { public static NicManagerFactory getInstance() { - return AbstractFactory.getInstance(NicManagerFactory.class); + return getInstance(NicManagerFactory.class); } } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/OrchestrationTemplateCandidateManagerFactory.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/OrchestrationTemplateCandidateManagerFactory.java index ddc24adeb6..b38355b8e9 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/OrchestrationTemplateCandidateManagerFactory.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/OrchestrationTemplateCandidateManagerFactory.java @@ -21,12 +21,11 @@ package org.openecomp.sdc.vendorsoftwareproduct; import org.openecomp.core.factory.api.AbstractComponentFactory; -import org.openecomp.core.factory.api.AbstractFactory; public abstract class OrchestrationTemplateCandidateManagerFactory extends AbstractComponentFactory { public static OrchestrationTemplateCandidateManagerFactory getInstance() { - return AbstractFactory.getInstance(OrchestrationTemplateCandidateManagerFactory.class); + return getInstance(OrchestrationTemplateCandidateManagerFactory.class); } } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/ProcessManagerFactory.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/ProcessManagerFactory.java index 7da0bef128..f8d1038ca2 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/ProcessManagerFactory.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/ProcessManagerFactory.java @@ -21,11 +21,10 @@ package org.openecomp.sdc.vendorsoftwareproduct; import org.openecomp.core.factory.api.AbstractComponentFactory; -import org.openecomp.core.factory.api.AbstractFactory; public abstract class ProcessManagerFactory extends AbstractComponentFactory { public static ProcessManagerFactory getInstance() { - return AbstractFactory.getInstance(ProcessManagerFactory.class); + return getInstance(ProcessManagerFactory.class); } } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/VspManagerFactory.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/VspManagerFactory.java index 99d6988b61..a2f186428a 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/VspManagerFactory.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/VspManagerFactory.java @@ -21,12 +21,11 @@ package org.openecomp.sdc.vendorsoftwareproduct; import org.openecomp.core.factory.api.AbstractComponentFactory; -import org.openecomp.core.factory.api.AbstractFactory; public abstract class VspManagerFactory extends AbstractComponentFactory { public static VspManagerFactory getInstance() { - return AbstractFactory.getInstance(VspManagerFactory.class); + return getInstance(VspManagerFactory.class); } } 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 REGISTRY = new ConcurrentHashMap<>(); - - /** - * Cached factory instances. - */ - private static final Map 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 Java interface type instantiated by abstract factory - * @param 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 void registerFactory(Class factory, - Class 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 the type parameter - * @param factory the factory - */ - public static void unregisterFactory(Class 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 Java interface type instantiated by abstract factory * @param 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 F getInstance(Class 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 the type parameter - * @param factoryType the factory type - * @return the boolean - */ - public static boolean isFactoryRegistered(Class 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 factorylist = FACTORY_MAP.values(); - for (AbstractFactoryBase factory : factorylist) { - factory.stop(); - } + public static F getInstance(Class 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 factoryRegistry = new ConcurrentHashMap<>(); + /** + * Cached factory instances. + */ + private final Map factoryInstanceMap = new ConcurrentHashMap<>(); + + + private FactoryManager() { + initializeFactoryRegistry(); + } + + public static synchronized FactoryManager getInstance() { + return instance; + } + + private void initializeFactoryRegistry() { + final Map factoryMap = FactoryConfig.getFactoriesMap(); + + for (final Map.Entry 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 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 getFactoryInstance(Class 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 the type parameter + * @param factory the factory + */ + public void removeFactoryInstance(Class 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); + } +} diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/FactoriesConfigImpl.java b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/FactoriesConfigImpl.java index 15b9f8c1c8..31df923bdd 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/FactoriesConfigImpl.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/FactoriesConfigImpl.java @@ -31,31 +31,24 @@ import java.util.Map; public final class FactoriesConfigImpl implements FactoriesConfiguration { + private final Map factoryConfigurationMap = new HashMap(); - private static final String FACTORY_CONFIG_FILE_NAME = "factoryConfiguration.json"; - private static final Map FACTORY_MAP = new HashMap(); - private static boolean initialized = false; + public FactoriesConfigImpl() { + init(); + } @SuppressWarnings("unchecked") @Override public Map getFactoriesMap() { - synchronized (this) { - if (!initialized) { - init(); - initialized = true; - } - } - return FACTORY_MAP; + return factoryConfigurationMap; } private void init() { - - List factoryConfigUrlList = FileUtils.getAllLocations(FACTORY_CONFIG_FILE_NAME); - for (URL factoryConfigUrl : factoryConfigUrlList) { - + final List factoryConfigUrlList = FileUtils.getAllLocations("factoryConfiguration.json"); + for (final URL factoryConfigUrl : factoryConfigUrlList) { try (InputStream stream = factoryConfigUrl.openStream()) { - FACTORY_MAP.putAll(JsonUtil.json2Object(stream, Map.class)); - } catch (IOException e) { + factoryConfigurationMap.putAll(JsonUtil.json2Object(stream, Map.class)); + } catch (final IOException e) { throw new SdcConfigurationException("Failed to initialize Factory from '" + factoryConfigUrl.getPath() +"'", e); } } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/FactoryConfig.java b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/FactoryConfig.java index 31822b6bdc..7dbf921ebf 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/FactoryConfig.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/FactoryConfig.java @@ -20,23 +20,17 @@ package org.openecomp.core.factory; +import java.util.Map; import org.openecomp.core.factory.api.FactoriesConfiguration; import org.openecomp.core.utilities.CommonMethods; -import java.util.Map; - public final class FactoryConfig { - private static final FactoriesConfiguration INSTANCE; + private static final FactoriesConfiguration INSTANCE = CommonMethods.newInstance( + "org.openecomp.core.factory.FactoriesConfigImpl", FactoriesConfiguration.class); - static { + private FactoryConfig() { - try { - INSTANCE = CommonMethods.newInstance( - "org.openecomp.core.factory.FactoriesConfigImpl", FactoriesConfiguration.class); - } catch (Exception exception) { - throw exception; - } } public static Map getFactoriesMap() { diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/api/AbstractComponentFactory.java b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/api/AbstractComponentFactory.java index 1ea6fe8d43..3de0d88f43 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/api/AbstractComponentFactory.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/api/AbstractComponentFactory.java @@ -16,66 +16,5 @@ package org.openecomp.core.factory.api; -import org.apache.commons.lang3.StringUtils; -import org.openecomp.core.factory.FactoryConfig; -import org.openecomp.core.factory.impl.AbstractFactoryBase; -import org.openecomp.sdc.common.errors.CoreException; -import org.openecomp.sdc.common.errors.ErrorCategory; -import org.openecomp.sdc.common.errors.ErrorCode; - -import java.util.Map; - public abstract class AbstractComponentFactory extends AbstractFactory { - - static { - Registry registry = new RegistryImpl(); - InitializationHelper.registerFactoryMapping(registry); - } - - @FunctionalInterface - interface Registry { - void register(String factory, String impl); - } - - private static class RegistryImpl implements Registry { - @Override - public void register(String factory, String impl) { - AbstractFactoryBase.registerFactory(factory, impl); - } - } - - static class InitializationHelper { - - - private static boolean isRegistered = false; - - private InitializationHelper() { - } - - static synchronized void registerFactoryMapping(Registry registry) { - if (!isRegistered) { - registerFactoryMappingImpl(registry); - isRegistered = true; - } - } - - private static void registerFactoryMappingImpl(Registry registry) { - Map factoryMap = FactoryConfig.getFactoriesMap(); - - for (Map.Entry entry : factoryMap.entrySet()) { - String abstractClassName = entry.getKey(); - 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()); - - } - registry.register(abstractClassName, concreteTypeName); - } - } - } - } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/CommonMethods.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/CommonMethods.java index b2cc289676..0bcdf8ebbf 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/CommonMethods.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/CommonMethods.java @@ -17,6 +17,7 @@ package org.openecomp.core.utilities; import java.lang.reflect.Array; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -30,6 +31,7 @@ import java.util.UUID; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; +import org.openecomp.core.utilities.exception.NewInstanceRuntimeException; /** * This class provides auxiliary static methods. @@ -160,11 +162,12 @@ public class CommonMethods { * @param cls the cls * @return the t */ - public static T newInstance(Class cls) { + public static T newInstance(final Class cls) { try { - return cls.newInstance(); - } catch (InstantiationException | IllegalAccessException exception) { - throw new RuntimeException(exception); + return cls.getDeclaredConstructor().newInstance(); + } catch (final InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException ex) { + throw new NewInstanceRuntimeException(String.format("Could not create instance for '%s'", cls.getName()) + , ex); } } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/exception/NewInstanceRuntimeException.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/exception/NewInstanceRuntimeException.java new file mode 100644 index 0000000000..386486ff9e --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/exception/NewInstanceRuntimeException.java @@ -0,0 +1,31 @@ +/* + * ============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.utilities.exception; + +public class NewInstanceRuntimeException extends RuntimeException { + + public NewInstanceRuntimeException(String s) { + super(s); + } + + public NewInstanceRuntimeException(String s, Throwable throwable) { + super(s, throwable); + } +} diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-api/src/main/java/org/openecomp/core/translator/factory/HeatToToscaTranslatorFactory.java b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-api/src/main/java/org/openecomp/core/translator/factory/HeatToToscaTranslatorFactory.java index 2b7b00f435..ebc497374d 100644 --- a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-api/src/main/java/org/openecomp/core/translator/factory/HeatToToscaTranslatorFactory.java +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-api/src/main/java/org/openecomp/core/translator/factory/HeatToToscaTranslatorFactory.java @@ -21,7 +21,6 @@ package org.openecomp.core.translator.factory; import org.openecomp.core.factory.api.AbstractComponentFactory; -import org.openecomp.core.factory.api.AbstractFactory; import org.openecomp.core.translator.api.HeatToToscaTranslator; @@ -30,6 +29,6 @@ public abstract class HeatToToscaTranslatorFactory extends AbstractComponentFactory { public static HeatToToscaTranslatorFactory getInstance() { - return AbstractFactory.getInstance(HeatToToscaTranslatorFactory.class); + return getInstance(HeatToToscaTranslatorFactory.class); } } -- cgit 1.2.3-korg