FileDocCategorySizeDatePackage
DebugTag.javaAPI DocExample5899Tue Feb 28 11:34:06 GMT 2006com.ora.jsp.tags

DebugTag

public class DebugTag extends TagSupport
This class is a custom action that can be used to extract debug information from a JSP PageContext. The debug info is sent to the browser, System.out, and the application log file, depending on the value of the "debug" request parameter sent with the request for the JSP page: "resp", "stdout" and "log". The values can be combined to get the information directed to multiple targets.
author
Hans Bergsten, Gefion software
version
1.0

Fields Summary
private static final String
LINE_FEED
private static final String
RESP_TYPE
private static final String
STDOUT_TYPE
private static final String
LOG_TYPE
private static final String
BEAN_ATTR
private String
type
Constructors Summary
Methods Summary
public intdoEndTag()
Writes the requested type of debug info to the sources specified by the "debug" request parameter: "resp" for response, "stdout" for System.out, and "log" for the application log file.

	// Get or create the DebugBean
	DebugBean bean = (DebugBean) pageContext.getAttribute(BEAN_ATTR);
	if (bean == null) {
	    bean = new DebugBean();
	    bean.setPageContext(pageContext);
	    pageContext.setAttribute(BEAN_ATTR, bean);
	}

	List debugTypes = getDebugType();
        if (debugTypes != null) {
	    Map info = getInfo(type, bean);
            log(type, info, debugTypes);
            if (debugTypes.contains(RESP_TYPE)) {
		try {
		    pageContext.getOut().write(toHTMLTable(type, info));
		}
		catch (IOException e) {} // Ignore
            }
        }
        return EVAL_PAGE;
    
private java.util.ListgetDebugType()
Returns a List with all debug types based on the debug request parameter.

	String debug = pageContext.getRequest().getParameter("debug");
	if (debug == null) {
	    return null;
	}
	List debugTypes = new ArrayList();
	if (debug.indexOf(RESP_TYPE) != -1) {
	    debugTypes.add(RESP_TYPE);
	}
	if (debug.indexOf(STDOUT_TYPE) != -1) {
	    debugTypes.add(STDOUT_TYPE);
	}
	if (debug.indexOf(LOG_TYPE) != -1) {
	    debugTypes.add(LOG_TYPE);
	}
	return debugTypes;
    
private java.util.MapgetInfo(java.lang.String type, com.ora.jsp.beans.debug.DebugBean bean)
Returns the debug info corresponding to the type.

	Map info = null;
	if ("requestInfo".equals(type)) {
	    info = bean.getRequestInfo();
	}
	else if ("headers".equals(type)) {
	    info = bean.getHeaders();
	}
	else if ("cookies".equals(type)) {
	    info = bean.getCookies();
	}
	else if ("params".equals(type)) {
	    info = bean.getParams();
	}
	else if ("pageScope".equals(type)) {
	    info = bean.getPageScope();
	}
	else if ("requestScope".equals(type)) {
	    info = bean.getRequestScope();
	}
	else if ("sessionScope".equals(type)) {
	    info = bean.getSessionScope();
	}
	else if ("applicationScope".equals(type)) {
	    info = bean.getApplicationScope();
	}
	else {
	    throw new JspTagException("ora:debug action: Invalid type: " +
		type);
	}
	return info;
    
private voidlog(java.lang.String propName, java.lang.String msg, java.util.List debugTypes)
Writes the specified property value to the System.out or the log file, depending on the requested debug type.

        HttpServletRequest request = 
            (HttpServletRequest) pageContext.getRequest();
        msg = "[DebugBean] " + request.getRequestURI() + " : " + 
            propName + " : " + msg;
        if (debugTypes.contains(STDOUT_TYPE)) {
            System.out.println(msg);
        }
        if (debugTypes.contains(LOG_TYPE)) {
            pageContext.getServletContext().log(msg);
        }
    
private voidlog(java.lang.String propName, java.util.Map values, java.util.List debugTypes)

        log(propName, toTabbedTable(values), debugTypes);
    
public voidsetType(java.lang.String type)
Set the type property. One of "requestInfo", "headers", "cookies", "params", "pageScope", "requestScope", "sessionScope" or "applicationScope".


                        
        
	this.type = type;
    
private java.lang.StringtoHTMLTable(java.lang.String propName, java.util.Map values)
Returns an HTML table with all the values of the specified property.

        StringBuffer tableSB = new StringBuffer("<table border=\"1\">");
        tableSB.append("<caption align=\"top\"><b>").
            append(propName).
            append("</b></caption>");
        Iterator keys = values.keySet().iterator();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            tableSB.append("<tr><td>").
                append(key).
                append("</td><td>").
                append(values.get(key)).
                append("</td></tr>");
        }
        tableSB.append("</table>");
        return tableSB.toString();
    
private java.lang.StringtoTabbedTable(java.util.Map values)
Returns an simple ASCII table with all the values of the specified property, used for log output.

        StringBuffer tableSB = new StringBuffer();
        Iterator keys = values.keySet().iterator();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            tableSB.append(LINE_FEED).
                append(key).append("\t\t").
                append(values.get(key));
        }
        return tableSB.toString();