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;
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.util.YamlToObjectConverter;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class ConfigFileChangeListener extends FileChangeListener {
38 private static Logger log = LoggerFactory.getLogger(ConfigFileChangeListener.class.getName());
40 private Map<String, List<ConfigurationListener>> fileChangeToCallBack = new HashMap<String, List<ConfigurationListener>>();
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
66 .convert(pFile.getAbsolutePath(), configClass);
68 if (basicConfiguration == null) {
70 "Cannot update the listeners for file Change since the file content is invalid");
73 log.debug("Loaded configuration after converting is " + basicConfiguration);
74 // System.out.println("New configuration is " +
75 // basicConfiguration);
77 configurationListener.getCallBack().reconfigure(basicConfiguration);
83 log.warn("Cannot calculate id from file " + pFile.getName());
89 log.debug("File {} was changed.", pFile);
92 private String findIdFromFileName(String name) {
97 int endIndex = name.length();
98 if (name.contains(File.separator)) {
99 startIndex = name.lastIndexOf(File.separator);
101 // String subNameString = name.substring(startIndex, endIndex);
102 // if (subNameString.contains(".")) {
103 // endIndex = subNameString.indexOf(".");
106 result = name.substring(startIndex, endIndex);
113 public void register(String id, ConfigurationListener configurationListener) {
115 if (configurationListener != null) {
117 synchronized (lock) {
119 List<ConfigurationListener> callbacks = fileChangeToCallBack.get(id);
120 if (callbacks == null) {
121 callbacks = new ArrayList<ConfigurationListener>();
122 fileChangeToCallBack.put(id, callbacks);
124 callbacks.add(configurationListener);
132 // public void notify(String id, BasicConfiguration object) {
134 // if (fileChangeToCallBack != null) {
135 // List<ConfigurationListener> listeners = fileChangeToCallBack
137 // if (listeners != null) {
138 // for (ConfigurationListener configurationListener : listeners) {
140 // configurationListener.getCallBack().reconfigure(object);