View Javadoc

1   /*
2    * $Id$
3    *
4    * Copyright 2006 Commsen International
5    * 
6    * Licensed under the Common Public License, Version 1.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * 
10   *      http://www.opensource.org/licenses/cpl1.0.txt
11   * 
12   * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS 
14   * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
15   *
16   */
17  package com.commsen.stopwatch.web.filter;
18  
19  import java.io.IOException;
20  
21  import javax.servlet.Filter;
22  import javax.servlet.FilterChain;
23  import javax.servlet.FilterConfig;
24  import javax.servlet.ServletException;
25  import javax.servlet.ServletRequest;
26  import javax.servlet.ServletResponse;
27  import javax.servlet.http.HttpServletRequest;
28  
29  import com.commsen.stopwatch.Stopwatch;
30  
31  /***
32   * TODO Dokumentacja 
33   *
34   * @author Milen Dyankov
35   *
36   */
37  public class RequestTimingFilter implements Filter {
38  
39  	private static final String PARAM_SKIP_ON_ERROR = "skipOnError";
40  	
41  	String group = null;
42  	boolean skipOnError = true;
43  	
44  	
45  	/***
46  	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
47  	 */
48  	public void init(FilterConfig config) throws ServletException {
49  		group = config.getServletContext().getServletContextName();
50  		String skipOnErrorParam = config.getInitParameter(PARAM_SKIP_ON_ERROR);
51  		if (skipOnErrorParam != null && skipOnErrorParam.trim().length() > 0) 
52  		skipOnError = Boolean.valueOf(skipOnErrorParam).booleanValue();
53  	}
54  
55  	
56  	/***
57  	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
58  	 */
59  	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
60  		String label = null;
61  		if (request instanceof HttpServletRequest) {
62  			HttpServletRequest servletRequest = (HttpServletRequest)request;
63  			label = servletRequest.getRequestURI();
64  		}
65  		
66  		
67  		long swId = Stopwatch.start(group, label);
68  		try {
69  			chain.doFilter(request, response);
70  		} catch (RuntimeException e) {
71  			if (skipOnError) {
72  				Stopwatch.skip(swId);
73  			}
74  			throw e;
75  		} finally {
76  			Stopwatch.stop(swId);
77  		}
78  	}
79  
80  	
81  	/***
82  	 * @see javax.servlet.Filter#destroy()
83  	 */
84  	public void destroy() {
85  		// TODO Auto-generated method stub
86  
87  	}
88  
89  }