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 */ 017 package org.apache.logging.log4j.core.filter; 018 019 import org.apache.logging.log4j.core.Filter; 020 import org.apache.logging.log4j.core.LifeCycle; 021 import org.apache.logging.log4j.core.LogEvent; 022 023 import java.util.Iterator; 024 025 /** 026 * Enhances a Class by allowing it to contain Filters. 027 */ 028 public abstract class AbstractFilterable implements Filterable { 029 030 private volatile Filter filter; 031 032 protected AbstractFilterable(Filter filter) { 033 this.filter = filter; 034 } 035 036 protected AbstractFilterable() { 037 } 038 039 /** 040 * Returns the Filter. 041 * @return the Filter. 042 */ 043 public Filter getFilter() { 044 return filter; 045 } 046 047 /** 048 * Add a filter. 049 * @param filter The Filter to add. 050 */ 051 public synchronized void addFilter(Filter filter) { 052 if (this.filter == null) { 053 this.filter = filter; 054 } else if (filter instanceof CompositeFilter) { 055 this.filter = ((CompositeFilter) this.filter).addFilter(filter); 056 } else { 057 Filter[] filters = new Filter[] {this.filter, filter}; 058 this.filter = CompositeFilter.createFilters(filters); 059 } 060 } 061 062 /** 063 * Remove a Filter. 064 * @param filter The Filter to remove. 065 */ 066 public synchronized void removeFilter(Filter filter) { 067 if (this.filter == filter) { 068 this.filter = null; 069 } else if (filter instanceof CompositeFilter) { 070 CompositeFilter composite = (CompositeFilter) filter; 071 composite = composite.removeFilter(filter); 072 if (composite.size() > 1) { 073 this.filter = composite; 074 } else if (composite.size() == 1) { 075 Iterator<Filter> iter = composite.iterator(); 076 this.filter = iter.next(); 077 } else { 078 this.filter = null; 079 } 080 } 081 } 082 083 /** 084 * Determines if a Filter is present. 085 * @return false if no Filter is present. 086 */ 087 public boolean hasFilter() { 088 return filter != null; 089 } 090 091 /** 092 * Make the Filter available for use. 093 */ 094 public void startFilter() { 095 if (filter != null && filter instanceof LifeCycle) { 096 ((LifeCycle) filter).start(); 097 } 098 } 099 100 /** 101 * Cleanup the Filter. 102 */ 103 public void stopFilter() { 104 if (filter != null && filter instanceof LifeCycle) { 105 ((LifeCycle) filter).stop(); 106 } 107 } 108 109 /** 110 * Determine if the LogEvent should be processed or ignored. 111 * @param event The LogEvent. 112 * @return true if the LogEvent should be processed. 113 */ 114 public boolean isFiltered(LogEvent event) { 115 return filter != null && filter.filter(event) == Filter.Result.DENY; 116 } 117 118 }