KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > logging > appender > RegexEventEvaluator


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.logging.appender;
23
24 import java.util.HashMap JavaDoc;
25 import java.util.regex.Matcher JavaDoc;
26 import java.util.regex.Pattern JavaDoc;
27
28 import org.apache.log4j.spi.LoggingEvent;
29 import org.apache.log4j.spi.TriggeringEventEvaluator;
30
31 /** An implementation of the log4j TriggeringEventEvaluator that matches the
32  * LoggingEvent message against the MDB{RegexEventEvaluator} regular
33  * expression.
34  * @author Scott.Stark@jboss.org
35  * @version $Revision: 1958 $
36  */

37 public class RegexEventEvaluator implements TriggeringEventEvaluator
38 {
39    /** A cache HashMap<String, RE> of previously compiled REs */
40    private HashMap JavaDoc regexMap = new HashMap JavaDoc();
41
42    /** Lookup the current MDC 'RegexEventEvaluator' to determine the regular
43     * expression context that should be applied to determine if the logging
44     * event should be considered a triggering event. If there is no value
45     * for the 'RegexEventEvaluator' key then no comparision is made.
46     *
47     * @param event the logging event to check
48     * @return true if MDC{RegexEventEvaluator} is a regex expression that
49     * matches the event.getRenderedMessage(), false otherwise.
50     */

51    public boolean isTriggeringEvent(LoggingEvent event)
52    {
53       String JavaDoc regex = (String JavaDoc) event.getMDC("RegexEventEvaluator");
54       boolean isTriggeringEvent = false;
55       if( regex != null )
56       {
57          // Look for a cached regex pattern
58
Pattern JavaDoc re = (Pattern JavaDoc) regexMap.get(regex);
59          if( re == null )
60          {
61             re = Pattern.compile(regex);
62             regexMap.put(regex, re);
63          }
64
65          if( re != null )
66          {
67             String JavaDoc msg = event.getRenderedMessage();
68             if( msg != null )
69             {
70                Matcher JavaDoc m = re.matcher(msg);
71                isTriggeringEvent = m.matches();
72             }
73          }
74       }
75       return isTriggeringEvent;
76    }
77 }
78
79
Popular Tags