Merge "Fix doc8"
[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 author
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                 String filePath= null;
56                 try
57                 {
58                         logger.info("Loading properties - " + (file != null?file.getName():""));
59                         
60                         //Store .json & .properties files into map of maps
61                         if (file != null) {
62                                 filePath = file.getPath();
63                         }
64                         
65                         if(filePath != null) {
66                         if(filePath.lastIndexOf(".json")>0){
67                                 
68                                 ObjectMapper om = new ObjectMapper();
69                                 TypeReference<HashMap<String, String>> typeRef = 
70                                                 new TypeReference<HashMap<String, String>>() {};
71                                 HashMap<String, String> propMap = om.readValue(file, typeRef);
72                                 HashMap<String, String> lcasePropMap = new HashMap<>();
73                                 for (Map.Entry<String,String> entry : propMap.entrySet())
74                                 {
75                                         String key = entry.getKey();
76                                         String lcaseKey = ifNullThenEmpty(key);
77                                         lcasePropMap.put(lcaseKey, propMap.get(key));
78                                 }
79                                 
80                                 mapOfMaps.put(file.getName(), lcasePropMap);
81                                 
82                                 
83                         }else if(filePath.lastIndexOf(".properties")>0){
84                                 Properties prop = new Properties();
85                                 FileInputStream fis = new FileInputStream(file);
86                                 prop.load(fis);
87                                 
88                                 @SuppressWarnings("unchecked")
89                                 HashMap<String, String> propMap = new HashMap<>((Map)prop);
90                                 
91                                 mapOfMaps.put(file.getName(), propMap);
92                         }
93                         }
94
95                         logger.info("File - " + file.getName() + " is loaded into the map and the "
96                                         + "corresponding system properties have been refreshed");
97                 }
98                 catch (Exception e)
99                 {
100                         logger.error("File " + (file != null?file.getName():"") + " cannot be loaded into the map ", e);
101                         throw new Exception("Error reading map file " + (file != null?file.getName():""), e);
102                 }
103         }
104         /**
105          * Get property
106          * @param fileName fileName
107          * @param propertyKey propertyKey
108          * @return str
109          */
110         public static String getProperty(String fileName, String propertyKey)
111         {
112                 HashMap<String, String> propMap = mapOfMaps.get(fileName);
113                 return propMap!=null?propMap.get(ifNullThenEmpty(propertyKey)):"";
114         }
115         /**
116          * get properties
117          * @param fileName fileName
118          * @return mapProp
119          */
120         public static Map<String, String> getProperties(String fileName){
121                 return mapOfMaps.get(fileName);
122         }
123         
124         private static String ifNullThenEmpty(String key) {
125                 if (key == null) {
126                         return "";
127                 } else {                                        
128                         return key;
129                 }               
130         }
131
132 }