-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Scope.java
542 lines (462 loc) · 19.4 KB
/
Scope.java
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
package liquibase;
import liquibase.configuration.LiquibaseConfiguration;
import liquibase.database.Database;
import liquibase.database.DatabaseConnection;
import liquibase.database.OfflineConnection;
import liquibase.database.jvm.JdbcConnection;
import liquibase.exception.UnexpectedLiquibaseException;
import liquibase.integration.commandline.LiquibaseCommandLineConfiguration;
import liquibase.listener.LiquibaseListener;
import liquibase.logging.LogService;
import liquibase.logging.Logger;
import liquibase.logging.core.JavaLogService;
import liquibase.logging.core.LogServiceFactory;
import liquibase.logging.mdc.CustomMdcObject;
import liquibase.logging.mdc.MdcManager;
import liquibase.logging.mdc.MdcManagerFactory;
import liquibase.logging.mdc.MdcObject;
import liquibase.osgi.Activator;
import liquibase.resource.ClassLoaderResourceAccessor;
import liquibase.resource.ResourceAccessor;
import liquibase.servicelocator.ServiceLocator;
import liquibase.servicelocator.StandardServiceLocator;
import liquibase.ui.ConsoleUIService;
import liquibase.ui.UIService;
import liquibase.util.CollectionUtil;
import liquibase.util.SmartMap;
import liquibase.util.StringUtil;
import java.lang.reflect.Constructor;
import java.nio.charset.Charset;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/**
* This scope object is used to hold configuration and other parameters within a call without needing complex method signatures.
* It also allows new parameters to be added by extensions without affecting standard method signatures.
* Scope objects can be created in a hierarchical manner with the {@link #child(Map, ScopedRunner)} or {@link #child(String, Object, ScopedRunner)} methods.
* Values set in parent scopes are visible in child scopes, but values in child scopes are not visible to parent scopes.
* Values with the same key in different scopes "mask" each other with the value furthest down the scope chain being returned.
*/
public class Scope {
/**
* Enumeration containing standard attributes. Normally use methods like convenience {@link #getResourceAccessor()} or {@link #getDatabase()}
*/
public enum Attr {
logService,
ui,
resourceAccessor,
classLoader,
database,
quotingStrategy,
changeLogHistoryService,
lockService,
executeMode,
lineSeparator,
serviceLocator,
/**
* @deprecated use {@link GlobalConfiguration#FILE_ENCODING}
*/
fileEncoding,
databaseChangeLog,
changeSet,
osgiPlatform,
checksumVersion
}
private static ScopeManager scopeManager;
private Scope parent;
private final SmartMap values = new SmartMap();
private String scopeId;
private static final Map<String, List<MdcObject>> addedMdcEntries = new ConcurrentHashMap<>();
private LiquibaseListener listener;
public static Scope getCurrentScope() {
if (scopeManager == null) {
scopeManager = new SingletonScopeManager();
}
if (scopeManager.getCurrentScope() == null) {
Scope rootScope = new Scope();
scopeManager.setCurrentScope(rootScope);
rootScope.values.put(Attr.logService.name(), new JavaLogService());
rootScope.values.put(Attr.serviceLocator.name(), new StandardServiceLocator());
rootScope.values.put(Attr.resourceAccessor.name(), new ClassLoaderResourceAccessor());
rootScope.values.put(Attr.checksumVersion.name(), ChecksumVersion.latest());
rootScope.values.put(Attr.ui.name(), new ConsoleUIService());
rootScope.getSingleton(LiquibaseConfiguration.class).init(rootScope);
LogService overrideLogService = rootScope.getSingleton(LogServiceFactory.class).getDefaultLogService();
if (overrideLogService == null) {
throw new UnexpectedLiquibaseException("Cannot find default log service");
}
rootScope.values.put(Attr.logService.name(), overrideLogService);
//check for higher-priority serviceLocator
ServiceLocator serviceLocator = rootScope.getServiceLocator();
for (ServiceLocator possibleLocator : serviceLocator.findInstances(ServiceLocator.class)) {
if (possibleLocator.getPriority() > serviceLocator.getPriority()) {
serviceLocator = possibleLocator;
}
}
rootScope.values.put(Attr.serviceLocator.name(), serviceLocator);
rootScope.values.put(Attr.osgiPlatform.name(), Activator.OSGIContainerChecker.isOsgiPlatform());
}
return scopeManager.getCurrentScope();
}
public static void setScopeManager(ScopeManager scopeManager) {
Scope currentScope = getCurrentScope();
if (currentScope == null) {
currentScope = new Scope();
}
try {
currentScope = scopeManager.init(currentScope);
} catch (Exception e) {
Scope.getCurrentScope().getLog(Scope.class).warning(e.getMessage(), e);
}
scopeManager.setCurrentScope(currentScope);
Scope.scopeManager = scopeManager;
}
/**
* Creates a new "root" scope.
* Defaults resourceAccessor to {@link ClassLoaderResourceAccessor}.
* Defaults serviceLocator to {@link StandardServiceLocator}
*/
private Scope() {
scopeId = "root";
parent = null;
}
/**
* @param parent The new Scopes parent in the hierarchy of Scopes, not null.
* @param scopeValues The values for the new Scope.
*/
protected Scope(Scope parent, Map<String, Object> scopeValues) {
if (parent == null) {
throw new UnexpectedLiquibaseException("Cannot pass a null parent to a new Scope. Use Scope.child to correctly create a nested scope");
}
this.parent = parent;
scopeId = generateScopeId();
if (scopeValues != null) {
for (Map.Entry<String, Object> entry : scopeValues.entrySet()) {
values.put(entry.getKey(), entry.getValue());
}
}
}
private String generateScopeId() {
return StringUtil.randomIdentifer(10).toLowerCase();
}
/**
* Returns the parent scope to this scope. Returns null if this is a root scope.
*/
public Scope getParent() {
return parent;
}
/**
* Creates a new scope that is a child of this scope.
*/
public static void child(Map<String, Object> scopeValues, ScopedRunner runner) throws Exception {
child((LiquibaseListener) null, scopeValues, runner);
}
/**
* Creates a new scope that is a child of this scope.
*/
public static <ReturnType> ReturnType child(Map<String, Object> scopeValues, ScopedRunnerWithReturn<ReturnType> runner) throws Exception {
return child(null, scopeValues, runner);
}
/**
* Creates a new child scope that includes the given {@link LiquibaseListener}.
* You cannot unassign a listener, they simply fall out of scope when the Scope does.
*
* @see #getListeners(Class)
*/
public static void child(LiquibaseListener listener, ScopedRunner runner) throws Exception {
child(listener, null, runner);
}
public static void child(LiquibaseListener listener, Map<String, Object> scopeValues, ScopedRunner runner) throws Exception {
child(listener, scopeValues, () -> {
runner.run();
return null;
});
}
public static <T> T child(LiquibaseListener listener, Map<String, Object> scopeValues, ScopedRunnerWithReturn<T> runner) throws Exception {
String scopeId = enter(listener, scopeValues);
try {
return runner.run();
} finally {
exit(scopeId);
}
}
/**
* Convenience version of {@link #enter(LiquibaseListener, Map)} with no {@link LiquibaseListener}
*/
public static String enter(Map<String, Object> scopeValues) throws Exception {
return enter(null, scopeValues);
}
/**
* Creates a new scope without passing a ScopedRunner.
* This mainly exists for tests where you have a setup/cleanup method pattern.
* The recommended way to create Scopes is the "child" methods.
* When done with the scope, call {@link #exit(String)}
*
* @return Returns the scopeId to pass to to {@link #exit(String)}
*/
public static String enter(LiquibaseListener listener, Map<String, Object> scopeValues) throws Exception {
Scope originalScope = getCurrentScope();
Scope child = new Scope(originalScope, scopeValues);
child.listener = listener;
scopeManager.setCurrentScope(child);
return child.scopeId;
}
/**
* Exits the scope started with {@link #enter(LiquibaseListener, Map)}
*
* @param scopeId The id of the scope to exit. Throws an exception if the name does not match the current scope.
*/
public static void exit(String scopeId) throws Exception {
Scope currentScope = getCurrentScope();
if (!currentScope.scopeId.equals(scopeId)) {
throw new RuntimeException("Cannot end scope " + scopeId + " when currently at scope " + currentScope.scopeId);
}
// clear the MDC values added in this scope
List<MdcObject> mdcObjects = addedMdcEntries.remove(currentScope.scopeId);
for (MdcObject mdcObject : CollectionUtil.createIfNull(mdcObjects)) {
mdcObject.close();
}
scopeManager.setCurrentScope(currentScope.getParent());
}
/**
* Creates a new scope that is a child of this scope.
*/
public static void child(String newValueKey, Object newValue, ScopedRunner runner) throws Exception {
Map<String, Object> scopeValues = new HashMap<>();
scopeValues.put(newValueKey, newValue);
child(scopeValues, runner);
}
public static void child(Enum newValueKey, Object newValue, ScopedRunner runner) throws Exception {
child(newValueKey.name(), newValue, runner);
}
/**
* Return true if the given key is defined.
*/
public boolean has(String key) {
return get(key, Object.class) != null;
}
/**
* Return true if the given key is defined.
*/
public boolean has(Enum key) {
return has(key.name());
}
public synchronized <T> T get(Enum key, Class<T> type) {
return get(key.name(), type);
}
public <T> T get(Enum key, T defaultValue) {
return get(key.name(), defaultValue);
}
/**
* Return the value associated with the given key in this scope or any parent scope.
* The value is converted to the given type if necessary using {@link liquibase.util.ObjectUtil#convert(Object, Class)}.
* Returns null if key is not defined in this or any parent scopes.
*/
public <T> T get(String key, Class<T> type) {
T value = values.get(key, type);
if (value == null && parent != null) {
value = parent.get(key, type);
}
return value;
}
/**
* Return the value associated with the given key in this scope or any parent scope.
* If the value is not defined, the passed defaultValue is returned.
* The value is converted to the given type if necessary using {@link liquibase.util.ObjectUtil#convert(Object, Class)}.
*/
public synchronized <T> T get(String key, T defaultValue) {
Class type;
if (defaultValue == null) {
type = Object.class;
} else {
type = defaultValue.getClass();
}
Object value = get(key, type);
if (value == null) {
return defaultValue;
}
return (T) value;
}
/**
* Looks up the singleton object of the given type. If the singleton has not been created yet, it will be instantiated.
* The singleton is a singleton based on the root scope and the same object will be returned for all child scopes of the root.
*/
public synchronized <T extends SingletonObject> T getSingleton(Class<T> type) {
if (getParent() != null) {
return getParent().getSingleton(type);
}
String key = type.getName();
T singleton = get(key, type);
if (singleton == null) {
try {
try {
Constructor<T> constructor = type.getDeclaredConstructor(Scope.class);
constructor.setAccessible(true);
singleton = constructor.newInstance(this);
} catch (NoSuchMethodException e) { //try without scope
Constructor<T> constructor = type.getDeclaredConstructor();
constructor.setAccessible(true);
singleton = constructor.newInstance();
}
} catch (Exception e) {
throw new UnexpectedLiquibaseException(e);
}
values.put(key, singleton);
}
return singleton;
}
public Logger getLog(Class clazz) {
return get(Attr.logService, LogService.class).getLog(clazz);
}
public UIService getUI() {
return get(Attr.ui, UIService.class);
}
public Database getDatabase() {
return get(Attr.database, Database.class);
}
public ClassLoader getClassLoader() {
return get(Attr.classLoader, Thread.currentThread().getContextClassLoader());
}
public ClassLoader getClassLoader(boolean fallbackToContextClassLoader) {
ClassLoader classLoader = getClassLoader();
if (classLoader == null && fallbackToContextClassLoader) {
return Thread.currentThread().getContextClassLoader();
}
return classLoader;
}
public ServiceLocator getServiceLocator() {
return get(Attr.serviceLocator, ServiceLocator.class);
}
public ResourceAccessor getResourceAccessor() {
return get(Attr.resourceAccessor, ResourceAccessor.class);
}
public ChecksumVersion getChecksumVersion() {
return get(Attr.checksumVersion, ChecksumVersion.class);
}
public String getLineSeparator() {
return get(Attr.lineSeparator, System.lineSeparator());
}
/**
* @deprecated use {@link GlobalConfiguration#FILE_ENCODING}
*/
public Charset getFileEncoding() {
return get(Attr.fileEncoding, Charset.defaultCharset());
}
/**
* Get the current MDC manager.
*/
public MdcManager getMdcManager() {
MdcManagerFactory mdcManagerFactory = getSingleton(MdcManagerFactory.class);
return mdcManagerFactory.getMdcManager();
}
/**
* Add a key value pair to the MDC using the MDC manager. This key value pair will be automatically removed from the
* MDC when this scope exits.
*/
public MdcObject addMdcValue(String key, String value) {
return addMdcValue(key, value, true);
}
/**
* Add a key value pair to the MDC using the MDC manager.
* @param removeWhenScopeExits if true, this key value pair will be automatically removed from the MDC when this
* scope exits. If there is not a demonstrable reason for setting this parameter to false
* then it should be set to true.
*/
public MdcObject addMdcValue(String key, String value, boolean removeWhenScopeExits) {
MdcObject mdcObject = getMdcManager().put(key, value);
removeMdcObjectWhenScopeExits(removeWhenScopeExits, mdcObject);
return mdcObject;
}
private void removeMdcObjectWhenScopeExits(boolean removeWhenScopeExits, MdcObject mdcObject) {
if (removeWhenScopeExits) {
Scope currentScope = getCurrentScope();
addedMdcEntries
.computeIfAbsent(currentScope.scopeId, k -> new ArrayList<>())
.add(mdcObject);
}
}
/**
* Add a key value pair to the MDC using the MDC manager. This key value pair will be automatically removed from the
* MDC when this scope exits.
*/
public MdcObject addMdcValue(String key, Map<String, String> value) {
return addMdcValue(key, value, true);
}
/**
* Add a key value pair to the MDC using the MDC manager.
* @param removeWhenScopeExits if true, this key value pair will be automatically removed from the MDC when this
* scope exits. If there is not a demonstrable reason for setting this parameter to false
* then it should be set to true.
*/
public MdcObject addMdcValue(String key, Map<String, String> value, boolean removeWhenScopeExits) {
MdcObject mdcObject = getMdcManager().put(key, value);
removeMdcObjectWhenScopeExits(removeWhenScopeExits, mdcObject);
return mdcObject;
}
/**
* Add a key value pair to the MDC using the MDC manager. This key value pair will be automatically removed from the
* MDC when this scope exits.
*/
public MdcObject addMdcValue(String key, CustomMdcObject customMdcObject) {
return addMdcValue(key, customMdcObject, true);
}
/**
* Add a key value pair to the MDC using the MDC manager.
* @param removeWhenScopeExits if true, this key value pair will be automatically removed from the MDC when this
* scope exits. If there is not a demonstrable reason for setting this parameter to false
* then it should be set to true.
*/
public MdcObject addMdcValue(String key, CustomMdcObject customMdcObject, boolean removeWhenScopeExits) {
MdcObject mdcObject = getMdcManager().put(key, customMdcObject);
removeMdcObjectWhenScopeExits(removeWhenScopeExits, mdcObject);
return mdcObject;
}
/**
* Check if the provided mdc key is present
* @return true if there is an existing key, false otherwise
*/
@Beta
public boolean isMdcKeyPresent(String key) {
Object mdc = getMdcManager().getAll().get(key);
return mdc != null;
}
/**
* Returns {@link LiquibaseListener}s defined in this scope and/or all its parents that are of the given type.
*/
public <T extends LiquibaseListener> Collection<T> getListeners(Class<T> type) {
List<T> returnList = new ArrayList<>();
Scope scopeToCheck = this;
while (scopeToCheck != null) {
if (scopeToCheck.listener != null && type.isAssignableFrom(scopeToCheck.listener.getClass())) {
returnList.add((T) scopeToCheck.listener);
}
scopeToCheck = scopeToCheck.getParent();
}
return returnList;
}
@Override
public String toString() {
return describe();
}
public String describe() {
String databaseName = null;
Database database = getDatabase();
if (database != null) {
databaseName = database.getShortName();
DatabaseConnection connection = database.getConnection();
if (connection == null) {
databaseName = "unconnected " + databaseName;
} else if (connection instanceof OfflineConnection) {
databaseName = "offline " + databaseName;
} else if (connection instanceof JdbcConnection) {
databaseName = "jdbc " + databaseName;
}
}
return "scope(database=" + databaseName + ")";
}
public interface ScopedRunner<T> {
void run() throws Exception;
}
public interface ScopedRunnerWithReturn<T> {
T run() throws Exception;
}
}