%@ 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
"%>
OpenEJB/Tomcat
<%!
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;
try {
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("");
// ---------------------------------------------------
// Were the OpenEJB classes installed?
// ---------------------------------------------------
printTest("Were the OpenEJB classes installed");
ClassLoader myLoader = null;
Class openejb = null;
try {
myLoader = this.getClass().getClassLoader();
openejb = Class.forName("org.apache.openejb.OpenEJB", true, myLoader);
out.print(OK);
} catch (Exception e) {
out.print(FAIL);
}
// ---------------------------------------------------
// Are the EJB libraries visible?
// ---------------------------------------------------
printTest("Were the EJB classes installed");
try {
Class.forName("javax.ejb.EJBHome", true, myLoader);
out.print(OK);
} catch (Exception e) {
out.print(FAIL);
}
// ---------------------------------------------------
// Was OpenEJB initialized (aka started)?
// ---------------------------------------------------
printTest("Was OpenEJB initialized (aka started)");
try {
Method isInitialized = openejb.getDeclaredMethod("isInitialized", new Class[0]);
Boolean running = (Boolean) isInitialized.invoke(openejb, new Object[0]);
if (running.booleanValue()) {
out.print(OK);
} else {
out.print(FAIL);
}
} catch (Exception e) {
out.print(FAIL);
}
// ---------------------------------------------------
// Can I lookup anything?
// ---------------------------------------------------
printTest("Performing a test lookup");
try {
Object obj = ctx.lookup("");
if (obj.getClass().getName().equals("org.apache.openejb.core.ivm.naming.IvmContext")) {
out.print(OK);
} else {
out.print(FAIL);
}
} catch (Exception e) {
out.print(FAIL);
}
out.print("
");
out.print(HR);
try {
Object obj = ctx.lookup("client");
if (obj instanceof Context) {
out.print("
");
}
} catch (Exception e) {
}
} 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();
}
%>