Initial OpenECOMP SDC commit
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / impl / ConfigFileChangeListener.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.common.impl;
22
23 import java.io.File;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
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;
35
36 public class ConfigFileChangeListener extends FileChangeListener {
37
38         private static Logger log = LoggerFactory.getLogger(ConfigFileChangeListener.class.getName());
39
40         private Map<String, List<ConfigurationListener>> fileChangeToCallBack = new HashMap<String, List<ConfigurationListener>>();
41
42         private Object lock = new Object();
43
44         private YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
45
46         @Override
47         public void onFileChange(File pFile) {
48
49                 super.onFileChange(pFile);
50
51                 if (pFile != null) {
52
53                         if (fileChangeToCallBack != null) {
54
55                                 String id = findIdFromFileName(pFile.getName());
56
57                                 if (id != null) {
58
59                                         List<ConfigurationListener> listeners = fileChangeToCallBack.get(id);
60                                         if (listeners != null) {
61                                                 for (ConfigurationListener configurationListener : listeners) {
62
63                                                         Class<? extends BasicConfiguration> configClass = configurationListener.getType();
64
65                                                         BasicConfiguration basicConfiguration = yamlToObjectConverter
66                                                                         .convert(pFile.getAbsolutePath(), configClass);
67
68                                                         if (basicConfiguration == null) {
69                                                                 log.warn(
70                                                                                 "Cannot update the listeners for file Change since the file content is invalid");
71                                                                 continue;
72                                                         }
73                                                         log.debug("Loaded configuration after converting is " + basicConfiguration);
74                                                         // System.out.println("New configuration is " +
75                                                         // basicConfiguration);
76
77                                                         configurationListener.getCallBack().reconfigure(basicConfiguration);
78
79                                                 }
80                                         }
81                                 } else {
82
83                                         log.warn("Cannot calculate id from file " + pFile.getName());
84                                 }
85                         }
86
87                 }
88
89                 log.debug("File {} was changed.", pFile);
90         }
91
92         private String findIdFromFileName(String name) {
93
94                 String result = null;
95                 if (name != null) {
96                         int startIndex = 0;
97                         int endIndex = name.length();
98                         if (name.contains(File.separator)) {
99                                 startIndex = name.lastIndexOf(File.separator);
100                         }
101                         // String subNameString = name.substring(startIndex, endIndex);
102                         // if (subNameString.contains(".")) {
103                         // endIndex = subNameString.indexOf(".");
104                         // }
105
106                         result = name.substring(startIndex, endIndex);
107
108                 }
109
110                 return result;
111         }
112
113         public void register(String id, ConfigurationListener configurationListener) {
114
115                 if (configurationListener != null) {
116
117                         synchronized (lock) {
118
119                                 List<ConfigurationListener> callbacks = fileChangeToCallBack.get(id);
120                                 if (callbacks == null) {
121                                         callbacks = new ArrayList<ConfigurationListener>();
122                                         fileChangeToCallBack.put(id, callbacks);
123                                 }
124                                 callbacks.add(configurationListener);
125
126                         }
127
128                 }
129
130         }
131
132         // public void notify(String id, BasicConfiguration object) {
133         //
134         // if (fileChangeToCallBack != null) {
135         // List<ConfigurationListener> listeners = fileChangeToCallBack
136         // .get(id);
137         // if (listeners != null) {
138         // for (ConfigurationListener configurationListener : listeners) {
139         //
140         // configurationListener.getCallBack().reconfigure(object);
141         //
142         // }
143         // }
144         // }
145         //
146         // }
147
148 }