View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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,
38        Attributes attributes) throws ActionException {
39      // We are just beginning, reset variables
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        // The execution context contains a bag which contains the appenders
70        // created thus far.
71        HashMap<String, Appender> appenderBag = (HashMap) ec.getObjectMap().get(
72            ActionConst.APPENDER_BAG);
73  
74        // add the appender just created to the appender bag.
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     * Once the children elements are also parsed, now is the time to activate the
88     * appender options.
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 }