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;
25 import java.io.FileInputStream;
26 import java.util.HashMap;
28 import java.util.Properties;
30 import com.att.eelf.configuration.EELFLogger;
31 import com.att.eelf.configuration.EELFManager;
32 import com.fasterxml.jackson.core.type.TypeReference;
33 import com.fasterxml.jackson.databind.ObjectMapper;
36 * ServicePropertiesMap class
40 @SuppressWarnings("squid:S1118")
41 public class ServicePropertiesMap
43 private static HashMap<String, HashMap<String, String>> mapOfMaps =
44 new HashMap<String, HashMap<String, String>>();
45 // static final Logger logger = LoggerFactory.getLogger(ServicePropertiesMap.class);
47 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ServicePropertiesMap.class);
51 * @throws Exception ex
53 public static void refresh(File file) throws Exception
55 String filePath= null;
58 logger.info("Loading properties - " + (file != null?file.getName():""));
60 //Store .json & .properties files into map of maps
62 filePath = file.getPath();
65 if(filePath.lastIndexOf(".json")>0){
67 ObjectMapper om = new ObjectMapper();
68 TypeReference<HashMap<String, String>> typeRef =
69 new TypeReference<HashMap<String, String>>() {};
70 HashMap<String, String> propMap = om.readValue(file, typeRef);
71 HashMap<String, String> lcasePropMap = new HashMap<>();
72 for (Map.Entry<String,String> entry : propMap.entrySet())
74 String key = entry.getKey();
75 String lcaseKey = ifNullThenEmpty(key);
76 lcasePropMap.put(lcaseKey, propMap.get(key));
79 mapOfMaps.put(file.getName(), lcasePropMap);
82 }else if(filePath.lastIndexOf(".properties")>0){
83 Properties prop = new Properties();
84 FileInputStream fis = new FileInputStream(file);
87 @SuppressWarnings("unchecked")
88 HashMap<String, String> propMap = new HashMap<>((Map)prop);
90 mapOfMaps.put(file.getName(), propMap);
93 logger.info("File - " + file.getName() + " is loaded into the map and the "
94 + "corresponding system properties have been refreshed");
98 logger.error("File " + (file != null?file.getName():"") + " cannot be loaded into the map ", e);
99 throw new Exception("Error reading map file " + (file != null?file.getName():""), e);
104 * @param fileName fileName
105 * @param propertyKey propertyKey
108 public static String getProperty(String fileName, String propertyKey)
110 HashMap<String, String> propMap = mapOfMaps.get(fileName);
111 return propMap!=null?propMap.get(ifNullThenEmpty(propertyKey)):"";
115 * @param fileName fileName
118 public static Map<String, String> getProperties(String fileName){
119 return mapOfMaps.get(fileName);
122 private static String ifNullThenEmpty(String key) {