Replace type specification with diamond operato
[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 com.fasterxml.jackson.core.type.TypeReference;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.Properties;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class ServicePropertiesMap {
34
35     private ServicePropertiesMap {
36     }
37
38     private static HashMap<String, HashMap<String, String>> mapOfMaps = new HashMap<>();
39     private static final Logger logger = LoggerFactory.getLogger(ServicePropertiesMap.class);
40
41     /**
42      * Refresh.
43      *
44      * @param file the file
45      * @throws Exception the exception
46      */
47     public static void refresh(File file) throws Exception {
48         try {
49             logger.info(String.format("Loading properties - %s", file != null ? file.getName() : ""));
50
51             //Store .json & .properties files into map of maps
52             if (file != null) {
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                     };
60                     HashMap<String, String> propMap = om.readValue(file, typeRef);
61                     HashMap<String, String> lcasePropMap = new HashMap<>();
62                     for (HashMap.Entry<String, String> entry : propMap.entrySet()) {
63                         String lcaseKey = ifNullThenEmpty(entry.getKey());
64                         lcasePropMap.put(lcaseKey, entry.getValue());
65                     }
66
67                     mapOfMaps.put(file.getName(), lcasePropMap);
68                 } else if (filePath.lastIndexOf(".properties") > 0) {
69                     Properties prop = new Properties();
70                     FileInputStream fis = new FileInputStream(file);
71                     prop.load(fis);
72
73                     @SuppressWarnings("unchecked")
74                     HashMap<String, String> propMap = new HashMap<>((Map) prop);
75
76                     mapOfMaps.put(file.getName(), propMap);
77                 }
78
79                 logger.info("File - " + file.getName()
80                     + " is loaded into the map and the corresponding system properties have been refreshed");
81             } else {
82                 logger.error("File  cannot be loaded into the map ");
83             }
84         } catch (Exception e) {
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         HashMap<String, String> propMap = mapOfMaps.get(fileName);
99         return propMap != null ? propMap.get(ifNullThenEmpty(propertyKey)) : "";
100     }
101
102     /**
103      * Gets the properties.
104      *
105      * @param fileName the file name
106      * @return the properties
107      */
108     public static HashMap<String, String> getProperties(String fileName) {
109         return mapOfMaps.get(fileName);
110     }
111
112     /**
113      * If null then empty.
114      *
115      * @param key the key
116      * @return the string
117      */
118     private static String ifNullThenEmpty(String key) {
119         return key == null ? "" : key;
120     }
121 }