EMMA Coverage Report (generated Tue Feb 12 22:23:49 ICT 2008)
[all classes][net.sourceforge.hiveutils.web.util]

COVERAGE SUMMARY FOR SOURCE FILE [PerformanceMonitorFilter.java]

nameclass, %method, %block, %line, %
PerformanceMonitorFilter.java0%   (0/1)0%   (0/5)0%   (0/180)0%   (0/34)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class PerformanceMonitorFilter0%   (0/1)0%   (0/5)0%   (0/180)0%   (0/34)
<static initializer> 0%   (0/1)0%   (0/4)0%   (0/1)
PerformanceMonitorFilter (): void 0%   (0/1)0%   (0/9)0%   (0/3)
destroy (): void 0%   (0/1)0%   (0/1)0%   (0/1)
doFilter (ServletRequest, ServletResponse, FilterChain): void 0%   (0/1)0%   (0/115)0%   (0/11)
init (FilterConfig): void 0%   (0/1)0%   (0/51)0%   (0/18)

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 
15package net.sourceforge.hiveutils.web.util;
16 
17import java.io.IOException;
18import java.text.MessageFormat;
19 
20import javax.servlet.Filter;
21import javax.servlet.FilterChain;
22import javax.servlet.FilterConfig;
23import javax.servlet.ServletException;
24import javax.servlet.ServletRequest;
25import javax.servlet.ServletResponse;
26import javax.servlet.http.HttpServletRequest;
27 
28import org.apache.commons.logging.Log;
29import org.apache.commons.logging.LogFactory;
30 
31/**
32 * Servlet Filter that logs the time spent by requests.
33 *
34 * @author Jean-Francois Poilpret
35 */
36public 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}

[all classes][net.sourceforge.hiveutils.web.util]
EMMA 2.0.5312 (C) Vladimir Roubtsov