1 22 package org.jboss.naming; 23 24 import java.io.PrintWriter ; 25 import java.io.StringWriter ; 26 import java.lang.reflect.Proxy ; 27 import java.util.Collection ; 28 import java.util.Iterator ; 29 import java.util.Set ; 30 31 import javax.management.Attribute ; 32 import javax.management.AttributeList ; 33 import javax.management.InstanceNotFoundException ; 34 import javax.management.MBeanServer ; 35 import javax.management.ObjectName ; 36 import javax.naming.Context ; 37 import javax.naming.InitialContext ; 38 import javax.naming.LinkRef ; 39 import javax.naming.NameClassPair ; 40 import javax.naming.NamingEnumeration ; 41 import javax.naming.NamingException ; 42 43 import org.jboss.ejb.Container; 44 import org.jboss.ejb.EjbModule; 45 import org.jboss.system.ServiceMBeanSupport; 46 import org.jboss.web.AbstractWebDeployerMBean; 47 import org.jboss.web.WebApplication; 48 49 61 public class JNDIView 62 extends ServiceMBeanSupport 63 implements JNDIViewMBean 64 { 65 66 protected static final String [] g_haAttributes = new String []{"BindAddress", "Port"}; 67 68 69 protected String haNamingService; 70 71 74 public JNDIView() 75 { 76 } 77 78 86 public String list(boolean verbose) 87 { 88 StringBuffer buffer = new StringBuffer (4096); 89 Context context = null; 90 ClassLoader currentLoader = Thread.currentThread().getContextClassLoader(); 91 92 try 93 { 94 Iterator it = (Iterator ) server.getAttribute(AbstractWebDeployerMBean.OBJECT_NAME, "DeployedApplications"); 97 98 if (it.hasNext() == true) 99 { 100 buffer.append("<h1>Web Applications</h1>\n"); 101 } 102 103 while (it.hasNext() == true) 104 { 105 WebApplication webApplication = (WebApplication) it.next(); 106 107 Thread.currentThread().setContextClassLoader(webApplication.getMetaData().getENCLoader()); 108 109 buffer.append("<h2>java:comp namespace of the " + webApplication.getCanonicalName() 110 + " application:</h2>\n"); 111 112 try 113 { 114 context = new InitialContext (); 115 context = (Context ) context.lookup("java:comp"); 116 } 117 catch (NamingException e) 118 { 119 buffer.append("Failed on lookup, " + e.toString(true)); 120 formatException(buffer, e); 121 continue; 122 } 123 buffer.append("<pre>\n"); 124 list(context, " ", buffer, verbose); 125 buffer.append("</pre>\n"); 126 } 127 128 } 129 catch (Throwable e) 130 { 131 log.debug("Unable to list web applications ENC", e); 132 } 133 134 Set ejbModules = null; 137 try 138 { 139 ejbModules = server.queryNames(EjbModule.EJB_MODULE_QUERY_NAME, null); 140 } 141 catch (Throwable e) 142 { 143 log.error("getDeployedApplications failed", e); 144 buffer.append("Failed to getDeployedApplications\n"); 145 formatException(buffer, e); 146 buffer.insert(0, "<pre>"); 147 buffer.append("</pre>"); 148 return buffer.toString(); 149 } 150 151 for (Iterator i = ejbModules.iterator(); i.hasNext();) 153 { 154 ObjectName app = (ObjectName ) i.next(); 155 String module = app.getKeyProperty("module"); 156 if( module == null ) 157 module = app.toString(); 158 buffer.append("<h1>Ejb Module: " + module + "</h1>\n"); 159 try 160 { 161 Collection containers = (Collection ) server.getAttribute(app, "Containers"); 162 for (Iterator iter = containers.iterator(); iter.hasNext();) 163 { 164 Container con = (Container) iter.next(); 165 169 Thread.currentThread().setContextClassLoader(con.getClassLoader()); 170 String bean = con.getBeanMetaData().getEjbName(); 171 buffer.append("<h2>java:comp namespace of the " + bean + " bean:</h2>\n"); 172 173 try 174 { 175 context = new InitialContext (); 176 context = (Context ) context.lookup("java:comp"); 177 } 178 catch (NamingException e) 179 { 180 buffer.append("Failed on lookup, " + e.toString(true)); 181 formatException(buffer, e); 182 continue; 183 } 184 buffer.append("<pre>\n"); 185 list(context, " ", buffer, verbose); 186 buffer.append("</pre>\n"); 187 } 188 } 189 catch (Throwable e) 190 { 191 log.error("getConainers failed", e); 192 buffer.append("<pre>"); 193 buffer.append("Failed to get ejbs in module\n"); 194 formatException(buffer, e); 195 buffer.append("</pre>"); 196 } 197 } 198 199 Thread.currentThread().setContextClassLoader(currentLoader); 201 try 202 { 203 context = new InitialContext (); 204 context = (Context ) context.lookup("java:"); 205 buffer.append("<h1>java: Namespace</h1>\n"); 206 buffer.append("<pre>\n"); 207 list(context, " ", buffer, verbose); 208 buffer.append("</pre>\n"); 209 } 210 catch (NamingException e) 211 { 212 log.error("lookup for java: failed", e); 213 buffer.append("Failed to get InitialContext, " + e.toString(true)); 214 formatException(buffer, e); 215 } 216 217 try 219 { 220 context = new InitialContext (); 221 buffer.append("<h1>Global JNDI Namespace</h1>\n"); 222 buffer.append("<pre>\n"); 223 list(context, " ", buffer, verbose); 224 buffer.append("</pre>\n"); 225 } 226 catch (NamingException e) 227 { 228 log.error("Failed to get InitialContext", e); 229 buffer.append("Failed to get InitialContext, " + e.toString(true)); 230 formatException(buffer, e); 231 } 232 233 try 235 { 236 String url = getHAUrl(); 237 if (url != null) 238 { 239 java.util.Hashtable env = new java.util.Hashtable (); 240 env.put(Context.PROVIDER_URL, url); 241 context = new InitialContext (env); 242 buffer.append("<h1>HA-JNDI Namespace</h1>\n"); 243 buffer.append("<pre>\n"); 244 list(context, " ", buffer, verbose); 245 buffer.append("</pre>\n"); 246 } 247 } 248 catch (NamingException ne) 249 { 250 log.error("Failed to get InitialContext", ne); 251 buffer.append("Failed to get InitialContext, " + ne.toString(true)); 252 formatException(buffer, ne); 253 } 254 return buffer.toString(); 255 } 256 257 266 public String listXML() 267 { 268 StringBuffer buffer = new StringBuffer (4096); 269 Set ejbModules = null; 270 Context context = null; 271 ClassLoader currentLoader = Thread.currentThread().getContextClassLoader(); 272 273 openJndiTag(buffer); 274 try 275 { 276 Iterator it = (Iterator ) server.getAttribute(AbstractWebDeployerMBean.OBJECT_NAME, "DeployedApplications"); 279 280 while ( it.hasNext()==true ) 281 { 282 WebApplication webApplication = (WebApplication) it.next(); 283 openWebModuleTag(buffer, webApplication.getCanonicalName()); 284 285 Thread.currentThread().setContextClassLoader(webApplication.getMetaData().getENCLoader()); 286 287 try 288 { 289 context = new InitialContext (); 290 context = (Context ) context.lookup("java:comp"); 291 292 listXML(context, buffer); 293 } 294 catch (NamingException e) 295 { 296 buffer.append("Failed on lookup, " + e.toString(true)); 297 formatException(buffer, e); 298 continue; 299 } 300 finally 301 { 302 closeWebModuleTag(buffer); 303 } 304 } 305 } 306 catch (Throwable e) 307 { 308 log.debug("Unable to list web applications ENC", e); 309 } 310 311 314 try 315 { 316 ejbModules = server.queryNames(EjbModule.EJB_MODULE_QUERY_NAME, null); 317 } 318 catch (Exception e) 319 { 320 log.error("getDeployedApplications failed", e); 321 appendErrorTag(buffer, 322 "Failed to getDeployedApplications " + e.toString()); 323 closeJndiTag(buffer); 324 return buffer.toString(); 325 } 326 327 for (Iterator i = ejbModules.iterator(); i.hasNext();) 329 { 330 ObjectName app = (ObjectName ) i.next(); 331 openEjbModuleTag(buffer, app.getKeyProperty("url")); 332 333 listModuleContainers(buffer, app); 334 335 closeEjbModuleTag(buffer); 336 } 337 338 Thread.currentThread().setContextClassLoader(currentLoader); 340 try 341 { 342 context = new InitialContext (); 343 context = (Context ) context.lookup("java:"); 344 } 345 catch (NamingException e) 346 { 347 log.error("Failed to get InitialContext for (java:)", e); 348 appendErrorTag(buffer, 349 "Failed to get InitialContext for (java:), " + 350 e.toString(true)); 351 } 352 353 if (context != null) 354 { 355 openContextTag(buffer); 356 appendJavaNameTag(buffer); 357 try 358 { 359 listXML(context, buffer); 360 } 361 catch (Throwable t) 362 { 363 log.error("Failed to list contents of (java:)", t); 364 appendErrorTag(buffer, 365 "Failed to list contents of (java:), " + 366 t.toString()); 367 } 368 closeContextTag(buffer); 369 370 } 372 try 374 { 375 context = new InitialContext (); 376 } 377 catch (NamingException e) 378 { 379 log.error("Failed to get InitialContext", e); 380 appendErrorTag(buffer, 381 "Failed to get InitialContext, " + e.toString(true)); 382 } 383 384 if (context != null) 385 { 386 openContextTag(buffer); 387 appendGlobalNameTag(buffer); 388 try 389 { 390 listXML(context, buffer); 391 } 392 catch (Throwable t) 393 { 394 log.error("Failed to list global contents ", t); 395 appendErrorTag(buffer, 396 "Failed to list global contents, " + t.toString()); 397 } 398 closeContextTag(buffer); 399 400 } 402 String url = null; 404 try 405 { 406 url = getHAUrl(); 407 if (url != null) 408 { 409 java.util.Hashtable env = new java.util.Hashtable (); 410 env.put(Context.PROVIDER_URL, url); 411 context = new InitialContext (env); 412 } 413 } 414 catch (NamingException e) 415 { 416 log.error("Failed to get InitialContext", e); 417 appendErrorTag(buffer, 418 "Failed to get InitialContext, " + e.toString(true)); 419 } 420 421 if (url != null && context != null) 422 { 423 openContextTag(buffer); 424 appendHANameTag(buffer); 425 try 426 { 427 listXML(context, buffer); 428 } 429 catch (Throwable t) 430 { 431 log.error("Failed to list HA-JNDI contents ", t); 432 appendErrorTag(buffer, 433 "Failed to list HA-JNDI contents, " + t.toString()); 434 } 435 closeContextTag(buffer); 436 437 } 439 closeJndiTag(buffer); 440 441 return buffer.toString(); 442 } 443 444 public String getHANamingService() 445 { 446 return haNamingService; 447 } 448 public void setHANamingService(String serviceName) 449 { 450 haNamingService = serviceName; 451 } 452 453 protected ObjectName getObjectName(MBeanServer server, ObjectName name) 454 throws javax.management.MalformedObjectNameException 455 { 456 return name == null ? OBJECT_NAME : name; 457 } 458 459 private void list(Context ctx, String indent, StringBuffer buffer, boolean verbose) 460 { 461 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 462 try 463 { 464 NamingEnumeration ne = ctx.list(""); 465 while (ne.hasMore()) 466 { 467 NameClassPair pair = (NameClassPair ) ne.next(); 468 log.trace("pair: " + pair); 469 470 String name = pair.getName(); 471 String className = pair.getClassName(); 472 boolean recursive = false; 473 boolean isLinkRef = false; 474 boolean isProxy = false; 475 Class c = null; 476 try 477 { 478 c = loader.loadClass(className); 479 log.trace("type: " + c); 480 481 if (Context .class.isAssignableFrom(c)) 482 recursive = true; 483 if (LinkRef .class.isAssignableFrom(c)) 484 isLinkRef = true; 485 486 isProxy = Proxy.isProxyClass(c); 487 } 488 catch (ClassNotFoundException cnfe) 489 { 490 if (className.startsWith("$Proxy")) 492 { 493 isProxy = true; 494 try 496 { 497 Object p = ctx.lookup(name); 498 c = p.getClass(); 499 } 500 catch (NamingException e) 501 { 502 Throwable t = e.getRootCause(); 503 if (t instanceof ClassNotFoundException ) 504 { 505 String msg = t.getMessage(); 507 if (msg != null) 508 { 509 className = msg; 511 } 512 } 513 } 514 } 515 } 516 517 buffer.append(indent + " +- " + name); 518 519 if (isLinkRef) 521 { 522 try 524 { 525 log.trace("looking up LinkRef; name=" + name); 526 Object obj = ctx.lookupLink(name); 527 log.trace("Object type: " + obj.getClass()); 528 529 LinkRef link = (LinkRef ) obj; 530 buffer.append("[link -> "); 531 buffer.append(link.getLinkName()); 532 buffer.append(']'); 533 } 534 catch (Throwable t) 535 { 536 log.debug("Invalid LinkRef for: " + name, t); 537 buffer.append("invalid]"); 538 } 539 } 540 541 if (isProxy) 543 { 544 buffer.append(" (proxy: " + pair.getClassName()); 545 if (c != null) 546 { 547 Class [] ifaces = c.getInterfaces(); 548 buffer.append(" implements "); 549 for (int i = 0; i < ifaces.length; i++) 550 { 551 buffer.append(ifaces[i]); 552 buffer.append(','); 553 } 554 buffer.setCharAt(buffer.length() - 1, ')'); 555 } 556 else 557 { 558 buffer.append(" implements " + className + ")"); 559 } 560 } 561 else if (verbose) 562 { 563 buffer.append(" (class: " + pair.getClassName() + ")"); 564 } 565 566 buffer.append('\n'); 567 if (recursive) 568 { 569 try 570 { 571 Object value = ctx.lookup(name); 572 if (value instanceof Context ) 573 { 574 Context subctx = (Context ) value; 575 list(subctx, indent + " | ", buffer, verbose); 576 } 577 else 578 { 579 buffer.append(indent + " | NonContext: " + value); 580 buffer.append('\n'); 581 } 582 } 583 catch (Throwable t) 584 { 585 buffer.append("Failed to lookup: " + name + ", errmsg=" + t.getMessage()); 586 buffer.append('\n'); 587 } 588 } 589 } 590 ne.close(); 591 } 592 catch (NamingException ne) 593 { 594 buffer.append("error while listing context " + ctx.toString() + ": " + ne.toString(true)); 595 formatException(buffer, ne); 596 } 597 } 598 599 private void listXML(Context ctx, StringBuffer buffer) 600 { 601 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 602 try 603 { 604 NamingEnumeration ne = ctx.list(""); 605 while (ne.hasMore()) 606 { 607 NameClassPair pair = (NameClassPair ) ne.next(); 608 boolean recursive = false; 609 boolean isLinkRef = false; 610 try 611 { 612 Class c = loader.loadClass(pair.getClassName()); 613 if (Context .class.isAssignableFrom(c)) 614 recursive = true; 615 if (LinkRef .class.isAssignableFrom(c)) 616 isLinkRef = true; 617 } 618 catch (ClassNotFoundException cnfe) 619 { 620 } 621 622 String name = pair.getName(); 623 if (isLinkRef) 624 { 625 Object obj = null; 626 LinkRef link = null; 627 try 628 { 629 obj = ctx.lookupLink(name); 630 link = (LinkRef ) obj; 631 } 632 catch (Throwable t) 633 { 634 log.error("Invalid LinkRef for: " + name, t); 635 636 appendLinkRefErrorTag(buffer); 637 } 638 639 appendLinkRefTag(buffer, link, pair); 640 } 641 else 642 { 643 if (recursive) 644 { 645 Object value = null; 646 647 try 648 { 649 value = ctx.lookup(name); 650 } 651 catch (Throwable t) 652 { 653 appendErrorTag(buffer, 654 "Failed to lookup: " + name + 655 ", errmsg=" + t.getMessage()); 656 } 657 658 if (value instanceof Context ) 659 { 660 Context subctx = (Context ) value; 661 openContextTag(buffer); 662 appendNCPTag(buffer, pair); 663 664 try 665 { 666 listXML(subctx, buffer); 667 } 668 catch (Throwable t) 669 { 670 appendErrorTag(buffer, 671 "Failed to list contents of: " + name + 672 ", errmsg=" + t.getMessage()); 673 } 674 675 closeContextTag(buffer); 676 } 677 else 678 { 679 appendNonContextTag(buffer, pair); 680 } 681 } 682 else 683 { 684 appendLeafTag(buffer, pair); 685 } 686 } 687 } 688 ne.close(); 689 } 690 catch (NamingException ne) 691 { 692 appendErrorTag(buffer, 693 "error while listing context " + 694 ctx.toString() + ": " + ne.toString(true)); 695 } 696 } 697 698 private void listModuleContainers(StringBuffer buffer, ObjectName app) 699 { 700 Collection containers = null; 701 702 try 703 { 704 containers = (Collection ) server.getAttribute(app, "Containers"); 705 } 706 catch (Throwable t) 707 { 708 log.error("getContainers failed", t); 709 appendPreExceptionTag(buffer, "Failed to get ejbs in module", t); 710 } 711 712 for (Iterator iter = containers.iterator(); iter.hasNext();) 713 { 714 listContainerContext(buffer, (Container) iter.next()); 715 } 716 717 } 719 private void listContainerContext(StringBuffer buffer, Container con) 720 { 721 725 Thread.currentThread().setContextClassLoader(con.getClassLoader()); 726 String bean = con.getBeanMetaData().getEjbName(); 727 openContextTag(buffer); 728 appendBeanTag(buffer, bean); 729 Context context = null; 730 try 731 { 732 context = new InitialContext (); 733 context = (Context ) context.lookup("java:comp"); 734 } 735 catch (NamingException e) 736 { 737 appendErrorTag(buffer, 738 "Failed on lookup " + e.toString(true)); 739 context = null; 740 } 741 742 if (context != null) 743 { 744 try 745 { 746 listXML(context, buffer); 747 } 748 catch (Throwable t) 749 { 750 appendErrorTag(buffer, 751 "Failed on list contents, " + t.toString()); 752 } 753 } 755 closeContextTag(buffer); 756 757 } 759 private void openJndiTag(StringBuffer buffer) 760 { 761 buffer.append("<jndi>\n"); 762 } 763 764 private void closeJndiTag(StringBuffer buffer) 765 { 766 buffer.append("</jndi>\n"); 767 } 768 769 private void openWebModuleTag(StringBuffer buffer, String file) 770 { 771 buffer.append("<webmodule>\n"); 772 buffer.append("<file>").append(file).append("</file>\n"); 773 } 774 775 private void closeWebModuleTag(StringBuffer buffer) 776 { 777 buffer.append("</webmodule>\n"); 778 } 779 780 private void openEjbModuleTag(StringBuffer buffer, String file) 781 { 782 buffer.append("<ejbmodule>\n"); 783 buffer.append("<file>" + file + "</file>\n"); 784 } 785 786 private void closeEjbModuleTag(StringBuffer buffer) 787 { 788 buffer.append("</ejbmodule>\n"); 789 } 790 791 private void appendPreExceptionTag(StringBuffer buffer, 792 String msg, 793 Throwable t) 794 { 795 buffer.append("<pre>\n" + msg+"\n"); 796 formatException(buffer, t); 797 buffer.append("</pre>\n"); 798 } 799 800 private void appendBeanTag(StringBuffer buffer, String bean) 801 { 802 buffer.append("<name>java:comp</name>\n"); 803 buffer.append("<attribute name='bean'>" + bean + "</attribute>\n"); 804 } 805 806 private void appendJavaNameTag(StringBuffer buffer) 807 { 808 buffer.append("<name>java:</name>\n"); 809 } 810 811 private void appendGlobalNameTag(StringBuffer buffer) 812 { 813 buffer.append("<name>Global</name>\n"); 814 } 815 816 private void appendHANameTag(StringBuffer buffer) 817 { 818 buffer.append("<name>HA</name>\n"); 819 } 820 821 private void appendLinkRefTag(StringBuffer buffer, 822 LinkRef link, 823 NameClassPair ncp) 824 { 825 buffer.append("<link-ref>\n"); 826 buffer.append("<name>" + ncp.getName() + "</name>\n"); 827 try 828 { 829 String lName = link.getLinkName(); 830 buffer.append("<link>" + lName + "</link>\n"); 831 } 832 catch (NamingException e) 833 { 834 appendErrorTag(buffer, 835 "Failed to getLinkName, " + e.toString(true)); 836 } 837 buffer.append("<attribute name='class'>" + ncp.getClassName() + 838 "</attribute>\n"); 839 buffer.append("</link-ref>\n"); 840 } 841 842 private void appendLinkRefErrorTag(StringBuffer buffer) 843 { 844 buffer.append("<link-ref>\n"); 845 buffer.append("<name>Invalid</name>\n"); 846 buffer.append("</link-ref>\n"); 847 } 848 849 private void openContextTag(StringBuffer buffer) 850 { 851 buffer.append("<context>\n"); 852 } 853 854 private void closeContextTag(StringBuffer buffer) 855 { 856 buffer.append("</context>\n"); 857 } 858 859 private void appendNonContextTag(StringBuffer buffer, NameClassPair ncp) 860 { 861 buffer.append("<non-context>\n"); 862 appendNCPTag(buffer, ncp); 863 buffer.append("</non-context>\n"); 864 } 865 866 private void appendLeafTag(StringBuffer buffer, NameClassPair ncp) 867 { 868 buffer.append("<leaf>\n"); 869 appendNCPTag(buffer, ncp); 870 buffer.append("</leaf>\n"); 871 } 872 873 private void appendNCPTag(StringBuffer buffer, NameClassPair ncp) 874 { 875 buffer.append("<name>" + ncp.getName() + "</name>\n"); 876 buffer.append("<attribute name='class'>" + ncp.getClassName() + 877 "</attribute>\n"); 878 } 879 880 private void appendErrorTag(StringBuffer buffer, String msg) 881 { 882 buffer.append("<error>\n"); 883 buffer.append("<message>" + msg + "</message>\n"); 884 buffer.append("</error>\n"); 885 } 886 887 private void formatException(StringBuffer buffer, Throwable t) 888 { 889 StringWriter sw = new StringWriter (); 890 PrintWriter pw = new PrintWriter (sw); 891 buffer.append("<pre>\n"); 892 t.printStackTrace(pw); 893 buffer.append(sw.toString()); 894 buffer.append("</pre>\n"); 895 } 896 897 private String getHAUrl() 898 { 899 String bindAddress = null; 900 String portNumber = null; 901 902 AttributeList list = getHAJndiAttributes(); 903 if (list == null) 905 return null; 906 907 Object o = list.get(0); 909 if (o != null) 910 bindAddress = ((Attribute )o).getValue().toString(); 911 912 o = list.get(1); 914 if (o != null) 915 portNumber = ((Attribute )o).getValue().toString(); 916 917 if (bindAddress != null && portNumber != null) 918 return "jnp://"+bindAddress+":"+portNumber; 919 920 return null; 921 } 922 923 private AttributeList getHAJndiAttributes() 924 { 925 if (haNamingService == null) 926 { 927 return null; 929 } 930 931 try 932 { 933 ObjectName name = new ObjectName (haNamingService); 934 return server.getAttributes(name, g_haAttributes); 935 } 936 catch (InstanceNotFoundException e1) 937 { 938 return null; 940 } 941 catch (Exception e) 942 { 943 log.error("JNDIView.getHAJndiAttributes() failed", e); 945 return null; 946 } 947 } 948 } 949 | Popular Tags |