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.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<IAccessEvent>, 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      @Override
36      public void start() {
37          if (URLList.size() == 0) {
38              addWarn("No URL was given to URLEvaluator");
39          } else {
40              started = true;
41          }
42      }
43  
44      @Override
45      public boolean evaluate(IAccessEvent event) throws NullPointerException, EvaluationException {
46          String url = event.getRequestURL();
47          for (String expected : URLList) {
48              if (url.contains(expected)) {
49                  return true;
50              }
51          }
52          return false;
53      }
54  
55      @Override
56      public String getName() {
57          return name;
58      }
59  
60      @Override
61      public void setName(String name) {
62          this.name = name;
63      }
64  
65      @Override
66      public boolean isStarted() {
67          return started;
68      }
69  
70      @Override
71      public void stop() {
72          started = false;
73      }
74  }