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;
23 import org.apache.commons.jci.listeners.FileChangeListener;
24 import org.openecomp.sdc.common.api.BasicConfiguration;
25 import org.openecomp.sdc.common.api.ConfigurationListener;
26 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
27 import org.openecomp.sdc.common.log.wrappers.Logger;
28 import org.openecomp.sdc.common.util.YamlToObjectConverter;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
36 public class ConfigFileChangeListener extends FileChangeListener {
38 private static Logger log = Logger.getLogger(ConfigFileChangeListener.class.getName());
40 private Map<String, List<ConfigurationListener>> fileChangeToCallBack = new HashMap<>();
42 private Object lock = new Object();
44 private YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
47 public void onFileChange(File pFile) {
49 super.onFileChange(pFile);
53 if (fileChangeToCallBack != null) {
55 String id = findIdFromFileName(pFile.getName());
59 List<ConfigurationListener> listeners = fileChangeToCallBack.get(id);
60 if (listeners != null) {
61 for (ConfigurationListener configurationListener : listeners) {
63 Class<? extends BasicConfiguration> configClass = configurationListener.getType();
65 BasicConfiguration basicConfiguration = yamlToObjectConverter.convert(pFile.getAbsolutePath(), configClass);
67 if (basicConfiguration == null) {
68 log.warn(EcompLoggerErrorCode.UNKNOWN_ERROR, "", "", "Cannot update the listeners for file Change since the file content is invalid");
71 log.debug("Loaded configuration after converting is {}", basicConfiguration);
74 configurationListener.getCallBack().reconfigure(basicConfiguration);
80 log.warn(EcompLoggerErrorCode.UNKNOWN_ERROR, "", "", "Cannot calculate id from file {}", pFile.getName());
86 log.debug("File {} was changed.", pFile);
89 private String findIdFromFileName(String name) {
94 int endIndex = name.length();
95 if (name.contains(File.separator)) {
96 startIndex = name.lastIndexOf(File.separator);
98 // String subNameString = name.substring(startIndex, endIndex);
99 // if (subNameString.contains(".")) {
100 // endIndex = subNameString.indexOf(".");
103 result = name.substring(startIndex, endIndex);
110 public void register(String id, ConfigurationListener configurationListener) {
112 if (configurationListener != null) {
114 synchronized (lock) {
116 List<ConfigurationListener> callbacks = fileChangeToCallBack.get(id);
117 if (callbacks == null) {
118 callbacks = new ArrayList<>();
119 fileChangeToCallBack.put(id, callbacks);
121 callbacks.add(configurationListener);
129 // public void notify(String id, BasicConfiguration object) {
131 // if (fileChangeToCallBack != null) {
132 // List<ConfigurationListener> listeners = fileChangeToCallBack
134 // if (listeners != null) {
135 // for (ConfigurationListener configurationListener : listeners) {
137 // configurationListener.getCallBack().reconfigure(object);