push addional code
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-facade-lib / openecomp-facade-core / src / main / java / org / openecomp / core / factory / api / AbstractComponentFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.core.factory.api;
22
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;
29
30 import java.util.Map;
31
32 public abstract class AbstractComponentFactory<I> extends AbstractFactory<I> {
33
34   static {
35     Registry registry = new RegistryImpl();
36     InitializationHelper.registerFactoryMapping(registry);
37   }
38
39   interface Registry {
40     void register(String factory, String impl);
41   }
42
43   private static class RegistryImpl implements Registry {
44     @Override
45     public void register(String factory, String impl) {
46       AbstractFactoryBase.registerFactory(factory, impl);
47     }
48   }
49
50   static class InitializationHelper {
51
52
53     private static boolean isRegistered = false;
54
55     private InitializationHelper() {
56     }
57
58     static synchronized boolean registerFactoryMapping(Registry registry) {
59
60       boolean done = !isRegistered;
61
62       if (!isRegistered) {
63         registerFactoryMappingImpl(registry);
64         isRegistered = true;
65       }
66
67       return done;
68     }
69
70     private static void registerFactoryMappingImpl(Registry registry) {
71       Map<String, String> factoryMap = FactoryConfig.getFactoriesMap();
72
73       try {
74         for (Map.Entry<String, String> entry : factoryMap.entrySet()) {
75           String abstractClassName = entry.getKey();
76           String concreteTypeName = entry.getValue();
77
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());
83
84           }
85
86           registry.register(abstractClassName, concreteTypeName);
87         }
88       } catch (RuntimeException exception) {
89         throw exception;
90       } catch (Exception exception) {
91         throw new RuntimeException(exception);
92       }
93     }
94
95     @SuppressWarnings("unchecked")
96     private static <T> Class<T> unsecureCast(Class<?> cls) {
97       return (Class<T>) cls;
98     }
99
100     private static String nameOf(Class<?> clazz) {
101       return (clazz != null) ? clazz.getName() : "null";
102     }
103   }
104
105 }