2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.core.factory.api;
23 import org.openecomp.core.factory.FactoryConfig;
24 import org.openecomp.core.factory.impl.AbstractFactoryBase;
25 import org.openecomp.core.utilities.CommonMethods;
26 import org.openecomp.sdc.common.errors.CoreException;
27 import org.openecomp.sdc.common.errors.ErrorCategory;
28 import org.openecomp.sdc.common.errors.ErrorCode;
32 public abstract class AbstractComponentFactory<I> extends AbstractFactory<I> {
35 Registry registry = new RegistryImpl();
36 InitializationHelper.registerFactoryMapping(registry);
40 void register(String factory, String impl);
43 private static class RegistryImpl implements Registry {
45 public void register(String factory, String impl) {
46 AbstractFactoryBase.registerFactory(factory, impl);
50 static class InitializationHelper {
53 private static boolean isRegistered = false;
55 private InitializationHelper() {
58 static synchronized boolean registerFactoryMapping(Registry registry) {
60 boolean done = !isRegistered;
63 registerFactoryMappingImpl(registry);
70 private static void registerFactoryMappingImpl(Registry registry) {
71 Map<String, String> factoryMap = FactoryConfig.getFactoriesMap();
74 for (Map.Entry<String, String> entry : factoryMap.entrySet()) {
75 String abstractClassName = entry.getKey();
76 String concreteTypeName = entry.getValue();
78 if (CommonMethods.isEmpty(concreteTypeName)) {
79 throw new CoreException(
80 new ErrorCode.ErrorCodeBuilder().withId("E0003")
81 .withMessage("Missing configuration value:" + concreteTypeName + ".")
82 .withCategory(ErrorCategory.SYSTEM).build());
86 registry.register(abstractClassName, concreteTypeName);
88 } catch (RuntimeException exception) {
90 } catch (Exception exception) {
91 throw new RuntimeException(exception);
95 @SuppressWarnings("unchecked")
96 private static <T> Class<T> unsecureCast(Class<?> cls) {
97 return (Class<T>) cls;
100 private static String nameOf(Class<?> clazz) {
101 return (clazz != null) ? clazz.getName() : "null";