% out.print(""); %>
<%@ 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
"%>
TomEE
Testing openejb.home validity
<%
final InitialContext ctx;
try{
synchronized (this) {
ctx = main(request, session, out);
}
} catch (Exception e){
out.println("FAIL
");
return;
}
%>
<%
try {
Object obj = ctx.lookup("client");
if (obj instanceof Context) {
out.print("
Continue tests");
}
} catch (Exception e) {
}
%>
<%!
static String invLock = "lock";
static int invCount;
String OK = "OK";
String FAIL = "FAIL";
/**
* The main method of this JSP
*/
public InitialContext main(final HttpServletRequest request, final HttpSession session, final JspWriter out) throws Exception {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.core.LocalInitialContextFactory");
p.put("openejb.loader", "embed");
final InitialContext ctx = new InitialContext(p);
// ---------------------------------------------------
// Were the OpenEJB classes installed?
// ---------------------------------------------------
printTest(out, "Were the OpenEJB classes installed", new TestAction() {
@Override
public String run() throws Exception {
ClassLoader myLoader = this.getClass().getClassLoader();
Class.forName("org.apache.openejb.OpenEJB", true, myLoader);
return OK;
}
});
// ---------------------------------------------------
// Are the EJB libraries visible?
// ---------------------------------------------------
printTest(out, "Were the EJB classes installed", new TestAction() {
@Override
public String run() throws Exception {
Class.forName("javax.ejb.EJBHome", true, this.getClass().getClassLoader());
return OK;
}
});
// ---------------------------------------------------
// Was OpenEJB initialized (aka started)?
// ---------------------------------------------------
printTest(out, "Was OpenEJB initialized (aka started)", new TestAction() {
@Override
public String run() throws Exception {
Class openejb = Class.forName("org.apache.openejb.OpenEJB", true, this.getClass().getClassLoader());
Method isInitialized = openejb.getDeclaredMethod("isInitialized");
Boolean running = (Boolean) isInitialized.invoke(openejb);
if (running) {
return OK;
} else {
return FAIL;
}
}
});
// ---------------------------------------------------
// Can I lookup anything?
// ---------------------------------------------------
printTest(out, "Performing a test lookup", new TestAction() {
@Override
public String run() throws Exception {
Object obj = ctx.lookup("");
if (obj.getClass().getName().equals("org.apache.openejb.core.ivm.naming.IvmContext")) {
return OK;
} else {
return FAIL;
}
}
});
return ctx;
}
private interface TestAction {
String run() throws Exception ;
}
protected void printTest(JspWriter out, String test, TestAction testAction) throws IOException {
out.print("");
out.print(test);
out.print(" | ");
try {
out.print(testAction.run());
} catch (Exception e) {
out.print(FAIL + " " + formatThrowable(e));
}
out.print(" |
");
}
public String formatThrowable(Throwable err) {
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();
}
%>