View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.filter;
18  
19  import org.apache.logging.log4j.core.Filter;
20  import org.apache.logging.log4j.core.LifeCycle;
21  import org.apache.logging.log4j.core.LogEvent;
22  
23  import java.util.Iterator;
24  
25  /**
26   * Enhances a Class by allowing it to contain Filters.
27   */
28  public abstract class AbstractFilterable implements Filterable {
29  
30      private volatile Filter filter;
31  
32      protected AbstractFilterable(Filter filter) {
33          this.filter = filter;
34      }
35  
36      protected AbstractFilterable() {
37      }
38  
39      /**
40       * Returns the Filter.
41       * @return the Filter.
42       */
43      public Filter getFilter() {
44          return filter;
45      }
46  
47      /**
48       * Add a filter.
49       * @param filter The Filter to add.
50       */
51      public synchronized void addFilter(Filter filter) {
52          if (this.filter == null) {
53              this.filter = filter;
54          } else if (filter instanceof CompositeFilter) {
55              this.filter = ((CompositeFilter) this.filter).addFilter(filter);
56          } else {
57              Filter[] filters = new Filter[] {this.filter, filter};
58              this.filter = CompositeFilter.createFilters(filters);
59          }
60      }
61  
62      /**
63       * Remove a Filter.
64       * @param filter The Filter to remove.
65       */
66      public synchronized void removeFilter(Filter filter) {
67          if (this.filter == filter) {
68              this.filter = null;
69          } else if (filter instanceof CompositeFilter) {
70              CompositeFilter composite = (CompositeFilter) filter;
71              composite = composite.removeFilter(filter);
72              if (composite.size() > 1) {
73                  this.filter = composite;
74              } else if (composite.size() == 1) {
75                  Iterator<Filter> iter = composite.iterator();
76                  this.filter = iter.next();
77              } else {
78                  this.filter = null;
79              }
80          }
81      }
82  
83      /**
84       * Determines if a Filter is present.
85       * @return false if no Filter is present.
86       */
87      public boolean hasFilter() {
88          return filter != null;
89      }
90  
91      /**
92       * Make the Filter available for use.
93       */
94      public void startFilter() {
95         if (filter != null && filter instanceof LifeCycle) {
96             ((LifeCycle) filter).start();
97         }
98      }
99  
100     /**
101      * Cleanup the Filter.
102      */
103     public void stopFilter() {
104        if (filter != null && filter instanceof LifeCycle) {
105            ((LifeCycle) filter).stop();
106        }
107     }
108 
109     /**
110      * Determine if the LogEvent should be processed or ignored.
111      * @param event The LogEvent.
112      * @return true if the LogEvent should be processed.
113      */
114     public boolean isFiltered(LogEvent event) {
115         return filter != null && filter.filter(event) == Filter.Result.DENY;
116     }
117 
118 }