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;
15  
16  import java.util.HashMap;
17  import java.util.Map;
18  
19  import ch.qos.logback.core.Appender;
20  import ch.qos.logback.core.joran.action.ActionConst;
21  import ch.qos.logback.core.joran.action.AppenderAction;
22  import ch.qos.logback.core.joran.action.AppenderRefAction;
23  import ch.qos.logback.core.joran.action.ContextPropertyAction;
24  import ch.qos.logback.core.joran.action.ConversionRuleAction;
25  import ch.qos.logback.core.joran.action.DefinePropertyAction;
26  import ch.qos.logback.core.joran.action.NestedBasicPropertyIA;
27  import ch.qos.logback.core.joran.action.NestedComplexPropertyIA;
28  import ch.qos.logback.core.joran.action.NewRuleAction;
29  import ch.qos.logback.core.joran.action.ParamAction;
30  import ch.qos.logback.core.joran.action.PropertyAction;
31  import ch.qos.logback.core.joran.action.ShutdownHookAction;
32  import ch.qos.logback.core.joran.action.StatusListenerAction;
33  import ch.qos.logback.core.joran.action.TimestampAction;
34  import ch.qos.logback.core.joran.spi.ElementSelector;
35  import ch.qos.logback.core.joran.spi.InterpretationContext;
36  import ch.qos.logback.core.joran.spi.Interpreter;
37  import ch.qos.logback.core.joran.spi.RuleStore;
38  
39  // Based on 310985 revision 310985 as attested by http://tinyurl.com/8njps
40  // see also http://tinyurl.com/c2rp5
41  
42  /**
43   * A JoranConfiguratorBase lays most of the groundwork for concrete
44   * configurators derived from it. Concrete configurators only need to implement
45   * the {@link #addInstanceRules} method.
46   * <p>
47   * A JoranConfiguratorBase instance should not be used more than once to
48   * configure a Context.
49   *
50   * @author Ceki G&uuml;lc&uuml;
51   */
52  abstract public class JoranConfiguratorBase<E> extends GenericConfigurator {
53  
54      @Override
55      protected void addInstanceRules(RuleStore rs) {
56  
57          // is "configuration/variable" referenced in the docs?
58          rs.addRule(new ElementSelector("configuration/variable"), new PropertyAction());
59          rs.addRule(new ElementSelector("configuration/property"), new PropertyAction());
60  
61          rs.addRule(new ElementSelector("configuration/substitutionProperty"), new PropertyAction());
62  
63          rs.addRule(new ElementSelector("configuration/timestamp"), new TimestampAction());
64          rs.addRule(new ElementSelector("configuration/shutdownHook"), new ShutdownHookAction());
65          rs.addRule(new ElementSelector("configuration/define"), new DefinePropertyAction());
66  
67          // the contextProperty pattern is deprecated. It is undocumented
68          // and will be dropped in future versions of logback
69          rs.addRule(new ElementSelector("configuration/contextProperty"), new ContextPropertyAction());
70  
71          rs.addRule(new ElementSelector("configuration/conversionRule"), new ConversionRuleAction());
72  
73          rs.addRule(new ElementSelector("configuration/statusListener"), new StatusListenerAction());
74  
75          rs.addRule(new ElementSelector("configuration/appender"), new AppenderAction<E>());
76          rs.addRule(new ElementSelector("configuration/appender/appender-ref"), new AppenderRefAction<E>());
77          rs.addRule(new ElementSelector("configuration/newRule"), new NewRuleAction());
78          rs.addRule(new ElementSelector("*/param"), new ParamAction(getBeanDescriptionCache()));
79      }
80  
81      @Override
82      protected void addImplicitRules(Interpreter interpreter) {
83          // The following line adds the capability to parse nested components
84          NestedComplexPropertyIA nestedComplexPropertyIA = new NestedComplexPropertyIA(getBeanDescriptionCache());
85          nestedComplexPropertyIA.setContext(context);
86          interpreter.addImplicitAction(nestedComplexPropertyIA);
87  
88          NestedBasicPropertyIA nestedBasicIA = new NestedBasicPropertyIA(getBeanDescriptionCache());
89          nestedBasicIA.setContext(context);
90          interpreter.addImplicitAction(nestedBasicIA);
91      }
92  
93      @Override
94      protected void buildInterpreter() {
95          super.buildInterpreter();
96          Map<String, Object> omap = interpreter.getInterpretationContext().getObjectMap();
97          omap.put(ActionConst.APPENDER_BAG, new HashMap<String, Appender<?>>());
98          //omap.put(ActionConst.FILTER_CHAIN_BAG, new HashMap());
99      }
100 
101     public InterpretationContext getInterpretationContext() {
102         return interpreter.getInterpretationContext();
103     }
104 }