1 45 package org.openejb.webadmin.clienttools; 46 47 import java.io.IOException ; 48 import java.io.PrintWriter ; 49 import java.util.Properties ; 50 51 import javax.ejb.EJBHome ; 52 import javax.ejb.EJBLocalHome ; 53 import javax.naming.Context ; 54 import javax.naming.InitialContext ; 55 import javax.naming.NameClassPair ; 56 import javax.naming.NamingEnumeration ; 57 import javax.naming.NamingException ; 58 59 import org.openejb.webadmin.HttpRequest; 60 import org.openejb.webadmin.HttpResponse; 61 import org.openejb.webadmin.HttpSession; 62 import org.openejb.webadmin.WebAdminBean; 63 64 67 public class ViewJndiBean extends WebAdminBean implements Constants { 68 69 70 private HttpSession session; 71 private String selected; 72 private String ctxID; 73 private Context ctx; 74 75 public void preProcess(HttpRequest request, HttpResponse response) 76 throws IOException { 77 session = request.getSession(true); 78 selected = request.getQueryParameter("selected"); 79 if (selected == null) { 80 selected = ""; 81 } 82 ctxID = request.getQueryParameter("ctx"); 83 ctx = null; 84 } 85 86 public void postProcess(HttpRequest request, HttpResponse response) 87 throws IOException { 88 } 89 90 public void writeHtmlTitle(PrintWriter out) throws IOException { 91 out.write("Client Tools -- JNDI Viewer"); 92 } 93 94 public void writePageTitle(PrintWriter out) throws IOException { 95 if (ctxID == null){ 96 out.print("JNDI Environment Naming Context (ENC)"); 97 } else if (ctxID.startsWith("enc")) { 98 out.print("OpenEJB Global JNDI Namespace"); 99 } 100 } 101 102 public void writeBody(PrintWriter out) throws IOException { 103 104 if (ctxID == null) { 105 Properties p = new Properties (); 106 p.put(Context.INITIAL_CONTEXT_FACTORY, "org.openejb.client.LocalInitialContextFactory"); 107 p.put("openejb.loader", "embed"); 108 try { 109 ctx = new InitialContext ( p ); 110 } catch (NamingException e) { 111 e.printStackTrace(); 113 } 114 ctxID = null; 115 out.print("<b>OpenEJB Global JNDI Namespace</b><br><br>"); 116 } else { 117 ctx = (Context )session.getAttribute(ctxID); 118 if (ctxID.startsWith("enc")) { 119 out.print("This is the private namespace of an Enterprise JavaBean."); 120 out.print("<BR><BR>"); 121 } 122 } 123 124 Node root = new RootNode(); 125 try { 126 buildNode(root,ctx); 127 128 printNodes(root, out, "", selected); 129 } catch (Exception e) { 130 e.printStackTrace(out); 132 } 133 } 134 135 class Node { 136 static final int CONTEXT = 1; 137 static final int BEAN = 2; 138 static final int OTHER = 3; 139 Node parent; 140 Node[] children = new Node[0]; 141 String name; 142 int type = 0; 143 144 public String getID(){ 145 if (parent instanceof RootNode) { 146 return name; 147 } else { 148 return parent.getID()+"/"+name; 149 } 150 } 151 public String getName(){ 152 return name; 153 } 154 public int getType(){ 155 return type; 156 } 157 public void addChild(Node child){ 158 int len = children.length; 159 Node[] newChildren = new Node[len+1]; 160 System.arraycopy(children,0,newChildren,0,len); 161 newChildren[len] = child; 162 children = newChildren; 163 child.parent = this; 164 } 165 } 166 167 class RootNode extends Node{ 168 public String getID() { 169 return ""; 170 } 171 public String getName() { 172 return ""; 173 } 174 public int getType() { 175 return Node.CONTEXT; 176 } 177 } 178 179 public void buildNode(Node parent, Context ctx) throws Exception { 180 if (false) throw new NullPointerException (); 181 NamingEnumeration enumeration = ctx.list( "" ); 182 while (enumeration.hasMoreElements()){ 183 NameClassPair pair = (NameClassPair )enumeration.next(); 184 Node node = new Node(); 185 parent.addChild(node); 186 node.name = pair.getName(); 187 188 Object obj = ctx.lookup(node.getName()); 189 if ( obj instanceof Context ){ 190 node.type = Node.CONTEXT; 191 buildNode(node,(Context )obj); 192 } else if (obj instanceof EJBHome || obj instanceof EJBLocalHome ) { 193 node.type = Node.BEAN; 194 } else { 195 node.type = Node.OTHER; 196 } 197 } 198 } 199 200 201 202 public void printNodes(Node node, PrintWriter out, String tabs, String selected) throws Exception { 203 switch (node.getType()) { 204 case Node.CONTEXT: printContextNode(node,out,tabs,selected); break; 205 case Node.BEAN: printBeanNode(node,out,tabs,selected); break; 206 default: printOtherNode(node,out,tabs,selected); break; 207 } 208 209 } 210 211 public void printContextNode(Node node, PrintWriter out, String tabs, String selected) throws Exception { 212 String id = node.getID(); 213 if ( selected.startsWith(id) ) { 214 if (ctxID != null) { 215 out.print(tabs+"<a HREF='"+VIEW_JNDI+"?ctx="+ctxID+"&selected="+id+"'>"+openImg+" "+node.getName()+"</a><br>"); 216 } else { 217 out.print(tabs+"<a HREF='"+VIEW_JNDI+"?selected="+id+"'>"+openImg+" "+node.getName()+"</a><br>"); 218 } 219 for (int i=0; i < node.children.length; i++){ 220 Node child = node.children[i]; 221 printNodes(child,out,tabs+" ",selected); 222 } 223 } else { 224 if (ctxID != null) { 225 out.print(tabs+"<a HREF='"+VIEW_JNDI+"?ctx="+ctxID+"&selected="+id+"'>"+closedImg+" "+node.getName()+"</a><br>"); 226 } else { 227 out.print(tabs+"<a HREF='"+VIEW_JNDI+"?selected="+id+"'>"+closedImg+" "+node.getName()+"</a><br>"); 228 } 229 } 230 } 231 232 public void printBeanNode(Node node, PrintWriter out, String tabs, String selected) throws Exception { 233 String id = node.getID(); 234 try{ 237 Object ejb = ctx.lookup(id); 238 Object handler = org.openejb.util.proxy.ProxyManager.getInvocationHandler(ejb); 239 Object deploymentID = ((org.openejb.core.ivm.BaseEjbProxyHandler)handler).deploymentID; 240 out.print(tabs+"<a HREF='"+VIEW_EJB+"?ejb="+deploymentID+"'>"+ejbImg+" "+node.getName()+"</a><br>"); 241 } catch (Exception e){ 242 out.print(tabs+ejbImg+" "+node.getName()+"<br>"); 243 } 244 } 248 249 public void printOtherNode(Node node, PrintWriter out, String tabs, String selected) throws Exception { 250 String id = node.getID(); 251 Object obj = ctx.lookup(id); 252 String clazz = obj.getClass().getName(); 253 out.print(tabs+"<a HREF='"+VIEW_CLASS+"?class="+clazz+"'>"+javaImg+" "+node.getName()+"</a><br>"); 254 } 255 } 256 | Popular Tags |