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.access.sift;
15  
16  import java.util.Collection;
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import ch.qos.logback.access.spi.IAccessEvent;
21  import ch.qos.logback.core.Appender;
22  import ch.qos.logback.core.joran.action.ActionConst;
23  import ch.qos.logback.core.joran.action.AppenderAction;
24  import ch.qos.logback.core.joran.spi.ElementPath;
25  import ch.qos.logback.core.joran.spi.ElementSelector;
26  import ch.qos.logback.core.joran.spi.RuleStore;
27  import ch.qos.logback.core.sift.SiftingJoranConfiguratorBase;
28  
29  public class SiftingJoranConfigurator extends SiftingJoranConfiguratorBase<IAccessEvent> {
30  
31      SiftingJoranConfigurator(String key, String value, Map<String, String> parentPropertyMap) {
32          super(key, value, parentPropertyMap);
33      }
34  
35      @Override
36      protected ElementPath initialElementPath() {
37          return new ElementPath("configuration");
38      }
39  
40      @Override
41      protected void addInstanceRules(RuleStore rs) {
42          rs.addRule(new ElementSelector("configuration/appender"), new AppenderAction<IAccessEvent>());
43      }
44  
45      @Override
46      protected void buildInterpreter() {
47          super.buildInterpreter();
48          Map<String, Object> omap = interpreter.getInterpretationContext().getObjectMap();
49          omap.put(ActionConst.APPENDER_BAG, new HashMap<String, Appender<IAccessEvent>>());
50          //omap.put(ActionConst.FILTER_CHAIN_BAG, new HashMap());
51          Map<String, String> propertiesMap = new HashMap<String, String>();
52          propertiesMap.putAll(parentPropertyMap);
53          propertiesMap.put(key, value);
54          interpreter.setInterpretationContextPropertiesMap(propertiesMap);
55      }
56  
57      @SuppressWarnings("unchecked")
58      @Override
59      public Appender<IAccessEvent> getAppender() {
60          Map<String, Object> omap = interpreter.getInterpretationContext().getObjectMap();
61          HashMap<String, Appender<?>> appenderMap = (HashMap<String, Appender<?>>) omap.get(ActionConst.APPENDER_BAG);
62          oneAndOnlyOneCheck(appenderMap);
63          Collection<Appender<?>> values = appenderMap.values();
64          if (values.size() == 0) {
65              return null;
66          }
67          return (Appender<IAccessEvent>) values.iterator().next();
68      }
69  }