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 1251 { 1252 1253 try 1254 1255 { 1256 1257 ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this); 1258 1259 logger.debug("setAttribute(" + objectName + "," + attribute + ")"); 1260 1261 service.getMBeanServer().setAttribute(objectName, attribute); 1262 1263 context.releaseService(this, this, ConnectionService.class); 1264 1265 } 1266 1267 catch (Exception e) 1268 1269 { 1270 1271 logger.error("Error while setting attribute", e); 1272 1273 throw e; 1274 1275 } 1276 1277 } 1278 1279 } 1280 1281 1282 1283 1284 1285 1298 1299 public AttributeList setAttributes(AttributeList list) 1300 1301 throws Exception 1302 1303 { 1304 1305 BeanContextServices context = (BeanContextServices ) getBeanContext(); 1306 1307 AttributeList result = null; 1308 1309 1310 1311 if (context.hasService(ConnectionService.class)) 1312 1313 { 1314 1315 try 1316 1317 { 1318 1319 this.unregisterForNotifications(); 1320 1321 1322 1323 ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this); 1324 1325 result = service.getMBeanServer().setAttributes(this.objectName, list); 1326 1327 context.releaseService(this, this, ConnectionService.class); 1328 1329 } 1330 1331 catch (Exception e) 1332 1333 { 1334 1335 logger.error("Error while setting attributes", e); 1336 1337 throw e; 1338 1339 } 1340 1341 } 1342 1343 return result; 1344 1345 } 1346 1347 1348 1349 1350 1351 1352 1353 1364 1365 public void setReference(ObjectInstance instance) 1366 1367 throws Exception 1368 1369 { 1370 1371 this.objectInstance = instance; 1372 1373 this.objectName = this.objectInstance.getObjectName(); 1374 1375 this.name = this.objectName.getCanonicalName(); 1376 1377 this.canonicalName = this.objectName.getCanonicalName(); 1378 1379 this.className = this.objectInstance.getClassName(); 1380 1381 this.domain = this.objectName.getDomain(); 1382 1383 } 1384 1385 1386 1387 1388 1389 1390 1391 public void unregisterForGraph() 1392 1393 { 1394 1395 BeanContextServices context = (BeanContextServices ) getBeanContext(); 1396 1397 1398 1399 if (context.hasService(GraphService.class)) 1400 1401 { 1402 1403 try 1404 1405 { 1406 1407 GraphService service = (GraphService) context.getService(this, this, GraphService.class, this, this); 1408 1409 for (Iterator iterator = this.graphProducers.keySet().iterator(); iterator.hasNext(); ) 1410 1411 { 1412 1413 String attribute = (String ) iterator.next(); 1414 1415 GraphProducer producer = (GraphProducer) this.graphProducers.get(attribute); 1416 1417 service.removeGraphProducer(producer); 1418 1419 } 1420 1421 context.releaseService(this, this, GraphService.class); 1422 1423 } 1424 1425 catch (Exception e) 1426 1427 { 1428 1429 logger.error("Error while using GraphService", e); 1430 1431 } 1432 1433 } 1434 1435 } 1436 1437 1438 1439 1440 1441 1450 1451 public void unregisterForNotifications() 1452 1453 throws Exception 1454 1455 { 1456 1457 BeanContextServices context = (BeanContextServices ) getBeanContext(); 1458 1459 1460 1461 if (this.isRegisteredForNotifications()) 1462 1463 { 1464 1465 if (context.hasService(NotificationService.class)) 1466 1467 { 1468 1469 try 1470 1471 { 1472 1473 NotificationService service = (NotificationService) context.getService(this, this, NotificationService.class, this, this); 1474 1475 service.removeObjectName(this.objectName); 1476 1477 logger.debug("ManagedObject " + objectName + " removed for broadcasting"); 1478 1479 } 1480 1481 catch (Exception e) 1482 1483 { 1484 1485 logger.error("Error while using NotificationService", e); 1486 1487 throw e; 1488 1489 } 1490 1491 finally 1492 1493 { 1494 1495 context.releaseService(this, this, NotificationService.class); 1496 1497 } 1498 1499 } 1500 1501 } 1502 1503 } 1504 1505 1506 1507 1508 1509 1518 1519 public void unregisterMBean() 1520 1521 throws Exception 1522 1523 { 1524 1525 BeanContextServices context = (BeanContextServices ) getBeanContext(); 1526 1527 1528 1529 if (context.hasService(ConnectionService.class)) 1530 1531 { 1532 1533 try 1534 1535 { 1536 1537 ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this); 1538 1539 service.getMBeanServer().unregisterMBean(this.objectName); 1540 1541 context.releaseService(this, this, ConnectionService.class); 1542 1543 } 1544 1545 catch (Exception e) 1546 1547 { 1548 1549 logger.error("Error while unregistering", e); 1550 1551 throw e; 1552 1553 } 1554 1555 } 1556 1557 } 1558 1559 1560 1561 1562 1563 1564 1565 protected void initializeBeanContextResources() 1566 1567 { 1568 1569 super.initializeBeanContextResources(); 1570 1571 1572 1573 BeanContextServices context = (BeanContextServices ) getBeanContext(); 1574 1575 this.useServices(context); 1576 1577 } 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 protected void releaseBeanContextResources() 1588 1589 { 1590 1591 super.releaseBeanContextResources(); 1592 1593 1594 1595 BeanContextServices context = (BeanContextServices ) getBeanContext(); 1596 1597 this.releaseServices(context); 1598 1599 } 1600 1601 1602 1603 1604 1605 1614 1615 protected void releaseServices(BeanContextServices context) 1616 1617 { 1618 1619 if (context.hasService(CacheService.class)) 1620 1621 { 1622 1623 1624 1625 1627 try 1628 1629 { 1630 1631 this.unregisterForNotifications(); 1632 1633 this.unregisterForGraph(); 1634 1635 1636 1637 CacheService service = (CacheService) context.getService(this, this, CacheService.class, this, this); 1638 1639 service.remove(CacheService.RESOURCE_TYPE, this.getCanonicalName()); 1640 1641 context.releaseService(this, this, CacheService.class); 1642 1643 } 1644 1645 catch (Exception e) 1646 1647 { 1648 1649 logger.error("Error during utilisation of service CacheService", e); 1650 1651 } 1652 1653 } 1654 1655 } 1656 1657 1658 1659 1660 1661 1670 1671 protected void useServices(BeanContextServices context) 1672 1673 { 1674 1675 if (context.hasService(CacheService.class)) 1676 1677 { 1678 1679 1681 try 1682 1683 { 1684 1685 CacheService service = (CacheService) context.getService(this, this, CacheService.class, this, this); 1686 1687 service.add(CacheService.RESOURCE_TYPE, this.getCanonicalName(), this); 1688 1689 context.releaseService(this, this, CacheService.class); 1690 1691 } 1692 1693 catch (Exception e) 1694 1695 { 1696 1697 logger.error("Error during utilisation of service CacheService", e); 1698 1699 } 1700 1701 } 1702 1703 } 1704 1705} 1706 1707 | Popular Tags |