<% out.print(""); %> <%@ page import=" javax.servlet.http.HttpServletRequest, javax.servlet.jsp.JspWriter, java.io.ByteArrayOutputStream, java.io.PrintStream, java.lang.reflect.InvocationTargetException, java.lang.reflect.Method, java.lang.reflect.Modifier, java.util.HashMap, java.util.Iterator, java.util.Set "%> <%@ page import="java.util.Map" %> TomEE

OpenEJB Object Invoker

<% try{ synchronized (this) { main(request, session, out); } } catch (Exception e){ out.println("

FAIL

"); //throw e; return; } %>

<%! String tab = "    "; static final String invLock = "lock"; static int invCount; HttpSession session; HttpServletRequest request; JspWriter out; class Invocation { String id = "inv"; String objID; Class clazz; Object target; Method method; Object[] args; Object result; Invocation() { synchronized (invLock) { id += ++invCount; } } public Object invoke() throws Exception { if (target == null || method == null || args == null) { throw new Exception("This invocation contains null objects."); } return method.invoke(target, args); } } /** * The main method of this JSP */ public void main(HttpServletRequest request, HttpSession session, JspWriter out) throws Exception { this.request = request; this.session = session; this.out = out; printObjectSection(); } /** * Print the list of objects with the focused object as * selected in the box. * If no object is selected, make an entry called "Pick an Object" */ public void printObjectSection() throws Exception { String removeID = request.getParameter("remove"); if (removeID != null) { removeObject(removeID); } Invocation inv = null; String invID = request.getParameter("inv"); if (invID == null) { String objID = request.getParameter("obj"); if (objID != null) { inv = new Invocation(); inv.target = getObject(objID); inv.objID = objID; setInvocation(inv.id, inv); } } else { inv = getInvocation(invID); } if (inv == null || inv.target == null) { // Pick from the list printObjectList(); } else { out.print("

Object

"); out.print("

" + inv.objID + " [change]

"); // Show the selected item and continue printMethodSection(inv); } } /** * Prints the list of objects that can be invoked */ public void printObjectList() throws Exception { Map objects = getObjectMap(); if (objects.size() == 0) { out.print("

No object have been created. Browse for an EJB

"); } else { out.print("

Pick and object to invoke

"); Set keys = objects.keySet(); Iterator iterator = keys.iterator(); out.print(""); } } /** * Print the list of methods with the focused method as * selected in the box. * If no method is selected, make an entry called "Pick a Method" */ public void printMethodSection(Invocation inv) throws Exception { String methodID = request.getParameter("m"); if (methodID != null) { int method = Integer.parseInt(methodID); Method[] methods = inv.clazz.getMethods(); if (method > -1 && method < methods.length) { inv.method = methods[method]; } else { inv.method = null; inv.args = null; } } if (inv.method == null) { // Pick from the list printMethodList(inv); } else { out.print("

Method

"); out.print("

" + formatMethod(inv.method) + " [change]"); // Show the selected item and continue printArgumentSection(inv); } } /** * Prints the list of methods that can be invoked */ public void printMethodList(Invocation inv) throws Exception { out.print("Pick a method to invoke
"); //out.print("Methods:
"); Object obj = inv.target; Class clazz = inv.target.getClass(); if (obj instanceof javax.ejb.EJBHome) { clazz = obj.getClass().getInterfaces()[0]; } else if (obj instanceof javax.ejb.EJBObject) { clazz = obj.getClass().getInterfaces()[0]; } else { clazz = obj.getClass(); } inv.clazz = clazz; out.print("

"); } /** * Print the list of arguments. * If no arguments have been selected, * show the argument entry form. */ public void printArgumentSection(Invocation inv) throws Exception { String args = request.getParameter("args"); if (args != null) { parseArgs(inv); } if (inv.method.getParameterTypes().length == 0) { inv.args = new Object[]{}; } if (inv.args == null) { printArgumentList(inv); } else { out.print("

Arguments

"); if (inv.args.length == 0) { out.print("

none

"); } out.print("
    "); for (int i = 0; i < inv.args.length; i++) { String val = formatObject(inv.args[i]); out.print("
  1. " + val + ""); } out.print("
"); printInvokeSection(inv); } } public void parseArgs(Invocation inv) throws Exception { Class[] pTypes = inv.method.getParameterTypes(); inv.args = new Object[pTypes.length]; for (int i = 0; i < pTypes.length; i++) { Class type = pTypes[i]; String unparsedArg = request.getParameter("arg" + i); inv.args[i] = getConverter(type).convert(type, unparsedArg); } } public void printArgumentList(Invocation inv) throws Exception { out.print("Fill in the arguments
"); Class[] pTypes = inv.method.getParameterTypes(); out.print("
"); out.print(""); out.print(""); for (int i = 0; i < pTypes.length; i++) { Converter con = getConverter(pTypes[i]); out.print(""); out.print(""); out.print(""); out.print(""); } out.print("
"); out.print(tab + getShortClassRef(pTypes[i])); out.print(""); out.print("  arg" + i); out.print(""); out.print("  " + con.getInputControl(i, pTypes[i])); out.print("
"); out.print("

"); out.print(""); out.print("
"); } /** * Print the list of arguments. * If no arguments have been selected, * show the argument entry form. */ public void printInvokeSection(Invocation inv) throws Exception { String doInvoke = request.getParameter("invoke"); if (doInvoke != null) { invoke(inv); } else { out.print("
"); out.print(""); out.print(""); out.print("
"); } } String pepperImg = ""; public void invoke(Invocation inv) throws Exception { try { inv.result = inv.invoke(); out.print("

Result:

"); if (inv.method.getReturnType() == java.lang.Void.TYPE) { out.print("

Done

"); } else if (inv.result == null) { out.print("

null

"); } else { String clazz = inv.result.getClass().getName(); String objID = getObjectID(inv.result); setObject(objID, inv.result); out.print(""); printRow("id", objID); printRow("class", "" + clazz + ""); printRow("toString", formatObject(inv.result)); out.print("
"); out.print("

Actions:

"); out.print("Invoke a method on the object or Discard the object"); } } catch (InvocationTargetException e) { out.print("

Exception:

"); Throwable t = e.getTargetException(); out.print("

"); out.print("Received a " + t.getClass().getName()); //out.print(inv.method+"

"); if (t instanceof java.rmi.RemoteException) { out.print("

"); out.print("RemoteException message:
"); out.print(t.getMessage()); out.print("

"); out.print("Nested exception's stack trace:
"); while (t instanceof java.rmi.RemoteException) { t = ((java.rmi.RemoteException) t).detail; } out.print(formatThrowable(t)); } else { out.print("

" + formatThrowable(t)); } out.print("

"); } catch (Throwable e) { out.print("

Exception:

"); out.print("

"); out.print(formatObject(e)); out.print("

"); } } public String formatThrowable(Throwable err) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); err.printStackTrace(new PrintStream(baos)); byte[] bytes = baos.toByteArray(); StringBuffer sb = new StringBuffer(bytes.length); for (int i = 0; i < bytes.length; i++) { char c = (char) bytes[i]; switch (c) { case' ': sb.append(" "); break; case'\n': sb.append("
"); break; case'\r': break; default: sb.append(c); } } return sb.toString(); } public String formatObject(Object obj) throws Exception { int max = 75; String val = obj.toString(); val = (val.length() > max) ? val.substring(0, max - 3) + "..." : val; char[] chars = new char[val.length()]; val.getChars(0, chars.length, chars, 0); StringBuffer sb = new StringBuffer(chars.length); for (int j = 0; j < chars.length; j++) { char c = chars[j]; switch (c) { case'<': sb.append("<"); break; case'>': sb.append(">"); break; case'&': sb.append("&"); break; default: sb.append(c); } } return sb.toString(); } /*-----------------------------------------------------------*/ // Method name formatting /*-----------------------------------------------------------*/ public String formatMethod(Method m) throws Exception { StringBuffer sb = new StringBuffer(); sb.append(getShortClassName(m.getReturnType())).append("  "); sb.append(m.getName()); Class[] params = m.getParameterTypes(); sb.append("("); for (int j = 0; j < params.length; j++) { sb.append(getShortClassName(params[j])); if (j != params.length - 1) { sb.append(", "); } } sb.append(")"); Class[] excp = m.getExceptionTypes(); if (excp.length > 0) { sb.append(" throws "); for (int j = 0; j < excp.length; j++) { sb.append(getShortClassName(excp[j])); if (j != excp.length - 1) { sb.append(", "); } } } return sb.toString(); } /*-----------------------------------------------------------*/ // Class name formatting /*-----------------------------------------------------------*/ public String getShortClassName(Class clazz) throws Exception { if (clazz.isPrimitive()) { return clazz.getName(); } else if (clazz.isArray() && clazz.getComponentType().isPrimitive()) { return clazz.getComponentType() + "[]"; } else if (clazz.isArray()) { String name = clazz.getComponentType().getName(); int dot = name.lastIndexOf(".") + 1; String shortName = name.substring(dot, name.length()); return shortName + "[]"; } else { String name = clazz.getName(); int dot = name.lastIndexOf(".") + 1; String shortName = name.substring(dot, name.length()); return shortName; } } public String getShortClassRef(Class clazz) throws Exception { if (clazz.isPrimitive()) { return "" + clazz.getName() + ""; } else if (clazz.isArray() && clazz.getComponentType().isPrimitive()) { return "" + clazz.getComponentType() + "[]"; } else if (clazz.isArray()) { String name = clazz.getComponentType().getName(); int dot = name.lastIndexOf(".") + 1; String shortName = name.substring(dot, name.length()); return "" + shortName + "[]"; } else { String name = clazz.getName(); int dot = name.lastIndexOf(".") + 1; String shortName = name.substring(dot, name.length()); return "" + shortName + ""; } } protected void printRow(String col1, String col2) throws Exception { out.print(""); out.print(col1); out.print(""); out.print(col2); out.print(""); } /*-----------------------------------------------------------*/ // Object list support /*-----------------------------------------------------------*/ public String getObjectID(Object obj) { Class clazz = obj.getClass(); if (obj instanceof javax.ejb.EJBHome) { clazz = obj.getClass().getInterfaces()[0]; } else if (obj instanceof javax.ejb.EJBObject) { clazz = obj.getClass().getInterfaces()[0]; } return clazz.getName() + "@" + obj.hashCode(); } public Object getObject(String objID) { return getObjectMap().get(objID); } public void setObject(String objID, Object obj) { getObjectMap().put(objID, obj); } public void removeObject(String objID) { getObjectMap().remove(objID); } @SuppressWarnings({"unchecked"}) public Map getObjectMap() { Map objects = (Map) session.getAttribute("objects"); return objects; } /*-----------------------------------------------------------*/ // Invocation list support /*-----------------------------------------------------------*/ public Invocation getInvocation(String invID) { return getInvocationMap().get(invID); } public void setInvocation(String invID, Invocation obj) { getInvocationMap().put(invID, obj); } @SuppressWarnings({"unchecked"}) public Map getInvocationMap() { Map invocations = (Map) session.getAttribute("invocations"); if (invocations == null) { invocations = new HashMap(); session.setAttribute("invocations", invocations); } return invocations; } /*-----------------------------------------------------------*/ // String conversion support /*-----------------------------------------------------------*/ final Map converters = initConverters(); public Converter getConverter(Class type) { Converter con = converters.get(type); if (con == null) { con = defaultConverter; } return con; } final Converter defaultConverter = new ObjectConverter(); private Map initConverters() { Map map = new HashMap(); map.put(String.class, new StringConverter()); map.put(Character.class, new CharacterConverter()); map.put(Boolean.class, new BooleanConverter()); map.put(Byte.class, new ByteConverter()); map.put(Short.class, new ShortConverter()); map.put(Integer.class, new IntegerConverter()); map.put(Long.class, new LongConverter()); map.put(Float.class, new FloatConverter()); map.put(Double.class, new DoubleConverter()); map.put(Object.class, new ObjectConverter()); map.put(Character.TYPE, map.get(Character.class)); map.put(Boolean.TYPE, map.get(Boolean.class)); map.put(Byte.TYPE, map.get(Byte.class)); map.put(Short.TYPE, map.get(Short.class)); map.put(Integer.TYPE, map.get(Integer.class)); map.put(Long.TYPE, map.get(Long.class)); map.put(Float.TYPE, map.get(Float.class)); map.put(Double.TYPE, map.get(Double.class)); return map; } abstract class Converter { public abstract Object convert(Class type, String raw) throws Exception; public String getInputControl(int argNumber, Class type) throws Exception { return ""; } } class StringConverter extends Converter { public Object convert(Class type, String raw) throws Exception { return raw; } } class CharacterConverter extends Converter { public Object convert(Class type, String raw) throws Exception { return new Character(raw.charAt(0)); } } class BooleanConverter extends Converter { public Object convert(Class type, String raw) throws Exception { return new Boolean(raw); } } class ByteConverter extends Converter { public Object convert(Class type, String raw) throws Exception { return new Byte(raw); } } class ShortConverter extends Converter { public Object convert(Class type, String raw) throws Exception { return new Short(raw); } } class IntegerConverter extends Converter { public Object convert(Class type, String raw) throws Exception { return new Integer(raw); } } class LongConverter extends Converter { public Object convert(Class type, String raw) throws Exception { return new Long(raw); } } class FloatConverter extends Converter { public Object convert(Class type, String raw) throws Exception { return new Float(raw); } } class DoubleConverter extends Converter { public Object convert(Class type, String raw) throws Exception { return new Double(raw); } } class ObjectConverter extends Converter { public Object convert(Class type, String raw) throws Exception { return raw; } } %>