View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
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       * Instantiates an appender of the given class and sets its name.
32       * 
33       * The appender thus generated is placed in the {@link InterpretationContext}'s
34       * appender bag.
35       */
36      @SuppressWarnings("unchecked")
37      public void begin(InterpretationContext ec, String localName, Attributes attributes) throws ActionException {
38          // We are just beginning, reset variables
39          appender = null;
40          inError = false;
41  
42          String className = attributes.getValue(CLASS_ATTRIBUTE);
43          if (OptionHelper.isEmpty(className)) {
44              addError("Missing class name for appender. Near [" + localName + "] line " + getLineNumber(ec));
45              inError = true;
46              return;
47          }
48  
49          try {
50              addInfo("About to instantiate appender of type [" + className + "]");
51  
52              appender = (Appender<E>) OptionHelper.instantiateByClassName(className, ch.qos.logback.core.Appender.class, context);
53  
54              appender.setContext(context);
55  
56              String appenderName = ec.subst(attributes.getValue(NAME_ATTRIBUTE));
57  
58              if (OptionHelper.isEmpty(appenderName)) {
59                  addWarn("No appender name given for appender of type " + className + "].");
60              } else {
61                  appender.setName(appenderName);
62                  addInfo("Naming appender as [" + appenderName + "]");
63              }
64  
65              // The execution context contains a bag which contains the appenders
66              // created thus far.
67              HashMap<String, Appender<E>> appenderBag = (HashMap<String, Appender<E>>) ec.getObjectMap().get(ActionConst.APPENDER_BAG);
68  
69              // add the appender just created to the appender bag.
70              appenderBag.put(appenderName, appender);
71  
72              ec.pushObject(appender);
73          } catch (Exception oops) {
74              inError = true;
75              addError("Could not create an Appender of type [" + className + "].", oops);
76              throw new ActionException(oops);
77          }
78      }
79  
80      /**
81       * Once the children elements are also parsed, now is the time to activate the
82       * appender options.
83       */
84      public void end(InterpretationContext ec, String name) {
85          if (inError) {
86              return;
87          }
88  
89          if (appender instanceof LifeCycle) {
90              ((LifeCycle) appender).start();
91          }
92  
93          Object o = ec.peekObject();
94  
95          if (o != appender) {
96              addWarn("The object at the of the stack is not the appender named [" + appender.getName() + "] pushed earlier.");
97          } else {
98              ec.popObject();
99          }
100     }
101 }