001    /*
002     * Databinder: a simple bridge from Wicket to Hibernate
003     * Copyright (C) 2008  Nathan Hamblen nathan@technically.us
004     *
005     * This library is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU Lesser General Public
007     * License as published by the Free Software Foundation; either
008     * version 2.1 of the License, or (at your option) any later version.
009     * 
010     * This library is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013     * Lesser General Public License for more details.
014     * 
015     * You should have received a copy of the GNU Lesser General Public
016     * License along with this library; if not, write to the Free Software
017     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
018     */
019    package net.databinder;
020    
021    import java.util.regex.Pattern;
022    
023    import org.apache.wicket.Response;
024    import org.apache.wicket.protocol.http.WebApplication;
025    import org.apache.wicket.protocol.http.WebRequest;
026    import org.apache.wicket.protocol.http.WebRequestCycle;
027    import org.slf4j.Logger;
028    import org.slf4j.LoggerFactory;
029    
030    /**
031     * Request cycle that logs runtime exceptions as warnings if their origin matches
032     * a pattern. RequestCycle logs any uncaught exception as an error.
033     * @author Nathan Hamblen
034     */
035    public abstract class ExceptionFilteringRequestCycle extends WebRequestCycle {
036            
037            private static final Logger log = LoggerFactory.getLogger(ExceptionFilteringRequestCycle.class);
038            /** Default pattern is ".*CodingStrategy". */
039            private static Pattern warnOnlySource = Pattern.compile(".*CodingStrategy");
040            
041            public ExceptionFilteringRequestCycle(WebApplication application, WebRequest request, Response response) {
042                    super(application, request, response);
043            }
044            
045            /**
046             * Logs runtime exception as warning if it matches the warnOnlySource pattern.
047             * Passes to super otherwise.
048             */
049            @Override
050            protected void logRuntimeException(RuntimeException e) {
051                    if (warnOnlySource != null &&
052                                    warnOnlySource.matcher(e.getStackTrace()[0].getClassName()).matches())
053                            log.warn(e.getMessage(), e);
054                    else
055                            super.logRuntimeException(e);
056            }
057            
058            /**
059             * Change the warn testing pattern used by all instances of this class. An exception
060             * thrown by a class (found by stack trace) matching this pattern is logged as a
061             * warning. Set this to null to disable filtering and log all exceptions in super.
062             * @param pattern to match against fully qualified class name of exception origin
063             */
064            public static void setWarnOnlySource(Pattern pattern) {
065                    warnOnlySource = pattern;
066            }
067    }