From 7dedc836781b221c3a1945a8247be8ed23248a1e Mon Sep 17 00:00:00 2001 From: Guangrong Fu Date: Thu, 2 Dec 2021 09:52:12 +0800 Subject: Added exception protection. Added try-catch block to prevent the polling thread from stop. Issue-ID: HOLMES-488 Signed-off-by: Guangrong Fu Change-Id: Iebe943ab53e15428d90fe3ca271ca4a478936676 --- .../rulemgt/dcae/ConfigFileScanningTask.java | 68 ++++++++++++---------- 1 file changed, 37 insertions(+), 31 deletions(-) (limited to 'rulemgt/src/main/java/org/onap') diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/dcae/ConfigFileScanningTask.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/dcae/ConfigFileScanningTask.java index 9f7b89f..88ab637 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/dcae/ConfigFileScanningTask.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/dcae/ConfigFileScanningTask.java @@ -54,41 +54,46 @@ public class ConfigFileScanningTask implements Runnable { if (null == configFileScanner) { configFileScanner = new ConfigFileScanner(); } - Map newConfig = extractConfigItems(configFileScanner.scan(configFile)); - List deployedRules = getExistingRules(); - - // deal with newly added rules - final Set existingKeys = new HashSet(configInEffect.keySet()); - final Set newKeys = new HashSet(newConfig.keySet()); - newKeys.stream() - .filter(key -> !existingKeys.contains(key)) - .forEach(key -> { - if (deployRule(key, newConfig.get(key))) { - configInEffect.put(key, newConfig.get(key)); - LOGGER.info("Rule '{}' has been deployed.", key); - } - }); - - // deal with removed rules - existingKeys.stream().filter(key -> !newKeys.contains(key)).forEach(key -> { - if (deleteRule(find(deployedRules, key))) { - configInEffect.remove(key); - LOGGER.info("Rule '{}' has been removed.", key); - } - }); - - // deal with changed rules - existingKeys.stream().filter(key -> newKeys.contains(key)).forEach(key -> { - if (changed(configInEffect.get(key), newConfig.get(key))) { + try { + Map newConfig = extractConfigItems(configFileScanner.scan(configFile)); + + List deployedRules = getExistingRules(); + + // deal with newly added rules + final Set existingKeys = new HashSet(configInEffect.keySet()); + final Set newKeys = new HashSet(newConfig.keySet()); + newKeys.stream() + .filter(key -> !existingKeys.contains(key)) + .forEach(key -> { + if (deployRule(key, newConfig.get(key))) { + configInEffect.put(key, newConfig.get(key)); + LOGGER.info("Rule '{}' has been deployed.", key); + } + }); + + // deal with removed rules + existingKeys.stream().filter(key -> !newKeys.contains(key)).forEach(key -> { if (deleteRule(find(deployedRules, key))) { configInEffect.remove(key); - deployRule(key, newConfig.get(key)); - configInEffect.put(key, newConfig.get(key)); - LOGGER.info("Rule '{}' has been updated.", key); + LOGGER.info("Rule '{}' has been removed.", key); } - } - }); + }); + + // deal with changed rules + existingKeys.stream().filter(key -> newKeys.contains(key)).forEach(key -> { + if (changed(configInEffect.get(key), newConfig.get(key))) { + if (deleteRule(find(deployedRules, key))) { + configInEffect.remove(key); + deployRule(key, newConfig.get(key)); + configInEffect.put(key, newConfig.get(key)); + LOGGER.info("Rule '{}' has been updated.", key); + } + } + }); + } catch (Exception e) { + LOGGER.warn("Unhandled error: \n" + e.getMessage(), e); + } } private Map extractConfigItems(Map configFiles) { @@ -113,6 +118,7 @@ public class ConfigFileScanningTask implements Runnable { } return path; } + private String readFile(String path) { String finalPath = normalizePath(path); File file = new File(finalPath); -- cgit 1.2.3-korg