1 7 package org.ejtools.management.browser.model; 8 9 import java.awt.Component ; 10 import java.beans.beancontext.BeanContextServices ; 11 import java.io.PrintWriter ; 12 import java.io.StringWriter ; 13 import java.lang.reflect.Method ; 14 import java.util.ArrayList ; 15 import java.util.Hashtable ; 16 import java.util.Iterator ; 17 18 import javax.management.Attribute ; 19 import javax.management.AttributeList ; 20 import javax.management.MBeanAttributeInfo ; 21 import javax.management.MBeanInfo ; 22 import javax.management.ObjectName ; 23 import javax.management.j2ee.statistics.Statistic ; 24 import javax.management.j2ee.statistics.Stats ; 25 26 import org.apache.log4j.Logger; 27 import org.ejtools.adwt.service.ConsoleService; 28 import org.ejtools.graph.service.GraphConsumer; 29 import org.ejtools.graph.service.GraphConsumerMediator; 30 import org.ejtools.graph.service.GraphProducer; 31 import org.ejtools.graph.service.GraphService; 32 import org.ejtools.jmx.MBeanInvokeAccessor; 33 import org.ejtools.management.browser.model.service.CacheService; 34 import org.ejtools.management.browser.model.service.ConnectionService; 35 import org.ejtools.management.browser.model.service.NotificationService; 36 import org.ejtools.management.browser.ui.ManagedObjectTabViewer; 37 import org.ejtools.util.ClassTools; 38 39 61 public class ManagedObject extends Node implements MBeanInvokeAccessor, GraphConsumerMediator 62 { 63 64 protected String canonicalName; 65 66 protected String description; 67 68 protected String domain; 69 70 protected boolean eventProvider = false; 71 72 protected transient Hashtable graphProducers = new Hashtable (); 73 74 protected transient MBeanInfo info; 75 76 protected String j2eeType; 77 78 protected transient ObjectName objectName; 79 80 protected boolean stateManageable = false; 81 82 protected boolean statisticsProvider = false; 83 84 private static Logger logger = Logger.getLogger(ManagedObject.class); 85 86 87 88 public ManagedObject() 89 { 90 super(); 91 } 92 93 94 95 public void clear() 96 { 97 BeanContextServices context = (BeanContextServices ) getBeanContext(); 98 this.releaseServices(context); 99 100 super.clear(); 101 } 102 103 104 111 public Object getAttribute(String attribute) 112 throws Exception 113 { 114 BeanContextServices context = (BeanContextServices ) getBeanContext(); 115 Object result = null; 116 117 if (context.hasService(ConnectionService.class)) 118 { 119 try 120 { 121 ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this); 122 result = service.getMEJB().getAttribute(objectName, attribute); 123 context.releaseService(this, this, ConnectionService.class); 124 } 125 catch (Exception e) 126 { 127 logger.error("Error while getting attribute", e); 128 throw e; 129 } 130 } 131 return result; 132 } 133 134 135 140 public AttributeList getAttributes(String [] as) 141 throws Exception 142 { 143 return null; 144 } 145 146 147 152 public String getCanonicalName() 153 { 154 return this.canonicalName; 155 } 156 157 158 165 public Component getComponent() 166 { 167 if (c == null) 168 { 169 c = new ManagedObjectTabViewer(this); 170 } 171 return (Component ) c; 172 } 173 174 175 180 public String getDescription() 181 { 182 return this.description; 183 } 184 185 186 191 public String getDomain() 192 { 193 return this.domain; 194 } 195 196 197 202 public GraphConsumer[] getGraphConsumers() 203 { 204 BeanContextServices context = (BeanContextServices ) getBeanContext(); 205 GraphConsumer[] result = new GraphConsumer[0]; 206 207 if (context.hasService(GraphService.class)) 208 { 209 try 210 { 211 GraphService service = (GraphService) context.getService(this, this, GraphService.class, this, this); 212 result = service.getGraphConsumers(); 213 context.releaseService(this, this, GraphService.class); 214 } 215 catch (Exception e) 216 { 217 logger.error("Error while using GraphService", e); 218 } 219 } 220 return result; 221 } 222 223 224 229 public Hashtable getGraphProducers() 230 { 231 return this.graphProducers; 232 } 233 234 235 240 public String getJ2EEType() 241 { 242 return this.j2eeType; 243 } 244 245 246 251 public MBeanInfo getMBeanInfo() 252 { 253 if (this.info == null) 254 { 255 BeanContextServices context = (BeanContextServices ) getBeanContext(); 256 if (context.hasService(ConnectionService.class)) 257 { 258 try 259 { 260 ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this); 261 this.info = service.getMEJB().getMBeanInfo(this.objectName); 262 if (this.info == null) 263 { 264 logger.warn("MBeanInfo for " + this.objectName + " is null"); 265 throw new Exception ("MBeanInfo for " + this.objectName + " is null"); 266 } 267 268 MBeanAttributeInfo [] infos = this.info.getAttributes(); 270 for (int i = 0; i < infos.length; i++) 271 { 272 MBeanAttributeInfo attribute = infos[i]; 273 if ("StateManageable".equalsIgnoreCase(attribute.getName())) 274 { 275 Object o = this.getAttribute(attribute.getName()); 276 this.stateManageable = Boolean.valueOf("" + o).booleanValue(); 277 } 278 if ("EventProvider".equalsIgnoreCase(attribute.getName())) 279 { 280 Object o = this.getAttribute(attribute.getName()); 281 this.eventProvider = Boolean.valueOf("" + o).booleanValue(); 282 } 283 if ("StatisticsProvider".equalsIgnoreCase(attribute.getName())) 284 { 285 Object o = this.getAttribute(attribute.getName()); 286 this.statisticsProvider = Boolean.valueOf("" + o).booleanValue(); 287 } 288 } 289 290 context.releaseService(this, this, ConnectionService.class); 291 } 292 catch (Exception e) 293 { 294 logger.error("Error during utilisation of service ConnectionService", e); 295 } 296 } 297 } 298 return this.info; 299 } 300 301 302 307 public ObjectName getObjectName() 308 { 309 return this.objectName; 310 } 311 312 313 319 public Stats getStats() 320 throws Exception 321 { 322 MBeanAttributeInfo [] infos = this.filter("stats", null); 324 if (infos.length == 0) 325 { 326 return null; 327 } 328 Stats stats = (Stats ) this.getAttribute(infos[0].getName()); 329 return stats; 330 } 331 332 333 342 public Object invoke(String action, Object [] params, String [] signatures) 343 throws Exception 344 { 345 BeanContextServices context = (BeanContextServices ) getBeanContext(); 346 Object result = null; 347 348 if (context.hasService(ConnectionService.class)) 349 { 350 try 351 { 352 this.log("Invoking " + action + "..."); 353 354 ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this); 355 result = service.getMEJB().invoke(objectName, action, params, signatures); 356 context.releaseService(this, this, ConnectionService.class); 357 358 this.log("Result :"); 359 this.log("" + result); 360 } 361 catch (Exception e) 362 { 363 this.log(e); 364 logger.error("Error while invoking method", e); 365 throw e; 366 } 367 } 368 this.log(""); 369 return result; 370 } 371 372 373 378 public boolean isEventProvider() 379 { 380 if (this.info == null) 381 { 382 this.getMBeanInfo(); 383 } 384 return this.eventProvider; 385 } 386 387 388 393 public boolean isInstanceOf(String s) 394 throws Exception 395 { 396 return false; 397 } 398 399 400 406 public boolean isRegistered() 407 throws Exception 408 { 409 BeanContextServices context = (BeanContextServices ) getBeanContext(); 410 boolean result = false; 411 412 if (context.hasService(ConnectionService.class)) 413 { 414 try 415 { 416 ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this); 417 result = service.getMEJB().isRegistered(this.objectName); 418 context.releaseService(this, this, ConnectionService.class); 419 } 420 catch (Exception e) 421 { 422 logger.error("Error while testing registration", e); 423 throw e; 424 } 425 } 426 return result; 427 } 428 429 430 436 public boolean isRegisteredForNotifications() 437 throws Exception 438 { 439 BeanContextServices context = (BeanContextServices ) getBeanContext(); 440 boolean result = false; 441 442 if (context.hasService(NotificationService.class)) 443 { 444 try 445 { 446 NotificationService service = (NotificationService) context.getService(this, this, NotificationService.class, this, this); 447 result = service.hasObjectName(objectName); 448 context.releaseService(this, this, NotificationService.class); 449 } 450 catch (Exception e) 451 { 452 logger.error("Error while using NotificationService", e); 453 throw e; 454 } 455 } 456 return result; 457 } 458 459 460 465 public boolean isStateManageable() 466 { 467 if (this.info == null) 468 { 469 this.getMBeanInfo(); 470 } 471 return this.stateManageable; 472 } 473 474 475 480 public boolean isStatisticsProvider() 481 { 482 if (this.info == null) 483 { 484 this.getMBeanInfo(); 485 } 486 return this.statisticsProvider; 487 } 488 489 490 495 public void log(String text) 496 { 497 BeanContextServices context = (BeanContextServices ) getBeanContext(); 498 499 if (context.hasService(ConsoleService.class)) 500 { 501 try 502 { 503 ConsoleService service = (ConsoleService) context.getService(this, this, ConsoleService.class, this, this); 504 service.append(text); 505 context.releaseService(this, this, ConsoleService.class); 506 } 507 catch (Exception e) 508 { 509 logger.error("Error while logging", e); 510 } 511 } 512 } 513 514 515 520 public void log(Throwable t) 521 { 522 BeanContextServices context = (BeanContextServices ) getBeanContext(); 523 524 if (context.hasService(ConsoleService.class)) 525 { 526 try 527 { 528 ConsoleService service = (ConsoleService) context.getService(this, this, ConsoleService.class, this, this); 529 StringWriter w = new StringWriter (); 530 PrintWriter pw = new PrintWriter (w); 531 t.printStackTrace(pw); 532 pw.close(); 533 this.log("Exception occured :"); 534 this.log(w.toString()); 535 context.releaseService(this, this, ConsoleService.class); 536 } 537 catch (Exception e) 538 { 539 logger.error("Error while logging", e); 540 } 541 } 542 } 543 544 545 552 public void registerForGraph(GraphConsumer consumer, String statisticName, String statisticProperty) 553 { 554 BeanContextServices context = (BeanContextServices ) getBeanContext(); 555 556 if (context.hasService(GraphService.class)) 557 { 558 try 559 { 560 GraphService service = (GraphService) context.getService(this, this, GraphService.class, this, this); 561 562 final String fStatisticName = statisticName; 563 final String fStatisticProperty = statisticProperty; 564 String key = statisticName + "@" + statisticProperty; 565 566 GraphProducer producer = (GraphProducer) this.graphProducers.get(key); 567 if (producer == null) 568 { 569 producer = 570 new GraphProducer() 571 { 572 575 public String getGraphProducerId() 576 { 577 return ManagedObject.this.getName() + " : " + fStatisticName + " : " + fStatisticProperty; 578 } 579 580 581 584 public double produce() 585 { 586 try 587 { 588 Stats stats = getStats(); 589 Statistic statistic = stats.getStatistic(fStatisticName); 590 Method m = statistic.getClass().getMethod("get" + fStatisticProperty, new Class [0]); 591 Object o = m.invoke(statistic, new Object [0]); 592 return ClassTools.getValue(o); 593 } 594 catch (Exception e) 595 { 596 logger.warn("Cannot produce a numerical value for " + this.getGraphProducerId(), e); 597 } 598 return 0; 599 } 600 }; 601 this.graphProducers.put(key, producer); 602 } 603 service.addGraphProducer(consumer, producer); 604 context.releaseService(this, this, GraphService.class); 605 } 606 catch (Exception e) 607 { 608 logger.error("Error while using GraphService", e); 609 } 610 } 611 } 612 613 614 619 public void registerForNotifications() 620 throws Exception 621 { 622 BeanContextServices context = (BeanContextServices ) getBeanContext(); 623 624 if (this.isEventProvider() && !this.isRegisteredForNotifications()) 625 { 626 if (context.hasService(NotificationService.class)) 627 { 628 try 629 { 630 NotificationService service = (NotificationService) context.getService(this, this, NotificationService.class, this, this); 631 service.addObjectName(objectName); 632 context.releaseService(this, this, NotificationService.class); 633 logger.debug("ManagedObject " + this.objectName + " added for broadcasting"); 634 } 635 catch (Exception e) 636 { 637 logger.error("Error while using NotificationService", e); 638 throw e; 639 } 640 } 641 } 642 } 643 644 645 651 public void setAttribute(Attribute attribute) 652 throws Exception 653 { 654 BeanContextServices context = (BeanContextServices ) getBeanContext(); 655 656 if (context.hasService(ConnectionService.class)) 657 { 658 try 659 { 660 ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this); 661 service.getMEJB().setAttribute(objectName, attribute); 662 context.releaseService(this, this, ConnectionService.class); 663 } 664 catch (Exception e) 665 { 666 logger.error("Error while setting attribute", e); 667 throw e; 668 } 669 } 670 } 671 672 673 680 public AttributeList setAttributes(AttributeList list) 681 throws Exception 682 { 683 BeanContextServices context = (BeanContextServices ) getBeanContext(); 684 AttributeList result = null; 685 686 if (context.hasService(ConnectionService.class)) 687 { 688 try 689 { 690 ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this); 691 result = service.getMEJB().setAttributes(this.objectName, list); 692 context.releaseService(this, this, ConnectionService.class); 693 } 694 catch (Exception e) 695 { 696 logger.error("Error while setting attributes", e); 697 throw e; 698 } 699 } 700 return result; 701 } 702 703 704 705 711 public void setReference(ObjectName objectName) 712 throws Exception 713 { 714 this.objectName = objectName; 715 this.name = this.objectName.getKeyProperty("name"); 716 this.j2eeType = this.objectName.getKeyProperty("j2eeType"); 717 this.canonicalName = this.objectName.getCanonicalName(); 718 this.domain = this.objectName.getDomain(); 719 } 720 721 722 723 public void unregisterForGraph() 724 { 725 BeanContextServices context = (BeanContextServices ) getBeanContext(); 726 727 if (context.hasService(GraphService.class)) 728 { 729 try 730 { 731 GraphService service = (GraphService) context.getService(this, this, GraphService.class, this, this); 732 for (Iterator iterator = this.graphProducers.keySet().iterator(); iterator.hasNext(); ) 733 { 734 String attribute = (String ) iterator.next(); 735 GraphProducer producer = (GraphProducer) this.graphProducers.get(attribute); 736 service.removeGraphProducer(producer); 737 } 738 context.releaseService(this, this, GraphService.class); 739 } 740 catch (Exception e) 741 { 742 logger.error("Error while using GraphService", e); 743 } 744 } 745 } 746 747 748 753 public void unregisterForNotifications() 754 throws Exception 755 { 756 BeanContextServices context = (BeanContextServices ) getBeanContext(); 757 758 if (this.isRegisteredForNotifications()) 759 { 760 if (context.hasService(NotificationService.class)) 761 { 762 try 763 { 764 NotificationService service = (NotificationService) context.getService(this, this, NotificationService.class, this, this); 765 service.removeObjectName(this.objectName); 766 context.releaseService(this, this, NotificationService.class); 767 logger.debug("ManagedObject " + this.objectName + " removed for broadcasting"); 768 } 769 catch (Exception e) 770 { 771 logger.error("Error while using NotificationService", e); 772 throw e; 773 } 774 } 775 } 776 } 777 778 779 784 protected MBeanAttributeInfo [] filter(String includes, String excludes) 785 { 786 MBeanAttributeInfo [] infos = this.getMBeanInfo().getAttributes(); 787 ArrayList filteredInfos = new ArrayList (); 788 789 if (includes != null) 790 { 791 for (int i = 0; i < infos.length; i++) 792 { 793 String name = infos[i].getName(); 794 795 if (includes.indexOf(name.toLowerCase()) >= 0) 796 { 797 filteredInfos.add(infos[i]); 798 } 799 } 800 } 801 if (excludes != null) 802 { 803 for (int i = 0; i < infos.length; i++) 804 { 805 String name = infos[i].getName(); 806 807 if (excludes.indexOf(name.toLowerCase()) < 0) 808 { 809 filteredInfos.add(infos[i]); 810 } 811 } 812 813 } 814 815 return (MBeanAttributeInfo []) filteredInfos.toArray(new MBeanAttributeInfo [0]); 816 } 817 818 819 820 protected void initializeBeanContextResources() 821 { 822 super.initializeBeanContextResources(); 823 824 BeanContextServices context = (BeanContextServices ) this.getBeanContext(); 825 this.useServices(context); 826 } 827 828 829 830 831 protected void releaseBeanContextResources() 832 { 833 super.releaseBeanContextResources(); 834 835 BeanContextServices context = (BeanContextServices ) this.getBeanContext(); 836 this.releaseServices(context); 837 } 838 839 840 845 protected void releaseServices(BeanContextServices context) 846 { 847 this.c = null; 848 849 if (context.hasService(CacheService.class)) 850 { 851 try 853 { 854 CacheService service = (CacheService) context.getService(this, this, CacheService.class, this, this); 855 service.remove(CacheService.RESOURCE_TYPE, this.getCanonicalName()); 856 context.releaseService(this, this, CacheService.class); 857 } 858 catch (Exception e) 859 { 860 logger.error("Error during utilisation of service CacheService for " + this.getName(), e); 861 } 862 } 863 } 864 865 866 871 protected void useServices(BeanContextServices context) 872 { 873 if (context.hasService(CacheService.class)) 874 { 875 try 877 { 878 CacheService service = (CacheService) context.getService(this, this, CacheService.class, this, this); 879 service.add(CacheService.RESOURCE_TYPE, this.getCanonicalName(), this); 880 context.releaseService(this, this, CacheService.class); 881 } 882 catch (Exception e) 883 { 884 logger.error("Error during utilisation of service CacheService for " + this.getName(), e); 885 } 886 } 887 } 888 } 889 | Popular Tags |