📄 debugservlet.java
字号:
/* -*- mode:java; indent-tabs-mode:nil; c-basic-offset:2 -*- * $RCSFile$ $Revision: 1.26 $ $Date: 2006/02/01 00:20:31 $ * * Copyright (c) 2003-2004 Autonomy Corp. All Rights Reserved. * * Permission to use, copy, modify, and distribute this file is hereby * granted without fee, provided that the above copyright notice appear * in all copies. */import java.io.*;import java.net.*;import java.util.*;import java.text.*;import javax.servlet.*;import javax.servlet.http.*;import com.ultraseek.xpa.server.*;import org.apache.commons.logging.*;/** * A extension of the standard SearchServlet * that emits debugging information with search results. * <p> * To enable debugging information, have your customized * SearchServlet extend this Servlet instead of SearchServlet. * <br> * Note: You must also change the definition of your customized * SearchRequest so it extends DebugServlet.SearchRequest instead * of SearchServlet.SearchRequest. * * @since XPA2.1.2 * @see SearchServlet * @see MyServlet * @serial exclude */public class DebugServlet extends SearchServlet { static { /** * Hook up the XPA Diagnostic log to the Servlet log function. * Set XPA logging level to "debug". * This must occur before any loggers have been bound. */// String loggerName = SimpleLogServletLogAdaptor.class.getName();// java.lang.System.setProperty("com.jakarta.commons.logging.Log",// loggerName );// java.lang.System.setProperty(loggerName + ".log." // + "com.ultraseek.xpa", "debug");// java.lang.System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");// java.lang.System.setProperty(loggerName + ".showdatetime", "true"); Log diagnostic = LogFactory.getLog("com.ultraseek.xpa"); System.err.println("xpa diagnostic log: " + diagnostic.getClass().getName() ); System.err.println(" Error : " + diagnostic.isErrorEnabled() ); System.err.println(" Warn : " + diagnostic.isWarnEnabled() ); System.err.println(" Info : " + diagnostic.isInfoEnabled() ); System.err.println(" Debug : " + diagnostic.isDebugEnabled() ); System.err.println(" Trace : " + diagnostic.isTraceEnabled() ); } public String getServletInfo() { return DebugServlet.class.getName() + " based on\n" + super.getServletInfo(); } { // Don't let client browser cache get in the way of debugging CLIENT_CACHE_INTERVAL = 0; // Flush each search result as it is prepared FLUSH_RESULTS_INTERVAL = 1; } /** * Number of Servlet log entries to display in debug output. */ private final int LOG_CACHE_SIZE = 10; private List logCache = new ArrayList(LOG_CACHE_SIZE); /** * Saves a copy of recent log messages for display with * debugging output. */ public void log(String msg, Throwable e) { if (e!=null) super.log(msg,e); else super.log(msg); StringBuffer sb = new StringBuffer(256); SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss.SSS"); // e.g. 08:49:37.123 sb.append( format.format( new Date() )); sb.append( " " ); sb.append( msg ); if (e !=null) { sb.append( " " ); sb.append( "" + e ); } if (logCache.size() >= LOG_CACHE_SIZE) logCache.remove(0); logCache.add( sb.toString() ); } public void log(String msg) { log(msg,null); } public void doLog(String msg) { log(msg,null); } protected void showDebugLogCache(PrintWriter out) { out.print("<p><a name=\"showDebugLogCache\">"); out.print( "<b>Recent log entries</b>" ); out.println("</a>"); out.println( "<table border=1 width=\"100%\">" ); Iterator it = logCache.iterator(); while (it.hasNext()) { String entry = (String) it.next(); out.print( "<tr><td>" ); printHTMLEncoded(out,entry); out.println( "</td></tr>" ); }; out.println( "</table>" ); } // Server can be changed with the &server=URL parameter URL ultraseekURL = null; protected URL getUltraseekURL() throws ServletException { if (ultraseekURL != null) return ultraseekURL; return super.getUltraseekURL(); } /** * Re-initialize the Ultraseek server used for administration * options. */ protected void initUltraseek() throws IOException, ServletException { try { super.initUltraseek(); } catch (IOException e) { // When debugging, always use the new server, // even if it is unreachable. ultraseek = null; try { super.initUltraseek(); } catch (IOException e2) { throw e2; } } } class SearchRequest extends SearchServlet.SearchRequest { public SearchRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException { super(req,resp); }; /** * The time the servlet started its response to this request. */ long request_start_time; protected void handle() throws IOException, ServletException { try { log( this.getClass().getName() + " handling request " + req.getQueryString() ); super.handle(); } catch (Exception e) { out.println( "handle() caught exception: " + e ); out.println( "<pre>" ); e.printStackTrace(out); out.println( "</pre>" ); showDebugLogCache(out); out.flush(); } } protected void showQueryFormHiddenVariables() throws IOException { super.showQueryFormHiddenVariables(); // Preserve request debug setting if ("YES".equals(getParameterString("debug"))) out.println("<input type=hidden name=debug value=\"YES\">"); } /** * Display debugging information specific to the Servlet being debugged. * @see #showDebugInformation */ protected void showDebugServletSpecific() { } /** * Emit debugging information about the Servlet and the Servlet container. */ protected void showDebugInformation() { long elapsed = System.currentTimeMillis() - request_start_time; if (!(debug || "YES".equals(getParameterString("debug")))) return; DEBUG(out,"showDebugInformation"); try { out.flush(); out.print("<div class=DebugServlet>"); out.print("<a NAME=\"showDebugInformation\"></a>"); out.print("<hr>"); out.println( "<b>Debugging output</b>" ); showDebugSearchOnServerLink(); out.println(" Time to prepare HTTP response: " + elapsed + " ms."); showDebugTableOfContents(); showDebugServletSpecific(); showDebugLogCache(out); out.println( "<p><pre>" + getServletInfo() + "</pre>" ); showDebugFindSAXParser(); showDebugSearchStatus(); showDebugHTTPSession(); showDebugHTTPRequest(); showDebugHTTPRequestHeaders(); showDebugHTTPResponse(); showDebugServletContainer(); showDebugServletConfig(); showDebugServletContext(); showDebugJRESettings(); out.print("</div>"); // class=DebugServlet } catch (Exception exc) { log( "Problem emitting debug information", exc ); out.println( "<p>Problem emitting debug information: " + exc ); } DEBUG(out,"/showDebugInformation"); } /** * Emit an index to all the debugging information */ protected void showDebugTableOfContents() { out.print("<blockquote><table border=1 width=\"80%\">"); out.print("<tr><td>"); out.println("<a href=\"#showDebugHTTPSession\">HttpSession</a><br>"); out.println("<a href=\"#showDebugHTTPResponse\">HttpServletResponse</a>"); out.print("</td><td>"); out.println("<a href=\"#showDebugHTTPRequest\">HttpServletRequest</a><br>"); out.println("<a href=\"#showDebugHTTPRequestHeaders\">HTTP Request Headers</a>"); out.print("</td><td>"); out.println("<a href=\"#showDebugServletConfig\">ServletConfig</a><br>"); out.println("<a href=\"#showDebugServletContext\">ServletContext</a>"); out.print("</td><td>"); out.println("<a href=\"#showDebugServletContainer\">Servlet Container</a><br>"); out.println("<a href=\"#showDebugLogCache\">Servlet.log</a><br>"); out.print("</td><td>"); out.println("<a href=\"#showDebugJRESettings\">JRE Properties</a>"); out.print("</td></tr></table></blockquote>"); } /** * Emit an HTML table-row of information, assumed * to be variableName and variableValue. * <p> * The variable value may have spaces inserted * to prevent the debug table from growing too * wide. * @param c1 variable name * @param c2 variable value */ protected void showDebugRow(String c1, String c2) { out.print( "<tr><td width=\"15%\">" ); out.print( HTMLEncoded(c1) ); out.print( "</td><td width=\"85%\">" ); out.print( "<code>" ); if (c2==null) out.print("<null>"); else if (c2.equals("")) { out.print("\"\""); } else { final int width = 60; final int rows = 5; boolean useTextarea = c2.length() >= rows*width; if (useTextarea) out.print("<textarea style=\"{font-family: monospace; width: 100%}\"" + "cols="+ width + " rows=" + rows + " readonly>"); while (!c2.equals("")) { int next_space = c2.indexOf(' '); if (next_space == -1) next_space = c2.length()-1; if (next_space > width) { int break_point = -1; break_point = c2.lastIndexOf(',',width); break_point = Math.max(break_point, c2.lastIndexOf(';',width)); break_point = Math.max(break_point, c2.lastIndexOf(')',width)); break_point = Math.max(break_point, c2.lastIndexOf('.',width)); break_point = Math.max(break_point, c2.lastIndexOf('(',width)); if (break_point==-1) break_point = width; out.print( HTMLEncoded(c2.substring(0,break_point+1)) ); out.print( " " ); c2 = c2.substring(break_point+1); } else { out.print( HTMLEncoded(c2.substring(0,next_space+1)) ); c2 = c2.substring(next_space+1); } }; if (useTextarea) out.print("</textarea>"); } out.print("</code>");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -