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
018package org.apache.log4j.varia;
019
020import org.apache.log4j.spi.Filter;
021import org.apache.log4j.spi.LoggingEvent;
022import org.apache.log4j.helpers.OptionConverter;
023
024/**
025 * This is a very simple filter based on string matching.
026 *
027 * <p>The filter admits two options <b>StringToMatch</b> and
028 * <b>AcceptOnMatch</b>. If there is a match between the value of the
029 * StringToMatch option and the message of the {@link org.apache.log4j.spi.LoggingEvent},
030 * then the {@link #decide(LoggingEvent)} method returns {@link org.apache.log4j.spi.Filter#ACCEPT} if
031 * the <b>AcceptOnMatch</b> option value is true, if it is false then
032 * {@link org.apache.log4j.spi.Filter#DENY} is returned. If there is no match, {@link
033 * org.apache.log4j.spi.Filter#NEUTRAL} is returned.
034 *
035 * @author Ceki G&uuml;lc&uuml;
036 * @since 0.9.0 
037 */
038public class StringMatchFilter extends Filter {
039  
040  /**
041     @deprecated Options are now handled using the JavaBeans paradigm.
042     This constant is not longer needed and will be removed in the
043     <em>near</em> term.
044   */
045  public static final String STRING_TO_MATCH_OPTION = "StringToMatch";
046
047  /**
048     @deprecated Options are now handled using the JavaBeans paradigm.
049     This constant is not longer needed and will be removed in the
050     <em>near</em> term.
051   */
052  public static final String ACCEPT_ON_MATCH_OPTION = "AcceptOnMatch";
053  
054  boolean acceptOnMatch = true;
055  String stringToMatch;
056  
057  /**
058     @deprecated We now use JavaBeans introspection to configure
059     components. Options strings are no longer needed.
060  */
061  public
062  String[] getOptionStrings() {
063    return new String[] {STRING_TO_MATCH_OPTION, ACCEPT_ON_MATCH_OPTION};
064  }
065
066  /**
067     @deprecated Use the setter method for the option directly instead
068     of the generic <code>setOption</code> method. 
069  */
070  public
071  void setOption(String key, String value) { 
072    
073    if(key.equalsIgnoreCase(STRING_TO_MATCH_OPTION)) {
074      stringToMatch = value;
075    } else if (key.equalsIgnoreCase(ACCEPT_ON_MATCH_OPTION)) {
076      acceptOnMatch = OptionConverter.toBoolean(value, acceptOnMatch);
077    }
078  }
079  
080  public
081  void setStringToMatch(String s) {
082    stringToMatch = s;
083  }
084  
085  public
086  String getStringToMatch() {
087    return stringToMatch;
088  }
089  
090  public
091  void setAcceptOnMatch(boolean acceptOnMatch) {
092    this.acceptOnMatch = acceptOnMatch;
093  }
094  
095  public
096  boolean getAcceptOnMatch() {
097    return acceptOnMatch;
098  }
099
100  /**
101     Returns {@link Filter#NEUTRAL} is there is no string match.
102   */
103  public
104  int decide(LoggingEvent event) {
105    String msg = event.getRenderedMessage();
106
107    if(msg == null ||  stringToMatch == null)
108      return Filter.NEUTRAL;
109    
110
111    if( msg.indexOf(stringToMatch) == -1 ) {
112      return Filter.NEUTRAL;
113    } else { // we've got a match
114      if(acceptOnMatch) {
115        return Filter.ACCEPT;
116      } else {
117        return Filter.DENY;
118      }
119    }
120  }
121}