Merge "Removed unused variables"
[so.git] / bpmn / MSOCoreBPMN / src / main / java / org / openecomp / mso / bpmn / core / PropertyConfiguration.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.mso.bpmn.core;
23
24 import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
25
26 import java.io.File;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.nio.file.ClosedWatchServiceException;
30 import java.nio.file.FileSystems;
31 import java.nio.file.Path;
32 import java.nio.file.WatchEvent;
33 import java.nio.file.WatchKey;
34 import java.nio.file.WatchService;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Map.Entry;
42 import java.util.Properties;
43 import java.util.Timer;
44 import java.util.TimerTask;
45 import java.util.concurrent.ConcurrentHashMap;
46
47 import org.slf4j.MDC;
48
49 import org.openecomp.mso.logger.MessageEnum;
50 import org.openecomp.mso.logger.MsoLogger;
51
52 /**
53  * Loads the property configuration from file system and refreshes the
54  * properties when the property gets changed.
55  *
56  * WARNING: automatic refreshes might not work on network filesystems.
57  */
58 public class PropertyConfiguration {
59
60         /**
61          * The base name of the MSO BPMN properties file (mso.bpmn.properties).
62          */
63         public static final String MSO_BPMN_PROPERTIES = "mso.bpmn.properties";
64
65         /**
66          * The base name of the MSO BPMN URN-Mappings properties file (mso.bpmn.urn.properties).
67          */
68         public static final String MSO_BPMN_URN_PROPERTIES = "mso.bpmn.urn.properties";
69
70         /**
71          * The base name of the MSO Topology properties file (topology.properties).
72          */
73         public static final String MSO_TOPOLOGY_PROPERTIES = "topology.properties";
74         /**
75          * The name of the meta-property holding the time the properties were loaded
76          * from the file.
77          */
78         public static final String TIMESTAMP_PROPERTY = "mso.properties.timestamp";
79
80         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
81
82         private static final List<String> SUPPORTED_FILES =
83                 Arrays.asList(MSO_BPMN_PROPERTIES, MSO_BPMN_URN_PROPERTIES, MSO_TOPOLOGY_PROPERTIES);
84
85         private volatile String msoConfigPath = null;
86
87         private final ConcurrentHashMap<String, Map<String, String>> propFileCache =
88                 new ConcurrentHashMap<>();
89
90         private final Object CACHELOCK = new Object();
91         private FileWatcherThread fileWatcherThread = null;
92
93         // The key is the file name
94         private Map<String, TimerTask> timerTaskMap = new HashMap<>();
95
96         /**
97      * Private Constructor.
98      */
99     private PropertyConfiguration() {
100         startUp();
101     }
102                         
103         /**
104          * Singleton holder pattern eliminates locking when accessing the instance
105          * and still provides for lazy initialization.
106          */
107         private static class PropertyConfigurationInstanceHolder {
108                 private static PropertyConfiguration instance = new PropertyConfiguration();
109         }
110
111         /**
112          * Gets the one and only instance of this class.
113          */
114         public static PropertyConfiguration getInstance() {
115                 return PropertyConfigurationInstanceHolder.instance;
116         }
117
118         /**
119          * Returns the list of supported files.
120          */
121         public static List<String> supportedFiles() {
122                 return new ArrayList<>(SUPPORTED_FILES);
123         }
124
125         /**
126          * May be called to restart the PropertyConfiguration if it was previously shut down.
127          */
128         public synchronized void startUp() {
129                 msoConfigPath = System.getProperty("mso.config.path");
130
131                 if (msoConfigPath == null) {
132                         LOGGER.debug("mso.config.path JVM system property is not set");
133                         return;
134                 }
135
136                 try {
137                         Path directory = FileSystems.getDefault().getPath(msoConfigPath);
138                         WatchService watchService = FileSystems.getDefault().newWatchService();
139                         directory.register(watchService, ENTRY_MODIFY);
140
141                         LOGGER.info(MessageEnum.BPMN_GENERAL_INFO, "BPMN", "Starting FileWatcherThread");
142                         LOGGER.debug("Starting FileWatcherThread");
143                         fileWatcherThread = new FileWatcherThread(watchService);
144                         fileWatcherThread.start();
145                 } catch (Exception e) {
146                         LOGGER.debug("Error occurred while starting FileWatcherThread:", e);
147                         LOGGER.error(
148                                 MessageEnum.BPMN_GENERAL_EXCEPTION,
149                                 "BPMN",
150                                 "Property Configuration",
151                                 MsoLogger.ErrorCode.UnknownError,
152                                 "Error occurred while starting FileWatcherThread:" + e);
153                 }
154         }
155
156         /**
157          * May be called to shut down the PropertyConfiguration.  A shutDown followed
158          * by a startUp will reset the PropertyConfiguration to its initial state.
159          */
160         public synchronized void shutDown() {
161                 if (fileWatcherThread != null) {
162                         LOGGER.debug("Shutting down FileWatcherThread " + System.identityHashCode(fileWatcherThread));
163                         fileWatcherThread.shutdown();
164
165                         long waitInSeconds = 10;
166
167                         try {
168                                 fileWatcherThread.join(waitInSeconds * 1000);
169                         } catch (InterruptedException e) {
170                                 LOGGER.debug("FileWatcherThread " + System.identityHashCode(fileWatcherThread)
171                                         + " shutdown did not occur within " + waitInSeconds + " seconds",e);
172                         }
173
174                         LOGGER.debug("Finished shutting down FileWatcherThread " + System.identityHashCode(fileWatcherThread));
175                         fileWatcherThread = null;
176                 }
177
178                 clearCache();
179                 msoConfigPath = null;
180         }
181
182         public synchronized boolean isFileWatcherRunning() {
183                 return fileWatcherThread != null;
184         }
185
186         public void clearCache() {
187                 synchronized(CACHELOCK) {
188                         propFileCache.clear();
189                 }
190         }
191
192         public int cacheSize() {
193                 return propFileCache.size();
194         }
195
196         // TODO: throw IOException?
197         public Map<String, String> getProperties(String fileName) {
198                 Map<String, String> properties = propFileCache.get(fileName);
199
200                 if (properties == null) {
201                         if (!SUPPORTED_FILES.contains(fileName)) {
202                                 throw new IllegalArgumentException("Not a supported property file: " + fileName);
203                         }
204
205                         if (msoConfigPath == null) {
206                                 LOGGER.debug("mso.config.path JVM system property must be set to load " + fileName);
207
208                                 LOGGER.error(
209                                                 MessageEnum.BPMN_GENERAL_EXCEPTION,
210                                                 "BPMN",
211                                                 MDC.get(fileName),
212                                                 MsoLogger.ErrorCode.UnknownError,
213                                                 "mso.config.path JVM system property must be set to load " + fileName);
214
215                                 return null;
216                         }
217
218                         try {
219                                 properties = readProperties(new File(msoConfigPath, fileName));
220                         } catch (Exception e) {
221                                 LOGGER.debug("Error loading " + fileName);
222
223                                 LOGGER.error(
224                                                 MessageEnum.BPMN_GENERAL_EXCEPTION,
225                                                 "BPMN",
226                                                 MDC.get(fileName),
227                                                 MsoLogger.ErrorCode.UnknownError,
228                                                 "Error loading " + fileName, e);
229
230                                 return null;
231                         }
232                 }
233
234                 return Collections.unmodifiableMap(properties);
235         }
236
237         /**
238          * Reads properties from the specified file, updates the property file cache, and
239          * returns the properties in a map.
240          * @param file the file to read
241          * @param reload true if this is a reload event
242          * @return a map of properties
243          */
244         private Map<String, String> readProperties(File file) throws IOException {
245                 String fileName = file.getName();
246                 LOGGER.debug("Reading " + fileName);
247
248                 Map<String, String> properties = new HashMap<>();
249                 Properties newProperties = new Properties();
250
251                 try (FileReader reader = new FileReader(file)) {
252                         newProperties.load(reader);
253                 }
254                 catch (Exception e) {
255                         LOGGER.debug("Exception :",e);
256                 }
257
258                 for (Entry<Object, Object> entry : newProperties.entrySet()) {
259                         properties.put(entry.getKey().toString(), entry.getValue().toString());
260                 }
261
262                 properties.put(TIMESTAMP_PROPERTY, String.valueOf(System.currentTimeMillis()));
263
264                 synchronized(CACHELOCK) {
265                         propFileCache.put(fileName, properties);
266                 }
267
268                 return properties;
269         }
270
271         /**
272          * File watcher thread which monitors a directory for file modification.
273          */
274         private class FileWatcherThread extends Thread {
275                 private final WatchService watchService;
276                 private final Timer timer = new Timer("FileWatcherTimer");
277
278                 public FileWatcherThread(WatchService service) {
279                         this.watchService = service;
280                 }
281
282                 public void shutdown() {
283                         interrupt();
284                 }
285
286                 @Override
287                 public void run() {
288                         LOGGER.info(MessageEnum.BPMN_GENERAL_INFO, "BPMN",
289                                 "FileWatcherThread started");
290
291                         LOGGER.debug("Started FileWatcherThread " + System.identityHashCode(fileWatcherThread));
292
293                         try {
294                                 WatchKey watchKey = null;
295
296                                 while (!isInterrupted()) {
297                                         try {
298                                                 if (watchKey != null) {
299                                                         watchKey.reset();
300                                                 }
301
302                                                 watchKey = watchService.take();
303
304                                                 for (WatchEvent<?> event : watchKey.pollEvents()) {
305                                                         @SuppressWarnings("unchecked")
306                                                         WatchEvent<Path> pathEvent = (WatchEvent<Path>) event;
307
308                                                         if ("EVENT_OVERFLOW".equals(pathEvent.kind())) {
309                                                                 LOGGER.debug("Ignored overflow event for " + msoConfigPath);
310                                                                 continue;
311                                                         }
312
313                                                         String fileName = pathEvent.context().getFileName().toString();
314
315                                                         if (!SUPPORTED_FILES.contains(fileName)) {
316                                                                 LOGGER.debug("Ignored modify event for " + fileName);
317                                                                 continue;
318                                                         }
319
320                                                         LOGGER.debug("Configuration file has changed: " + fileName);
321
322                                                         LOGGER.info(MessageEnum.BPMN_GENERAL_INFO, "BPMN",
323                                                                         "Configuation file has changed: " + fileName);
324
325                                                         // There's a potential problem here. The MODIFY event is
326                                                         // triggered as soon as somebody starts writing the file but
327                                                         // there's no obvious way to know when the write is done.  If we
328                                                         // read the file while the write is still in progress, then the
329                                                         // cache can really be messed up. As a workaround, we use a timer
330                                                         // to sleep for at least one second, and then we sleep for as long
331                                                         // as it takes for the file's lastModified time to stop changing.
332                                                         // The timer has another benefit: it consolidates multiple events
333                                                         // that we seem to receive when a file is modified.
334
335                                                         synchronized(timerTaskMap) {
336                                                                 TimerTask task = timerTaskMap.get(fileName);
337
338                                                                 if (task != null) {
339                                                                         task.cancel();
340                                                                 }
341
342                                                                 File file = new File(msoConfigPath, fileName);
343                                                                 task = new DelayTimerTask(timer, file, 1000);
344                                                                 timerTaskMap.put(fileName, task);
345                                                         }
346                                                 }
347                                         } catch (InterruptedException e) {
348                                                 LOGGER.debug("InterruptedException :",e);
349                                                 break;
350                                         } catch (ClosedWatchServiceException e) {
351                                                 LOGGER.info(
352                                                                 MessageEnum.BPMN_GENERAL_INFO,
353                                                                 "BPMN",
354                                                                 "FileWatcherThread shut down because the watch service was closed");
355                                                 LOGGER.debug("ClosedWatchServiceException :",e);
356                                                 break;
357                                         } catch (Exception e) {
358                                                 LOGGER.error(
359                                                                 MessageEnum.BPMN_GENERAL_EXCEPTION,
360                                                                 "BPMN",
361                                                                 "Property Configuration",
362                                                                 MsoLogger.ErrorCode.UnknownError,
363                                                                 "FileWatcherThread caught unexpected " + e.getClass().getSimpleName(), e);
364                                         }
365
366                                 }
367                         } finally {
368                                 timer.cancel();
369
370                                 synchronized(timerTaskMap) {
371                                         timerTaskMap.clear();
372                                 }
373
374                                 try {
375                                         watchService.close();
376                                 } catch (IOException e) {
377                                         LOGGER.debug("FileWatcherThread caught " + e.getClass().getSimpleName()
378                                                 + " while closing the watch service",e);
379                                 }
380
381                                 LOGGER.info(MessageEnum.BPMN_GENERAL_INFO, "BPMN",
382                                         "FileWatcherThread stopped");
383                         }
384                 }
385         }
386
387         private class DelayTimerTask extends TimerTask {
388                 private final File file;
389                 private final long lastModifiedTime;
390                 private final Timer timer;
391
392                 public DelayTimerTask(Timer timer, File file, long delay) {
393                         this.timer = timer;
394                         this.file = file;
395                         this.lastModifiedTime = file.lastModified();
396                         timer.schedule(this, delay);
397                 }
398
399                 @Override
400                 public void run() {
401                         try {
402                                 long newLastModifiedTime = file.lastModified();
403
404                                 if (newLastModifiedTime == lastModifiedTime) {
405                                         try {
406                                                 readProperties(file);
407                                         } catch (Exception e) {
408                                                 LOGGER.error(
409                                                         MessageEnum.BPMN_GENERAL_EXCEPTION,
410                                                         "BPMN",
411                                                         "Property Configuration",
412                                                         MsoLogger.ErrorCode.UnknownError,
413                                                         "Unable to reload " + file, e);
414                                         }
415                                 } else {
416                                         LOGGER.debug("Delaying reload of " + file + " by 1 second");
417
418                                         synchronized(timerTaskMap) {
419                                                 TimerTask task = timerTaskMap.get(file.getName());
420
421                                                 if (task != null && task != this) {
422                                                         task.cancel();
423                                                 }
424
425                                                 task = new DelayTimerTask(timer, file, 1000);
426                                                 timerTaskMap.put(file.getName(), task);
427                                         }
428                                 }
429                         } finally {
430                                 synchronized(timerTaskMap) {
431                                         TimerTask task = timerTaskMap.get(file.getName());
432
433                                         if (task == this) {
434                                                 timerTaskMap.remove(file.getName());
435                                         }
436                                 }
437                         }
438                 }
439         }
440 }