b9d03f4d1abd537fdfb235d512e8f2069f39d762
[sdc.git] /
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;
22
23
24 import org.openecomp.core.factory.api.FactoriesConfiguration;
25 import org.openecomp.core.utilities.file.FileUtils;
26 import org.openecomp.core.utilities.json.JsonUtil;
27
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.net.URL;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34
35 public final class FactoriesConfigImpl implements FactoriesConfiguration {
36
37
38   private static final String FACTORY_CONFIG_FILE_NAME = "factoryConfiguration.json";
39   private static Map factoryMap = new HashMap();
40   private static boolean initialized = false;
41
42   @SuppressWarnings("unchecked")
43   @Override
44   public Map<String, String> getFactoriesMap() {
45     synchronized (this) {
46       if (!initialized) {
47         init();
48         initialized = true;
49       }
50     }
51     return factoryMap;
52   }
53
54   private void init() {
55
56     List<URL> factoryConfigUrlList = FileUtils.getAllLocations(FACTORY_CONFIG_FILE_NAME);
57     for (URL factoryConfigUrl : factoryConfigUrlList) {
58
59       try (InputStream stream = factoryConfigUrl.openStream()) {
60         factoryMap.putAll(JsonUtil.json2Object(stream, Map.class));
61       } catch (IOException e) {
62         throw new RuntimeException(e);
63       }
64     }
65   }
66 }
67