+public class ServicePropertiesMap {
+
+ private static HashMap<String, HashMap<String, String>> mapOfMaps = new HashMap<>();
+ private static final Logger logger = LoggerFactory.getLogger(ServicePropertiesMap.class);
+
+ /**
+ * Refresh.
+ *
+ * @param file the file
+ * @throws Exception the exception
+ */
+ public static void refresh(File file) throws Exception {
+ try {
+ logger.info(String.format("Loading properties - %s", file != null ? file.getName() : ""));
+
+ //Store .json & .properties files into map of maps
+ if (file != null) {
+ String filePath = file.getPath();
+
+ if (filePath.lastIndexOf(".json") > 0) {
+
+ ObjectMapper om = new ObjectMapper();
+ TypeReference<HashMap<String, String>> typeRef = new TypeReference<HashMap<String, String>>() {
+ };
+ HashMap<String, String> propMap = om.readValue(file, typeRef);
+ HashMap<String, String> lcasePropMap = new HashMap<>();
+ for (HashMap.Entry<String, String> entry : propMap.entrySet()) {
+ String lcaseKey = ifNullThenEmpty(entry.getKey());
+ lcasePropMap.put(lcaseKey, entry.getValue());
+ }
+
+ mapOfMaps.put(file.getName(), lcasePropMap);
+ } else if (filePath.lastIndexOf(".properties") > 0) {
+ Properties prop = new Properties();
+ FileInputStream fis = new FileInputStream(file);
+ prop.load(fis);
+
+ @SuppressWarnings("unchecked")
+ HashMap<String, String> propMap = new HashMap<String, String>((Map) prop);
+
+ mapOfMaps.put(file.getName(), propMap);
+ }
+
+ logger.info("File - " + file.getName()
+ + " is loaded into the map and the corresponding system properties have been refreshed");
+ } else {
+ logger.error("File cannot be loaded into the map ");
+ }
+ } catch (Exception e) {
+ logger.error("File " + (file != null ? file.getName() : "") + " cannot be loaded into the map ", e);
+ throw new Exception("Error reading map file " + (file != null ? file.getName() : ""), e);
+ }
+ }