<%@ page import=" javax.naming.Context, javax.naming.InitialContext, javax.servlet.http.HttpServletRequest, javax.servlet.jsp.JspWriter, java.io.ByteArrayOutputStream, java.io.IOException, java.io.PrintStream, java.lang.reflect.Method, java.util.Properties "%> <%@ page import="java.io.PrintWriter" %> OpenEJB/Tomcat
OpenEJBIndexJNDIEJBClassInvoke
   



Testing an Enterprise JavaBean

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


 
<%! String tab = "    "; static String invLock = "lock"; static int invCount; HttpSession session; HttpServletRequest request; JspWriter out; String OK = "OK"; String FAIL = "FAIL"; String HR = "
"; String pepperImg = ""; /** * 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; InitialContext ctx = null; ClassLoader myLoader = null; try { myLoader = this.getClass().getClassLoader(); Properties p = new Properties(); p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory"); p.put("openejb.loader", "embed"); ctx = new InitialContext(p); } catch (Exception e) { formatThrowable(e); } try { out.print(HR); out.print(""); // --------------------------------------------------- // Can I lookup a home interface from the testsuite? // --------------------------------------------------- printTest("Looking up an ejb home "); Object ejbHome = null; try { ejbHome = ctx.lookup("MEJB"); if (ejbHome instanceof java.rmi.Remote) out.println(OK); } catch (Exception e) { e.printStackTrace(new PrintWriter(out)); out.println(FAIL); return; } // --------------------------------------------------- // Is the home interface visible? // --------------------------------------------------- printTest("Checking for the home interface class definition "); Class homeInterface; try { homeInterface = Class.forName("javax.management.j2ee.ManagementHome", true, myLoader); out.println(OK); } catch (Exception e) { out.println(FAIL); return; } // --------------------------------------------------- // Can I invoke a create method on the ejb home? // --------------------------------------------------- printTest("Invoking the create method on the ejb home "); Object ejbObject = null; try { Class[] params = new Class[0]; Method create = null; create = homeInterface.getDeclaredMethod("create", params); ejbObject = create.invoke(ejbHome); if (ejbObject instanceof java.rmi.Remote) out.println(OK); } catch (Exception e) { out.println(FAIL); return; } // --------------------------------------------------- // Is the remote interface visible? // --------------------------------------------------- printTest("Checking for the remote interface class definition "); Class remoteInterface; try { remoteInterface = Class.forName("javax.management.j2ee.Management", true, myLoader); out.println(OK); } catch (Exception e) { out.println(FAIL); return; } // --------------------------------------------------- // Can I invoke a business method on the ejb object? // --------------------------------------------------- printTest("Invoking a business method on the ejb object "); Object returnValue = null; try { Method businessMethod = remoteInterface.getDeclaredMethod("getMBeanCount"); returnValue = businessMethod.invoke(ejbObject); if (returnValue instanceof java.lang.Integer) out.println(OK); } catch (Exception e) { out.println(FAIL); return; } out.print("
"); out.print(HR); // out.println("
The Enterprise Bean returned the following message:

"); // out.println("" + returnValue + ""); } catch (Exception e) { out.print(FAIL); out.print(""); out.print(HR); out.print(e.getMessage()); } } protected void printTest(String test) throws IOException { out.print(""); out.print(test); 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(); } %>