update the package name
[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.util.HashMap;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28
29 /**
30  * ServicePropertiesMap class
31  * @author rajashree.khare
32  *
33  */
34 @SuppressWarnings("squid:S1118") 
35 public class ServicePropertiesMap 
36 {
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);
40
41         private static final EELFLogger logger = EELFManager.getInstance().getLogger(ServicePropertiesMap.class);
42         /**
43          * refresh method
44          * @param file file
45          * @throws Exception ex
46          */
47         /*public static void refresh(File file) throws Exception
48         {
49                 try
50                 {
51                         logger.info("Loading properties - " + (file != null?file.getName():""));
52                         
53                         //Store .json & .properties files into map of maps
54                         String filePath = file.getPath();
55                         
56                         if(filePath.lastIndexOf(".json")>0){
57                                 
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() )
64                                 {
65                                         String lcaseKey = ifNullThenEmpty(key);
66                                         lcasePropMap.put(lcaseKey, propMap.get(key));
67                                 }
68                                 
69                                 mapOfMaps.put(file.getName(), lcasePropMap);
70                                 
71                                 
72                         }else if(filePath.lastIndexOf(".properties")>0){
73                                 Properties prop = new Properties();
74                                 FileInputStream fis = new FileInputStream(file);
75                                 prop.load(fis);
76                                 
77                                 @SuppressWarnings("unchecked")
78                                 HashMap<String, String> propMap = new HashMap<String, String>((Map)prop);
79                                 
80                                 mapOfMaps.put(file.getName(), propMap);
81                         }
82
83                         logger.info("File - " + file.getName() + " is loaded into the map and the "
84                                         + "corresponding system properties have been refreshed");
85                 }
86                 catch (Exception e)
87                 {
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);
90                 }
91         }*/
92         /**
93          * Get property
94          * @param fileName fileName
95          * @param propertyKey propertyKey
96          * @return str
97          */
98         public static String getProperty(String fileName, String propertyKey)
99         {
100                 HashMap<String, String> propMap = mapOfMaps.get(fileName);
101                 return propMap!=null?propMap.get(ifNullThenEmpty(propertyKey)):"";
102         }
103         /**
104          * get properties
105          * @param fileName fileName
106          * @return mapProp
107          */
108         public static HashMap<String, String> getProperties(String fileName){
109                 return mapOfMaps.get(fileName);
110         }
111         
112         private static String ifNullThenEmpty(String key) {
113                 if (key == null) {
114                         return "";
115                 } else {                                        
116                         return key;
117                 }               
118         }
119
120 }