1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 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 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package com.att.nsa.dmaap.filemonitor;
24 import java.util.HashMap;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
30 * ServicePropertiesMap class
31 * @author rajashree.khare
34 @SuppressWarnings("squid:S1118")
35 public class ServicePropertiesMap
37 private static HashMap<String, HashMap<String, String>> mapOfMaps =
38 new HashMap<String, HashMap<String, String>>();
39 // static final Logger logger = LoggerFactory.getLogger(ServicePropertiesMap.class);
41 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ServicePropertiesMap.class);
45 * @throws Exception ex
47 /*public static void refresh(File file) throws Exception
51 logger.info("Loading properties - " + (file != null?file.getName():""));
53 //Store .json & .properties files into map of maps
54 String filePath = file.getPath();
56 if(filePath.lastIndexOf(".json")>0){
58 ObjectMapper om = new ObjectMapper();
59 TypeReference<HashMap<String, String>> typeRef =
60 new TypeReference<HashMap<String, String>>() {};
61 HashMap<String, String> propMap = om.readValue(file, typeRef);
62 HashMap<String, String> lcasePropMap = new HashMap<String, String>();
63 for (String key : propMap.keySet() )
65 String lcaseKey = ifNullThenEmpty(key);
66 lcasePropMap.put(lcaseKey, propMap.get(key));
69 mapOfMaps.put(file.getName(), lcasePropMap);
72 }else if(filePath.lastIndexOf(".properties")>0){
73 Properties prop = new Properties();
74 FileInputStream fis = new FileInputStream(file);
77 @SuppressWarnings("unchecked")
78 HashMap<String, String> propMap = new HashMap<String, String>((Map)prop);
80 mapOfMaps.put(file.getName(), propMap);
83 logger.info("File - " + file.getName() + " is loaded into the map and the "
84 + "corresponding system properties have been refreshed");
88 logger.error("File " + (file != null?file.getName():"") + " cannot be loaded into the map ", e);
89 throw new Exception("Error reading map file " + (file != null?file.getName():""), e);
94 * @param fileName fileName
95 * @param propertyKey propertyKey
98 public static String getProperty(String fileName, String propertyKey)
100 HashMap<String, String> propMap = mapOfMaps.get(fileName);
101 return propMap!=null?propMap.get(ifNullThenEmpty(propertyKey)):"";
105 * @param fileName fileName
108 public static HashMap<String, String> getProperties(String fileName){
109 return mapOfMaps.get(fileName);
112 private static String ifNullThenEmpty(String key) {