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.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.NestedBasicPropertyIA;
23  import ch.qos.logback.core.joran.action.NestedComplexPropertyIA;
24  import ch.qos.logback.core.joran.event.SaxEvent;
25  import ch.qos.logback.core.joran.spi.Interpreter;
26  import ch.qos.logback.core.joran.spi.JoranException;
27  
28  public abstract class SiftingJoranConfiguratorBase<E> extends
29      GenericConfigurator {
30  
31    final static String ONE_AND_ONLY_ONE_URL = CoreConstants.CODES_URL
32        + "#1andOnly1";
33  
34    @Override
35    protected void addImplicitRules(Interpreter interpreter) {
36      NestedComplexPropertyIA nestedComplexIA = new NestedComplexPropertyIA();
37      nestedComplexIA.setContext(context);
38      interpreter.addImplicitAction(nestedComplexIA);
39  
40      NestedBasicPropertyIA nestedSimpleIA = new NestedBasicPropertyIA();
41      nestedSimpleIA.setContext(context);
42      interpreter.addImplicitAction(nestedSimpleIA);
43    }
44  
45    abstract public Appender<E> getAppender();
46  
47    int errorEmmissionCount = 0;
48  
49    protected void oneAndOnlyOneCheck(Map appenderMap) {
50      String errMsg = null;
51      if (appenderMap.size() == 0) {
52        errorEmmissionCount++;
53        errMsg = "No nested appenders found within the <sift> element in SiftingAppender.";
54      } else if (appenderMap.size() > 1) {
55        errorEmmissionCount++;
56        errMsg = "Only and only one appender can be nested the <sift> element in SiftingAppender. See also "
57            + ONE_AND_ONLY_ONE_URL;
58      }
59  
60      if (errMsg != null && errorEmmissionCount < CoreConstants.MAX_ERROR_COUNT) {
61        addError(errMsg);
62      }
63    }
64  
65    public void doConfigure(final List<SaxEvent> eventList) throws JoranException {
66      super.doConfigure(eventList);
67    }
68  }