View Javadoc

1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 2000-2009, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.classic.sift;
11  
12  import java.util.Collection;
13  import java.util.HashMap;
14  import java.util.Map;
15  
16  import ch.qos.logback.classic.spi.LoggingEvent;
17  import ch.qos.logback.classic.util.DefaultNestedComponentRules;
18  import ch.qos.logback.core.Appender;
19  import ch.qos.logback.core.joran.action.ActionConst;
20  import ch.qos.logback.core.joran.action.AppenderAction;
21  import ch.qos.logback.core.joran.spi.DefaultNestedComponentRegistry;
22  import ch.qos.logback.core.joran.spi.Pattern;
23  import ch.qos.logback.core.joran.spi.RuleStore;
24  import ch.qos.logback.core.sift.SiftingJoranConfiguratorBase;
25  
26  public class SiftingJoranConfigurator  extends SiftingJoranConfiguratorBase<LoggingEvent> {
27  
28    String key;
29    String value;
30    
31    SiftingJoranConfigurator(String key, String value) {
32      this.key = key;
33      this.value = value;
34    }
35    
36    @Override
37    protected Pattern initialPattern() {
38      return new Pattern("configuration");
39    }
40    
41    @Override
42    protected void addInstanceRules(RuleStore rs) {
43      rs.addRule(new Pattern("configuration/appender"), new AppenderAction());
44    }
45  
46  
47    @Override
48    protected void addDefaultNestedComponentRegistryRules(
49        DefaultNestedComponentRegistry registry) {
50      DefaultNestedComponentRules.addDefaultNestedComponentRegistryRules(registry);
51    }
52      
53    @Override
54    protected void buildInterpreter() {
55      super.buildInterpreter();
56      Map<String, Object> omap = interpreter.getInterpretationContext().getObjectMap();
57      omap.put(ActionConst.APPENDER_BAG, new HashMap());
58      omap.put(ActionConst.FILTER_CHAIN_BAG, new HashMap());
59      Map<String, String> propertiesMap = new HashMap<String, String>();
60      propertiesMap.put(key, value);
61      interpreter.setInterpretationContextPropertiesMap(propertiesMap);
62    }
63  
64    @SuppressWarnings("unchecked")
65    public Appender<LoggingEvent> getAppender() {
66      Map<String, Object> omap = interpreter.getInterpretationContext().getObjectMap();
67      HashMap map = (HashMap) omap.get(ActionConst.APPENDER_BAG);
68      Collection values = map.values();
69      return (Appender<LoggingEvent>) values.iterator().next();
70    }
71  }