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.Property;
27  import org.apache.logging.log4j.core.config.plugins.PluginElement;
28  
29  /**
30   * Enhances a Class by allowing it to contain Filters.
31   */
32  public abstract class AbstractFilterable extends AbstractLifeCycle implements Filterable {
33  
34      /**
35       * Subclasses can extend this abstract Builder.
36       *
37       * @param <B> The type to build.
38       */
39      public abstract static class Builder<B extends Builder<B>> {
40  
41          @PluginElement("Filter")
42          private Filter filter;
43  
44          // We are calling this attribute propertyArray because we use the more generic "properties" in several places
45          // with different types: Array, Map and List.
46          @PluginElement("Properties")
47          private Property[] propertyArray;
48  
49          @SuppressWarnings("unchecked")
50          public B asBuilder() {
51              return (B) this;
52          }
53  
54          public Filter getFilter() {
55              return filter;
56          }
57  
58          public Property[] getPropertyArray() {
59              return propertyArray;
60          }
61  
62          public B setFilter(final Filter filter) {
63              this.filter = filter;
64              return asBuilder();
65          }
66  
67          public B setPropertyArray(final Property[] properties) {
68              this.propertyArray = properties;
69              return asBuilder();
70          }
71  
72          /**
73           * Sets the filter.
74           *
75           * @param filter The filter
76           * @return this
77           * @deprecated Use {@link #setFilter(Filter)}.
78           */
79          @Deprecated
80          public B withFilter(final Filter filter) {
81              return setFilter(filter);
82          }
83  
84      }
85  
86      /**
87       * May be null.
88       */
89      private volatile Filter filter;
90  
91      @PluginElement("Properties")
92      private final Property[] propertyArray;
93  
94      protected AbstractFilterable() {
95          this(null, Property.EMPTY_ARRAY);
96      }
97  
98      protected AbstractFilterable(final Filter filter) {
99          this(filter, Property.EMPTY_ARRAY);
100     }
101 
102     /**
103      * @since 2.11.2
104      */
105     protected AbstractFilterable(final Filter filter, final Property[] propertyArray) {
106         this.filter = filter;
107         this.propertyArray = propertyArray == null ? Property.EMPTY_ARRAY : propertyArray;
108     }
109 
110     /**
111      * Adds a filter.
112      * @param filter The Filter to add.
113      */
114     @Override
115     public synchronized void addFilter(final Filter filter) {
116         if (filter == null) {
117             return;
118         }
119         if (this.filter == null) {
120             this.filter = filter;
121         } else if (this.filter instanceof CompositeFilter) {
122             this.filter = ((CompositeFilter) this.filter).addFilter(filter);
123         } else {
124             final Filter[] filters = new Filter[] {this.filter, filter};
125             this.filter = CompositeFilter.createFilters(filters);
126         }
127     }
128 
129     /**
130      * Returns the Filter.
131      * @return the Filter or null.
132      */
133     @Override
134     public Filter getFilter() {
135         return filter;
136     }
137 
138     /**
139      * Determines if a Filter is present.
140      * @return false if no Filter is present.
141      */
142     @Override
143     public boolean hasFilter() {
144         return filter != null;
145     }
146 
147     /**
148      * Determine if the LogEvent should be processed or ignored.
149      * @param event The LogEvent.
150      * @return true if the LogEvent should be processed.
151      */
152     @Override
153     public boolean isFiltered(final LogEvent event) {
154         return filter != null && filter.filter(event) == Filter.Result.DENY;
155     }
156 
157     /**
158      * Removes a Filter.
159      * @param filter The Filter to remove.
160      */
161     @Override
162     public synchronized void removeFilter(final Filter filter) {
163         if (this.filter == null || filter == null) {
164             return;
165         }
166         if (this.filter == filter || this.filter.equals(filter)) {
167             this.filter = null;
168         } else if (this.filter instanceof CompositeFilter) {
169             CompositeFilter composite = (CompositeFilter) this.filter;
170             composite = composite.removeFilter(filter);
171             if (composite.size() > 1) {
172                 this.filter = composite;
173             } else if (composite.size() == 1) {
174                 final Iterator<Filter> iter = composite.iterator();
175                 this.filter = iter.next();
176             } else {
177                 this.filter = null;
178             }
179         }
180     }
181 
182     /**
183      * Make the Filter available for use.
184      */
185     @Override
186     public void start() {
187         this.setStarting();
188         if (filter != null) {
189             filter.start();
190         }
191         this.setStarted();
192     }
193 
194     /**
195      * Cleanup the Filter.
196      */
197     @Override
198     public boolean stop(final long timeout, final TimeUnit timeUnit) {
199         return stop(timeout, timeUnit, true);
200     }
201 
202     /**
203      * Cleanup the Filter.
204      */
205     protected boolean stop(final long timeout, final TimeUnit timeUnit, final boolean changeLifeCycleState) {
206         if (changeLifeCycleState) {
207             this.setStopping();
208         }
209         boolean stopped = true;
210         if (filter != null) {
211             if (filter instanceof LifeCycle2) {
212                 stopped = ((LifeCycle2) filter).stop(timeout, timeUnit);
213             } else {
214                 filter.stop();
215                 stopped = true;
216             }
217         }
218         if (changeLifeCycleState) {
219             this.setStopped();
220         }
221         return stopped;
222     }
223 
224     public Property[] getPropertyArray() {
225         return propertyArray;
226     }
227 
228 }