View Javadoc

1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 2000-2008, 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  
11  package ch.qos.logback.core.joran.action;
12  
13  import java.util.Map;
14  
15  import org.xml.sax.Attributes;
16  
17  import ch.qos.logback.core.CoreConstants;
18  import ch.qos.logback.core.boolex.EventEvaluator;
19  import ch.qos.logback.core.joran.spi.InterpretationContext;
20  import ch.qos.logback.core.spi.LifeCycle;
21  import ch.qos.logback.core.util.OptionHelper;
22  
23  abstract public class AbstractEventEvaluatorAction extends Action {
24  
25    EventEvaluator evaluator;
26    boolean inError = false;
27  
28    /**
29     * Instantiates an evaluator of the given class and sets its name.
30     */
31    @SuppressWarnings("unchecked")
32    public void begin(InterpretationContext ec, String name, Attributes attributes) {
33      // Let us forget about previous errors (in this instance)
34      inError = false;
35      evaluator = null;
36  
37      String className = attributes.getValue(CLASS_ATTRIBUTE);
38      if (OptionHelper.isEmpty(className)) {
39        className = defaultClassName();
40        addWarn("Assuming default evaluator class [" + className + "]");
41      }
42  
43      if (OptionHelper.isEmpty(className)) {
44        className = defaultClassName();
45        inError = true;
46        addError("Mandatory \"" + CLASS_ATTRIBUTE
47            + "\" attribute not set for <evaluator>");
48        return;
49      }
50  
51      String evaluatorName = attributes.getValue(Action.NAME_ATTRIBUTE);
52      if (OptionHelper.isEmpty(evaluatorName)) {
53        inError = true;
54        addError("Mandatory \"" + NAME_ATTRIBUTE
55            + "\" attribute not set for <evaluator>");
56        return;
57      }
58      try {
59        evaluator = (EventEvaluator) OptionHelper.instantiateByClassName(
60            className, ch.qos.logback.core.boolex.EventEvaluator.class, context);
61  
62        evaluator.setContext(this.context);
63        evaluator.setName(evaluatorName);
64  
65        ec.pushObject(evaluator);
66        addInfo("Adding evaluator named [" + evaluatorName
67            + "] to the object stack");
68  
69      } catch (Exception oops) {
70        inError = true;
71        addError("Could not create evaluator of type " + className + "].", oops);
72      }
73    }
74  
75    /**
76     * Returns a default class name in case the class attribute is not specified
77     * 
78     * @return
79     */
80    abstract protected String defaultClassName();
81  
82    /**
83     * Once the children elements are also parsed, now is the time to activate the
84     * evaluator options.
85     */
86    @SuppressWarnings("unchecked")
87    public void end(InterpretationContext ec, String e) {
88      if (inError) {
89        return;
90      }
91  
92      if (evaluator instanceof LifeCycle) {
93        ((LifeCycle) evaluator).start();
94        addInfo("Starting evaluator named [" + evaluator.getName() + "]");
95      }
96  
97      Object o = ec.peekObject();
98  
99      if (o != evaluator) {
100       addWarn("The object on the top the of the stack is not the evaluator pushed earlier.");
101     } else {
102       ec.popObject();
103 
104       try {
105         Map<String, EventEvaluator> evaluatorMap = (Map<String, EventEvaluator>) context
106             .getObject(CoreConstants.EVALUATOR_MAP);
107         evaluatorMap.put(evaluator.getName(), evaluator);
108       } catch (Exception ex) {
109         addError("Could not set evaluator named [" + evaluator + "].", ex);
110       }
111     }
112   }
113 
114   public void finish(InterpretationContext ec) {
115   }
116 }