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  import java.util.concurrent.TimeUnit;
21  
22  import org.apache.logging.log4j.core.AbstractLifeCycle;
23  import org.apache.logging.log4j.core.Filter;
24  import org.apache.logging.log4j.core.LifeCycle2;
25  import org.apache.logging.log4j.core.LogEvent;
26  import org.apache.logging.log4j.core.config.plugins.PluginElement;
27  
28  /**
29   * Enhances a Class by allowing it to contain Filters.
30   */
31  public abstract class AbstractFilterable extends AbstractLifeCycle implements Filterable {
32  
33      /**
34       * Subclasses can extend this abstract Builder.
35       *
36       * @param <B> The type to build.
37       */
38      public abstract static class Builder<B extends Builder<B>> {
39  
40          @PluginElement("Filter")
41          private Filter filter;
42  
43          public Filter getFilter() {
44              return filter;
45          }
46  
47          @SuppressWarnings("unchecked")
48          public B asBuilder() {
49              return (B) this;
50          }
51  
52          public B withFilter(final Filter filter) {
53              this.filter = filter;
54              return asBuilder();
55          }
56  
57      }
58  
59      /**
60       * May be null.
61       */
62      private volatile Filter filter;
63  
64      protected AbstractFilterable(final Filter filter) {
65          this.filter = filter;
66      }
67  
68      protected AbstractFilterable() {
69      }
70  
71      /**
72       * Returns the Filter.
73       * @return the Filter or null.
74       */
75      @Override
76      public Filter getFilter() {
77          return filter;
78      }
79  
80      /**
81       * Adds a filter.
82       * @param filter The Filter to add.
83       */
84      @Override
85      public synchronized void addFilter(final Filter filter) {
86          if (filter == null) {
87              return;
88          }
89          if (this.filter == null) {
90              this.filter = filter;
91          } else if (this.filter instanceof CompositeFilter) {
92              this.filter = ((CompositeFilter) this.filter).addFilter(filter);
93          } else {
94              final Filter[] filters = new Filter[] {this.filter, filter};
95              this.filter = CompositeFilter.createFilters(filters);
96          }
97      }
98  
99      /**
100      * Removes a Filter.
101      * @param filter The Filter to remove.
102      */
103     @Override
104     public synchronized void removeFilter(final Filter filter) {
105         if (this.filter == null || filter == null) {
106             return;
107         }
108         if (this.filter == filter || this.filter.equals(filter)) {
109             this.filter = null;
110         } else if (this.filter instanceof CompositeFilter) {
111             CompositeFilter composite = (CompositeFilter) this.filter;
112             composite = composite.removeFilter(filter);
113             if (composite.size() > 1) {
114                 this.filter = composite;
115             } else if (composite.size() == 1) {
116                 final Iterator<Filter> iter = composite.iterator();
117                 this.filter = iter.next();
118             } else {
119                 this.filter = null;
120             }
121         }
122     }
123 
124     /**
125      * Determines if a Filter is present.
126      * @return false if no Filter is present.
127      */
128     @Override
129     public boolean hasFilter() {
130         return filter != null;
131     }
132 
133     /**
134      * Make the Filter available for use.
135      */
136     @Override
137     public void start() {
138         this.setStarting();
139         if (filter != null) {
140             filter.start();
141         }
142         this.setStarted();
143     }
144 
145     /**
146      * Cleanup the Filter.
147      */
148     @Override
149     public boolean stop(final long timeout, final TimeUnit timeUnit) {
150         return stop(timeout, timeUnit, true);
151     }
152 
153     /**
154      * Cleanup the Filter.
155      */
156     protected boolean stop(final long timeout, final TimeUnit timeUnit, final boolean changeLifeCycleState) {
157         if (changeLifeCycleState) {
158             this.setStopping();
159         }
160         boolean stopped = true;
161         if (filter != null) {
162             if (filter instanceof LifeCycle2) {
163                 stopped = ((LifeCycle2) filter).stop(timeout, timeUnit);
164             } else {
165                 filter.stop();
166                 stopped = true;
167             }
168         }
169         if (changeLifeCycleState) {
170             this.setStopped();
171         }
172         return stopped;
173     }
174 
175     /**
176      * Determine if the LogEvent should be processed or ignored.
177      * @param event The LogEvent.
178      * @return true if the LogEvent should be processed.
179      */
180     @Override
181     public boolean isFiltered(final LogEvent event) {
182         return filter != null && filter.filter(event) == Filter.Result.DENY;
183     }
184 
185 }