-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
LoggerContextListenerModelHandler.java
executable file
·79 lines (66 loc) · 2.8 KB
/
LoggerContextListenerModelHandler.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
package ch.qos.logback.classic.model.processor;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.model.LoggerContextListenerModel;
import ch.qos.logback.classic.spi.LoggerContextListener;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.model.Model;
import ch.qos.logback.core.model.processor.ModelHandlerBase;
import ch.qos.logback.core.model.processor.ModelHandlerException;
import ch.qos.logback.core.model.processor.ModelInterpretationContext;
import ch.qos.logback.core.spi.ContextAware;
import ch.qos.logback.core.spi.LifeCycle;
import ch.qos.logback.core.util.OptionHelper;
public class LoggerContextListenerModelHandler extends ModelHandlerBase {
boolean inError = false;
LoggerContextListener lcl;
public LoggerContextListenerModelHandler(Context context) {
super(context);
}
static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
return new LoggerContextListenerModelHandler(context);
}
@Override
protected Class<LoggerContextListenerModel> getSupportedModelClass() {
return LoggerContextListenerModel.class;
}
@Override
public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
LoggerContextListenerModel lclModel = (LoggerContextListenerModel) model;
String className = lclModel.getClassName();
if (OptionHelper.isNullOrEmptyOrAllSpaces(className)) {
addError("Empty class name for LoggerContextListener");
inError = true;
} else {
className = mic.getImport(className);
}
try {
lcl = (LoggerContextListener) OptionHelper.instantiateByClassName(className, LoggerContextListener.class,
context);
if (lcl instanceof ContextAware) {
((ContextAware) lcl).setContext(context);
}
mic.pushObject(lcl);
addInfo("Adding LoggerContextListener of type [" + className + "] to the object stack");
} catch (Exception oops) {
inError = true;
addError("Could not create LoggerContextListener of type " + className + "].", oops);
}
}
@Override
public void postHandle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
if (inError) {
return;
}
Object o = mic.peekObject();
if (o != lcl) {
addWarn("The object on the top the of the stack is not the LoggerContextListener pushed earlier.");
} else {
if (lcl instanceof LifeCycle) {
((LifeCycle) lcl).start();
addInfo("Starting LoggerContextListener");
}
((LoggerContext) context).addListener(lcl);
mic.popObject();
}
}
}