fixing warnings from checkstyle in common-app-api
[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 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;
29
30 import java.io.File;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35
36 public class ConfigFileChangeListener extends FileChangeListener {
37
38     private static Logger log = Logger.getLogger(ConfigFileChangeListener.class.getName());
39
40     private Map<String, List<ConfigurationListener>> fileChangeToCallBack = new HashMap<>();
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.convert(pFile.getAbsolutePath(), configClass);
66
67                             if (basicConfiguration == null) {
68                                 log.warn(EcompLoggerErrorCode.UNKNOWN_ERROR, "", "", "Cannot update the listeners for file Change since the file content is invalid");
69                                 continue;
70                             }
71                             log.debug("Loaded configuration after converting is {}", basicConfiguration);
72
73
74                             configurationListener.getCallBack().reconfigure(basicConfiguration);
75
76                         }
77                     }
78                 } else {
79
80                     log.warn(EcompLoggerErrorCode.UNKNOWN_ERROR, "", "", "Cannot calculate id from file {}", pFile.getName());
81                 }
82             }
83
84         }
85
86         log.debug("File {} was changed.", pFile);
87     }
88
89     private String findIdFromFileName(String name) {
90
91         String result = null;
92         if (name != null) {
93             int startIndex = 0;
94             int endIndex = name.length();
95             if (name.contains(File.separator)) {
96                 startIndex = name.lastIndexOf(File.separator);
97             }
98             // String subNameString = name.substring(startIndex, endIndex);
99             // if (subNameString.contains(".")) {
100             // endIndex = subNameString.indexOf(".");
101             // }
102
103             result = name.substring(startIndex, endIndex);
104
105         }
106
107         return result;
108     }
109
110     public void register(String id, ConfigurationListener configurationListener) {
111
112         if (configurationListener != null) {
113
114             synchronized (lock) {
115
116                 List<ConfigurationListener> callbacks = fileChangeToCallBack.get(id);
117                 if (callbacks == null) {
118                     callbacks = new ArrayList<>();
119                     fileChangeToCallBack.put(id, callbacks);
120                 }
121                 callbacks.add(configurationListener);
122
123             }
124
125         }
126
127     }
128
129     // public void notify(String id, BasicConfiguration object) {
130     //
131     // if (fileChangeToCallBack != null) {
132     // List<ConfigurationListener> listeners = fileChangeToCallBack
133     // .get(id);
134     // if (listeners != null) {
135     // for (ConfigurationListener configurationListener : listeners) {
136     //
137     // configurationListener.getCallBack().reconfigure(object);
138     //
139     // }
140     // }
141     // }
142     //
143     // }
144
145 }