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.HashMap;
17
18 import org.xml.sax.Attributes;
19
20 import ch.qos.logback.core.Appender;
21 import ch.qos.logback.core.joran.spi.ActionException;
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 public class AppenderAction<E> extends Action {
27 Appender<E> appender;
28 private boolean inError = false;
29
30
31
32
33
34
35
36 @SuppressWarnings("unchecked")
37 public void begin(InterpretationContext ec, String localName,
38 Attributes attributes) throws ActionException {
39
40 appender = null;
41 inError = false;
42
43 String className = attributes.getValue(CLASS_ATTRIBUTE);
44 if (OptionHelper.isEmpty(className)) {
45 addError("Missing class name for appender. Near [" + localName
46 + "] line " + getLineNumber(ec));
47 inError = true;
48 return;
49 }
50
51 try {
52 addInfo("About to instantiate appender of type [" + className + "]");
53
54 appender = (Appender) OptionHelper.instantiateByClassName(className,
55 ch.qos.logback.core.Appender.class, context);
56
57 appender.setContext(context);
58
59 String appenderName = ec.subst(attributes.getValue(NAME_ATTRIBUTE));
60
61 if (OptionHelper.isEmpty(appenderName)) {
62 addWarn("No appender name given for appender of type " + className
63 + "].");
64 } else {
65 appender.setName(appenderName);
66 addInfo("Naming appender as [" + appenderName + "]");
67 }
68
69
70
71 HashMap<String, Appender> appenderBag = (HashMap) ec.getObjectMap().get(
72 ActionConst.APPENDER_BAG);
73
74
75 appenderBag.put(appenderName, appender);
76
77 ec.pushObject(appender);
78 } catch (Exception oops) {
79 inError = true;
80 addError("Could not create an Appender of type [" + className + "].",
81 oops);
82 throw new ActionException(oops);
83 }
84 }
85
86
87
88
89
90 public void end(InterpretationContext ec, String name) {
91 if (inError) {
92 return;
93 }
94
95 if (appender instanceof LifeCycle) {
96 ((LifeCycle) appender).start();
97 }
98
99 Object o = ec.peekObject();
100
101 if (o != appender) {
102 addWarn("The object at the of the stack is not the appender named ["
103 + appender.getName() + "] pushed earlier.");
104 } else {
105 ec.popObject();
106 }
107 }
108 }