View Javadoc

1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 2000-2008, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.core.spi;
11  
12  import java.util.ArrayList;
13  import java.util.List;
14  import java.util.concurrent.CopyOnWriteArrayList;
15  
16  import ch.qos.logback.core.filter.Filter;
17  
18  /**
19   * Implementation of FilterAttachable.
20   * 
21   * @author Ceki Gülcü
22   */
23  final public class FilterAttachableImpl<E> implements FilterAttachable<E> {
24  
25    CopyOnWriteArrayList<Filter<E>> filterList = new CopyOnWriteArrayList<Filter<E>>();
26  
27    /**
28     * Add a filter to end of the filter list.
29     */
30    public void addFilter(Filter<E> newFilter) {
31      filterList.add(newFilter);
32    }
33  
34    /**
35     * Get first filter in the chain.
36     */
37    public Filter<E> getFirstFilter() {
38      if (filterList.size() > 0) {
39        return filterList.get(0);
40      } else {
41        return null;
42      }
43    }
44  
45    /**
46     * Clear the filter chain
47     */
48    public void clearAllFilters() {
49      filterList.clear();
50    }
51  
52    /**
53     * Loop through the filters in the list. As soon as a filter decides on
54     * ACCEPT or DENY, then that value is returned. If all of the filters return
55     * NEUTRAL, then NEUTRAL is returned.
56     */
57    public FilterReply getFilterChainDecision(E event) {
58      for (Filter<E> f : filterList) {
59        final FilterReply r = f.decide(event);
60        if (r == FilterReply.DENY || r == FilterReply.ACCEPT) {
61          return r;
62        }
63      }
64      return FilterReply.NEUTRAL;
65    }
66  
67    public List<Filter<E>> getCopyOfAttachedFiltersList() {
68      return new ArrayList<Filter<E>>(filterList);
69    }
70  }