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.access.boolex;
15  
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  import ch.qos.logback.access.spi.IAccessEvent;
20  import ch.qos.logback.core.CoreConstants;
21  import ch.qos.logback.core.boolex.JaninoEventEvaluatorBase;
22  import ch.qos.logback.core.boolex.Matcher;
23  
24  public class JaninoEventEvaluator extends JaninoEventEvaluatorBase<IAccessEvent> {
25  
26      public final static List<String> DEFAULT_PARAM_NAME_LIST = new ArrayList<String>();
27      public final static List<Class> DEFAULT_PARAM_TYPE_LIST = new ArrayList<Class>();
28  
29      static {
30          DEFAULT_PARAM_NAME_LIST.add("event");
31          DEFAULT_PARAM_TYPE_LIST.add(IAccessEvent.class);
32      }
33  
34      @Override
35      protected String getDecoratedExpression() {
36          String expression = getExpression();
37          if (!expression.contains("return")) {
38              expression = "return " + expression + ";";
39              addInfo("Adding [return] prefix and a semicolon suffix. Expression becomes [" + expression + "]");
40              addInfo("See also " + CoreConstants.CODES_URL + "#block");
41          }
42          return expression;
43      }
44  
45      @Override
46      protected String[] getParameterNames() {
47          List<String> fullNameList = new ArrayList<String>();
48          fullNameList.addAll(DEFAULT_PARAM_NAME_LIST);
49  
50          for (int i = 0; i < matcherList.size(); i++) {
51              Matcher m = (Matcher) matcherList.get(i);
52              fullNameList.add(m.getName());
53          }
54  
55          return (String[]) fullNameList.toArray(CoreConstants.EMPTY_STRING_ARRAY);
56      }
57  
58      @Override
59      protected Class[] getParameterTypes() {
60          List<Class> fullTypeList = new ArrayList<Class>();
61          fullTypeList.addAll(DEFAULT_PARAM_TYPE_LIST);
62          for (int i = 0; i < matcherList.size(); i++) {
63              fullTypeList.add(Matcher.class);
64          }
65          return (Class[]) fullTypeList.toArray(CoreConstants.EMPTY_CLASS_ARRAY);
66      }
67  
68      @Override
69      protected Object[] getParameterValues(IAccessEvent accessEvent) {
70          final int matcherListSize = matcherList.size();
71  
72          int i = 0;
73          Object[] values = new Object[DEFAULT_PARAM_NAME_LIST.size() + matcherListSize];
74  
75          values[i++] = accessEvent;
76  
77          for (int j = 0; j < matcherListSize; j++) {
78              values[i++] = matcherList.get(j);
79          }
80  
81          return values;
82      }
83  
84  }