1 45 package org.openejb.webadmin; 46 47 import java.io.IOException ; 48 import java.io.InputStream ; 49 import java.io.PrintWriter ; 50 import java.net.URL ; 51 import java.util.ArrayList ; 52 import java.util.HashMap ; 53 import java.util.Map ; 54 import java.util.Properties ; 55 56 import javax.ejb.CreateException ; 57 import javax.ejb.SessionContext ; 58 import javax.naming.Context ; 59 import javax.naming.NameClassPair ; 60 import javax.naming.NamingEnumeration ; 61 62 86 public abstract class WebAdminBean implements HttpBean { 87 89 protected SessionContext ejbContext; 90 92 public static final int SUBSTITUTE = 26; 93 95 public static HashMap sections; 96 98 protected String section = ""; 99 101 protected HttpRequest request; 102 104 protected HttpResponse response; 105 106 public static final String HTML_TITLE = "OpenEJB Web Administration Console"; 107 108 109 114 public void onMessage(HttpRequest request, HttpResponse response) throws IOException { 115 this.request = request; 116 this.response = response; 117 118 preProcess(request, response); 119 120 java.io.PrintWriter body = response.getPrintWriter(); 122 InputStream template = getTemplate(); 123 124 writeTemplate(body, template); 126 writeHtmlTitle(body); 127 128 writeTemplate(body, template); 130 writeTopNavBar(body); 131 132 writeTemplate(body, template); 134 writeLeftNavBar(body); 135 136 writeTemplate(body, template); 138 writePageTitle(body); 139 140 writeTemplate(body, template); 142 writeBody(body); 143 144 writeTemplate(body, template); 146 writeFooter(body); 147 148 writeTemplate(body, template); 150 postProcess(request, response); 151 } 152 153 158 public abstract void preProcess(HttpRequest request, HttpResponse response) throws IOException ; 159 164 public abstract void postProcess(HttpRequest request, HttpResponse response) throws IOException ; 165 166 174 public abstract void writeHtmlTitle(PrintWriter body) throws IOException ; 175 176 182 public abstract void writePageTitle(PrintWriter body) throws IOException ; 183 184 205 public void writeTopNavBar(PrintWriter body) throws IOException { 206 } 217 218 248 public void writeLeftNavBar(PrintWriter body) throws IOException { 249 Object [] entries = sections.entrySet().toArray(); 250 251 for (int i = 0; i < entries.length; i++) { 252 Map.Entry entry = (Map.Entry )entries[i]; 253 String section = (String )entry.getKey(); 254 String [] subSections = (String [])entry.getValue(); 255 256 body.println("<tr><td valign=\"top\" align=\"left\">"); 257 body.print("<span class=\"subMenuOn\">"); 258 body.print(section); 259 body.print("</td></tr>"); 260 261 for (int j=0; j < subSections.length; j+=2){ 262 String name = subSections[j]; 263 String url = subSections[j+1]; 264 265 body.print("<tr>"); 266 body.print("<td valign=\"top\" align=\"left\">"); 267 body.print("<a HREF=\"/"); 268 body.print(section); 269 body.print('/'); 270 body.print(url); 271 body.print("\" class=\"subMenuOff\">"); 272 body.print(" "); 273 body.print(name); 274 body.print("</a></td></tr>"); 275 } 276 } 277 } 278 279 280 285 public String formatSubMenuItem(String itemName, String url){ 286 StringBuffer buff = new StringBuffer (); 287 288 return buff.toString(); 289 } 290 291 297 public abstract void writeBody(PrintWriter body) throws IOException ; 298 299 304 public void writeFooter(PrintWriter body) throws IOException { 305 body.print(footer); 306 } 307 308 310 protected static String footer = getFooter(); 311 312 315 public static String getFooter(){ 316 StringBuffer out = new StringBuffer (100); 317 try { 318 Properties openejbProps = new Properties (); 319 openejbProps.load( new URL ( "resource:/openejb-version.properties" ).openConnection().getInputStream() ); 320 out.append( "<a HREF=\""+openejbProps.get("url")+"\">OpenEJB</a> "); 321 out.append( openejbProps.get( "version" ) +"<br>"); 322 out.append( "build: "+openejbProps.get( "date" )+"-"+openejbProps.get( "time" )); 323 } catch (java.io.IOException e) { 324 } 325 326 return out.toString(); 327 } 328 329 334 public void writeTemplate(PrintWriter out, InputStream template) throws IOException { 335 int b = template.read(); 336 while (b != -1 && b != SUBSTITUTE) { 338 out.write( b ); 339 b = template.read(); 340 } 341 } 343 344 348 public InputStream getTemplate() throws IOException { 349 return new URL ( "resource:/htdocs/template.html" ).openConnection().getInputStream(); 351 } 352 353 355 public HashMap initNavSections(){ 356 HashMap sections = new HashMap (); 357 try{ 358 Context ctx = org.openejb.OpenEJB.getJNDIContext(); 359 ctx = (Context ) ctx.lookup("openejb/ejb"); 360 NamingEnumeration enumeration = ctx.list(""); 361 363 if ( enumeration == null){ 364 return sections; 365 } 366 367 while (enumeration.hasMore()) { 368 NameClassPair entry = (NameClassPair )enumeration.next(); 369 if ( !entry.getClassName().equals("org.openejb.core.ivm.naming.IvmContext") ) { 372 continue; 373 } 374 375 Context subCtx = (Context ) ctx.lookup(entry.getName()); 376 String [] subSections = getSubsections(subCtx); 377 if (subSections.length > 0){ 378 sections.put(entry.getName(), subSections ); 379 } 380 } 381 } catch (Exception e){ 382 e.printStackTrace(); 383 } 384 return sections; 385 } 386 387 private String [] getSubsections(Context ctx){ 388 ArrayList sections = new ArrayList (); 389 try{ 390 NamingEnumeration enumeration = ctx.list(""); 391 392 if ( enumeration == null){ 393 return new String [0]; 394 } 395 396 while (enumeration.hasMore()) { 397 NameClassPair entry = (NameClassPair )enumeration.next(); 398 if ( !entry.getClassName().equals("org.openejb.core.stateless.EncReference") ) { 401 continue; 402 } 403 404 if ( entry.getName().startsWith("Default") ) { 405 continue; 406 } 407 408 Object obj = ctx.lookup(entry.getName()); 409 if (obj instanceof HttpHome){ 410 String beanName = entry.getName(); 411 sections.add(beanName); 412 sections.add(beanName); 413 } 414 } 415 } catch (Exception e){ 416 e.printStackTrace(); 417 } 418 return (String []) sections.toArray(new String [0]); 419 } 420 431 protected void printRow(String col1, String col2, PrintWriter out) throws IOException { 432 out.println("<tr>" ); 433 out.print("<td class=\"bodyBlack\">"); 434 out.print(col1); 435 out.println("</td>"); 436 out.print("<td class=\"bodyBlack\">"); 437 out.print(col2); 438 out.println("</td>"); 439 out.println("</tr>"); 440 } 441 442 455 protected void printRow(String col1, String col2, String col3, PrintWriter out) throws IOException { 456 out.println("<tr>"); 457 out.print("<td class=\"bodyBlack\">"); 458 out.print(col1); 459 out.println("</td>"); 460 out.print("<td class=\"bodyBlack\">"); 461 out.print(col2); 462 out.println("</td>"); 463 out.print("<td class=\"bodyBlack\">"); 464 out.print(col3); 465 out.println("</td>"); 466 out.println("</tr>"); 467 } 468 469 470 471 472 475 public void ejbCreate() throws CreateException {} 476 477 480 public void ejbActivate() {} 481 482 486 public void ejbPassivate() {} 487 488 490 public void ejbRemove() {} 491 492 495 public void setSessionContext(SessionContext sessionContext) { 496 ejbContext = sessionContext; 497 if (sections == null) { 498 sections = initNavSections(); 499 } 500 } 501 } 502 | Popular Tags |