001package ch.qos.logback.access.common.boolex;
002
003import ch.qos.logback.access.common.spi.IAccessEvent;
004import ch.qos.logback.core.boolex.EvaluationException;
005import ch.qos.logback.core.boolex.EventEvaluatorBase;
006
007/**
008 * A very simple {@link ch.qos.logback.core.boolex.EventEvaluator EventEvaluator}
009 * which checks whether the status of {@link IAccessEvent access event} is equal
010 * to a status code given as a parameter.
011 *
012 * @since 2.0.6
013 */
014public class StatusCodeEventEvaluator extends EventEvaluatorBase<IAccessEvent> {
015
016    final static int UNSET = -1;
017    int statusCode = UNSET;
018
019    @Override
020    public void start() {
021        if(statusCode == UNSET) {
022            addWarn("No status code set");
023            return;
024        }
025        super.start();
026    }
027    @Override
028    public boolean evaluate(IAccessEvent iAccessEvent) throws NullPointerException, EvaluationException {
029        if (!isStarted()) {
030            throw new IllegalStateException("Evaluator [" + this + "] was called in stopped state");
031        }
032        boolean result = (iAccessEvent.getStatusCode() == statusCode);
033        return result;
034    }
035
036    public int getStatusCode() {
037        return statusCode;
038    }
039
040    public void setStatusCode(int statusCode) {
041        this.statusCode = statusCode;
042    }
043}