1 45 package org.openejb.webadmin.clienttools; 46 47 import java.io.IOException ; 48 import java.io.PrintWriter ; 49 import java.lang.reflect.Method ; 50 import java.lang.reflect.Modifier ; 51 52 import org.openejb.webadmin.HttpRequest; 53 import org.openejb.webadmin.HttpResponse; 54 import org.openejb.webadmin.WebAdminBean; 55 56 59 public class ViewClassBean extends WebAdminBean implements Constants { 60 61 boolean hasMethods; 62 63 public void preProcess(HttpRequest request, HttpResponse response) 64 throws IOException { 65 } 66 67 public void postProcess(HttpRequest request, HttpResponse response) 68 throws IOException { 69 } 70 71 public void writeHtmlTitle(PrintWriter out) throws IOException { 72 out.write("Client Tools -- JNDI Viewer"); 73 } 74 75 public void writePageTitle(PrintWriter out) throws IOException { 76 out.write("JNDI Viewer"); 77 } 78 79 public void writeBody(PrintWriter out) throws IOException { 80 try { 81 String className = request.getQueryParameter("class"); 82 if (className == null) { 83 out.print("<b>Enter a class name to browse:</b>"); 84 out.print( 85 "<FORM NAME='view' METHOD='GET' ACTION='"+VIEW_CLASS+"'>"); 86 out.print( 87 "<INPUT type='text' NAME='class' size='40' VALUE=''>"); 88 out.print("<INPUT type='SUBMIT' NAME='view' value='View'>"); 89 out.print("</form>"); 90 out.print("<b>Or browse one of these fun classes:</b><br><br>"); 91 out.print(tab + getClassRef("javax.ejb.EJBHome") + "<br>"); 92 out.print(tab + getClassRef("javax.ejb.EJBObject") + "<br>"); 93 out.print( 94 tab + getClassRef("javax.ejb.EnterpriseBean") + "<br>"); 95 out.print(tab + getClassRef("javax.ejb.SessionBean") + "<br>"); 96 out.print(tab + getClassRef("javax.ejb.EntityBean") + "<br>"); 97 out.print( 98 tab 99 + getClassRef("javax.servlet.http.HttpServlet") 100 + "<br>"); 101 out.print( 102 tab 103 + getClassRef("javax.servlet.http.HttpServletRequest") 104 + "<br>"); 105 out.print( 106 tab 107 + getClassRef("javax.servlet.http.HttpServletResponse") 108 + "<br>"); 109 out.print( 110 tab 111 + getClassRef("javax.servlet.http.HttpSession") 112 + "<br>"); 113 out.print( 114 tab + getClassRef("javax.naming.InitialContext") + "<br>"); 115 out.print(tab + getClassRef("javax.naming.Context") + "<br>"); 116 117 } else { 118 Class clazz = this.getClass().forName(className); 119 printClass(clazz, out); 120 } 121 } catch (Exception e) { 122 out.println("FAIL"); 123 return; 124 } 125 out.print("<BR><BR>"); 126 if (hasMethods) { 127 out.print("<font color='green'>*</font> Public "); 128 out.print("<font color='red'>*</font> Private "); 129 out.print("<font color='blue'>*</font> Protected "); 130 out.print("<font color='yellow'>*</font> Default "); 131 out.print("<BR>"); 132 } 133 } 134 135 136 public void printClass(Class clazz, PrintWriter out) 137 throws Exception { 138 out.print("<b>" + clazz.getName() + "</b><br>"); 139 Method [] methods = clazz.getDeclaredMethods(); 140 hasMethods = (methods.length > 0); 141 for (int i = 0; i < methods.length; i++) { 142 printMethod(methods[i], out); 143 } 144 145 159 Class sup = clazz.getSuperclass(); 160 if (sup != null) { 161 out.print("<br><b>Extends:</b><br>"); 162 out.print(tab + getClassRef(sup) + "<br>"); 163 } 164 165 Class [] intf = clazz.getInterfaces(); 166 167 if (intf.length > 0) { 168 out.print("<br><b>Implements:</b><br>"); 169 for (int i = 0; i < intf.length; i++) { 170 out.print(tab + getClassRef(intf[i]) + "<br>"); 171 } 172 } 173 } 174 175 public void printMethod(Method m, PrintWriter out) 176 throws Exception { 177 out.print(tab); 178 out.print(" " + getModifier(m)); 179 180 out.print(" " + getShortClassRef(m.getReturnType()) + " "); 181 182 out.print("" + m.getName() + " "); 183 Class [] params = m.getParameterTypes(); 184 out.print("<font color='gray'>(</font>"); 185 for (int j = 0; j < params.length; j++) { 186 out.print(getShortClassRef(params[j])); 187 if (j != params.length - 1) { 188 out.print(", "); 189 } 190 } 191 out.print("<font color='gray'>)</font>"); 192 193 Class [] excp = m.getExceptionTypes(); 194 if (excp.length > 0) { 195 out.print(" <font color='gray'>throws</font> "); 196 for (int j = 0; j < excp.length; j++) { 197 out.print(getShortClassRef(excp[j])); 198 if (j != excp.length - 1) { 199 out.print(", "); 200 } 201 } 202 } 203 out.print("<br>"); 204 } 205 206 public String getModifier(Method m) throws Exception { 207 int mod = m.getModifiers(); 208 String color = ""; 209 210 if (Modifier.isPublic(mod)) { 211 color = "green"; 212 } else if (Modifier.isPrivate(mod)) { 213 color = "red"; 214 } else if (Modifier.isProtected(mod)) { 215 color = "blue"; 216 } else { 217 color = "yellow"; 218 } 219 return "<font color='" + color + "'>*</font>"; 220 } 221 222 public String getClassRef(Class clazz) throws Exception { 223 String name = clazz.getName(); 224 return "<a HREF='"+VIEW_CLASS+"?class=" + name + "'>" + name + "</a>"; 225 } 226 227 public String getClassRef(String name) throws Exception { 228 return "<a HREF='"+VIEW_CLASS+"?class=" + name + "'>" + name + "</a>"; 229 } 230 231 public String getShortClassRef(Class clazz) throws Exception { 232 if (clazz.isPrimitive()) { 233 return "<font color='gray'>" + clazz.getName() + "</font>"; 234 } else if (clazz.isArray() && clazz.getComponentType().isPrimitive()) { 235 return "<font color='gray'>" 236 + clazz.getComponentType() 237 + "[]</font>"; 238 } else if (clazz.isArray()) { 239 String name = clazz.getComponentType().getName(); 240 int dot = name.lastIndexOf(".") + 1; 241 String shortName = name.substring(dot, name.length()); 242 return "<a HREF='"+VIEW_CLASS+"?class=" 243 + name 244 + "'>" 245 + shortName 246 + "[]</a>"; 247 } else { 248 String name = clazz.getName(); 249 int dot = name.lastIndexOf(".") + 1; 250 String shortName = name.substring(dot, name.length()); 251 return "<a HREF='"+VIEW_CLASS+"?class=" 252 + name 253 + "'>" 254 + shortName 255 + "</a>"; 256 } 257 } 258 } 259 | Popular Tags |