View Javadoc

1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework for Java.
3    * 
4    * Copyright (C) 2000-2006, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.core.joran.action;
11  
12  import java.util.HashMap;
13  
14  import org.xml.sax.Attributes;
15  
16  import ch.qos.logback.core.Appender;
17  import ch.qos.logback.core.joran.spi.ActionException;
18  import ch.qos.logback.core.joran.spi.InterpretationContext;
19  import ch.qos.logback.core.spi.LifeCycle;
20  import ch.qos.logback.core.util.OptionHelper;
21  
22  public class AppenderAction<E> extends Action {
23    Appender appender;
24    private boolean inError = false;
25  
26    /**
27     * Instantiates an appender of the given class and sets its name.
28     * 
29     * The appender thus generated is placed in the {@link InterpretationContext}'s
30     * appender bag.
31     */
32    @SuppressWarnings("unchecked")
33    public void begin(InterpretationContext ec, String localName,
34        Attributes attributes) throws ActionException {
35      // We are just beginning, reset variables
36      appender = null;
37      inError = false;
38  
39      String className = attributes.getValue(CLASS_ATTRIBUTE);
40      if (OptionHelper.isEmpty(className)) {
41        addError("Missing class name for appender. Near [" + localName
42            + "] line " + getLineNumber(ec));
43        inError = true;
44        return;
45      }
46  
47      try {
48        addInfo("About to instantiate appender of type [" + className + "]");
49  
50        appender = (Appender) OptionHelper.instantiateByClassName(className,
51            ch.qos.logback.core.Appender.class, context);
52  
53        appender.setContext(context);
54  
55        String appenderName = ec.subst(attributes.getValue(NAME_ATTRIBUTE));
56  
57        if (OptionHelper.isEmpty(appenderName)) {
58          addWarn("No appender name given for appender of type " + className
59              + "].");
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> appenderBag = (HashMap) ec.getObjectMap().get(
68            ActionConst.APPENDER_BAG);
69  
70        // add the appender just created to the appender bag.
71        appenderBag.put(appenderName, appender);
72  
73        ec.pushObject(appender);
74      } catch (Exception oops) {
75        inError = true;
76        addError("Could not create an Appender of type [" + className + "].",
77            oops);
78        throw new ActionException(oops);
79      }
80    }
81  
82    /**
83     * Once the children elements are also parsed, now is the time to activate the
84     * appender options.
85     */
86    public void end(InterpretationContext ec, String name) {
87      if (inError) {
88        return;
89      }
90  
91      if (appender instanceof LifeCycle) {
92        ((LifeCycle) appender).start();
93      }
94  
95      Object o = ec.peekObject();
96  
97      if (o != appender) {
98        addWarn("The object at the of the stack is not the appender named ["
99            + appender.getName() + "] pushed earlier.");
100     } else {
101       addInfo("Popping appender named [" + appender.getName()
102           + "] from the object stack");
103       ec.popObject();
104     }
105   }
106 }