1 package dinamica; 2 3 import java.io.*; 4 import javax.servlet.*; 5 import javax.servlet.http.*; 6 7 23 24 public class PerformanceFilter implements Filter 25 { 26 27 private FilterConfig _config = null; 29 30 private int _limit = 0; 32 33 36 public void init(FilterConfig config) throws ServletException 37 { 38 39 _config = config; 40 41 String l = _config.getInitParameter("limit"); 43 if (l!=null) 44 _limit = Integer.parseInt(l); 45 46 } 47 48 51 public void destroy() 52 { 53 _config = null; 54 } 55 56 59 public void doFilter ( 60 ServletRequest request, 61 ServletResponse response, 62 FilterChain next 63 ) 64 65 throws IOException, ServletException 66 67 { 68 69 long t1 = 0; 70 long t2 = 0; 71 72 t1 = System.currentTimeMillis(); 73 74 HttpServletRequest req = (HttpServletRequest) request; 75 76 try 78 { 79 80 t1 = System.currentTimeMillis(); 81 next.doFilter(request, response); 82 t2 = System.currentTimeMillis(); 83 long tt = t2-t1; 84 85 if (tt > _limit) 87 { 88 saveLog(req, tt); 89 } 90 91 } 92 93 catch (Throwable e) 94 { 95 throw new ServletException(e); 96 } 97 98 } 99 100 106 void saveLog(HttpServletRequest req, long t) throws Throwable 107 { 108 String d = StringUtil.formatDate(new java.util.Date (), "yyyy-MM-dd HH:mm:ss"); 109 String uri = req.getRequestURI(); 110 String tt = String.valueOf(t); 111 String ip = req.getRemoteAddr(); 112 String method = req.getMethod(); 113 114 String msg = d + "\t" + uri + "\t" + method + "\t" + ip + "\t" + tt; 115 116 String file = _config.getInitParameter("path"); 117 118 StringUtil.saveMessage(file, msg); 119 } 120 121 } | Popular Tags |