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