KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > tools > profiler > ProfilerFilter


1 /*
2  * Copyright (c) 1998-2005 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Sam
27  */

28
29
30 package com.caucho.tools.profiler;
31
32 import com.caucho.util.CharBuffer;
33 import com.caucho.util.L10N;
34
35 import javax.servlet.Filter JavaDoc;
36 import javax.servlet.FilterChain JavaDoc;
37 import javax.servlet.FilterConfig JavaDoc;
38 import javax.servlet.ServletException JavaDoc;
39 import javax.servlet.ServletRequest JavaDoc;
40 import javax.servlet.ServletResponse JavaDoc;
41 import javax.servlet.http.HttpServletRequest JavaDoc;
42 import javax.servlet.http.HttpServletResponse JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.util.logging.Level JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47 /**
48  * Creates a {@link ProfilerPoint} for each invocation of {@link #doFilter}.
49  */

50 public class ProfilerFilter
51   implements Filter JavaDoc
52 {
53   private static final L10N L = new L10N(ProfilerFilter.class);
54   private static final Logger JavaDoc log = Logger.getLogger(ProfilerFilter.class.getName());
55
56   // can do this because an instance of Filter is created for each environment
57
private final ProfilerManager _profilerManager = ProfilerManager.getLocal();
58
59   private boolean _isUseQuery = false;
60
61   public ProfilerFilter()
62   {
63   }
64
65   /**
66    * If true, use the query portion of the url to distinguish requests, default
67    * is false.
68    */

69   public void setUseQuery(boolean useQuery)
70   {
71     _isUseQuery = useQuery;
72   }
73
74   public boolean isUseQuery()
75   {
76     return _isUseQuery;
77   }
78
79   public void init(FilterConfig JavaDoc filterConfig)
80     throws ServletException JavaDoc
81   {
82   }
83
84   public void doFilter(ServletRequest JavaDoc servletRequest,
85                        ServletResponse JavaDoc servletResponse,
86                        FilterChain JavaDoc chain)
87     throws ServletException JavaDoc, IOException JavaDoc
88   {
89     HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) servletRequest;
90     HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) servletResponse;
91
92     String JavaDoc uri = request.getRequestURI();
93
94     String JavaDoc servletPath = (String JavaDoc) request.getAttribute(
95       "javax.servlet.include.servlet_path");
96     String JavaDoc pathInfo;
97     String JavaDoc queryString;
98
99     if (servletPath == null) {
100       servletPath = request.getServletPath();
101       pathInfo = request.getPathInfo();
102
103       if (isUseQuery())
104         queryString = request.getQueryString();
105       else
106         queryString = null;
107     }
108     else {
109       pathInfo = (String JavaDoc) request.getAttribute("javax.servlet.include.path_info");
110
111       if (isUseQuery())
112         queryString = (String JavaDoc) request.getAttribute(
113           "javax.servlet.include.query_string");
114       else
115         queryString = null;
116     }
117
118     String JavaDoc name;
119
120     if (pathInfo == null && queryString == null)
121       name = servletPath;
122     else {
123       CharBuffer nameBuilder = new CharBuffer();
124
125       nameBuilder.append(servletPath);
126
127       if (pathInfo != null)
128         nameBuilder.append(pathInfo);
129
130       if (queryString != null) {
131         nameBuilder.append('?');
132         nameBuilder.append(queryString);
133       }
134
135       name = nameBuilder.toString();
136     }
137
138     ProfilerPoint profilerPoint = _profilerManager.getProfilerPoint(name);
139
140     if (log.isLoggable(Level.FINEST))
141       log.finest(profilerPoint.toString());
142
143     Profiler profiler = profilerPoint.start();
144
145     try {
146       chain.doFilter(request, response);
147     }
148     finally {
149       profiler.finish();
150     }
151   }
152
153   public void destroy()
154   {
155   }
156
157 }
158
Popular Tags