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 org.xml.sax.Attributes;
17  
18  import ch.qos.logback.core.Appender;
19  import ch.qos.logback.core.CoreConstants;
20  import ch.qos.logback.core.joran.spi.InterpretationContext;
21  import ch.qos.logback.core.spi.AppenderAttachable;
22  import ch.qos.logback.core.util.OptionHelper;
23  
24  import java.util.HashMap;
25  
26  public class AppenderRefAction<E> extends Action {
27      boolean inError = false;
28  
29      @SuppressWarnings("unchecked")
30      public void begin(InterpretationContext ec, String tagName, Attributes attributes) {
31          // Let us forget about previous errors (in this object)
32          inError = false;
33  
34          // logger.debug("begin called");
35  
36          Object o = ec.peekObject();
37  
38          if (!(o instanceof AppenderAttachable)) {
39              String errMsg = "Could not find an AppenderAttachable at the top of execution stack. Near [" + tagName + "] line " + getLineNumber(ec);
40              inError = true;
41              addError(errMsg);
42              return;
43          }
44  
45          AppenderAttachable<E> appenderAttachable = (AppenderAttachable<E>) o;
46  
47          String appenderName = ec.subst(attributes.getValue(ActionConst.REF_ATTRIBUTE));
48  
49          if (OptionHelper.isEmpty(appenderName)) {
50              // print a meaningful error message and return
51              String errMsg = "Missing appender ref attribute in <appender-ref> tag.";
52              inError = true;
53              addError(errMsg);
54  
55              return;
56          }
57  
58          HashMap<String, Appender<E>> appenderBag = (HashMap<String, Appender<E>>) ec.getObjectMap().get(ActionConst.APPENDER_BAG);
59          Appender<E> appender = (Appender<E>) appenderBag.get(appenderName);
60  
61          if (appender == null) {
62              String msg = "Could not find an appender named [" + appenderName + "]. Did you define it below instead of above in the configuration file?";
63              inError = true;
64              addError(msg);
65              addError("See " + CoreConstants.CODES_URL + "#appender_order for more details.");
66              return;
67          }
68  
69          addInfo("Attaching appender named [" + appenderName + "] to " + appenderAttachable);
70          appenderAttachable.addAppender(appender);
71      }
72  
73      public void end(InterpretationContext ec, String n) {
74      }
75  
76  }