1 45 package org.openejb.webadmin.clienttools; 46 47 import java.io.IOException ; 48 import java.io.PrintWriter ; 49 import java.util.HashMap ; 50 import java.util.Map ; 51 import java.util.Properties ; 52 53 import javax.naming.Context ; 54 import javax.naming.InitialContext ; 55 56 import org.openejb.webadmin.HttpRequest; 57 import org.openejb.webadmin.HttpResponse; 58 import org.openejb.webadmin.HttpSession; 59 import org.openejb.webadmin.WebAdminBean; 60 import org.openejb.alt.assembler.classic.ContainerInfo; 61 import org.openejb.alt.assembler.classic.EnterpriseBeanInfo; 62 import org.openejb.alt.config.ConfigurationFactory; 63 import org.openejb.core.DeploymentInfo; 64 65 68 public class ViewEjbBean extends WebAdminBean implements Constants { 69 70 public void preProcess(HttpRequest request, HttpResponse response) 71 throws IOException { 72 } 73 74 public void postProcess(HttpRequest request, HttpResponse response) 75 throws IOException { 76 } 77 78 public void writeHtmlTitle(PrintWriter out) throws IOException { 79 out.write("Client Tools -- EJB Viewer"); 80 } 81 82 public void writePageTitle(PrintWriter out) throws IOException { 83 out.write("EJB Viewer"); 84 } 85 86 public void writeBody(PrintWriter out) throws IOException { 87 try { 88 String ejb = request.getQueryParameter("ejb"); 89 if (ejb == null) { 90 ContainerInfo[] cnt = ConfigurationFactory.sys.containerSystem.containers; 91 for (int i = 0; i < cnt.length; i++) { 92 EnterpriseBeanInfo[] beans = cnt[i].ejbeans; 93 for (int x = 0; x < beans.length; x++) { 94 EnterpriseBeanInfo bean = beans[x]; 95 out.print("<a HREF='"+VIEW_EJB+"?ejb="+bean.ejbDeploymentId+"'>"+ejbImg+" "+bean.ejbDeploymentId+"</a><br>"); 96 } 97 } 98 } else { 99 printEjb(ejb, out, request.getSession()); 100 } 101 } catch (Exception e) { 102 103 out.println("FAIL: "); 104 out.print(e.getMessage()); 105 } 108 } 109 110 public void printEjb(String name, PrintWriter out, HttpSession session) 111 throws Exception { 112 113 114 String id = 115 (name.startsWith("/")) ? name.substring(1, name.length()) : name; 116 org.openejb.DeploymentInfo ejb = 117 org.openejb.OpenEJB.getDeploymentInfo(id); 118 119 120 if (ejb == null) { 121 out.print("No such EJB: " + id); 122 return; 123 } 124 String type = null; 125 126 switch (ejb.getComponentType()) { 127 case org.openejb.core.DeploymentInfo.CMP_ENTITY : 128 type = "EntityBean with Container-Managed Persistence"; 129 break; 130 case org.openejb.core.DeploymentInfo.BMP_ENTITY : 131 type = "EntityBean with Bean-Managed Persistence"; 132 break; 133 case org.openejb.core.DeploymentInfo.STATEFUL : 134 type = "Stateful SessionBean"; 135 break; 136 case org.openejb.core.DeploymentInfo.STATELESS : 137 type = "Stateless SessionBean"; 138 break; 139 default : 140 type = "Unkown Bean Type"; 141 break; 142 } 143 out.print("<b>" + type + "</b><br>"); 144 out.print("<table>"); 145 printRow("JNDI Name", name, out); 146 147 148 boolean hasLocal = ejb.getLocalInterface() != null; 149 boolean hasRemote = ejb.getRemoteInterface() != null; 150 151 String remoteInterfaceClassRef; 152 String homeInterfaceClassRef; 153 if (hasRemote){ 154 remoteInterfaceClassRef = getClassRef(ejb.getRemoteInterface()); 155 homeInterfaceClassRef = getClassRef(ejb.getHomeInterface()); 156 } else { 157 remoteInterfaceClassRef = "none"; 158 homeInterfaceClassRef = "none"; 159 } 160 161 printRow("Remote Interface",remoteInterfaceClassRef,out); 162 printRow("Home Interface", homeInterfaceClassRef, out); 163 164 if (hasLocal){ 165 String clzz = getClassRef(ejb.getLocalInterface()); 166 printRow("Local Interface",clzz,out); 167 clzz = getClassRef(ejb.getLocalHomeInterface()); 168 printRow("LocalHome Interface",clzz,out); 169 } 170 171 printRow("Bean Class", getClassRef(ejb.getBeanClass()), out); 172 173 if (ejb.getComponentType() == DeploymentInfo.BMP_ENTITY 174 || ejb.getComponentType() == DeploymentInfo.CMP_ENTITY) { 175 printRow("Primary Key", getClassRef(ejb.getPrimaryKeyClass()), out); 176 } 177 178 out.print("</table>"); 179 out.print("<br><br><b>Actions:</b><br>"); 180 out.print("<table>"); 181 182 HashMap objects = (HashMap ) session.getAttribute("objects"); 185 if (objects == null) { 186 objects = new HashMap (); 187 session.setAttribute("objects", objects); 188 } 189 190 InitialContext ctx; 191 Properties p = new Properties (); 192 193 p.put( 194 Context.INITIAL_CONTEXT_FACTORY, 195 "org.openejb.client.LocalInitialContextFactory"); 196 p.put("openejb.loader", "embed"); 197 198 ctx = new InitialContext (p); 199 200 201 if (hasRemote){ 202 Object obj = ctx.lookup(name); 203 String objID = ejb.getHomeInterface().getName() + "@" + obj.hashCode(); 204 objects.put(objID, obj); 205 String invokerURL = 206 "<a HREF='" 207 + INVOKE_OBJ 208 + "?obj=" 209 + objID 210 + "'>Invoke this EJB's home interface</a>"; 211 printRow(pepperImg, invokerURL, out); 212 } 213 if (hasLocal){ 214 Object obj = ctx.lookup(name+"Local"); 215 String objID = ejb.getLocalHomeInterface().getName() + "@" + obj.hashCode(); 216 objects.put(objID, obj); 217 String invokerURL = 218 "<a HREF='" 219 + INVOKE_OBJ 220 + "?obj=" 221 + objID 222 + "'>Invoke this EJB's local home interface</a>"; 223 printRow(pepperImg, invokerURL, out); 224 } 225 226 Context enc = ((org.openejb.core.DeploymentInfo) ejb).getJndiEnc(); 227 String ctxID = "enc" + enc.hashCode(); 228 session.setAttribute(ctxID, enc); 229 String jndiURL = 230 "<a HREF='" 231 + VIEW_JNDI 232 + "?ctx=" 233 + ctxID 234 + "'>Browse this EJB's private JNDI namespace</a>"; 235 printRow(pepperImg, jndiURL, out); 236 out.print("</table>"); 237 238 } 239 240 protected void printRow(String col1, String col2, PrintWriter out) 241 throws IOException { 242 out.print("<tr><td><font size='2'>"); 243 out.print(col1); 244 out.print("</font></td><td><font size='2'>"); 245 out.print(col2); 246 out.print("</font></td></tr>"); 247 } 248 249 public String getClassRef(Class clazz) throws Exception { 250 String name = clazz.getName(); 251 return "<a HREF='" 252 + VIEW_CLASS 253 + "?class=" 254 + name 255 + "'>" 256 + name 257 + "</a>"; 258 } 259 260 public String getShortClassRef(Class clazz) throws Exception { 261 if (clazz.isPrimitive()) { 262 return "<font color='gray'>" + clazz.getName() + "</font>"; 263 } else if (clazz.isArray() && clazz.getComponentType().isPrimitive()) { 264 return "<font color='gray'>" 265 + clazz.getComponentType() 266 + "[]</font>"; 267 } else if (clazz.isArray()) { 268 String name = clazz.getComponentType().getName(); 269 int dot = name.lastIndexOf(".") + 1; 270 String shortName = name.substring(dot, name.length()); 271 return "<a HREF='" 272 + VIEW_CLASS 273 + "?class=" 274 + name 275 + "'>" 276 + shortName 277 + "[]</a>"; 278 } else { 279 String name = clazz.getName(); 280 int dot = name.lastIndexOf(".") + 1; 281 String shortName = name.substring(dot, name.length()); 282 return "<a HREF='" 283 + VIEW_CLASS 284 + "?class=" 285 + name 286 + "'>" 287 + shortName 288 + "</a>"; 289 } 290 } 291 } 292 | Popular Tags |