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.sdc.common.impl;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
28 import org.apache.commons.collections.CollectionUtils;
29 import org.apache.commons.jci.listeners.FileChangeListener;
30 import org.openecomp.sdc.common.api.BasicConfiguration;
31 import org.openecomp.sdc.common.api.ConfigurationListener;
32 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
33 import org.openecomp.sdc.common.log.wrappers.Logger;
34 import org.openecomp.sdc.common.util.YamlToObjectConverter;
35 import org.openecomp.sdc.exception.YamlConversionException;
37 public class ConfigFileChangeListener extends FileChangeListener {
39 private static final Logger LOGGER = Logger.getLogger(ConfigFileChangeListener.class.getName());
41 private Map<String, List<ConfigurationListener>> fileChangeToCallBack = new HashMap<>();
43 private Object lock = new Object();
45 private YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
48 public void onFileChange(File pFile) {
49 super.onFileChange(pFile);
52 LOGGER.debug("Invalid file '{}'.", pFile);
56 if (fileChangeToCallBack == null) {
57 LOGGER.debug("File '{}' callback is null.", pFile);
61 final String id = findIdFromFileName(pFile.getName());
63 LOGGER.warn(EcompLoggerErrorCode.UNKNOWN_ERROR,"","",
64 "Cannot calculate id from file {}", pFile.getName());
68 final List<ConfigurationListener> listeners = fileChangeToCallBack.get(id);
69 if (CollectionUtils.isEmpty(listeners)) {
70 LOGGER.debug("No file listeners for file '{}', id '{}'.", pFile, id);
73 for (final ConfigurationListener configurationListener : listeners) {
74 final Class<? extends BasicConfiguration> configClass = configurationListener.getType();
75 final BasicConfiguration basicConfiguration;
77 basicConfiguration = yamlToObjectConverter.convert(pFile.getAbsolutePath(), configClass);
78 } catch (final YamlConversionException e) {
79 LOGGER.warn(EcompLoggerErrorCode.SCHEMA_ERROR,
80 "Configuration", "Configuration",
81 "Cannot update the listeners for file Change since the file content is invalid: {}",
82 e.getLocalizedMessage());
85 LOGGER.debug("Loaded configuration after converting is {}", basicConfiguration);
86 configurationListener.getCallBack().reconfigure(basicConfiguration);
88 LOGGER.debug("File {} was changed.", pFile);
91 private String findIdFromFileName(String name) {
96 int endIndex = name.length();
97 if (name.contains(File.separator)) {
98 startIndex = name.lastIndexOf(File.separator);
101 result = name.substring(startIndex, endIndex);
108 public void register(String id, ConfigurationListener configurationListener) {
110 if (configurationListener != null) {
112 synchronized (lock) {
114 List<ConfigurationListener> callbacks = fileChangeToCallBack.get(id);
115 if (callbacks == null) {
116 callbacks = new ArrayList<>();
117 fileChangeToCallBack.put(id, callbacks);
119 callbacks.add(configurationListener);