<% out.print(""); %> <%@ page import=" javax.naming.Context, javax.naming.InitialContext, javax.naming.NameClassPair, javax.naming.NamingEnumeration, java.util.Properties "%> <%@ page import="javax.naming.NamingException" %> <%@ page import="java.lang.reflect.Method" %> <%@ page import="java.lang.reflect.Field" %> <%@ page import="org.apache.openejb.util.proxy.LocalBeanProxyFactory" %> TomEE
<% try{ String selected = request.getParameter("selected"); if (selected == null) { selected = ""; } ctxID = request.getParameter("ctxID"); ctx = null; if (ctxID == null) { Properties p = new Properties(); p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.core.LocalInitialContextFactory"); p.put("openejb.loader", "embed"); try { ctx = new InitialContext( p ); out.print("

OpenEJB Global JNDI Namespace

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

OpenEJB Not Installed

"); } } else { ctx = (Context)session.getAttribute(ctxID); if (ctxID.startsWith("enc")) { %>

JNDI Environment Naming Context (ENC)

<% } } if (ctx != null) { Node root = new RootNode(); buildNode(root,ctx); out.println("

"); printNodes(root, out, "",selected); out.println("

"); } } catch (Exception e) { out.println("

FAIL

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

<%! String ctxID; Context ctx; class Node { static final int CONTEXT = 1; static final int BEAN = 2; static final int OTHER = 3; Node parent; Node[] children = new Node[0]; String name; int type = 0; // returns the JNDI name public String getID() { if (parent instanceof RootNode) { return name; } else { return parent.getID() + "/" + name; } } public String getName() { return name; } public int getType() { return type; } public void addChild(Node child) { int len = children.length; Node[] newChildren = new Node[len + 1]; System.arraycopy(children, 0, newChildren, 0, len); newChildren[len] = child; children = newChildren; child.parent = this; } } class RootNode extends Node { public String getID() { return ""; } public String getName() { return ""; } public int getType() { return Node.CONTEXT; } } public void buildNode(Node parent, Context ctx) throws Exception { NamingEnumeration namingEnumeration = ctx.list(""); while (namingEnumeration.hasMoreElements()) { NameClassPair pair = (NameClassPair) namingEnumeration.next(); Node node = new Node(); parent.addChild(node); node.name = pair.getName(); Object obj = lookup(ctx, node.getName()); if (obj instanceof Context) { node.type = Node.CONTEXT; buildNode(node, (Context) obj); } else if (obj instanceof java.rmi.Remote || obj instanceof org.apache.openejb.core.ivm.IntraVmProxy || (obj != null && LocalBeanProxyFactory.isProxy(obj.getClass()))) { node.type = Node.BEAN; } else { node.type = Node.OTHER; } } } String openImg = ""; String closedImg = ""; String ejbImg = ""; String javaImg = ""; public void printNodes(Node node, javax.servlet.jsp.JspWriter out, String tabs, String selected) throws Exception { switch (node.getType()) { case Node.CONTEXT: printContextNode(node, out, tabs, selected); break; case Node.BEAN: printBeanNode(node, out, tabs); break; default: printOtherNode(node, out, tabs); break; } } public void printContextNode(Node node, javax.servlet.jsp.JspWriter out, String tabs, String selected) throws Exception { String id = node.getID(); if (selected.startsWith(id)) { if (ctxID != null) { out.print(tabs + "" + openImg + "  " + node.getName() + "
"); } else { out.print(tabs + "" + openImg + "  " + node.getName() + "
"); } for (int i = 0; i < node.children.length; i++) { Node child = node.children[i]; printNodes(child, out, tabs + "     ", selected); } } else { if (ctxID != null) { out.print(tabs + "" + closedImg + "  " + node.getName() + "
"); } else { out.print(tabs + "" + closedImg + "  " + node.getName() + "
"); } } } public void printBeanNode(Node node, javax.servlet.jsp.JspWriter out, String tabs) throws Exception { String id = node.getID(); if (ctxID != null && ctxID.startsWith("enc")) { // HACK! try { Object ejb = lookup(ctx, id); Object deploymentID = getDeploymentId(ejb); out.print(tabs + "" + ejbImg + "  " + node.getName() + "
"); } catch (Exception e) { out.print(tabs + ejbImg + "  " + node.getName() + "
"); } } else { try { Object ejb = lookup(ctx, id); Object deploymentID = getDeploymentId(ejb); out.print(tabs + "" + ejbImg + "  " + node.getName() + "
"); } catch (Exception e) { out.print(tabs + ejbImg + "  " + node.getName() + "
"); } } } public void printOtherNode(Node node, javax.servlet.jsp.JspWriter out, String tabs) throws Exception { String id = node.getID(); Object obj = lookup(ctx, id); String clazz = obj.getClass().getName(); out.print(tabs + "" + javaImg + "  " + node.getName() + "
"); } private Object getDeploymentId(Object ejb) throws Exception { org.apache.openejb.core.ivm.BaseEjbProxyHandler handler = (org.apache.openejb.core.ivm.BaseEjbProxyHandler)org.apache.openejb.util.proxy.ProxyManager.getInvocationHandler(ejb); return handler.deploymentID; } private Object lookup(Context ctx, String name) throws NamingException { try { Object obj = ctx.lookup(name); return obj; } catch (Exception e) { return "ERROR: " + e.getMessage(); } } %>