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