View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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.net;
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.boolex.EvaluationException;
21  import ch.qos.logback.core.boolex.EventEvaluator;
22  import ch.qos.logback.core.spi.ContextAwareBase;
23  import ch.qos.logback.core.spi.LifeCycle;
24  
25  public class URLEvaluator extends ContextAwareBase implements EventEvaluator, LifeCycle {
26  
27    boolean started;
28    String name;
29    private List<String> URLList = new ArrayList<String>();
30  
31    public void addURL(String url) {
32      URLList.add(url);
33    }
34  
35    public void start() {
36      if (URLList.size() == 0) {
37        addWarn("No URL was given to URLEvaluator");
38      } else {
39        started = true;
40      }
41    }
42      
43    public boolean evaluate(Object eventObject) throws NullPointerException, EvaluationException {
44      IAccessEvent event = (IAccessEvent)eventObject;
45      String url = event.getRequestURL();
46      for(String expected:URLList) {
47        if (url.contains(expected)) {
48          return true;
49        }
50      }
51      return false;
52    }
53  
54    public String getName() {
55      return name;
56    }
57  
58    public void setName(String name) {
59      this.name = name;
60    }
61  
62    public boolean isStarted() {
63      return started;
64    }
65  
66    public void stop() {
67      started = false;
68    }
69  }