<%@ page language="java" import="java.io.*, java.sql.*, java.math.*, java.util.*,java.beans.*,java.lang.reflect.*"%><% String beans = request.getParameter("beans"); %> Servlet Debugger - <%= beans == null ? "" : beans %> Servlet.Debugger 2.2 <% // Do not edit this piece of the code. You should put your code only at the end /** * Class for debugging jsps. * You can print the values of publicly accessible properties of a bean. * Usage: Suppose you have a session bean called foo and bar. You want to * display their properties in the middle of testing your code. * Start the corresponding form. Open another browser with the following * url * http:///debug/debug.jsp?beans= * * Here I am assuming that you put this debug.jsp file under the directory * debug in your jsptop (in case of tomcat, debug is a servlet context). * And is a list of beans you want to check * the values. Example, * http://localhost/debug/debug.jsp?beans=abc,i_know_you_don't_exist * * Any bean you want to introspect, you can just put it in the session with a * particular id (if you already don't have it in the session) and call this * debug page by passing that id as value to the beans parameter. * * @author Siva K Dirisala (siva.dirisala at gmail dot com) * @version 2.2 * @date 05/02/2000 * @license Free to use and distribute as long as this comments section is * retained and use at your own risk. * * Change : Added navigating through objects. So, you can do something like * beans=abc.fielda.fieldb * Change : 10/03/2000 * True navigation experience through the objects. If an object has * members which are themselves objects, they are displayed as links. * Clicking the link would take the user to the corresponding object * information. * Also, added support for array * Change : 10/04/2000 * webserver was configured wrongly and it took some time to realize * where the java files were pulled from. So, added the version * functionality that would atleast give a clue to the version of the * java class * Change : 05/08/2004 * Displays the list of all the session beans * Change : 05/04/2005 * Added support for Map. The keys of the Map are listed as links. * Added breadcrumbs for easy navigation. * Fixed errors with accessing arrays. */ final HashMap listInterfaces = new HashMap(); class ArrayInterfaceClass { public ArrayInterfaceClass(String className,String sizePropertyName,String getItemName) { try { clas = Class.forName(className); BeanInfo info = Introspector.getBeanInfo(clas); PropertyDescriptor[] pd = info.getPropertyDescriptors(); for(int i=0;i" + error + ""); } catch(IOException ex) { } } public Object getBean(String beanName,Object scope,String scopeName) throws Exception { Object object = null; if(beanName.indexOf(".") > 0) { String cscope = beanName.substring(0,beanName.indexOf(".")); object = getBean(cscope,scope,scopeName); scopeName = scopeName.length() == 0 ? scopeName : scopeName + "."; if(object != null) { object = getBean(beanName.substring(beanName.indexOf(".")+1),object,scopeName); } else { printError("Bean " + scopeName + cscope + " is null!"); } } else if(scope != null) { object = getValue(scope,beanName); } else { object = session.getValue(beanName); } return object; } public Object getValue(Object bean,String property) throws Exception { Object value = null; if(bean == null) return value; if(bean instanceof Map) { return ((Map)bean).get(property); } try { BeanInfo info = Introspector.getBeanInfo (bean.getClass ()); if(info == null) { printError("
Bean info is null!"); return null; } PropertyDescriptor[] pd = info.getPropertyDescriptors (); if(pd == null) { printError("
Property Descriptors is null!"); return null; } boolean isArray = false; int index = 0; if(property.indexOf('[') > 0) { if(property.indexOf(']') == property.length()-1) { isArray = true; try { index = Integer.parseInt(property.substring(property.indexOf('[')+1, property.indexOf(']'))); property = property.substring(0,property.indexOf('[')); } catch(Exception ex) { throw new Exception("Wrong index format for array!"); } } else throw new Exception("Wrong format for the bean!"); } for (int i = 0; i < pd.length; i++) { if(property.equals(pd[i].getName())) { Method method = pd[i].getReadMethod(); if(method == null) continue; value = method.invoke(bean, new Object[] { }); if(isArray) { if(value instanceof Vector) { Vector vec = (Vector)value; if(index >= 0 && index < vec.size()) return vec.elementAt(index); else throw new Exception("Index out of bound for vector of size " + vec.size()); } else if(value instanceof Object[]) { Object [] vec = (Object[])value; if(index >= 0 && index < vec.length) return vec[index]; else throw new Exception("Index out of bound for array of size " + vec.length); } else { ArrayInterfaceClass clas = tempclass.getArrayInterfaceClass(value); if(clas == null) throw new Exception("Index can be used only for arrays and collections and classes with array interface metadata"); Object lengthValue = clas.getSize.invoke(value, new Object[] { }); if(lengthValue instanceof Integer) { int length = ((Integer)lengthValue).intValue(); if(index >= 0 && index < length) { Object args[] = new Object[1]; args[0] = new Integer(index); return clas.getItem.invoke(value,args); } else throw new Exception("Index out of bound for vector of size " + length); } } } break; } } } catch(IntrospectionException ex) { } catch(InvocationTargetException ex) { } catch(IllegalAccessException ex) { } return value; } public void printBreadCrumbs(String beanName) throws Exception { out.print("

