X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=common-app-api%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenecomp%2Fsdc%2Fcommon%2Fimpl%2FConfigFileChangeListener.java;h=c94db9a3c9f6bfcec63fc80c24fd31d985fa8303;hb=4aa20bc42b7bd98dde15f7594084669eb92412c2;hp=e243bac730df70b8c0113a556181758193021e9a;hpb=b2b6accda7c04a5df5029dcf250b8138c231f38e;p=sdc.git diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/impl/ConfigFileChangeListener.java b/common-app-api/src/main/java/org/openecomp/sdc/common/impl/ConfigFileChangeListener.java index e243bac730..c94db9a3c9 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/impl/ConfigFileChangeListener.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/impl/ConfigFileChangeListener.java @@ -20,22 +20,23 @@ package org.openecomp.sdc.common.impl; +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.commons.collections.CollectionUtils; import org.apache.commons.jci.listeners.FileChangeListener; import org.openecomp.sdc.common.api.BasicConfiguration; import org.openecomp.sdc.common.api.ConfigurationListener; import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode; import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.common.util.YamlToObjectConverter; - -import java.io.File; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import org.openecomp.sdc.exception.YamlConversionException; public class ConfigFileChangeListener extends FileChangeListener { - private static Logger log = Logger.getLogger(ConfigFileChangeListener.class.getName()); + private static final Logger LOGGER = Logger.getLogger(ConfigFileChangeListener.class.getName()); private Map> fileChangeToCallBack = new HashMap<>(); @@ -45,45 +46,46 @@ public class ConfigFileChangeListener extends FileChangeListener { @Override public void onFileChange(File pFile) { - super.onFileChange(pFile); - if (pFile != null) { - - if (fileChangeToCallBack != null) { - - String id = findIdFromFileName(pFile.getName()); - - if (id != null) { - - List listeners = fileChangeToCallBack.get(id); - if (listeners != null) { - for (ConfigurationListener configurationListener : listeners) { - - Class configClass = configurationListener.getType(); - - BasicConfiguration basicConfiguration = yamlToObjectConverter.convert(pFile.getAbsolutePath(), configClass); - - if (basicConfiguration == null) { - log.warn(EcompLoggerErrorCode.UNKNOWN_ERROR,"","","Cannot update the listeners for file Change since the file content is invalid"); - continue; - } - log.debug("Loaded configuration after converting is {}", basicConfiguration); - + if (pFile == null) { + LOGGER.debug("Invalid file '{}'.", pFile); + return; + } - configurationListener.getCallBack().reconfigure(basicConfiguration); + if (fileChangeToCallBack == null) { + LOGGER.debug("File '{}' callback is null.", pFile); + return; + } - } - } - } else { + final String id = findIdFromFileName(pFile.getName()); + if (id == null) { + LOGGER.warn(EcompLoggerErrorCode.UNKNOWN_ERROR,"","", + "Cannot calculate id from file {}", pFile.getName()); + return; + } - log.warn(EcompLoggerErrorCode.UNKNOWN_ERROR,"","","Cannot calculate id from file {}", pFile.getName()); - } + final List listeners = fileChangeToCallBack.get(id); + if (CollectionUtils.isEmpty(listeners)) { + LOGGER.debug("No file listeners for file '{}', id '{}'.", pFile, id); + return; + } + for (final ConfigurationListener configurationListener : listeners) { + final Class configClass = configurationListener.getType(); + final BasicConfiguration basicConfiguration; + try { + basicConfiguration = yamlToObjectConverter.convert(pFile.getAbsolutePath(), configClass); + } catch (final YamlConversionException e) { + LOGGER.warn(EcompLoggerErrorCode.SCHEMA_ERROR, + "Configuration", "Configuration", + "Cannot update the listeners for file Change since the file content is invalid: {}", + e.getLocalizedMessage()); + continue; } - + LOGGER.debug("Loaded configuration after converting is {}", basicConfiguration); + configurationListener.getCallBack().reconfigure(basicConfiguration); } - - log.debug("File {} was changed.", pFile); + LOGGER.debug("File {} was changed.", pFile); } private String findIdFromFileName(String name) { @@ -95,10 +97,6 @@ public class ConfigFileChangeListener extends FileChangeListener { if (name.contains(File.separator)) { startIndex = name.lastIndexOf(File.separator); } - // String subNameString = name.substring(startIndex, endIndex); - // if (subNameString.contains(".")) { - // endIndex = subNameString.indexOf("."); - // } result = name.substring(startIndex, endIndex); @@ -126,20 +124,4 @@ public class ConfigFileChangeListener extends FileChangeListener { } - // public void notify(String id, BasicConfiguration object) { - // - // if (fileChangeToCallBack != null) { - // List listeners = fileChangeToCallBack - // .get(id); - // if (listeners != null) { - // for (ConfigurationListener configurationListener : listeners) { - // - // configurationListener.getCallBack().reconfigure(object); - // - // } - // } - // } - // - // } - }