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;
24 import java.io.FileInputStream;
25 import java.util.HashMap;
27 import java.util.Properties;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import com.fasterxml.jackson.core.type.TypeReference;
33 import com.fasterxml.jackson.databind.ObjectMapper;
35 public class ServicePropertiesMap
37 private static HashMap<String, HashMap<String, String>> mapOfMaps = new HashMap<String, HashMap<String, String>>();
38 static final Logger logger = LoggerFactory.getLogger(ServicePropertiesMap.class);
43 * @param file the file
44 * @throws Exception the exception
46 public static void refresh(File file) throws Exception
50 logger.info("Loading properties - " + (file != null?file.getName():""));
52 //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>>() {};
59 HashMap<String, String> propMap = om.readValue(file, typeRef);
60 HashMap<String, String> lcasePropMap = new HashMap<String, String>();
61 for (String key : propMap.keySet() )
63 String lcaseKey = ifNullThenEmpty(key);
64 lcasePropMap.put(lcaseKey, propMap.get(key));
67 mapOfMaps.put(file.getName(), lcasePropMap);
70 }else if(filePath.lastIndexOf(".properties")>0){
71 Properties prop = new Properties();
72 FileInputStream fis = new FileInputStream(file);
75 @SuppressWarnings("unchecked")
76 HashMap<String, String> propMap = new HashMap<String, String>((Map)prop);
78 mapOfMaps.put(file.getName(), propMap);
81 logger.info("File - " + file.getName() + " is loaded into the map and the corresponding system properties have been refreshed");
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)
99 HashMap<String, String> propMap = mapOfMaps.get(fileName);
100 return propMap!=null?propMap.get(ifNullThenEmpty(propertyKey)):"";
104 * Gets the properties.
106 * @param fileName the file name
107 * @return the properties
109 public static HashMap<String, String> getProperties(String fileName){
110 return mapOfMaps.get(fileName);
114 * If null then empty.
119 private static String ifNullThenEmpty(String key) {