/ » "); String beans[] = beanName.split("[.]"); for(int i=0;i" + beans[i] + " » "); } out.println(beans[beans.length-1]); } public void printProperties(String beanName) throws Exception { Object bean = getBean(beanName,null,""); printProperties(bean,beanName); } public void printArray(Object array[],String beanName) throws IOException { out.println("

    "); for(int i=0;i" + array[i]); else out.println("
  1. " + array[i] + ""); out.println("
"); } public void printAbstractList(AbstractList alist,String beanName) throws IOException { out.println("
    "); for(int i=0;i" + value); else out.println("
  1. " + value + ""); } out.println("
"); } public void printProperties(Object bean,String beanName) { String propertyName = null; try { if(bean == null) { printError("
Bean " + beanName + " doesn't exist in the session or it is null!"); return; } BeanInfo info = Introspector.getBeanInfo (bean.getClass ()); if(info == null) { printError("
Bean info is null!"); return; } PropertyDescriptor[] pd = info.getPropertyDescriptors (); if(pd == null) { printError("
Property Descriptors is null!"); return; } out.println("

"); try { out.println(""); } catch(NoSuchFieldException ex) { // out.println(""); } for (int i = 0; i < pd.length; i++) { try { propertyName = pd[i].getName(); Method method = pd[i].getReadMethod(); if(method == null) continue; if(method.getReturnType().isArray()) { out.println(""); } else { Object value = method.invoke(bean, new Object[] { }); if(isPrimitive(value)) out.println(""); else out.println(""); } } catch(InvocationTargetException ex) { printError("InvocationTargetException for " + propertyName + " " + ex.getMessage()); } catch(IllegalAccessException ex) { printError("IllegalAccessException for " + propertyName + " " + ex.getMessage()); } catch(Exception ex) { } } if(bean instanceof AbstractList) printAbstractList((AbstractList)bean,beanName); else if(bean instanceof Object[]) printArray((Object[])bean,beanName); else if(bean instanceof Map) { Map map = (Map)bean; out.println("{"); for(Iterator iter=map.keySet().iterator();iter.hasNext();) { Object key = iter.next(); if(key instanceof String) { out.println("" + key + " "); } } out.println("}"); } else { ArrayInterfaceClass clas = tempclass.getArrayInterfaceClass(bean); if(clas != null) { Object lengthValue = clas.getSize.invoke(bean, new Object[] { }); if(lengthValue instanceof Integer) { int length = ((Integer)lengthValue).intValue(); Object args[] = new Object[1]; out.println("
    "); for(int i=0;i" + item); else out.println("
  1. " + item + ""); out.println("
"); } } } } } catch(IOException ex) { } catch(IntrospectionException ex) { printError("Introspection Exception"); } catch(InvocationTargetException ex) { printError("InvocationTargetException for " + propertyName + " " + ex.getMessage()); } catch(IllegalAccessException ex) { printError("IllegalAccessException for " + propertyName + " " + ex.getMessage()); } finally { try { out.println("
RCS_ID" + bean.getClass().getField("RCS_ID")+"
RCS_ID doesn't exist for this bean
" + pd[i].getName() + ""); Object value[] = (Object [])method.invoke(bean, new Object[] { }); if(value != null) printArray(value,beanName+"."+pd[i].getName()); else out.println(value); out.println("
" + pd[i].getName() + "" + value + "
" + pd[i].getName() + "" + value + "
"); } catch(IOException ex) { }; } } public boolean isPrimitive(Object value) { return (value == null || value instanceof String || value instanceof Integer || value instanceof Boolean || value instanceof Double || value instanceof Short || value instanceof Long || value instanceof Float); } public void printVersion(String className) { try { out.println("Version of " + className + " = " + Class.forName(className).getField("RCS_ID").get(null)); } catch(Exception ex) { try { out.println(ex.getClass().getName() + " " + ex.getMessage()); } catch(IOException e) { } } } } Debugger debugger = new Debugger(session,request.getRequestURI(),out); if(request.getParameter("beans") != null) { beans = beans.trim(); Vector beanNames = new Vector(); while(beans != null) { if(beans.indexOf(",") > 0) { beanNames.addElement(beans.substring(0,beans.indexOf(","))); beans = beans.substring(beans.indexOf(",")+1); } else { beanNames.addElement(beans); beans = null; } } Enumeration elms = beanNames.elements(); while(elms.hasMoreElements()) { String elm = (String)elms.nextElement(); try { debugger.printBreadCrumbs(elm); %>
<% debugger.printProperties(elm); %>
<% } catch(Exception er) { out.println("

"+er+":"+er.getMessage()+"

"); er.printStackTrace(); } } } if(request.getParameter("version") != null) { debugger.printVersion(request.getParameter("version")); } %>

Session beans
<% Enumeration attrs = session.getAttributeNames(); boolean hasAttrs = false; while(attrs.hasMoreElements()) { String attr = (String)attrs.nextElement(); hasAttrs = true; %> <%= attr %>
<% } if(!hasAttrs) { %> There are no session beans <% } %>
<% // Add your debugging code here. %> <% if(request.getParameter("beans") == null &&request.getParameter("version") == null) { %>
Help: various parameters can be passed to the url
beans=<list of beans that can be accessed from the session vars>
E.g. debug.jsp?beans=abc.fielda.fieldb
if the object is an array or a vector, possible to access ith element
E.g. debug.jsp?beans=xyz.abc.def[1]
if the object is a Map, possible to access keyed value if the key is a string
E.g. debug.jsp?beans=tablemap.USERS
version=<fully qualified path name> to get the RCS_ID
E.g. debug.jsp?version=com.abcxyz.util.xml.XS <% } %>

© 2007 Dirisala.Net