| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hiveutils.web.util; |
| 16 | |
| 17 | import java.io.IOException; |
| 18 | import java.text.MessageFormat; |
| 19 | |
| 20 | import javax.servlet.Filter; |
| 21 | import javax.servlet.FilterChain; |
| 22 | import javax.servlet.FilterConfig; |
| 23 | import javax.servlet.ServletException; |
| 24 | import javax.servlet.ServletRequest; |
| 25 | import javax.servlet.ServletResponse; |
| 26 | import javax.servlet.http.HttpServletRequest; |
| 27 | |
| 28 | import org.apache.commons.logging.Log; |
| 29 | import org.apache.commons.logging.LogFactory; |
| 30 | |
| 31 | /** |
| 32 | * Servlet Filter that logs the time spent by requests. |
| 33 | * |
| 34 | * @author Jean-Francois Poilpret |
| 35 | */ |
| 36 | public class PerformanceMonitorFilter implements Filter |
| 37 | { |
| 38 | private static final Log _logger = LogFactory.getLog(PerformanceMonitorFilter.class); |
| 39 | |
| 40 | public void init(FilterConfig config) throws ServletException |
| 41 | { |
| 42 | // Get logger for performance monitoring |
| 43 | String logger = config.getInitParameter("logger"); |
| 44 | if (logger != null) |
| 45 | { |
| 46 | _monitorLogger = LogFactory.getLog(logger); |
| 47 | } |
| 48 | else |
| 49 | { |
| 50 | _monitorLogger = _logger; |
| 51 | } |
| 52 | |
| 53 | // Get format message for performance monitoring |
| 54 | String format = config.getInitParameter("format"); |
| 55 | if (format == null) |
| 56 | { |
| 57 | format = "Request <{0}> #{2} processed in {1} ms"; |
| 58 | } |
| 59 | _formatter = new MessageFormat(format); |
| 60 | |
| 61 | // Get request time threshold |
| 62 | String threshold = config.getInitParameter("time-threshold"); |
| 63 | if (threshold != null) |
| 64 | { |
| 65 | try |
| 66 | { |
| 67 | _timeThreshold = Integer.parseInt(threshold); |
| 68 | if (_timeThreshold < 0) |
| 69 | { |
| 70 | _timeThreshold = 0; |
| 71 | } |
| 72 | } |
| 73 | catch (NumberFormatException e) |
| 74 | { |
| 75 | _logger.warn("init() time-threshold is incorrect", e); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | public void destroy() |
| 81 | { |
| 82 | } |
| 83 | |
| 84 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) |
| 85 | throws IOException, ServletException |
| 86 | { |
| 87 | long time = System.currentTimeMillis(); |
| 88 | int counter = ++_counter; |
| 89 | try |
| 90 | { |
| 91 | chain.doFilter(request, response); |
| 92 | } |
| 93 | finally |
| 94 | { |
| 95 | time = System.currentTimeMillis() - time; |
| 96 | if (time >= _timeThreshold && _monitorLogger.isDebugEnabled()) |
| 97 | { |
| 98 | String info = ((HttpServletRequest) request).getRequestURL().toString(); |
| 99 | String message = _formatter.format( new Object[] |
| 100 | { |
| 101 | info, |
| 102 | time, |
| 103 | counter |
| 104 | }, |
| 105 | new StringBuffer(), |
| 106 | null).toString(); |
| 107 | _monitorLogger.debug(message); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | protected Log _monitorLogger; |
| 113 | protected MessageFormat _formatter; |
| 114 | protected int _timeThreshold = 0; |
| 115 | protected int _counter = 0; |
| 116 | } |