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.boolex;
15  
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  import org.codehaus.janino.ScriptEvaluator;
20  
21  /**
22   * Abstract class which sets the groundwork for janino based evaluations.
23   * 
24   * @author Ceki Gülcü
25   * 
26   * @param <E> event type
27   */
28  abstract public class JaninoEventEvaluatorBase<E> extends EventEvaluatorBase<E> {
29  
30      static Class<?> EXPRESSION_TYPE = boolean.class;
31      static Class<?>[] THROWN_EXCEPTIONS = new Class[1];
32  
33      static public final int ERROR_THRESHOLD = 4;
34      static {
35          THROWN_EXCEPTIONS[0] = EvaluationException.class;
36      }
37  
38      private String expression;
39  
40      ScriptEvaluator scriptEvaluator;
41      private int errorCount = 0;
42  
43      abstract protected String getDecoratedExpression();
44  
45      abstract protected String[] getParameterNames();
46  
47      abstract protected Class<?>[] getParameterTypes();
48  
49      abstract protected Object[] getParameterValues(E event);
50  
51      protected List<Matcher> matcherList = new ArrayList<Matcher>();
52  
53      @Override
54      public void start() {
55          try {
56              assert context != null;
57              scriptEvaluator = new ScriptEvaluator(getDecoratedExpression(), EXPRESSION_TYPE, getParameterNames(), getParameterTypes(), THROWN_EXCEPTIONS);
58              super.start();
59          } catch (Exception e) {
60              addError("Could not start evaluator with expression [" + expression + "]", e);
61          }
62      }
63  
64      public boolean evaluate(E event) throws EvaluationException {
65          if (!isStarted()) {
66              throw new IllegalStateException("Evaluator [" + name + "] was called in stopped state");
67          }
68          try {
69              Boolean result = (Boolean) scriptEvaluator.evaluate(getParameterValues(event));
70              return result.booleanValue();
71          } catch (Exception ex) {
72              errorCount++;
73              if (errorCount >= ERROR_THRESHOLD) {
74                  stop();
75              }
76              throw new EvaluationException("Evaluator [" + name + "] caused an exception", ex);
77          }
78      }
79  
80      public String getExpression() {
81          return expression;
82      }
83  
84      public void setExpression(String expression) {
85          this.expression = expression;
86      }
87  
88      public void addMatcher(Matcher matcher) {
89          matcherList.add(matcher);
90      }
91  
92      public List<Matcher> getMatcherList() {
93          return matcherList;
94      }
95  }