1 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 ; 36 import javax.servlet.FilterChain ; 37 import javax.servlet.FilterConfig ; 38 import javax.servlet.ServletException ; 39 import javax.servlet.ServletRequest ; 40 import javax.servlet.ServletResponse ; 41 import javax.servlet.http.HttpServletRequest ; 42 import javax.servlet.http.HttpServletResponse ; 43 import java.io.IOException ; 44 import java.util.logging.Level ; 45 import java.util.logging.Logger ; 46 47 50 public class ProfilerFilter 51 implements Filter 52 { 53 private static final L10N L = new L10N(ProfilerFilter.class); 54 private static final Logger log = Logger.getLogger(ProfilerFilter.class.getName()); 55 56 private final ProfilerManager _profilerManager = ProfilerManager.getLocal(); 58 59 private boolean _isUseQuery = false; 60 61 public ProfilerFilter() 62 { 63 } 64 65 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 filterConfig) 80 throws ServletException 81 { 82 } 83 84 public void doFilter(ServletRequest servletRequest, 85 ServletResponse servletResponse, 86 FilterChain chain) 87 throws ServletException , IOException 88 { 89 HttpServletRequest request = (HttpServletRequest ) servletRequest; 90 HttpServletResponse response = (HttpServletResponse ) servletResponse; 91 92 String uri = request.getRequestURI(); 93 94 String servletPath = (String ) request.getAttribute( 95 "javax.servlet.include.servlet_path"); 96 String pathInfo; 97 String 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 ) request.getAttribute("javax.servlet.include.path_info"); 110 111 if (isUseQuery()) 112 queryString = (String ) request.getAttribute( 113 "javax.servlet.include.query_string"); 114 else 115 queryString = null; 116 } 117 118 String 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 |