1 /**
2 * Logback: the reliable, generic, fast and flexible logging framework.
3 * Copyright (C) 1999-2009, 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.filter;
15
16 import ch.qos.logback.core.boolex.EvaluationException;
17 import ch.qos.logback.core.boolex.EventEvaluator;
18 import ch.qos.logback.core.spi.FilterReply;
19
20 /**
21 * The value of the {@link #onMatch} and {@link #onMismatch} attributes is set
22 * to {@link Filter.NEUTRAL}, so that a badly configured evaluator filter does
23 * not disturb the functioning of the filter chain.
24 *
25 * <p>It is expected that one of the two attributes will have its value changed
26 * to {@link Filter.ACCEPT} or {@link Filter.DENY}. That way, it is possible to
27 * decide if a given result must be returned after the evaluation either failed
28 * or succeeded.
29 *
30 *
31 * <p> For more information about filters, please refer to the online manual at
32 * http://logback.qos.ch/manual/filters.html
33 *
34 * @author Ceki Gülcü
35 * @author Sébastien Pennec
36 */
37 public class EvaluatorFilter<E> extends AbstractMatcherFilter<E> {
38
39 EventEvaluator<E> evaluator;
40
41 @Override
42 public void start() {
43 if (evaluator != null) {
44 super.start();
45 } else {
46 addError("No evaluator set for filter " + this.getName());
47 }
48 }
49
50 public EventEvaluator<E> getEvaluator() {
51 return evaluator;
52 }
53
54 public void setEvaluator(EventEvaluator<E> evaluator) {
55 this.evaluator = evaluator;
56 }
57
58 public FilterReply decide(E event) {
59 // let us not throw an exception
60 // see also bug #17.
61 if (!isStarted() || !evaluator.isStarted()) {
62 return FilterReply.NEUTRAL;
63 }
64 try {
65 if (evaluator.evaluate(event)) {
66 return onMatch;
67 } else {
68 return onMismatch;
69 }
70 } catch (EvaluationException e) {
71 addError("Evaluator " + evaluator.getName() + " threw an exception", e);
72 return FilterReply.NEUTRAL;
73 }
74 }
75
76 }