001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.access.net;
015
016import java.util.ArrayList;
017import java.util.List;
018
019import ch.qos.logback.access.spi.IAccessEvent;
020import ch.qos.logback.core.boolex.EvaluationException;
021import ch.qos.logback.core.boolex.EventEvaluator;
022import ch.qos.logback.core.spi.ContextAwareBase;
023import ch.qos.logback.core.spi.LifeCycle;
024
025public class URLEvaluator extends ContextAwareBase implements EventEvaluator<IAccessEvent>, LifeCycle {
026
027    boolean started;
028    String name;
029    private List<String> URLList = new ArrayList<String>();
030
031    public void addURL(String url) {
032        URLList.add(url);
033    }
034
035    @Override
036    public void start() {
037        if (URLList.size() == 0) {
038            addWarn("No URL was given to URLEvaluator");
039        } else {
040            started = true;
041        }
042    }
043
044    @Override
045    public boolean evaluate(IAccessEvent event) throws NullPointerException, EvaluationException {
046        String url = event.getRequestURL();
047        for (String expected : URLList) {
048            if (url.contains(expected)) {
049                return true;
050            }
051        }
052        return false;
053    }
054
055    @Override
056    public String getName() {
057        return name;
058    }
059
060    @Override
061    public void setName(String name) {
062        this.name = name;
063    }
064
065    @Override
066    public boolean isStarted() {
067        return started;
068    }
069
070    @Override
071    public void stop() {
072        started = false;
073    }
074}