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