0ecdf2255df02bd1966051e92d804426f5d67aa2
[aai/aai-service.git] / ajsc-aai / src / main / java / org / openecomp / aai / ajsc_aai / filemonitor / ServicePropertiesMap.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 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  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.aai.ajsc_aai.filemonitor;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Properties;
28
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import com.fasterxml.jackson.core.type.TypeReference;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34
35 public class ServicePropertiesMap 
36 {
37         private static HashMap<String, HashMap<String, String>> mapOfMaps = new HashMap<String, HashMap<String, String>>();
38         static final Logger logger = LoggerFactory.getLogger(ServicePropertiesMap.class);
39         
40         /**
41          * Refresh.
42          *
43          * @param file the file
44          * @throws Exception the exception
45          */
46         public static void refresh(File file) throws Exception
47         {
48                 try
49                 {
50                         logger.info("Loading properties - " + (file != null?file.getName():""));
51                         
52                         //Store .json & .properties files into map of maps
53                         String filePath = file.getPath();
54                         
55                         if(filePath.lastIndexOf(".json")>0){
56                                 
57                                 ObjectMapper om = new ObjectMapper();
58                                 TypeReference<HashMap<String, String>> typeRef = new TypeReference<HashMap<String, String>>() {};
59                                 HashMap<String, String> propMap = om.readValue(file, typeRef);
60                                 HashMap<String, String> lcasePropMap = new HashMap<String, String>();
61                                 for (String key : propMap.keySet() )
62                                 {
63                                         String lcaseKey = ifNullThenEmpty(key);
64                                         lcasePropMap.put(lcaseKey, propMap.get(key));
65                                 }
66                                 
67                                 mapOfMaps.put(file.getName(), lcasePropMap);
68                                 
69                                 
70                         }else if(filePath.lastIndexOf(".properties")>0){
71                                 Properties prop = new Properties();
72                                 FileInputStream fis = new FileInputStream(file);
73                                 prop.load(fis);
74                                 
75                                 @SuppressWarnings("unchecked")
76                                 HashMap<String, String> propMap = new HashMap<String, String>((Map)prop);
77                                 
78                                 mapOfMaps.put(file.getName(), propMap);
79                         }
80
81                         logger.info("File - " + file.getName() + " is loaded into the map and the corresponding system properties have been refreshed");
82                 }
83                 catch (Exception e)
84                 {
85                         logger.error("File " + (file != null?file.getName():"") + " cannot be loaded into the map ", e);
86                         throw new Exception("Error reading map file " + (file != null?file.getName():""), e);
87                 }
88         }
89         
90         /**
91          * Gets the property.
92          *
93          * @param fileName the file name
94          * @param propertyKey the property key
95          * @return the property
96          */
97         public static String getProperty(String fileName, String propertyKey)
98         {
99                 HashMap<String, String> propMap = mapOfMaps.get(fileName);
100                 return propMap!=null?propMap.get(ifNullThenEmpty(propertyKey)):"";
101         }
102         
103         /**
104          * Gets the properties.
105          *
106          * @param fileName the file name
107          * @return the properties
108          */
109         public static HashMap<String, String> getProperties(String fileName){
110                 return mapOfMaps.get(fileName);
111         }
112         
113         /**
114          * If null then empty.
115          *
116          * @param key the key
117          * @return the string
118          */
119         private static String ifNullThenEmpty(String key) {
120                 if (key == null) {
121                         return "";
122                 } else {                                        
123                         return key;
124                 }               
125         }
126
127 }