aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/PropertyConfiguration.java
blob: 90df1da7e56e2b9b01f9c9b3c8e0f39e44b8e4a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*-
 * ============LICENSE_START=======================================================
 * OPENECOMP - MSO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.mso.bpmn.core;

import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.ClosedWatchServiceException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.MDC;

import org.openecomp.mso.logger.MessageEnum;
import org.openecomp.mso.logger.MsoLogger;

/**
 * Loads the property configuration from file system and refreshes the
 * properties when the property gets changed.
 * 
 * WARNING: automatic refreshes might not work on network filesystems.
 */
public class PropertyConfiguration {
	
	/**
	 * The base name of the MSO BPMN properties file (mso.bpmn.properties).
	 */
	public static final String MSO_BPMN_PROPERTIES = "mso.bpmn.properties";

	/**
	 * The base name of the MSO BPMN URN-Mappings properties file (mso.bpmn.urn.properties).
	 */
	public static final String MSO_BPMN_URN_PROPERTIES = "mso.bpmn.urn.properties";
	
	/**
	 * The name of the meta-property holding the time the properties were loaded
	 * from the file.
	 */
	public static final String TIMESTAMP_PROPERTY = "mso.properties.timestamp";

	private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);

	private static final List<String> SUPPORTED_FILES =
		Arrays.asList(MSO_BPMN_PROPERTIES, MSO_BPMN_URN_PROPERTIES);

	private volatile String msoConfigPath = null;

	private final ConcurrentHashMap<String, Map<String, String>> propFileCache =
		new ConcurrentHashMap<String, Map<String, String>>();

	private final Object CACHELOCK = new Object();
	private FileWatcherThread fileWatcherThread = null;

	// The key is the file name
	private Map<String, TimerTask> timerTaskMap = new HashMap<String, TimerTask>();
	
	/**
	 * Singleton holder pattern eliminates locking when accessing the instance
	 * and still provides for lazy initialization.
	 */
	private static class PropertyConfigurationInstanceHolder {
		private static PropertyConfiguration instance = new PropertyConfiguration();
	}

	/**
	 * Gets the one and only instance of this class.
	 */
	public static PropertyConfiguration getInstance() {
		return PropertyConfigurationInstanceHolder.instance;
	}

	/**
	 * Returns the list of supported files.
	 */
	public static List<String> supportedFiles() {
		return new ArrayList<String>(SUPPORTED_FILES);
	}

	/**
	 * Private Constructor.
	 */
	private PropertyConfiguration() {
		startUp();
	}
	
	/**
	 * May be called to restart the PropertyConfiguration if it was previously shut down.
	 */
	public synchronized void startUp() {
		msoConfigPath = System.getProperty("mso.config.path");

		if (msoConfigPath == null) {
			LOGGER.debug("mso.config.path JVM system property is not set");
			return;
		}

		try {
			Path directory = FileSystems.getDefault().getPath(msoConfigPath);
			WatchService watchService = FileSystems.getDefault().newWatchService();
			directory.register(watchService, ENTRY_MODIFY);

			LOGGER.info(MessageEnum.BPMN_GENERAL_INFO, "BPMN", "Starting FileWatcherThread");
			LOGGER.debug("Starting FileWatcherThread");
			fileWatcherThread = new FileWatcherThread(watchService);
			fileWatcherThread.start();
		} catch (Exception e) {
			LOGGER.debug("Error occurred while starting FileWatcherThread:", e);
			LOGGER.error(
				MessageEnum.BPMN_GENERAL_EXCEPTION,
				"BPMN",
				"Property Configuration",
				MsoLogger.ErrorCode.UnknownError,
				"Error occurred while starting FileWatcherThread:" + e);
		}
	}

	/**
	 * May be called to shut down the PropertyConfiguration.  A shutDown followed
	 * by a startUp will reset the PropertyConfiguration to its initial state.
	 */
	public synchronized void shutDown() {
		if (fileWatcherThread != null) {
			LOGGER.debug("Shutting down FileWatcherThread " + System.identityHashCode(fileWatcherThread));
			fileWatcherThread.shutdown();

			long waitInSeconds = 10;

			try {
				fileWatcherThread.join(waitInSeconds * 1000);
			} catch (InterruptedException e) {
				LOGGER.debug("FileWatcherThread " + System.identityHashCode(fileWatcherThread)
					+ " shutdown did not occur within " + waitInSeconds + " seconds");
			}

			LOGGER.debug("Finished shutting down FileWatcherThread " + System.identityHashCode(fileWatcherThread));
			fileWatcherThread = null;
		}

		clearCache();
		msoConfigPath = null;
	}

	public synchronized boolean isFileWatcherRunning() {
		return fileWatcherThread != null;
	}

	public void clearCache() {
		synchronized(CACHELOCK) {
			propFileCache.clear();
		}
	}

	public int cacheSize() {
		return propFileCache.size();
	}

	// TODO: throw IOException?
	public Map<String, String> getProperties(String fileName) {
		Map<String, String> properties = propFileCache.get(fileName);

		if (properties == null) {
			if (!SUPPORTED_FILES.contains(fileName)) {
				throw new IllegalArgumentException("Not a supported property file: " + fileName);
			}

			if (msoConfigPath == null) {
				LOGGER.debug("mso.config.path JVM system property must be set to load " + fileName);

				LOGGER.error(
						MessageEnum.BPMN_GENERAL_EXCEPTION,
						"BPMN",
						MDC.get(fileName),
						MsoLogger.ErrorCode.UnknownError,
						"mso.config.path JVM system property must be set to load " + fileName);

				return null;
			}

			try {
				properties = readProperties(new File(msoConfigPath, fileName));
			} catch (Exception e) {
				LOGGER.debug("Error loading " + fileName);

				LOGGER.error(
						MessageEnum.BPMN_GENERAL_EXCEPTION,
						"BPMN",
						MDC.get(fileName),
						MsoLogger.ErrorCode.UnknownError,
						"Error loading " + fileName, e);

				return null;
			}
		}

		return Collections.unmodifiableMap(properties);
	}
	
	/**
	 * Reads properties from the specified file, updates the property file cache, and
	 * returns the properties in a map.
	 * @param file the file to read
	 * @param reload true if this is a reload event
	 * @return a map of properties
	 */
	private Map<String, String> readProperties(File file) throws IOException {
		String fileName = file.getName();
		LOGGER.debug("Reading " + fileName);

		Map<String, String> properties = new HashMap<String, String>();
		Properties newProperties = new Properties();

		FileReader reader = null;
		try {
			reader = new FileReader(file);
			newProperties.load(reader);
		} finally {
			if (reader != null) {
				try {
					reader.close();
					LOGGER.debug("Closed " + fileName);
				} catch (Exception e) {
					// Ignore
				}
			}
		}

		for (Entry<Object, Object> entry : newProperties.entrySet()) {
			properties.put(entry.getKey().toString(), entry.getValue().toString());
		}

		properties.put(TIMESTAMP_PROPERTY, String.valueOf(System.currentTimeMillis()));

		synchronized(CACHELOCK) {
			propFileCache.put(fileName, properties);
		}

		return properties;
	}
	
	/**
	 * File watcher thread which monitors a directory for file modification.
	 */
	private class FileWatcherThread extends Thread {
		private final WatchService watchService;
		private final Timer timer = new Timer("FileWatcherTimer");

		public FileWatcherThread(WatchService service) {
			this.watchService = service;
		}

		public void shutdown() {
			interrupt();
		}

		public void run() {
			LOGGER.info(MessageEnum.BPMN_GENERAL_INFO, "BPMN",
				"FileWatcherThread started");

			LOGGER.debug("Started FileWatcherThread " + System.identityHashCode(fileWatcherThread));

			try {
				WatchKey watchKey = null;

				while (!isInterrupted()) {
					try {
						if (watchKey != null) {
							watchKey.reset();
						}

						watchKey = watchService.take();

						for (WatchEvent<?> event : watchKey.pollEvents()) {
							@SuppressWarnings("unchecked")
							WatchEvent<Path> pathEvent = (WatchEvent<Path>) event;

							if ("EVENT_OVERFLOW".equals(pathEvent.kind())) {
								LOGGER.debug("Ignored overflow event for " + msoConfigPath);
								continue;
							}

							String fileName = pathEvent.context().getFileName().toString();

							if (!SUPPORTED_FILES.contains(fileName)) {
								LOGGER.debug("Ignored modify event for " + fileName);
								continue;
							}

							LOGGER.debug("Configuration file has changed: " + fileName);

							LOGGER.info(MessageEnum.BPMN_GENERAL_INFO, "BPMN",
									"Configuation file has changed: " + fileName);

							// There's a potential problem here. The MODIFY event is
							// triggered as soon as somebody starts writing the file but
							// there's no obvious way to know when the write is done.  If we
							// read the file while the write is still in progress, then the
							// cache can really be messed up. As a workaround, we use a timer
							// to sleep for at least one second, and then we sleep for as long
							// as it takes for the file's lastModified time to stop changing.
							// The timer has another benefit: it consolidates multiple events
							// that we seem to receive when a file is modified.

							synchronized(timerTaskMap) {
								TimerTask task = timerTaskMap.get(fileName);

								if (task != null) {
									task.cancel();
								}

								File file = new File(msoConfigPath, fileName);
								task = new DelayTimerTask(timer, file, 1000);
								timerTaskMap.put(fileName, task);
							}
						}
					} catch (InterruptedException e) {
						break;
					} catch (ClosedWatchServiceException e) {
						LOGGER.info(
								MessageEnum.BPMN_GENERAL_INFO,
								"BPMN",
								"FileWatcherThread shut down because the watch service was closed");
						break;
					} catch (Exception e) {
						LOGGER.error(
								MessageEnum.BPMN_GENERAL_EXCEPTION,
								"BPMN",
								"Property Configuration",
								MsoLogger.ErrorCode.UnknownError,
								"FileWatcherThread caught unexpected " + e.getClass().getSimpleName(), e);
					}

				}
			} finally {
				timer.cancel();

				synchronized(timerTaskMap) {
					timerTaskMap.clear();
				}

				try {
					watchService.close();
				} catch (IOException e) {
					LOGGER.debug("FileWatcherThread caught " + e.getClass().getSimpleName()
						+ " while closing the watch service");
				}

				LOGGER.info(MessageEnum.BPMN_GENERAL_INFO, "BPMN",
					"FileWatcherThread stopped");
			}
		}
	}

	private class DelayTimerTask extends TimerTask {
		private final File file;
		private final long lastModifiedTime;
		private final Timer timer;

		public DelayTimerTask(Timer timer, File file, long delay) {
			this.timer = timer;
			this.file = file;
			this.lastModifiedTime = file.lastModified();
			timer.schedule(this, delay);
		}

		@Override
		public void run() {
			try {
				long newLastModifiedTime = file.lastModified();

				if (newLastModifiedTime == lastModifiedTime) {
					try {
						readProperties(file);
					} catch (Exception e) {
						LOGGER.error(
							MessageEnum.BPMN_GENERAL_EXCEPTION,
							"BPMN",
							"Property Configuration",
							MsoLogger.ErrorCode.UnknownError,
							"Unable to reload " + file, e);
					}
				} else {
					LOGGER.debug("Delaying reload of " + file + " by 1 second");

					synchronized(timerTaskMap) {
						TimerTask task = timerTaskMap.get(file.getName());

						if (task != null && task != this) {
							task.cancel();
						}

						task = new DelayTimerTask(timer, file, 1000);
						timerTaskMap.put(file.getName(), task);
					}
				}
			} finally {
				synchronized(timerTaskMap) {
					TimerTask task = timerTaskMap.get(file.getName());

					if (task == this) {
						timerTaskMap.remove(file.getName());
					}
				}
			}
		}
	}
}