-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path182.java
More file actions
294 lines (263 loc) · 8.98 KB
/
Copy path182.java
File metadata and controls
294 lines (263 loc) · 8.98 KB
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
package de.oglimmer.utils;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonReader;
import javax.json.JsonValue;
import javax.json.JsonWriter;
import javax.json.JsonWriterFactory;
import javax.json.stream.JsonGenerator;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
/**
* A base class for application property classes. Supports json-based config
* files, automatic reloads when the file changes, merging in-classpath and
* out-of-classpath files and can take the whole configuration from a string to
* support unit test configurations.
*
* All property files must be in JSON format.
*
* <div> Search-Logic:
* <ol>
* <li>Always load default properties (filename: defaultPropertyFile) from
* classpath</li>
* <li>if systemPropertiesKey starts with "memory:", treat systemPropertiesKey
* as a JSON object and merge it</li>
* <li>else if System.getProperties(systemPropertiesKey) is not null => load
* the file coming from this and merge it</li>
* <li>else if file /etc/${systemPropertiesKey} exists => load and merge
* it</li>
* </ol>
* </div>
*
* @author Oli Zimpasser
*
*/
@Slf4j
public class AbstractProperties {
private static final boolean DEBUG = false;
final private String systemPropertiesKey;
final private String defaultPropertyFile;
@Getter(value = AccessLevel.PROTECTED)
private JsonObject json = Json.createObjectBuilder().build();
private volatile boolean running = true;
private Thread propertyFileWatcherThread;
final private List<Runnable> reloadables = Collections.synchronizedList(new ArrayList<>());
private String sourceLocation;
private boolean reload;
/**
* Uses /default.properties from classpath for default properties file. Reload will be enabled.
*
* All property files must be in JSON format.
*
* @param systemPropertiesKey name of a -D parameter which holds a filesystem (not classpath) filename.
*/
protected AbstractProperties(String systemPropertiesKey) {
this(systemPropertiesKey, "/default.properties");
}
/**
* Reload will be enabled.
*
* All property files must be in JSON format.
*
* @param systemPropertiesKey name of a -D parameter which holds a filesystem (not classpath) filename.
* @param defaultPropertyFile classpath based filename to the default properties file
*/
protected AbstractProperties(String systemPropertiesKey, String defaultPropertyFile) {
this(systemPropertiesKey, defaultPropertyFile, true);
}
/**
* All property files must be in JSON format.
*
* @param systemPropertiesKey name of a -D parameter which holds a filesystem (not classpath) filename.
* @param defaultPropertyFile classpath based filename to the default properties file
* @param reload enable/disable a file watcher to automatically reload the config
*/
protected AbstractProperties(String systemPropertiesKey, String defaultPropertyFile, boolean reload) {
this.systemPropertiesKey = systemPropertiesKey;
this.defaultPropertyFile = defaultPropertyFile;
this.reload = reload;
init();
}
/**
* Overwrite this to manipulate the properties after default and extra properties are loaded.
*
* @return the manipulated properties
*/
protected JsonObject createExtraInitAttributes() {
return json;
}
private void initMemory() {
final String memoryConfigStr = sourceLocation.substring("memory:".length());
final InputStream is = new ByteArrayInputStream(memoryConfigStr.getBytes(StandardCharsets.UTF_8));
mergeJson(is);
}
private boolean initExternalFile() {
try (final InputStream fis = new FileInputStream(sourceLocation)) {
mergeJson(fis);
return true;
} catch (IOException e) {
log.error("Failed to load properties file " + sourceLocation, e);
}
return false;
}
private boolean initExternalEtcFile() {
File extDefaultFile = new File("/etc/" + systemPropertiesKey);
if (extDefaultFile.exists()) {
sourceLocation = extDefaultFile.getAbsolutePath();
return initExternalFile();
}
return false;
}
private void init() {
sourceLocation = System.getProperty(systemPropertiesKey);
loadDefaultProperties();
loadExtraPropertiesAndStartWatcherThread();
json = createExtraInitAttributes();
if (DEBUG) {
System.out.println("Used config: " + prettyPrint(json));
}
}
private void loadExtraPropertiesAndStartWatcherThread() {
boolean startFileWatcher = false;
if (sourceLocation != null) {
if (sourceLocation.startsWith("memory:")) {
initMemory();
} else {
startFileWatcher = initExternalFile();
}
} else {
startFileWatcher = initExternalEtcFile();
}
startWatcherThread(startFileWatcher);
}
private void startWatcherThread(boolean startFileWatcher) {
if (startFileWatcher && reload && propertyFileWatcherThread == null) {
propertyFileWatcherThread = new Thread(new PropertyFileWatcher());
propertyFileWatcherThread.start();
}
}
private void loadDefaultProperties() {
try (final JsonReader rdr = Json.createReader(this.getClass().getResourceAsStream(defaultPropertyFile))) {
json = rdr.readObject();
System.out.println("Successfully loaded properties from " + defaultPropertyFile);
}
}
private String prettyPrint(final JsonObject json) {
final Map<String, Object> properties = new HashMap<>(1);
properties.put(JsonGenerator.PRETTY_PRINTING, true);
final JsonWriterFactory writerFactory = Json.createWriterFactory(properties);
final StringWriter sw = new StringWriter();
try (final JsonWriter jsonWriter = writerFactory.createWriter(sw)) {
jsonWriter.writeObject(json);
}
return sw.toString();
}
private void mergeJson(final InputStream is) {
try (final JsonReader rdr = Json.createReader(is)) {
final JsonObject toBeMerged = rdr.readObject();
json = merge(json, toBeMerged);
System.out.println("Successfully loaded " + systemPropertiesKey + " from " + sourceLocation + " for merge");
}
}
protected JsonObject merge(final JsonObject base, final JsonObject toOverwrite) {
final JsonObjectBuilder job = Json.createObjectBuilder();
for (final Entry<String, JsonValue> entry : base.entrySet()) {
final String key = entry.getKey();
if (toOverwrite.containsKey(key)) {
job.add(key, toOverwrite.get(key));
} else {
job.add(key, entry.getValue());
}
}
for (final Entry<String, JsonValue> entry : toOverwrite.entrySet()) {
final String key = entry.getKey();
// if JsonObjectBuilder would have a containsKey we could skip all
// already existing keys
job.add(key, toOverwrite.get(key));
}
return job.build();
}
/**
* Register your callbacks here to get informed when the property file has changed
*
* @param toCall
* a runnable to be called
*/
public void registerOnReload(final Runnable toCall) {
if (!reload) {
throw new RuntimeException("Not allowed as reload is false");
}
reloadables.add(toCall);
}
void reload() {
init();
reloadables.forEach(Runnable::run);
}
/**
* Call do stop the watch thread
*/
public void shutdown() {
running = false;
if (propertyFileWatcherThread != null) {
propertyFileWatcherThread.interrupt();
}
}
class PropertyFileWatcher implements Runnable {
public void run() {
final File toWatch = new File(sourceLocation);
try {
final Path path = FileSystems.getDefault().getPath(toWatch.getParent());
log.info("PropertyFileWatcher started on {}", path);
loopAndWatch(toWatch, path);
} catch (InterruptedException e) {
} catch (Exception e) {
log.error("PropertyFileWatcher failed", e);
}
log.info("PropertyFileWatcher ended");
}
private void loopAndWatch(final File toWatch, final Path path) throws IOException, InterruptedException {
try (final WatchService watchService = FileSystems.getDefault().newWatchService()) {
path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
while (running) {
final WatchKey wk = watchService.take();
processWatchKey(toWatch, wk);
}
}
}
private void processWatchKey(final File toWatch, final WatchKey wk) throws InterruptedException {
for (final WatchEvent<?> event : wk.pollEvents()) {
// we only register "ENTRY_MODIFY" so the context is always a Path.
final Path changed = (Path) event.context();
if (changed.endsWith(toWatch.getName())) {
log.debug("{} changed => reload", toWatch.getAbsolutePath());
reload();
}
}
boolean valid = wk.reset();
if (!valid) {
log.warn("The PropertyFileWatcher's key has been unregistered.");
}
}
}
}