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.aai.ajsc_aai.filemonitor;
23 import com.fasterxml.jackson.core.type.TypeReference;
24 import com.fasterxml.jackson.databind.ObjectMapper;
26 import java.io.FileInputStream;
27 import java.util.HashMap;
29 import java.util.Properties;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class ServicePropertiesMap {
35 private ServicePropertiesMap {
38 private static HashMap<String, HashMap<String, String>> mapOfMaps = new HashMap<>();
39 private static final Logger logger = LoggerFactory.getLogger(ServicePropertiesMap.class);
44 * @param file the file
45 * @throws Exception the exception
47 public static void refresh(File file) throws Exception {
49 logger.info(String.format("Loading properties - %s", file != null ? file.getName() : ""));
51 //Store .json & .properties files into map of maps
53 String filePath = file.getPath();
55 if (filePath.lastIndexOf(".json") > 0) {
57 ObjectMapper om = new ObjectMapper();
58 TypeReference<HashMap<String, String>> typeRef = new TypeReference<HashMap<String, String>>() {
60 HashMap<String, String> propMap = om.readValue(file, typeRef);
61 HashMap<String, String> lcasePropMap = new HashMap<>();
62 for (HashMap.Entry<String, String> entry : propMap.entrySet()) {
63 String lcaseKey = ifNullThenEmpty(entry.getKey());
64 lcasePropMap.put(lcaseKey, entry.getValue());
67 mapOfMaps.put(file.getName(), lcasePropMap);
68 } else if (filePath.lastIndexOf(".properties") > 0) {
69 Properties prop = new Properties();
70 FileInputStream fis = new FileInputStream(file);
73 @SuppressWarnings("unchecked")
74 HashMap<String, String> propMap = new HashMap<>((Map) prop);
76 mapOfMaps.put(file.getName(), propMap);
79 logger.info("File - " + file.getName()
80 + " is loaded into the map and the corresponding system properties have been refreshed");
82 logger.error("File cannot be loaded into the map ");
84 } catch (Exception e) {
85 logger.error("File " + (file != null ? file.getName() : "") + " cannot be loaded into the map ", e);
86 throw new Exception("Error reading map file " + (file != null ? file.getName() : ""), e);
93 * @param fileName the file name
94 * @param propertyKey the property key
95 * @return the property
97 public static String getProperty(String fileName, String propertyKey) {
98 HashMap<String, String> propMap = mapOfMaps.get(fileName);
99 return propMap != null ? propMap.get(ifNullThenEmpty(propertyKey)) : "";
103 * Gets the properties.
105 * @param fileName the file name
106 * @return the properties
108 public static HashMap<String, String> getProperties(String fileName) {
109 return mapOfMaps.get(fileName);
113 * If null then empty.
118 private static String ifNullThenEmpty(String key) {
119 return key == null ? "" : key;