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 (this.filter == null) {
58              this.filter = filter;
59          } else if (filter instanceof CompositeFilter) {
60              this.filter = ((CompositeFilter) this.filter).addFilter(filter);
61          } else {
62              final Filter[] filters = new Filter[] {this.filter, filter};
63              this.filter = CompositeFilter.createFilters(filters);
64          }
65      }
66  
67      /**
68       * Removes a Filter.
69       * @param filter The Filter to remove.
70       */
71      @Override
72      public synchronized void removeFilter(final Filter filter) {
73          if (this.filter == filter) {
74              this.filter = null;
75          } else if (filter instanceof CompositeFilter) {
76              CompositeFilter composite = (CompositeFilter) filter;
77              composite = composite.removeFilter(filter);
78              if (composite.size() > 1) {
79                  this.filter = composite;
80              } else if (composite.size() == 1) {
81                  final Iterator<Filter> iter = composite.iterator();
82                  this.filter = iter.next();
83              } else {
84                  this.filter = null;
85              }
86          }
87      }
88  
89      /**
90       * Determines if a Filter is present.
91       * @return false if no Filter is present.
92       */
93      @Override
94      public boolean hasFilter() {
95          return filter != null;
96      }
97  
98      /**
99       * Make the Filter available for use.
100      */
101     @Override
102     public void start() {
103         this.setStarting();
104         if (filter != null) {
105             filter.start();
106         }
107         this.setStarted();
108     }
109 
110     /**
111      * Cleanup the Filter.
112      */
113     @Override
114     public void stop() {
115         this.setStopping();
116        if (filter != null) {
117            filter.stop();
118        }
119        this.setStopped();
120     }
121 
122     /**
123      * Determine if the LogEvent should be processed or ignored.
124      * @param event The LogEvent.
125      * @return true if the LogEvent should be processed.
126      */
127     @Override
128     public boolean isFiltered(final LogEvent event) {
129         return filter != null && filter.filter(event) == Filter.Result.DENY;
130     }
131 
132 }