| 1 12 13 package org.ejtools.jmx.browser.model; 14 15 16 17 import java.awt.Component ; 18 19 import java.beans.beancontext.BeanContextServiceAvailableEvent ; 20 21 import java.beans.beancontext.BeanContextServiceRevokedEvent ; 22 23 import java.beans.beancontext.BeanContextServices ; 24 25 import java.io.PrintWriter ; 26 27 import java.io.StringWriter ; 28 29 import java.util.Hashtable ; 30 31 import java.util.Iterator ; 32 33 34 35 import javax.management.Attribute ; 36 37 import javax.management.AttributeList ; 38 39 import javax.management.MBeanInfo ; 40 41 import javax.management.MBeanNotificationInfo ; 42 43 import javax.management.ObjectInstance ; 44 45 import javax.management.ObjectName ; 46 47 48 49 import org.apache.log4j.Logger; 50 51 import org.ejtools.adwt.service.ConsoleService; 52 53 import org.ejtools.graph.service.GraphConsumer; 54 55 import org.ejtools.graph.service.GraphConsumerMediator; 56 57 import org.ejtools.graph.service.GraphProducer; 58 59 import org.ejtools.graph.service.GraphService; 60 61 import org.ejtools.jmx.MBeanAccessor; 62 63 import org.ejtools.jmx.browser.model.service.CacheService; 64 65 import org.ejtools.jmx.browser.model.service.ConnectionService; 66 67 import org.ejtools.jmx.browser.model.service.NotificationService; 68 69 import org.ejtools.jmx.browser.ui.MBeanCustomizer; 70 71 import org.ejtools.util.ClassTools; 72 73 74 75 148 149 public class Resource extends Node implements MBeanAccessor, GraphConsumerMediator 150 151 { 152 153 154 155 protected String canonicalName; 156 157 158 159 protected String description; 160 161 162 163 protected String domain; 164 165 166 167 protected transient Hashtable graphProducers = new Hashtable (); 168 169 170 171 protected transient MBeanInfo info; 172 173 174 175 protected transient ObjectInstance objectInstance; 176 177 178 179 protected transient ObjectName objectName; 180 181 182 183 private static Logger logger = Logger.getLogger(Resource.class); 184 185 186 187 188 189 190 191 public Resource() 192 193 { 194 195 super(); 196 197 } 198 199 200 201 202 203 204 205 public void clear() 206 207 { 208 209 BeanContextServices context = (BeanContextServices ) getBeanContext(); 210 211 this.releaseServices(context); 212 213 214 215 super.clear(); 216 217 } 218 219 220 221 222 223 236 237 public Object getAttribute(String attribute) 238 239 throws Exception  240 241 { 242 243 BeanContextServices context = (BeanContextServices ) getBeanContext(); 244 245 Object result = null; 246 247 248 249 if (context.hasService(ConnectionService.class)) 250 251 { 252 253 try 254 255 { 256 257 ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this); 258 259 result = service.getMBeanServer().getAttribute(objectName, attribute); 260 261 context.releaseService(this, this, ConnectionService.class); 262 263 } 264 265 catch (Exception e) 266 267 { 268 269 logger.error("Error while getting attribute", e); 270 271 throw e; 272 273 } 274 275 } 276 277 return result; 278 279 } 280 281 282 283 284 285 294 295 public AttributeList getAttributes(String [] as) 296 297 throws Exception  298 299 { 300 301 return null; 302 303 } 304 305 306 307 308 309 318 319 public String getCanonicalName() 320 321 { 322 323 return this.canonicalName; 324 325 } 326 327 328 329 330 331 344 345 public Component getComponent() 346 347 { 348 349 if (c == null) 350 351 { 352 353 c = new MBeanCustomizer(this); 354 355 } 356 357 return (Component ) c; 358 359 } 360 361 362 363 364 365 374 375 public String getDescription() 376 377 { 378 379 return this.description; 380 381 } 382 383 384 385 386 387 396 397 public String getDomain() 398 399 { 400 401 return this.domain; 402 403 } 404 405 406 407 408 409 418 419 public GraphConsumer[] getGraphConsumers() 420 421 { 422 423 BeanContextServices context = (BeanContextServices ) getBeanContext(); 424 425 GraphConsumer[] result = new GraphConsumer[0]; 426 427 428 429 if (context.hasService(GraphService.class)) 430 431 { 432 433 try 434 435 { 436 437 GraphService service = (GraphService) context.getService(this, this, GraphService.class, this, this); 438 439 result = service.getGraphConsumers(); 440 441 context.releaseService(this, this, GraphService.class); 442 443 } 444 445 catch (Exception e) 446 447 { 448 449 logger.error("Error while using GraphService", e); 450 451 } 452 453 } 454 455 return result; 456 457 } 458 459 460 461 462 463 472 473 public Hashtable getGraphProducers() 474 475 { 476 477 return this.graphProducers; 478 479 } 480 481 482 483 484 485 494 495 public MBeanInfo getMBeanInfo() 496 497 { 498 499 if (this.info == null) 500 501 { 502 503 BeanContextServices context = (BeanContextServices ) getBeanContext(); 504 505 if (context.hasService(ConnectionService.class)) 506 507 { 508 509 try 510 511 { 512 513 ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this); 514 515 this.info = service.getMBeanServer().getMBeanInfo(this.objectName); 516 517 if (this.info == null) 518 519 { 520 521 logger.warn("MBeanInfo for " + this.objectName + " is null"); 522 523 } 524 525 context.releaseService(this, this, ConnectionService.class); 526 527 } 528 529 catch (Exception e) 530 531 { 532 533 logger.error("Error during utilisation of service ConnectionService", e); 534 535 } 536 537 } 538 539 } 540 541 return this.info; 542 543 } 544 545 546 547 548 549 560 561 public ObjectInstance getObjectInstance() 562 563 throws Exception  564 565 { 566 567 return this.objectInstance; 568 569 } 570 571 572 573 574 575 584 585 public ObjectName getObjectName() 586 587 { 588 589 return this.objectName; 590 591 } 592 593 594 595 596 597 614 615 public Object invoke(String action, Object [] params, String [] signatures) 616 617 throws Exception  618 619 { 620 621 BeanContextServices context = (BeanContextServices ) getBeanContext(); 622 623 Object result = null; 624 625 626 627 if (context.hasService(ConnectionService.class)) 628 629 { 630 631 try 632 633 { 634 635 this.log("Invoking " + action + "..."); 636 637 638 639 ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this); 640 641 result = service.getMBeanServer().invoke(objectName, action, params, signatures); 642 643 context.releaseService(this, this, ConnectionService.class); 644 645 646 647 this.log("Result :"); 648 649 this.log("" + result); 650 651 } 652 653 catch (Exception e) 654 655 { 656 657 this.log(e); 658 659 logger.error("Error while invoking method", e); 660 661 throw e; 662 663 } 664 665 } 666 667 this.log(""); 668 669 return result; 670 671 } 672 673 674 675 676 677 688 689 public boolean isBroadcaster() 690 691 throws Exception  692 693 { 694 695 697 MBeanNotificationInfo [] ni = this.getMBeanInfo().getNotifications(); 698 699 700 701 if (ni != null) 702 703 { 704 705 return (ni.length > 0); 706 707 } 708 709 return false; 710 711 } 712 713 714 715 716 717 726 727 public boolean isInstanceOf(String s) 728 729 throws Exception  730 731 { 732 733 return false; 734 735 } 736 737 738 739 740 741 752 753 public boolean isRegistered() 754 755 throws Exception  756 757 { 758 759 BeanContextServices context = (BeanContextServices ) getBeanContext(); 760 761 boolean result = false; 762 763 764 765 if (context.hasService(ConnectionService.class)) 766 767 { 768 769 try 770 771 { 772 773 ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this); 774 775 result = service.getMBeanServer().isRegistered(this.objectName); 776 777 context.releaseService(this, this, ConnectionService.class); 778 779 } 780 781 catch (Exception e) 782 783 { 784 785 logger.error("Error while testing registration", e); 786 787 throw e; 788 789 } 790 791 } 792 793 return result; 794 795 } 796 797 798 799 800 801 812 813 public boolean isRegisteredForNotifications() 814 815 throws Exception  816 817 { 818 819 BeanContextServices context = (BeanContextServices ) getBeanContext(); 820 821 boolean result = false; 822 823 824 825 if (context.hasService(NotificationService.class)) 826 827 { 828 829 try 830 831 { 832 833 NotificationService service = (NotificationService) context.getService(this, this, NotificationService.class, this, this); 834 835 result = service.hasObjectName(objectName); 836 837 context.releaseService(this, this, NotificationService.class); 838 839 } 840 841 catch (Exception e) 842 843 { 844 845 logger.error("Error while using NotificationService", e); 846 847 throw e; 848 849 } 850 851 } 852 853 return result; 854 855 } 856 857 858 859 860 861 870 871 public void log(String text) 872 873 { 874 875 BeanContextServices context = (BeanContextServices ) getBeanContext(); 876 877 878 879 if (context.hasService(ConsoleService.class)) 880 881 { 882 883 try 884 885 { 886 887 ConsoleService service = (ConsoleService) context.getService(this, this, ConsoleService.class, this, this); 888 889 service.append(text); 890 891 context.releaseService(this, this, ConsoleService.class); 892 893 } 894 895 catch (Exception e) 896 897 { 898 899 logger.error("Error while logging", e); 900 901 } 902 903 } 904 905 } 906 907 908 909 910 911 920 921 public void log(Throwable t) 922 923 { 924 925 StringWriter w = new StringWriter (); 926 927 PrintWriter pw = new PrintWriter (w); 928 929 t.printStackTrace(pw); 930 931 pw.close(); 932 933 this.log("Exception occured :"); 934 935 this.log(w.toString()); 936 937 } 938 939 940 941 942 943 954 955 public void registerForGraph(GraphConsumer consumer, String attribute) 956 957 { 958 959 BeanContextServices context = (BeanContextServices ) getBeanContext(); 960 961 962 963 if (context.hasService(GraphService.class)) 964 965 { 966 967 try 968 969 { 970 971 GraphService service = (GraphService) context.getService(this, this, GraphService.class, this, this); 972 973 final String attrName = attribute; 974 975 GraphProducer producer = (GraphProducer) this.graphProducers.get(attribute); 976 977 if (producer == null) 978 979 { 980 981 producer = 982 983 new GraphProducer() 984 985 { 986 987 992 993 public String getGraphProducerId() 994 995 { 996 997 return Resource.this.getCanonicalName() + " : " + attrName; 998 999 } 1000 1001 1002 1003 1004 1005 1010 1011 public double produce() 1012 1013 { 1014 1015 try 1016 1017 { 1018 1019 Object o = getAttribute(attrName); 1020 1021 return ClassTools.getValue(o); 1022 1023 } 1024 1025 catch (Exception e) 1026 1027 { 1028 1029 logger.warn("Cannot produce a numerical value for " + this.getGraphProducerId()); 1030 1031 } 1032 1033 return 0; 1034 1035 } 1036 1037 }; 1038 1039 this.graphProducers.put(attribute, producer); 1040 1041 } 1042 1043 service.addGraphProducer(consumer, producer); 1044 1045 context.releaseService(this, this, GraphService.class); 1046 1047 } 1048 1049 catch (Exception e) 1050 1051 { 1052 1053 logger.error("Error while using GraphService", e); 1054 1055 } 1056 1057 } 1058 1059 } 1060 1061 1062 1063 1064 1065 1066 1067 1076 1077 public void registerForNotifications() 1078 1079 throws Exception  1080 1081 { 1082 1083 BeanContextServices context = (BeanContextServices ) getBeanContext(); 1084 1085 1086 1087 if (this.isBroadcaster() && !this.isRegisteredForNotifications()) 1088 1089 { 1090 1091 if (context.hasService(NotificationService.class)) 1092 1093 { 1094 1095 try 1096 1097 { 1098 1099 NotificationService service = (NotificationService) context.getService(this, this, NotificationService.class, this, this); 1100 1101 service.addObjectName(objectName); 1102 1103 context.releaseService(this, this, NotificationService.class); 1104 1105 logger.debug("ManagedObject " + objectName + " added for broadcasting"); 1106 1107 } 1108 1109 catch (Exception e) 1110 1111 { 1112 1113 logger.error("Error while using NotificationService", e); 1114 1115 throw e; 1116 1117 } 1118 1119 } 1120 1121 } 1122 1123 } 1124 1125 1126 1127 1128 1129 1138 1139 public void serviceAvailable(BeanContextServiceAvailableEvent bcsae) 1140 1141 { 1142 1143 if (CacheService.class.equals(bcsae.getServiceClass())) 1144 1145 { 1146 1147 BeanContextServices context = (BeanContextServices ) getBeanContext(); 1148 1149 if (context != null) 1150 1151 { 1152 1153 this.useServices(context); 1154 1155 } 1156 1157 } 1158 1159 super.serviceAvailable(bcsae); 1160 1161 } 1162 1163 1164 1165 1166 1167 1168 1169 1178 1179 public void serviceRevoked(BeanContextServiceRevokedEvent bcsre) 1180 1181 { 1182 1183 if (NotificationService.class.equals(bcsre.getServiceClass())) 1184 1185 { 1186 1187 try 1188 1189 { 1190 1191 this.unregisterForNotifications(); 1192 1193 } 1194 1195 catch (Exception e) 1196 1197 { 1198 1199 } 1200 1201 } 1202 1203 if (CacheService.class.equals(bcsre.getServiceClass())) 1204 1205 { 1206 1207 BeanContextServices context = (BeanContextServices ) getBeanContext(); 1208 1209 if (context != null) 1210 1211 { 1212 1213 this.releaseServices(context); 1214 1215 } 1216 1217 } 1218 1219 super.serviceRevoked(bcsre); 1220 1221 } 1222 1223 1224 1225 1226 1227 1238 1239 public void setAttribute(Attribute attribute) 1240 1241 throws Exception  1242 1243 { 1244 1245 BeanContextServices context = (BeanContextServices ) getBeanContext(); 1246 1247 1248 1249 if (context.hasService(ConnectionService.class)) 1250
|