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.sift;
15  
16  import java.util.List;
17  import java.util.Map;
18  
19  import ch.qos.logback.core.Appender;
20  import ch.qos.logback.core.CoreConstants;
21  import ch.qos.logback.core.joran.GenericConfigurator;
22  import ch.qos.logback.core.joran.action.*;
23  import ch.qos.logback.core.joran.event.SaxEvent;
24  import ch.qos.logback.core.joran.spi.ElementSelector;
25  import ch.qos.logback.core.joran.spi.Interpreter;
26  import ch.qos.logback.core.joran.spi.JoranException;
27  import ch.qos.logback.core.joran.spi.RuleStore;
28  
29  public abstract class SiftingJoranConfiguratorBase<E> extends GenericConfigurator {
30  
31      protected final String key;
32      protected final String value;
33      // properties inherited from the main joran run
34      protected final Map<String, String> parentPropertyMap;
35  
36      protected SiftingJoranConfiguratorBase(String key, String value, Map<String, String> parentPropertyMap) {
37  		this.key = key;
38          this.value = value;
39          this.parentPropertyMap = parentPropertyMap;
40      }
41  
42      final static String ONE_AND_ONLY_ONE_URL = CoreConstants.CODES_URL + "#1andOnly1";
43  
44      @Override
45      protected void addImplicitRules(Interpreter interpreter) {
46          NestedComplexPropertyIA nestedComplexIA = new NestedComplexPropertyIA(getBeanDescriptionCache());
47          nestedComplexIA.setContext(context);
48          interpreter.addImplicitAction(nestedComplexIA);
49  
50          NestedBasicPropertyIA nestedSimpleIA = new NestedBasicPropertyIA(getBeanDescriptionCache());
51          nestedSimpleIA.setContext(context);
52          interpreter.addImplicitAction(nestedSimpleIA);
53      }
54  
55      @Override
56      protected void addInstanceRules(RuleStore rs) {
57          rs.addRule(new ElementSelector("configuration/property"), new PropertyAction());
58          rs.addRule(new ElementSelector("configuration/timestamp"), new TimestampAction());
59          rs.addRule(new ElementSelector("configuration/define"), new DefinePropertyAction());
60      }
61  
62      abstract public Appender<E> getAppender();
63  
64      int errorEmmissionCount = 0;
65  
66      protected void oneAndOnlyOneCheck(Map<?, ?> appenderMap) {
67          String errMsg = null;
68          if (appenderMap.size() == 0) {
69              errorEmmissionCount++;
70              errMsg = "No nested appenders found within the <sift> element in SiftingAppender.";
71          } else if (appenderMap.size() > 1) {
72              errorEmmissionCount++;
73              errMsg = "Only and only one appender can be nested the <sift> element in SiftingAppender. See also " + ONE_AND_ONLY_ONE_URL;
74          }
75  
76          if (errMsg != null && errorEmmissionCount < CoreConstants.MAX_ERROR_COUNT) {
77              addError(errMsg);
78          }
79      }
80  
81      public void doConfigure(final List<SaxEvent> eventList) throws JoranException {
82          super.doConfigure(eventList);
83      }
84  
85      @Override
86      public String toString() {
87          return this.getClass().getName() + "{" + key + "=" + value + '}';
88      }
89  }