24f71e79532ea6a7bfa3f77a15bd33928ab6905a
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.core.factory.api;
18
19 import org.apache.commons.lang3.StringUtils;
20 import org.openecomp.core.factory.FactoryConfig;
21 import org.openecomp.core.factory.impl.AbstractFactoryBase;
22 import org.openecomp.core.utilities.CommonMethods;
23 import org.openecomp.sdc.common.errors.CoreException;
24 import org.openecomp.sdc.common.errors.ErrorCategory;
25 import org.openecomp.sdc.common.errors.ErrorCode;
26
27 import java.util.Map;
28
29 public abstract class AbstractComponentFactory<I> extends AbstractFactory<I> {
30
31   static {
32     Registry registry = new RegistryImpl();
33     InitializationHelper.registerFactoryMapping(registry);
34   }
35
36   @FunctionalInterface
37   interface Registry {
38     void register(String factory, String impl);
39   }
40
41   private static class RegistryImpl implements Registry {
42     @Override
43     public void register(String factory, String impl) {
44       AbstractFactoryBase.registerFactory(factory, impl);
45     }
46   }
47
48   static class InitializationHelper {
49
50
51     private static boolean isRegistered = false;
52
53     private InitializationHelper() {
54     }
55
56     static synchronized void registerFactoryMapping(Registry registry) {
57       if (!isRegistered) {
58         registerFactoryMappingImpl(registry);
59         isRegistered = true;
60       }
61     }
62
63     private static void registerFactoryMappingImpl(Registry registry) {
64       Map<String, String> factoryMap = FactoryConfig.getFactoriesMap();
65
66         for (Map.Entry<String, String> entry : factoryMap.entrySet()) {
67           String abstractClassName = entry.getKey();
68           String concreteTypeName = entry.getValue();
69
70           if (StringUtils.isEmpty(concreteTypeName)) {
71             throw new CoreException(
72                 new ErrorCode.ErrorCodeBuilder().withId("E0003")
73                     .withMessage("Missing configuration value:" + concreteTypeName + ".")
74                     .withCategory(ErrorCategory.SYSTEM).build());
75
76           }
77           registry.register(abstractClassName, concreteTypeName);
78         }
79     }
80   }
81
82 }