Merge "Reorder modifiers"
[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         static void resetPropertyConfigurationSingletonInstance(){
119                 PropertyConfigurationInstanceHolder.instance = new PropertyConfiguration();
120         }
121
122         /**
123          * Returns the list of supported files.
124          */
125         public static List<String> supportedFiles() {
126                 return new ArrayList<>(SUPPORTED_FILES);
127         }
128
129         /**
130          * May be called to restart the PropertyConfiguration if it was previously shut down.
131          */
132         public synchronized void startUp() {
133                 msoConfigPath = System.getProperty("mso.config.path");
134
135                 if (msoConfigPath == null) {
136                         LOGGER.debug("mso.config.path JVM system property is not set");
137                         return;
138                 }
139
140                 try {
141                         Path directory = FileSystems.getDefault().getPath(msoConfigPath);
142                         WatchService watchService = FileSystems.getDefault().newWatchService();
143                         directory.register(watchService, ENTRY_MODIFY);
144
145                         LOGGER.info(MessageEnum.BPMN_GENERAL_INFO, "BPMN", "Starting FileWatcherThread");
146                         LOGGER.debug("Starting FileWatcherThread");
147                         fileWatcherThread = new FileWatcherThread(watchService);
148                         fileWatcherThread.start();
149                 } catch (Exception e) {
150                         LOGGER.debug("Error occurred while starting FileWatcherThread:", e);
151                         LOGGER.error(
152                                 MessageEnum.BPMN_GENERAL_EXCEPTION,
153                                 "BPMN",
154                                 "Property Configuration",
155                                 MsoLogger.ErrorCode.UnknownError,
156                                 "Error occurred while starting FileWatcherThread:" + e);
157                 }
158         }
159
160         /**
161          * May be called to shut down the PropertyConfiguration.  A shutDown followed
162          * by a startUp will reset the PropertyConfiguration to its initial state.
163          */
164         public synchronized void shutDown() {
165                 if (fileWatcherThread != null) {
166                         LOGGER.debug("Shutting down FileWatcherThread " + System.identityHashCode(fileWatcherThread));
167                         fileWatcherThread.shutdown();
168
169                         long waitInSeconds = 10;
170
171                         try {
172                                 fileWatcherThread.join(waitInSeconds * 1000);
173                         } catch (InterruptedException e) {
174                                 LOGGER.debug("FileWatcherThread " + System.identityHashCode(fileWatcherThread)
175                                         + " shutdown did not occur within " + waitInSeconds + " seconds",e);
176                         }
177
178                         LOGGER.debug("Finished shutting down FileWatcherThread " + System.identityHashCode(fileWatcherThread));
179                         fileWatcherThread = null;
180                 }
181
182                 clearCache();
183                 msoConfigPath = null;
184         }
185
186         public synchronized boolean isFileWatcherRunning() {
187                 return fileWatcherThread != null;
188         }
189
190         public void clearCache() {
191                 synchronized(CACHELOCK) {
192                         propFileCache.clear();
193                 }
194         }
195
196         public int cacheSize() {
197                 return propFileCache.size();
198         }
199
200         // TODO: throw IOException?
201         public Map<String, String> getProperties(String fileName) {
202                 Map<String, String> properties = propFileCache.get(fileName);
203
204                 if (properties == null) {
205                         if (!SUPPORTED_FILES.contains(fileName)) {
206                                 throw new IllegalArgumentException("Not a supported property file: " + fileName);
207                         }
208
209                         if (msoConfigPath == null) {
210                                 LOGGER.debug("mso.config.path JVM system property must be set to load " + fileName);
211
212                                 LOGGER.error(
213                                                 MessageEnum.BPMN_GENERAL_EXCEPTION,
214                                                 "BPMN",
215                                                 MDC.get(fileName),
216                                                 MsoLogger.ErrorCode.UnknownError,
217                                                 "mso.config.path JVM system property must be set to load " + fileName);
218
219                                 return null;
220                         }
221
222                         try {
223                                 properties = readProperties(new File(msoConfigPath, fileName));
224                         } catch (Exception e) {
225                                 LOGGER.debug("Error loading " + fileName);
226
227                                 LOGGER.error(
228                                                 MessageEnum.BPMN_GENERAL_EXCEPTION,
229                                                 "BPMN",
230                                                 MDC.get(fileName),
231                                                 MsoLogger.ErrorCode.UnknownError,
232                                                 "Error loading " + fileName, e);
233
234                                 return null;
235                         }
236                 }
237
238                 return Collections.unmodifiableMap(properties);
239         }
240
241         /**
242          * Reads properties from the specified file, updates the property file cache, and
243          * returns the properties in a map.
244          * @param file the file to read
245          * @return a map of properties
246          */
247         private Map<String, String> readProperties(File file) throws IOException {
248                 String fileName = file.getName();
249                 LOGGER.debug("Reading " + fileName);
250
251                 Map<String, String> properties = new HashMap<>();
252                 Properties newProperties = new Properties();
253
254                 try (FileReader reader = new FileReader(file)) {
255                         newProperties.load(reader);
256                 }
257                 catch (Exception e) {
258                         LOGGER.debug("Exception :",e);
259                 }
260
261                 for (Entry<Object, Object> entry : newProperties.entrySet()) {
262                         properties.put(entry.getKey().toString(), entry.getValue().toString());
263                 }
264
265                 properties.put(TIMESTAMP_PROPERTY, String.valueOf(System.currentTimeMillis()));
266
267                 synchronized(CACHELOCK) {
268                         propFileCache.put(fileName, properties);
269                 }
270
271                 return properties;
272         }
273
274         /**
275          * File watcher thread which monitors a directory for file modification.
276          */
277         private class FileWatcherThread extends Thread {
278                 private final WatchService watchService;
279                 private final Timer timer = new Timer("FileWatcherTimer");
280
281                 public FileWatcherThread(WatchService service) {
282                         this.watchService = service;
283                 }
284
285                 public void shutdown() {
286                         interrupt();
287                 }
288
289                 @Override
290                 public void run() {
291                         LOGGER.info(MessageEnum.BPMN_GENERAL_INFO, "BPMN",
292                                 "FileWatcherThread started");
293
294                         LOGGER.debug("Started FileWatcherThread " + System.identityHashCode(fileWatcherThread));
295
296                         try {
297                                 WatchKey watchKey = null;
298
299                                 while (!isInterrupted()) {
300                                         try {
301                                                 if (watchKey != null) {
302                                                         watchKey.reset();
303                                                 }
304
305                                                 watchKey = watchService.take();
306
307                                                 for (WatchEvent<?> event : watchKey.pollEvents()) {
308                                                         @SuppressWarnings("unchecked")
309                                                         WatchEvent<Path> pathEvent = (WatchEvent<Path>) event;
310
311                                                         if ("EVENT_OVERFLOW".equals(pathEvent.kind())) {
312                                                                 LOGGER.debug("Ignored overflow event for " + msoConfigPath);
313                                                                 continue;
314                                                         }
315
316                                                         String fileName = pathEvent.context().getFileName().toString();
317
318                                                         if (!SUPPORTED_FILES.contains(fileName)) {
319                                                                 LOGGER.debug("Ignored modify event for " + fileName);
320                                                                 continue;
321                                                         }
322
323                                                         LOGGER.debug("Configuration file has changed: " + fileName);
324
325                                                         LOGGER.info(MessageEnum.BPMN_GENERAL_INFO, "BPMN",
326                                                                         "Configuation file has changed: " + fileName);
327
328                                                         // There's a potential problem here. The MODIFY event is
329                                                         // triggered as soon as somebody starts writing the file but
330                                                         // there's no obvious way to know when the write is done.  If we
331                                                         // read the file while the write is still in progress, then the
332                                                         // cache can really be messed up. As a workaround, we use a timer
333                                                         // to sleep for at least one second, and then we sleep for as long
334                                                         // as it takes for the file's lastModified time to stop changing.
335                                                         // The timer has another benefit: it consolidates multiple events
336                                                         // that we seem to receive when a file is modified.
337
338                                                         synchronized(timerTaskMap) {
339                                                                 TimerTask task = timerTaskMap.get(fileName);
340
341                                                                 if (task != null) {
342                                                                         task.cancel();
343                                                                 }
344
345                                                                 File file = new File(msoConfigPath, fileName);
346                                                                 task = new DelayTimerTask(timer, file, 1000);
347                                                                 timerTaskMap.put(fileName, task);
348                                                         }
349                                                 }
350                                         } catch (InterruptedException e) {
351                                                 LOGGER.debug("InterruptedException :",e);
352                                                 break;
353                                         } catch (ClosedWatchServiceException e) {
354                                                 LOGGER.info(
355                                                                 MessageEnum.BPMN_GENERAL_INFO,
356                                                                 "BPMN",
357                                                                 "FileWatcherThread shut down because the watch service was closed");
358                                                 LOGGER.debug("ClosedWatchServiceException :",e);
359                                                 break;
360                                         } catch (Exception e) {
361                                                 LOGGER.error(
362                                                                 MessageEnum.BPMN_GENERAL_EXCEPTION,
363                                                                 "BPMN",
364                                                                 "Property Configuration",
365                                                                 MsoLogger.ErrorCode.UnknownError,
366                                                                 "FileWatcherThread caught unexpected " + e.getClass().getSimpleName(), e);
367                                         }
368
369                                 }
370                         } finally {
371                                 timer.cancel();
372
373                                 synchronized(timerTaskMap) {
374                                         timerTaskMap.clear();
375                                 }
376
377                                 try {
378                                         watchService.close();
379                                 } catch (IOException e) {
380                                         LOGGER.debug("FileWatcherThread caught " + e.getClass().getSimpleName()
381                                                 + " while closing the watch service",e);
382                                 }
383
384                                 LOGGER.info(MessageEnum.BPMN_GENERAL_INFO, "BPMN",
385                                         "FileWatcherThread stopped");
386                         }
387                 }
388         }
389
390         private class DelayTimerTask extends TimerTask {
391                 private final File file;
392                 private final long lastModifiedTime;
393                 private final Timer timer;
394
395                 public DelayTimerTask(Timer timer, File file, long delay) {
396                         this.timer = timer;
397                         this.file = file;
398                         this.lastModifiedTime = file.lastModified();
399                         timer.schedule(this, delay);
400                 }
401
402                 @Override
403                 public void run() {
404                         try {
405                                 long newLastModifiedTime = file.lastModified();
406
407                                 if (newLastModifiedTime == lastModifiedTime) {
408                                         try {
409                                                 readProperties(file);
410                                         } catch (Exception e) {
411                                                 LOGGER.error(
412                                                         MessageEnum.BPMN_GENERAL_EXCEPTION,
413                                                         "BPMN",
414                                                         "Property Configuration",
415                                                         MsoLogger.ErrorCode.UnknownError,
416                                                         "Unable to reload " + file, e);
417                                         }
418                                 } else {
419                                         LOGGER.debug("Delaying reload of " + file + " by 1 second");
420
421                                         synchronized(timerTaskMap) {
422                                                 TimerTask task = timerTaskMap.get(file.getName());
423
424                                                 if (task != null && task != this) {
425                                                         task.cancel();
426                                                 }
427
428                                                 task = new DelayTimerTask(timer, file, 1000);
429                                                 timerTaskMap.put(file.getName(), task);
430                                         }
431                                 }
432                         } finally {
433                                 synchronized(timerTaskMap) {
434                                         TimerTask task = timerTaskMap.get(file.getName());
435
436                                         if (task == this) {
437                                                 timerTaskMap.remove(file.getName());
438                                         }
439                                 }
440                         }
441                 }
442         }
443 }