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(final Filter filter) {
33          this.filter = filter;
34      }
35  
36      protected AbstractFilterable() {
37      }
38  
39      /**
40       * Returns the Filter.
41       * @return the Filter.
42       */
43      @Override
44      public Filter getFilter() {
45          return filter;
46      }
47  
48      /**
49       * Add a filter.
50       * @param filter The Filter to add.
51       */
52      @Override
53      public synchronized void addFilter(final Filter filter) {
54          if (this.filter == null) {
55              this.filter = filter;
56          } else if (filter instanceof CompositeFilter) {
57              this.filter = ((CompositeFilter) this.filter).addFilter(filter);
58          } else {
59              final Filter[] filters = new Filter[] {this.filter, filter};
60              this.filter = CompositeFilter.createFilters(filters);
61          }
62      }
63  
64      /**
65       * Remove a Filter.
66       * @param filter The Filter to remove.
67       */
68      @Override
69      public synchronized void removeFilter(final Filter filter) {
70          if (this.filter == filter) {
71              this.filter = null;
72          } else if (filter instanceof CompositeFilter) {
73              CompositeFilter composite = (CompositeFilter) filter;
74              composite = composite.removeFilter(filter);
75              if (composite.size() > 1) {
76                  this.filter = composite;
77              } else if (composite.size() == 1) {
78                  final Iterator<Filter> iter = composite.iterator();
79                  this.filter = iter.next();
80              } else {
81                  this.filter = null;
82              }
83          }
84      }
85  
86      /**
87       * Determines if a Filter is present.
88       * @return false if no Filter is present.
89       */
90      @Override
91      public boolean hasFilter() {
92          return filter != null;
93      }
94  
95      /**
96       * Make the Filter available for use.
97       */
98      public void startFilter() {
99         if (filter != null && filter instanceof LifeCycle) {
100            ((LifeCycle) filter).start();
101        }
102     }
103 
104     /**
105      * Cleanup the Filter.
106      */
107     public void stopFilter() {
108        if (filter != null && filter instanceof LifeCycle) {
109            ((LifeCycle) filter).stop();
110        }
111     }
112 
113     /**
114      * Determine if the LogEvent should be processed or ignored.
115      * @param event The LogEvent.
116      * @return true if the LogEvent should be processed.
117      */
118     @Override
119     public boolean isFiltered(final LogEvent event) {
120         return filter != null && filter.filter(event) == Filter.Result.DENY;
121     }
122 
123 }