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 java.util.Iterator;
20  
21  import org.apache.logging.log4j.core.AbstractLifeCycle;
22  import org.apache.logging.log4j.core.Filter;
23  import org.apache.logging.log4j.core.LogEvent;
24  
25  /**
26   * Enhances a Class by allowing it to contain Filters.
27   */
28  public abstract class AbstractFilterable extends AbstractLifeCycle implements Filterable {
29  
30      /**
31       * May be null.
32       */
33      private volatile Filter filter;
34  
35      protected AbstractFilterable(final Filter filter) {
36          this.filter = filter;
37      }
38  
39      protected AbstractFilterable() {
40      }
41  
42      /**
43       * Returns the Filter.
44       * @return the Filter or null.
45       */
46      @Override
47      public Filter getFilter() {
48          return filter;
49      }
50  
51      /**
52       * Adds a filter.
53       * @param filter The Filter to add.
54       */
55      @Override
56      public synchronized void addFilter(final Filter filter) {
57          if (filter == null) {
58              return;
59          }
60          if (this.filter == null) {
61              this.filter = filter;
62          } else if (this.filter instanceof CompositeFilter) {
63              this.filter = ((CompositeFilter) this.filter).addFilter(filter);
64          } else {
65              final Filter[] filters = new Filter[] {this.filter, filter};
66              this.filter = CompositeFilter.createFilters(filters);
67          }
68      }
69  
70      /**
71       * Removes a Filter.
72       * @param filter The Filter to remove.
73       */
74      @Override
75      public synchronized void removeFilter(final Filter filter) {
76          if (this.filter == null || filter == null) {
77              return;
78          }
79          if (this.filter == filter || this.filter.equals(filter)) {
80              this.filter = null;
81          } else if (this.filter instanceof CompositeFilter) {
82              CompositeFilter composite = (CompositeFilter) this.filter;
83              composite = composite.removeFilter(filter);
84              if (composite.size() > 1) {
85                  this.filter = composite;
86              } else if (composite.size() == 1) {
87                  final Iterator<Filter> iter = composite.iterator();
88                  this.filter = iter.next();
89              } else {
90                  this.filter = null;
91              }
92          }
93      }
94  
95      /**
96       * Determines if a Filter is present.
97       * @return false if no Filter is present.
98       */
99      @Override
100     public boolean hasFilter() {
101         return filter != null;
102     }
103 
104     /**
105      * Make the Filter available for use.
106      */
107     @Override
108     public void start() {
109         this.setStarting();
110         if (filter != null) {
111             filter.start();
112         }
113         this.setStarted();
114     }
115 
116     /**
117      * Cleanup the Filter.
118      */
119     @Override
120     public void stop() {
121         this.setStopping();
122         if (filter != null) {
123             filter.stop();
124         }
125         this.setStopped();
126     }
127 
128     /**
129      * Determine if the LogEvent should be processed or ignored.
130      * @param event The LogEvent.
131      * @return true if the LogEvent should be processed.
132      */
133     @Override
134     public boolean isFiltered(final LogEvent event) {
135         return filter != null && filter.filter(event) == Filter.Result.DENY;
136     }
137 
138 }