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 chapter10.newRule;
11  
12  import java.util.HashMap;
13  import java.util.Map;
14  
15  import ch.qos.logback.core.Context;
16  import ch.qos.logback.core.ContextBase;
17  import ch.qos.logback.core.joran.action.Action;
18  import ch.qos.logback.core.joran.action.NewRuleAction;
19  import ch.qos.logback.core.joran.spi.Pattern;
20  import ch.qos.logback.core.util.StatusPrinter;
21  import chapter10.SimpleConfigurator;
22  import chapter10.calculator.ComputationAction1;
23  
24  /**
25   * This example illustrates the usage of NewRuleAction which allows the Joran
26   * interpreter to learn new rules on the fly.
27   * 
28   * <p>This example relies heavily on the code from the joran.calculator
29   * package.
30   * 
31   * @author Ceki G&uuml;ulc&uuml;
32   */
33  public class NewRuleCalculator {
34    public static void main(String[] args) throws Exception {
35  
36      Context context = new ContextBase();
37  
38      Map<Pattern, Action> ruleMap = new HashMap<Pattern, Action>();
39  
40      // we start with the rule for the top-most (root) element
41      ruleMap.put(new Pattern("*/computation"), new ComputationAction1());
42  
43      // Associate "/new-rule" pattern with NewRuleAction from the
44      // org.apache.joran.action package.
45      // 
46      // We will let the XML file to teach the Joran interpreter about new rules
47      ruleMap.put(new Pattern("/computation/new-rule"), new NewRuleAction());
48  
49      SimpleConfigurator simpleConfigurator = new SimpleConfigurator(ruleMap);
50      // link the configurator with its context
51      simpleConfigurator.setContext(context);
52  
53      simpleConfigurator.doConfigure(args[0]);
54  
55      // Print any errors that might have occured.
56      StatusPrinter.printInCaseOfErrorsOrWarnings(context);
57    }
58  
59  }