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.servlet.jsp.JspException ; 24 25 import javax.servlet.jsp.tagext.TagSupport ; 26 27 28 29 import org.apache.log4j.Logger; 30 31 import org.apache.struts.util.RequestUtils; 32 33 import org.apache.struts.util.ResponseUtils; 34 35 import org.ejtools.beans.CustomPropertyEditorManager; 36 37 38 39 56 57 public class MBeanResultEditorTag extends TagSupport 58 59 { 60 61 62 63 protected boolean filter = true; 64 65 66 67 protected boolean ignore = false; 68 69 70 71 protected String name = null; 72 73 74 75 protected String page = "/detail.do"; 76 77 78 79 protected String scope = null; 80 81 82 83 private static Logger logger = Logger.getLogger(MBeanAttributeEditorTag.class); 84 85 86 87 88 89 100 101 public int doStartTag() 102 103 throws JspException 104 105 { 106 107 109 Object result = RequestUtils.lookup(pageContext, name, scope); 110 111 112 113 if (ignore) 114 115 { 116 117 if (result == null) 118 119 { 120 121 return (SKIP_BODY); 122 123 } 124 125 127 } 128 129 130 131 PropertyEditor propertyeditor = null; 132 133 Class c = result.getClass(); 134 135 136 137 if (c.isArray()) 138 139 { 140 141 Class componentType = c.getComponentType(); 142 143 propertyeditor = CustomPropertyEditorManager.findEditor(componentType); 144 145 146 147 if (propertyeditor == null) 148 149 { 150 151 propertyeditor = CustomPropertyEditorManager.findEditor(String .class); 152 153 } 154 155 addArrayResult(result, propertyeditor, componentType); 156 157 } 158 159 else 160 161 { 162 163 propertyeditor = CustomPropertyEditorManager.findEditor(c); 164 165 166 167 if (propertyeditor == null) 168 169 { 170 171 propertyeditor = CustomPropertyEditorManager.findEditor(String .class); 172 173 } 174 175 addResult(result, propertyeditor); 176 177 } 178 179 180 181 183 return (SKIP_BODY); 184 185 } 186 187 188 189 190 191 202 203 public boolean getFilter() 204 205 { 206 207 return (this.filter); 208 209 } 210 211 212 213 214 215 226 227 public boolean getIgnore() 228 229 { 230 231 return (this.ignore); 232 233 } 234 235 236 237 238 239 250 251 public String getName() 252 253 { 254 255 return (this.name); 256 257 } 258 259 260 261 262 263 274 275 public String getScope() 276 277 { 278 279 return (this.scope); 280 281 } 282 283 284 285 286 287 288 289 public void release() 290 291 { 292 293 super.release(); 294 295 filter = true; 296 297 ignore = false; 298 299 name = null; 300 301 scope = null; 302 303 } 304 305 306 307 308 309 318 319 public void setFilter(boolean filter) 320 321 { 322 323 this.filter = filter; 324 325 } 326 327 328 329 330 331 340 341 public void setIgnore(boolean ignore) 342 343 { 344 345 this.ignore = ignore; 346 347 } 348 349 350 351 352 353 362 363 public void setName(String name) 364 365 { 366 367 this.name = name; 368 369 } 370 371 372 373 374 375 384 385 public void setScope(String scope) 386 387 { 388 389 this.scope = scope; 390 391 } 392 393 394 395 396 397 412 413 protected void addArrayResult(Object result, PropertyEditor propertyeditor, Class componentType) 414 415 throws JspException 416 417 { 418 419 StringBuffer buffer = new StringBuffer (); 420 421 Object [] array = null; 422 423 424 425 logger.debug("addArrayResult " + propertyeditor + " " + result.getClass().getName()); 426 427 428 429 try 430 431 { 432 433 array = (Object []) result; 434 435 } 436 437 catch (Throwable t) 438 439 { 440 441 logger.warn("Exception during result array reading " + t.getMessage()); 442 443 } 444 445 446 447 if (array != null) 448 449 { 450 451 String output = ""; 452 453 454 455 for (int i = 0; i < array.length; i++) 456 457 { 458 459 try 460 461 { 462 463 PropertyEditor propertyeditor1 = (PropertyEditor ) propertyeditor.getClass().newInstance(); 464 465 propertyeditor1.setValue(array[i]); 466 467 468 469 output = propertyeditor1.getAsText(); 470 471 472 473 if (filter) 474 475 { 476 477 output = ResponseUtils.filter(output); 478 479 } 480 481 482 483 output = computeReadableEditor(result.getClass().getName(), output); 484 485 486 487 buffer.append(output); 488 489 buffer.append("<br/>"); 490 491 } 492 493 catch (Exception e) 494 495 { 496 497 logger.warn("Exception during array output " + e.getMessage()); 498 499 } 500 501 } 502 503 ResponseUtils.write(pageContext, buffer.toString()); 504 505 } 506 507 } 508 509 510 511 512 513 526 527 protected void addResult(Object result, PropertyEditor propertyeditor) 528 529 throws JspException 530 531 { 532 533 String output = ""; 534 535 536 537 logger.debug("addArrayResult " + propertyeditor + " " + result.getClass().getName()); 538 539 540 541 try 542 543 { 544 545 propertyeditor.setValue(result); 546 547 } 548 549 catch (Throwable t) 550 551 { 552 553 logger.warn("Exception during result reading " + t.getMessage()); 554 555 } 556 557 558 559 output = propertyeditor.getAsText(); 560 561 562 563 if (filter) 564 565 { 566 567 output = ResponseUtils.filter(output); 568 569 } 570 571 572 573 output = computeReadableEditor(result.getClass().getName(), output); 574 575 576 577 ResponseUtils.write(pageContext, output); 578 579 } 580 581 582 583 584 585 598 599 protected String computeReadableEditor(String type, String value) 600 601 { 602 603 if ((value == null) || ("".equals(value)) || ("null".equals(value))) 604 605 { 606 607 return value; 608 609 } 610 611 612 613 if ("javax.management.ObjectName".equals(type)) 614 615 { 616 617 try 618 619 { 620 621 Hashtable params = new Hashtable (); 622 623 params.put("reference", value); 624 625 626 627 String url = RequestUtils.computeURL(pageContext, null, null, page, params, null, false); 628 629 630 631 return ("<a HREF=\"" + url + "\">" + value + "</a>"); 632 633 } 634 635 catch (Exception e) 636 637 { 638 639 logger.warn("Exception during computation of readable editor " + e.getMessage()); 640 641 } 642 643 } 644 645 646 647 return value; 648 649 } 650 651 } 652 653 | Popular Tags |