<%@ page import=" javax.naming.InitialContext, javax.naming.Context, javax.naming.*, java.util.Properties, javax.naming.Context, javax.naming.InitialContext, javax.servlet.ServletConfig, javax.servlet.ServletException, javax.servlet.http.HttpServlet, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.jsp.JspWriter, java.io.PrintWriter, java.util.*, java.io.*, java.lang.reflect.Method, java.lang.reflect.InvocationTargetException, java.lang.reflect.Modifier "%> OpenEJB Tomcat Integration/1.0
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.openejb.client.LocalInitialContextFactory"); 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( "client/tests/stateless/BasicStatelessHome" ); if (ejbHome instanceof java.rmi.Remote) out.println(OK); } catch (Exception e){ out.println(FAIL); return; } // --------------------------------------------------- // Is the home interface visible? // --------------------------------------------------- printTest("Checking for the home interface class definition "); Class homeInterface; try{ homeInterface = Class.forName("org.openejb.test.stateless.BasicStatelessHome",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, new Object[0]); 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("org.openejb.test.stateless.BasicStatelessObject",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{ String message = "!snoitalutargnoC ,skroW gnihtyrevE"; Class[] params = new Class[]{String.class}; Method businessMethod = null; businessMethod = remoteInterface.getDeclaredMethod("businessMethod", params); returnValue = businessMethod.invoke(ejbObject, new Object[]{message}); if (returnValue instanceof java.lang.String) 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(); } %>