67b9e043dd32cdc1ad082f9d0bb74937cb5853f6
[dmaap/messagerouter/messageservice.git] / src / main / java / com / att / nsa / dmaap / filemonitor / ServicePropertiesMap.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
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
11 *  
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=========================================================
18  *  
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22 package com.att.nsa.dmaap.filemonitor;
23
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.Properties;
29
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;
34
35 /**
36  * ServicePropertiesMap class
37  * @author rajashree.khare
38  *
39  */
40 @SuppressWarnings("squid:S1118") 
41 public class ServicePropertiesMap 
42 {
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);
46
47         private static final EELFLogger logger = EELFManager.getInstance().getLogger(ServicePropertiesMap.class);
48         /**
49          * refresh method
50          * @param file file
51          * @throws Exception ex
52          */
53         public static void refresh(File file) throws Exception
54         {
55                 try
56                 {
57                         logger.info("Loading properties - " + (file != null?file.getName():""));
58                         
59                         //Store .json & .properties files into map of maps
60                         String filePath = file.getPath();
61                         
62                         if(filePath.lastIndexOf(".json")>0){
63                                 
64                                 ObjectMapper om = new ObjectMapper();
65                                 TypeReference<HashMap<String, String>> typeRef = 
66                                                 new TypeReference<HashMap<String, String>>() {};
67                                 HashMap<String, String> propMap = om.readValue(file, typeRef);
68                                 HashMap<String, String> lcasePropMap = new HashMap<String, String>();
69                                 for (String key : propMap.keySet() )
70                                 {
71                                         String lcaseKey = ifNullThenEmpty(key);
72                                         lcasePropMap.put(lcaseKey, propMap.get(key));
73                                 }
74                                 
75                                 mapOfMaps.put(file.getName(), lcasePropMap);
76                                 
77                                 
78                         }else if(filePath.lastIndexOf(".properties")>0){
79                                 Properties prop = new Properties();
80                                 FileInputStream fis = new FileInputStream(file);
81                                 prop.load(fis);
82                                 
83                                 @SuppressWarnings("unchecked")
84                                 HashMap<String, String> propMap = new HashMap<String, String>((Map)prop);
85                                 
86                                 mapOfMaps.put(file.getName(), propMap);
87                         }
88
89                         logger.info("File - " + file.getName() + " is loaded into the map and the "
90                                         + "corresponding system properties have been refreshed");
91                 }
92                 catch (Exception e)
93                 {
94                         logger.error("File " + (file != null?file.getName():"") + " cannot be loaded into the map ", e);
95                         throw new Exception("Error reading map file " + (file != null?file.getName():""), e);
96                 }
97         }
98         /**
99          * Get property
100          * @param fileName fileName
101          * @param propertyKey propertyKey
102          * @return str
103          */
104         public static String getProperty(String fileName, String propertyKey)
105         {
106                 HashMap<String, String> propMap = mapOfMaps.get(fileName);
107                 return propMap!=null?propMap.get(ifNullThenEmpty(propertyKey)):"";
108         }
109         /**
110          * get properties
111          * @param fileName fileName
112          * @return mapProp
113          */
114         public static HashMap<String, String> getProperties(String fileName){
115                 return mapOfMaps.get(fileName);
116         }
117         
118         private static String ifNullThenEmpty(String key) {
119                 if (key == null) {
120                         return "";
121                 } else {                                        
122                         return key;
123                 }               
124         }
125
126 }