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.core.spi;
15
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.concurrent.CopyOnWriteArrayList;
19
20 import ch.qos.logback.core.filter.Filter;
21
22 /**
23 * Implementation of FilterAttachable.
24 *
25 * @author Ceki Gülcü
26 */
27 final public class FilterAttachableImpl<E> implements FilterAttachable<E> {
28
29 CopyOnWriteArrayList<Filter<E>> filterList = new CopyOnWriteArrayList<Filter<E>>();
30
31 /**
32 * Add a filter to end of the filter list.
33 */
34 public void addFilter(Filter<E> newFilter) {
35 filterList.add(newFilter);
36 }
37
38 /**
39 * Clear the filter chain
40 */
41 public void clearAllFilters() {
42 filterList.clear();
43 }
44
45 /**
46 * Loop through the filters in the list. As soon as a filter decides on
47 * ACCEPT or DENY, then that value is returned. If all of the filters return
48 * NEUTRAL, then NEUTRAL is returned.
49 */
50 public FilterReply getFilterChainDecision(E event) {
51 for (Filter<E> f : filterList) {
52 final FilterReply r = f.decide(event);
53 if (r == FilterReply.DENY || r == FilterReply.ACCEPT) {
54 return r;
55 }
56 }
57 return FilterReply.NEUTRAL;
58 }
59
60 public List<Filter<E>> getCopyOfAttachedFiltersList() {
61 return new ArrayList<Filter<E>>(filterList);
62 }
63 }