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<>();
52  
53      @Override
54      public void start() {
55          try {
56              assert context != null;
57              scriptEvaluator = new ScriptEvaluator(getDecoratedExpression(), EXPRESSION_TYPE, getParameterNames(),
58                      getParameterTypes(), THROWN_EXCEPTIONS);
59              super.start();
60          } catch (Exception e) {
61              addError("Could not start evaluator with expression [" + expression + "]", e);
62          }
63      }
64  
65      public boolean evaluate(E event) throws EvaluationException {
66          if (!isStarted()) {
67              throw new IllegalStateException("Evaluator [" + name + "] was called in stopped state");
68          }
69          try {
70              Boolean result = (Boolean) scriptEvaluator.evaluate(getParameterValues(event));
71              return result;
72          } catch (Exception ex) {
73              errorCount++;
74              if (errorCount >= ERROR_THRESHOLD) {
75                  stop();
76              }
77              throw new EvaluationException("Evaluator [" + name + "] caused an exception", ex);
78          }
79      }
80  
81      public String getExpression() {
82          return expression;
83      }
84  
85      public void setExpression(String expression) {
86          this.expression = expression;
87      }
88  
89      public void addMatcher(Matcher matcher) {
90          matcherList.add(matcher);
91      }
92  
93      public List<Matcher> getMatcherList() {
94          return matcherList;
95      }
96  }