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(final Filter filter) { 033 this.filter = filter; 034 } 035 036 protected AbstractFilterable() { 037 } 038 039 /** 040 * Returns the Filter. 041 * @return the Filter. 042 */ 043 @Override 044 public Filter getFilter() { 045 return filter; 046 } 047 048 /** 049 * Add a filter. 050 * @param filter The Filter to add. 051 */ 052 @Override 053 public synchronized void addFilter(final Filter filter) { 054 if (this.filter == null) { 055 this.filter = filter; 056 } else if (filter instanceof CompositeFilter) { 057 this.filter = ((CompositeFilter) this.filter).addFilter(filter); 058 } else { 059 final Filter[] filters = new Filter[] {this.filter, filter}; 060 this.filter = CompositeFilter.createFilters(filters); 061 } 062 } 063 064 /** 065 * Remove a Filter. 066 * @param filter The Filter to remove. 067 */ 068 @Override 069 public synchronized void removeFilter(final Filter filter) { 070 if (this.filter == filter) { 071 this.filter = null; 072 } else if (filter instanceof CompositeFilter) { 073 CompositeFilter composite = (CompositeFilter) filter; 074 composite = composite.removeFilter(filter); 075 if (composite.size() > 1) { 076 this.filter = composite; 077 } else if (composite.size() == 1) { 078 final Iterator<Filter> iter = composite.iterator(); 079 this.filter = iter.next(); 080 } else { 081 this.filter = null; 082 } 083 } 084 } 085 086 /** 087 * Determines if a Filter is present. 088 * @return false if no Filter is present. 089 */ 090 @Override 091 public boolean hasFilter() { 092 return filter != null; 093 } 094 095 /** 096 * Make the Filter available for use. 097 */ 098 public void startFilter() { 099 if (filter != null && filter instanceof LifeCycle) { 100 ((LifeCycle) filter).start(); 101 } 102 } 103 104 /** 105 * Cleanup the Filter. 106 */ 107 public void stopFilter() { 108 if (filter != null && filter instanceof LifeCycle) { 109 ((LifeCycle) filter).stop(); 110 } 111 } 112 113 /** 114 * Determine if the LogEvent should be processed or ignored. 115 * @param event The LogEvent. 116 * @return true if the LogEvent should be processed. 117 */ 118 @Override 119 public boolean isFiltered(final LogEvent event) { 120 return filter != null && filter.filter(event) == Filter.Result.DENY; 121 } 122 123 }