001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.core.filter; 018 019import java.util.Iterator; 020 021import org.apache.logging.log4j.core.AbstractLifeCycle; 022import org.apache.logging.log4j.core.Filter; 023import org.apache.logging.log4j.core.LogEvent; 024 025/** 026 * Enhances a Class by allowing it to contain Filters. 027 */ 028public abstract class AbstractFilterable extends AbstractLifeCycle implements Filterable { 029 030 /** 031 * May be null. 032 */ 033 private volatile Filter filter; 034 035 protected AbstractFilterable(final Filter filter) { 036 this.filter = filter; 037 } 038 039 protected AbstractFilterable() { 040 } 041 042 /** 043 * Returns the Filter. 044 * @return the Filter or null. 045 */ 046 @Override 047 public Filter getFilter() { 048 return filter; 049 } 050 051 /** 052 * Adds a filter. 053 * @param filter The Filter to add. 054 */ 055 @Override 056 public synchronized void addFilter(final Filter filter) { 057 if (filter == null) { 058 return; 059 } 060 if (this.filter == null) { 061 this.filter = filter; 062 } else if (this.filter instanceof CompositeFilter) { 063 this.filter = ((CompositeFilter) this.filter).addFilter(filter); 064 } else { 065 final Filter[] filters = new Filter[] {this.filter, filter}; 066 this.filter = CompositeFilter.createFilters(filters); 067 } 068 } 069 070 /** 071 * Removes a Filter. 072 * @param filter The Filter to remove. 073 */ 074 @Override 075 public synchronized void removeFilter(final Filter filter) { 076 if (this.filter == null || filter == null) { 077 return; 078 } 079 if (this.filter == filter || this.filter.equals(filter)) { 080 this.filter = null; 081 } else if (this.filter instanceof CompositeFilter) { 082 CompositeFilter composite = (CompositeFilter) this.filter; 083 composite = composite.removeFilter(filter); 084 if (composite.size() > 1) { 085 this.filter = composite; 086 } else if (composite.size() == 1) { 087 final Iterator<Filter> iter = composite.iterator(); 088 this.filter = iter.next(); 089 } else { 090 this.filter = null; 091 } 092 } 093 } 094 095 /** 096 * Determines if a Filter is present. 097 * @return false if no Filter is present. 098 */ 099 @Override 100 public boolean hasFilter() { 101 return filter != null; 102 } 103 104 /** 105 * Make the Filter available for use. 106 */ 107 @Override 108 public void start() { 109 this.setStarting(); 110 if (filter != null) { 111 filter.start(); 112 } 113 this.setStarted(); 114 } 115 116 /** 117 * Cleanup the Filter. 118 */ 119 @Override 120 public void stop() { 121 this.setStopping(); 122 if (filter != null) { 123 filter.stop(); 124 } 125 this.setStopped(); 126 } 127 128 /** 129 * Determine if the LogEvent should be processed or ignored. 130 * @param event The LogEvent. 131 * @return true if the LogEvent should be processed. 132 */ 133 @Override 134 public boolean isFiltered(final LogEvent event) { 135 return filter != null && filter.filter(event) == Filter.Result.DENY; 136 } 137 138}