KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > varia > StringMatchFilter


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.log4j.varia;
18
19 import org.apache.log4j.spi.Filter;
20 import org.apache.log4j.spi.LoggingEvent;
21 import org.apache.log4j.helpers.OptionConverter;
22
23 /**
24    This is a very simple filter based on string matching.
25    
26
27    <p>The filter admits two options <b>StringToMatch</b> and
28    <b>AcceptOnMatch</b>. If there is a match between the value of the
29    StringToMatch option and the message of the {@link LoggingEvent},
30    then the {@link #decide} method returns {@link Filter#ACCEPT} if
31    the <b>AcceptOnMatch</b> option value is true, if it is false then
32    {@link Filter#DENY} is returned. If there is no match, {@link
33    Filter#NEUTRAL} is returned.
34
35    <p>See configuration files <a
36    href="../xml/doc-files/test6.xml">test6.xml</a>, <a
37    href="../xml/doc-files/test7.xml">test7.xml</a>, <a
38    href="../xml/doc-files/test8.xml">test8.xml</a>, <a
39    href="../xml/doc-files/test9.xml">test9.xml</a>, and <a
40    href="../xml/doc-files/test10.xml">test10.xml</a> for examples of
41    seeting up a <code>StringMatchFilter</code>.
42
43
44    @author Ceki G&uuml;lc&uuml;
45
46    @since 0.9.0 */

47 public class StringMatchFilter extends Filter {
48   
49   /**
50      @deprecated Options are now handled using the JavaBeans paradigm.
51      This constant is not longer needed and will be removed in the
52      <em>near</em> term.
53    */

54   public static final String JavaDoc STRING_TO_MATCH_OPTION = "StringToMatch";
55
56   /**
57      @deprecated Options are now handled using the JavaBeans paradigm.
58      This constant is not longer needed and will be removed in the
59      <em>near</em> term.
60    */

61   public static final String JavaDoc ACCEPT_ON_MATCH_OPTION = "AcceptOnMatch";
62   
63   boolean acceptOnMatch = true;
64   String JavaDoc stringToMatch;
65   
66   /**
67      @deprecated We now use JavaBeans introspection to configure
68      components. Options strings are no longer needed.
69   */

70   public
71   String JavaDoc[] getOptionStrings() {
72     return new String JavaDoc[] {STRING_TO_MATCH_OPTION, ACCEPT_ON_MATCH_OPTION};
73   }
74
75   /**
76      @deprecated Use the setter method for the option directly instead
77      of the generic <code>setOption</code> method.
78   */

79   public
80   void setOption(String JavaDoc key, String JavaDoc value) {
81     
82     if(key.equalsIgnoreCase(STRING_TO_MATCH_OPTION)) {
83       stringToMatch = value;
84     } else if (key.equalsIgnoreCase(ACCEPT_ON_MATCH_OPTION)) {
85       acceptOnMatch = OptionConverter.toBoolean(value, acceptOnMatch);
86     }
87   }
88   
89   public
90   void setStringToMatch(String JavaDoc s) {
91     stringToMatch = s;
92   }
93   
94   public
95   String JavaDoc getStringToMatch() {
96     return stringToMatch;
97   }
98   
99   public
100   void setAcceptOnMatch(boolean acceptOnMatch) {
101     this.acceptOnMatch = acceptOnMatch;
102   }
103   
104   public
105   boolean getAcceptOnMatch() {
106     return acceptOnMatch;
107   }
108
109   /**
110      Returns {@link Filter#NEUTRAL} is there is no string match.
111    */

112   public
113   int decide(LoggingEvent event) {
114     String JavaDoc msg = event.getRenderedMessage();
115
116     if(msg == null || stringToMatch == null)
117       return Filter.NEUTRAL;
118     
119
120     if( msg.indexOf(stringToMatch) == -1 ) {
121       return Filter.NEUTRAL;
122     } else { // we've got a match
123
if(acceptOnMatch) {
124     return Filter.ACCEPT;
125       } else {
126     return Filter.DENY;
127       }
128     }
129   }
130 }
131
Popular Tags