9bfcad0b754b14a9a7dbef3045be8bdc086110a6
[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 static HashMap<String, HashMap<String, String>> mapOfMaps = new HashMap<>();
36     private static final Logger logger = LoggerFactory.getLogger(ServicePropertiesMap.class);
37
38     /**
39      * Refresh.
40      *
41      * @param file the file
42      * @throws Exception the exception
43      */
44     public static void refresh(File file) throws Exception {
45         try {
46             logger.info(String.format("Loading properties - %s", file != null ? file.getName() : ""));
47
48             //Store .json & .properties files into map of maps
49             if (file != null) {
50                 String filePath = file.getPath();
51
52                 if (filePath.lastIndexOf(".json") > 0) {
53
54                     ObjectMapper om = new ObjectMapper();
55                     TypeReference<HashMap<String, String>> typeRef = new TypeReference<HashMap<String, String>>() {
56                     };
57                     HashMap<String, String> propMap = om.readValue(file, typeRef);
58                     HashMap<String, String> lcasePropMap = new HashMap<>();
59                     for (HashMap.Entry<String, String> entry : propMap.entrySet()) {
60                         String lcaseKey = ifNullThenEmpty(entry.getKey());
61                         lcasePropMap.put(lcaseKey, entry.getValue());
62                     }
63
64                     mapOfMaps.put(file.getName(), lcasePropMap);
65                 } else if (filePath.lastIndexOf(".properties") > 0) {
66                     Properties prop = new Properties();
67                     FileInputStream fis = new FileInputStream(file);
68                     prop.load(fis);
69
70                     @SuppressWarnings("unchecked")
71                     HashMap<String, String> propMap = new HashMap<String, String>((Map) prop);
72
73                     mapOfMaps.put(file.getName(), propMap);
74                 }
75
76                 logger.info("File - " + file.getName()
77                     + " is loaded into the map and the corresponding system properties have been refreshed");
78             } else {
79                 logger.error("File  cannot be loaded into the map ");
80             }
81         } catch (Exception e) {
82             logger.error("File " + (file != null ? file.getName() : "") + " cannot be loaded into the map ", e);
83             throw new Exception("Error reading map file " + (file != null ? file.getName() : ""), e);
84         }
85     }
86
87     /**
88      * Gets the property.
89      *
90      * @param fileName the file name
91      * @param propertyKey the property key
92      * @return the property
93      */
94     public static String getProperty(String fileName, String propertyKey) {
95         HashMap<String, String> propMap = mapOfMaps.get(fileName);
96         return propMap != null ? propMap.get(ifNullThenEmpty(propertyKey)) : "";
97     }
98
99     /**
100      * Gets the properties.
101      *
102      * @param fileName the file name
103      * @return the properties
104      */
105     public static HashMap<String, String> getProperties(String fileName) {
106         return mapOfMaps.get(fileName);
107     }
108
109     /**
110      * If null then empty.
111      *
112      * @param key the key
113      * @return the string
114      */
115     private static String ifNullThenEmpty(String key) {
116         return key == null ? "" : key;
117     }
118 }