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