2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.aai.ajsc_aai.filemonitor;
23 import com.fasterxml.jackson.core.type.TypeReference;
24 import com.fasterxml.jackson.databind.ObjectMapper;
26 import java.io.FileInputStream;
27 import java.util.HashMap;
29 import java.util.Properties;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class ServicePropertiesMap {
35 private static HashMap<String, HashMap<String, String>> mapOfMaps = new HashMap<>();
36 private static final Logger logger = LoggerFactory.getLogger(ServicePropertiesMap.class);
41 * @param file the file
42 * @throws Exception the exception
44 public static void refresh(File file) throws Exception {
46 logger.info(String.format("Loading properties - %s", file != null ? file.getName() : ""));
48 //Store .json & .properties files into map of maps
50 String filePath = file.getPath();
52 if (filePath.lastIndexOf(".json") > 0) {
54 ObjectMapper om = new ObjectMapper();
55 TypeReference<HashMap<String, String>> typeRef = new TypeReference<HashMap<String, String>>() {
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());
64 mapOfMaps.put(file.getName(), lcasePropMap);
65 } else if (filePath.lastIndexOf(".properties") > 0) {
66 Properties prop = new Properties();
67 FileInputStream fis = new FileInputStream(file);
70 @SuppressWarnings("unchecked")
71 HashMap<String, String> propMap = new HashMap<String, String>((Map) prop);
73 mapOfMaps.put(file.getName(), propMap);
76 logger.info("File - " + file.getName()
77 + " is loaded into the map and the corresponding system properties have been refreshed");
79 logger.error("File cannot be loaded into the map ");
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);
90 * @param fileName the file name
91 * @param propertyKey the property key
92 * @return the property
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)) : "";
100 * Gets the properties.
102 * @param fileName the file name
103 * @return the properties
105 public static HashMap<String, String> getProperties(String fileName) {
106 return mapOfMaps.get(fileName);
110 * If null then empty.
115 private static String ifNullThenEmpty(String key) {
116 return key == null ? "" : key;