1 12 13 package org.ejtools.jmx.browser.web.taglib; 14 15 16 17 import java.beans.PropertyEditor ; 18 19 import java.util.Hashtable ; 20 21 22 23 import javax.management.MBeanAttributeInfo ; 24 25 import javax.servlet.jsp.JspException ; 26 27 import javax.servlet.jsp.tagext.TagSupport ; 28 29 30 31 import org.apache.log4j.Logger; 32 33 import org.apache.struts.util.MessageResources; 34 35 import org.apache.struts.util.RequestUtils; 36 37 import org.apache.struts.util.ResponseUtils; 38 39 import org.ejtools.beans.CustomPropertyEditorManager; 40 41 import org.ejtools.jmx.MBeanAccessor; 42 43 import org.ejtools.util.ClassTools; 44 45 46 47 64 65 public class MBeanAttributeEditorTag extends TagSupport 66 67 { 68 69 70 71 protected boolean filter = true; 72 73 74 75 protected boolean ignore = false; 76 77 78 79 protected String name = null; 80 81 82 83 protected String page = "/detail.do"; 84 85 86 87 protected String property = null; 88 89 90 91 protected String scope = null; 92 93 94 95 private static Logger logger = Logger.getLogger(MBeanAttributeEditorTag.class); 96 97 98 99 private static MessageResources messages = MessageResources.getMessageResources("org.ejtools.jmx.browser.web.taglib.MBeanAttributeInfoEditor"); 100 101 102 103 104 105 106 107 118 119 public int doStartTag() 120 121 throws JspException 122 123 { 124 125 127 MBeanAccessor bean = (MBeanAccessor) RequestUtils.lookup(pageContext, name, scope); 128 129 if (ignore) 130 131 { 132 133 if (bean == null) 134 135 { 136 137 return (SKIP_BODY); 138 139 } 140 141 143 } 144 145 146 147 MBeanAttributeInfo info = (MBeanAttributeInfo ) RequestUtils.lookup(pageContext, property, scope); 148 149 150 151 PropertyEditor propertyeditor = null; 152 153 Class c = ClassTools.getClass(info.getType()); 154 155 156 157 logger.debug("Class is " + c); 158 159 160 161 if (c == null) 162 163 { 164 165 this.addUnsupportedProperty(info); 166 167 } 168 169 else 170 171 { 172 173 if (c.isArray()) 174 175 { 176 177 Class componentType = c.getComponentType(); 178 179 propertyeditor = CustomPropertyEditorManager.findEditor(componentType); 180 181 182 183 if (propertyeditor == null) 184 185 { 186 187 propertyeditor = CustomPropertyEditorManager.findEditor(String .class); 188 189 } 190 191 this.addArrayProperty(bean, propertyeditor, info, componentType); 192 193 } 194 195 else 196 197 { 198 199 propertyeditor = CustomPropertyEditorManager.findEditor(c); 200 201 202 203 if (propertyeditor == null) 204 205 { 206 207 propertyeditor = CustomPropertyEditorManager.findEditor(String .class); 208 209 } 210 211 this.addProperty(bean, propertyeditor, info); 212 213 } 214 215 } 216 217 218 219 221 return (SKIP_BODY); 222 223 } 224 225 226 227 228 229 240 241 public boolean getFilter() 242 243 { 244 245 return (this.filter); 246 247 } 248 249 250 251 252 253 264 265 public boolean getIgnore() 266 267 { 268 269 return (this.ignore); 270 271 } 272 273 274 275 276 277 288 289 public String getName() 290 291 { 292 293 return (this.name); 294 295 } 296 297 298 299 300 301 312 313 public String getProperty() 314 315 { 316 317 return (this.property); 318 319 } 320 321 322 323 324 325 336 337 public String getScope() 338 339 { 340 341 return (this.scope); 342 343 } 344 345 346 347 348 349 350 351 public void release() 352 353 { 354 355 super.release(); 356 357 filter = true; 358 359 ignore = false; 360 361 name = null; 362 363 scope = null; 364 365 property = null; 366 367 } 368 369 370 371 372 373 382 383 public void setFilter(boolean filter) 384 385 { 386 387 this.filter = filter; 388 389 } 390 391 392 393 394 395 404 405 public void setIgnore(boolean ignore) 406 407 { 408 409 this.ignore = ignore; 410 411 } 412 413 414 415 416 417 426 427 public void setName(String name) 428 429 { 430 431 this.name = name; 432 433 } 434 435 436 437 438 439 448 449 public void setProperty(String property) 450 451 { 452 453 this.property = property; 454 455 } 456 457 458 459 460 461 470 471 public void setScope(String scope) 472 473 { 474 475 this.scope = scope; 476 477 } 478 479 480 481 482 483 500 501 protected void addArrayProperty(MBeanAccessor object, PropertyEditor propertyeditor, MBeanAttributeInfo info, Class componentType) 502 503 throws JspException 504 505 { 506 507 StringBuffer buffer = new StringBuffer (); 508 509 Object obj = null; 510 511 Object [] array = null; 512 513 514 515 logger.debug("addArrayProperty " + propertyeditor + " " + info.getName()); 516 517 518 519 try 520 521 { 522 523 if (info.isReadable()) 524 525 { 526 527 obj = object.getAttribute(info.getName()); 528 529 } 530 531 if (obj != null) 532 533 { 534 535 array = (Object []) obj; 536 537 } 538 539 } 540 541 catch (Throwable t) 542 543 { 544 545 logger.warn("Exception during array reading " + t.getMessage()); 546 547 } 548 549 550 551 if (array != null) 552 553 { 554 555 String output = ""; 556 557 558 559 for (int i = 0; i < array.length; i++) 560 561 { 562 563 try 564 565 { 566 567 PropertyEditor propertyeditor1 = (PropertyEditor ) propertyeditor.getClass().newInstance(); 568 569 propertyeditor1.setValue(array[i]); 570 571 572 573 output = propertyeditor1.getAsText(); 574 575 576 577 if (filter) 578 579 { 580 581 output = ResponseUtils.filter(output); 582 583 } 584 585 586 587 output = computeReadableEditor(componentType.getName(), output); 588 589 590 591 buffer.append(output); 592 593 buffer.append("<br/>"); 594 595 } 596 597 catch (Exception e) 598 599 { 600 601 logger.warn("Exception during array output " + e.getMessage()); 602 603 } 604 605 } 606 607 ResponseUtils.write(pageContext, buffer.toString()); 608 609 } 610 611 } 612 613 614 615 616 617 632 633 protected void addProperty(MBeanAccessor object, PropertyEditor propertyeditor, MBeanAttributeInfo info) 634 635 throws JspException 636 637 { 638 639 StringBuffer buffer = new StringBuffer (); 640 641 Object value = null; 642 643 String output = ""; 644 645 646 647 logger.debug("addProperty " + propertyeditor + " " + info.getName()); 648 649 650 651 try 652 653 { 654 655 if (info.isReadable()) 656 657 { 658 659 value = object.getAttribute(info.getName()); 660 661 propertyeditor.setValue(value); 662 663 } 664 665 } 666 667 catch (Throwable t) 668 669 { 670 671 logger.warn("Exception during property reading " + t.getMessage()); 672 673 } 674 675 676 677 if (info.isWritable()) 678 679 { 680 681 buffer.append("<input type=\"hidden\" name=\":attribute:"); 682 683 buffer.append(info.getName()); 684 685 buffer.append("\" value=\""); 686 687 buffer.append(info.getType()); 688 689 buffer.append("\"/>"); 690 691 692 693 buffer.append("<input type=\"hidden\" name=\":editor:"); 694 695 buffer.append(info.getName()); 696 697 buffer.append("\" value=\""); 698 699 buffer.append(propertyeditor.getClass().getName()); 700 701 buffer.append("\"/>"); 702 703 704 705 buffer.append("<input type=\"hidden\" name=\":previous:"); 706 707 buffer.append(info.getName()); 708 709 buffer.append("\" value=\""); 710 711 712 713 if (value != null) 714 715 { 716 717 buffer.append(value); 718 719 } 720 721 buffer.append("\"/>"); 722 723 724 725 727 if (("java.lang.Boolean".equals(info.getType())) || ("boolean".equals(info.getType()))) 728 729 { 730 731 buffer.append("<select name=\":value:"); 732 733 buffer.append(info.getName()); 734 735 buffer.append("\">"); 736 737 738 739 buffer.append("<option value=\"true\" "); 740 741 if (Boolean.TRUE.equals(value)) 742 743 { 744 745 buffer.append("selected"); 746 747 } 748 749 buffer.append(">"); 750 751 buffer.append(messages.getMessage("boolean.true")); 752 753 buffer.append("</option>"); 754 755 756 757 buffer.append("<option value=\"false\" "); 758 759 if (Boolean.FALSE.equals(value)) 760 761 { 762 763 buffer.append("selected"); 764 765 } 766 767 buffer.append(">"); 768 769 buffer.append(messages.getMessage("boolean.false")); 770 771 buffer.append("</option>"); 772 773 774 775 buffer.append("</select>"); 776 777 } 778 779 781 else 782 783 { 784 785 String content = ""; 786 787 String link = ""; 788 789 790 791 buffer.append("<input type=\"text\" name=\":value:"); 792 793 buffer.append(info.getName()); 794 795 buffer.append("\" value=\""); 796 797 798 799 if (value != null) 800 801 { 802 803 content = propertyeditor.getAsText(); 804 805 806 807 if (filter) 808 809 { 810 811 content = ResponseUtils.filter(content); 812 813 } 814 815 } 816 817 818 819 buffer.append(content); 820 821 buffer.append("\"/>"); 822 823 824 825 link = computeWritableEditor(info.getType(), content); 826 827 828 829 buffer.append(link); 830 831 ; 832 833 } 834 835 } 836 837 else 838 839 { 840 841 output = propertyeditor.getAsText(); 842 843 844 845 if (filter) 846 847 { 848 849 output = ResponseUtils.filter(output); 850 851 } 852 853 854 855 buffer.append(computeReadableEditor(info.getType(), output)); 856 857 } 858 859 860 861 ResponseUtils.write(pageContext, buffer.toString()); 862 863 } 864 865 866 867 868 869 882 883 protected void addUnsupportedProperty(MBeanAttributeInfo info) 884 885 throws JspException 886 887 { 888 889 String output = "Unsupported Class"; 890 891 892 893 if (filter) 894 895 { 896 897 output = "<i>" + ResponseUtils.filter(output) + "</i>"; 898 899 } 900 901 else 902 903 { 904 905 output = "<i>" + output + "</i>"; 906 907 } 908 909 910 911 ResponseUtils.write(pageContext, output); 912 913 } 914 915 916 917 918 919 932 933 protected String computeReadableEditor(String type, String value) 934 935 { 936 937 if ((value == null) || ("".equals(value)) || ("null".equals(value))) 938 939 { 940 941 return value; 942 943 } 944 945 946 947 if ("javax.management.ObjectName".equals(type)) 948 949 { 950 951 try 952 953 { 954 955 Hashtable params = new Hashtable (); 956 957 params.put("reference", value); 958 959 960 961 String url = RequestUtils.computeURL(pageContext, null, null, page, params, null, false); 962 963 964 965 return ("<a HREF=\"" + url + "\">" + value + "</a>"); 966 967 } 968 969 catch (Exception e) 970 971 { 972 973 logger.warn("Exception during computation of readable editor " + e.getMessage()); 974 975 } 976 977 } 978 979 980 981 return value; 982 983 } 984 985 986 987 988 989 1002 1003 protected String computeWritableEditor(String type, String value) 1004 1005 { 1006 1007 if ((value == null) || ("".equals(value)) || ("null".equals(value))) 1008 1009 { 1010 1011 return ""; 1012 1013 } 1014 1015 1016 1017 if ("javax.management.ObjectName".equals(type)) 1018 1019 { 1020 1021 try 1022 1023 { 1024 1025 Hashtable params = new Hashtable (); 1026 1027 params.put("reference", value); 1028 1029 1030 1031 String url = RequestUtils.computeURL(pageContext, null, null, page, params, null, false); 1032 1033 1034 1035 return (" <a HREF=\"" + url + "\">" + messages.getMessage("hyperlink.go") + "</a>"); 1036 1037 } 1038 1039 catch (Exception e) 1040 1041 { 1042 1043 logger.warn("Exception during computation of writable editor " + e.getMessage()); 1044 1045 } 1046 1047 } 1048 1049 1050 1051 return ""; 1052 1053 } 1054 1055} 1056 1057 | Popular Tags |