1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.joran.action;
15
16 import java.util.Map;
17
18 import org.xml.sax.Attributes;
19
20 import ch.qos.logback.core.CoreConstants;
21 import ch.qos.logback.core.boolex.EventEvaluator;
22 import ch.qos.logback.core.joran.spi.InterpretationContext;
23 import ch.qos.logback.core.spi.LifeCycle;
24 import ch.qos.logback.core.util.OptionHelper;
25
26 abstract public class AbstractEventEvaluatorAction extends Action {
27
28 EventEvaluator evaluator;
29 boolean inError = false;
30
31
32
33
34 public void begin(InterpretationContext ec, String name, Attributes attributes) {
35
36 inError = false;
37 evaluator = null;
38
39 String className = attributes.getValue(CLASS_ATTRIBUTE);
40 if (OptionHelper.isEmpty(className)) {
41 className = defaultClassName();
42 addInfo("Assuming default evaluator class [" + className + "]");
43 }
44
45 if (OptionHelper.isEmpty(className)) {
46 className = defaultClassName();
47 inError = true;
48 addError("Mandatory \"" + CLASS_ATTRIBUTE
49 + "\" attribute not set for <evaluator>");
50 return;
51 }
52
53 String evaluatorName = attributes.getValue(Action.NAME_ATTRIBUTE);
54 if (OptionHelper.isEmpty(evaluatorName)) {
55 inError = true;
56 addError("Mandatory \"" + NAME_ATTRIBUTE
57 + "\" attribute not set for <evaluator>");
58 return;
59 }
60 try {
61 evaluator = (EventEvaluator) OptionHelper.instantiateByClassName(
62 className, ch.qos.logback.core.boolex.EventEvaluator.class, context);
63
64 evaluator.setContext(this.context);
65 evaluator.setName(evaluatorName);
66
67 ec.pushObject(evaluator);
68 addInfo("Adding evaluator named [" + evaluatorName
69 + "] to the object stack");
70
71 } catch (Exception oops) {
72 inError = true;
73 addError("Could not create evaluator of type " + className + "].", oops);
74 }
75 }
76
77
78
79
80
81
82 abstract protected String defaultClassName();
83
84
85
86
87
88 @SuppressWarnings("unchecked")
89 public void end(InterpretationContext ec, String e) {
90 if (inError) {
91 return;
92 }
93
94 if (evaluator instanceof LifeCycle) {
95 ((LifeCycle) evaluator).start();
96 addInfo("Starting evaluator named [" + evaluator.getName() + "]");
97 }
98
99 Object o = ec.peekObject();
100
101 if (o != evaluator) {
102 addWarn("The object on the top the of the stack is not the evaluator pushed earlier.");
103 } else {
104 ec.popObject();
105
106 try {
107 Map<String, EventEvaluator> evaluatorMap = (Map<String, EventEvaluator>) context
108 .getObject(CoreConstants.EVALUATOR_MAP);
109 if(evaluatorMap == null) {
110 addError("Could not find EvaluatorMap");
111 } else {
112 evaluatorMap.put(evaluator.getName(), evaluator);
113 }
114 } catch (Exception ex) {
115 addError("Could not set evaluator named [" + evaluator + "].", ex);
116 }
117 }
118 }
119
120 public void finish(InterpretationContext ec) {
121 }
122 }