1 23 package com.sun.appserv.management.util.jmx; 24 25 import javax.management.*; 26 import java.io.IOException ; 27 import java.util.Set ; 28 29 import com.sun.appserv.management.util.stringifier.ArrayStringifier; 30 import com.sun.appserv.management.util.misc.TypeCast; 31 32 33 39 public class MBeanServerConnection_Hook implements MBeanServerConnection 40 { 41 private final MBeanServerConnection mConn; 42 43 60 public interface Hook 61 { 62 public long preHook( String methodName ); 63 public long preHook( String methodName, Object [] args ); 64 public void postHook( long id, String methodName ); 65 public void postHook( long id, String methodName, Object [] args ); 66 public void postHook( long id, String methodName, Object [] args, Object result ); 67 public ObjectName nameHook( long id, ObjectName methodName ) throws IOException ; 68 69 70 73 public void IOExceptionHook( long id, IOException e, 74 String operationName, ObjectName objectName, Object [] allArgs ) 75 throws IOException ; 76 77 public void InstanceNotFoundExceptionHook( String methodName, long id, InstanceNotFoundException e) 78 throws InstanceNotFoundException; 79 } 80 81 static public class HookImpl 82 implements Hook 83 { 84 public static final HookImpl HOOK = new HookImpl(); 85 86 long mID; 87 public 88 HookImpl() 89 { 90 mID = 0; 91 } 92 93 synchronized long 94 getNewID() 95 { 96 return( mID++ ); 97 } 98 99 public long preHook( String methodName ) { return( getNewID() ); } 100 public long preHook( String methodName, Object [] args ) { return( getNewID() ); } 101 public void postHook( long id, String methodName ) {} 102 public void postHook( long id, String methodName, Object [] args ) {} 103 public void postHook( long id, String methodName, Object [] args, Object result ) {} 104 public ObjectName nameHook( long id, ObjectName methodName ) throws IOException { return( methodName ); } 105 106 public void IOExceptionHook( long id, IOException e, 107 String operationName, ObjectName objectName, Object [] allArgs ) 108 throws IOException 109 { throw e; } 110 111 public void InstanceNotFoundExceptionHook( String methodName, long id, InstanceNotFoundException e) 112 throws InstanceNotFoundException 113 { throw e; } 114 115 116 String 117 getInvocationString( String methodName, final Object [] args ) 118 { 119 assert( methodName != null ); 120 121 String msg = methodName + "("; 122 123 if ( args != null ) 124 { 125 for( int i = 0; i < args.length; ++i ) 126 { 127 final Object arg = args[ i ]; 128 129 String s = ""; 130 if ( arg == null ) 131 { 132 s = "null"; 133 } 134 else if ( arg instanceof String || arg instanceof ObjectName ) 135 { 136 s = "\"" + arg + "\""; 137 } 138 else 139 { 140 s = arg.toString(); 141 } 142 143 msg = msg + s; 144 if ( i != args.length - 1 ) 145 { 146 msg = msg + ","; 147 } 148 } 149 } 150 151 msg = msg + ")"; 152 153 return( msg ); 154 } 155 156 String 157 getInvocationString( final long id, String methodName, final Object [] args ) 158 { 159 return "" + id + getInvocationString( methodName, args ); 160 } 161 } 162 163 Hook 164 getHook() 165 { 166 return( HookImpl.HOOK ); 167 } 168 169 public 170 MBeanServerConnection_Hook( MBeanServerConnection conn ) 171 { 172 mConn = conn; 173 174 assert( getConn() != null ); 175 176 } 177 178 MBeanServerConnection 179 getConn() 180 { 181 return( mConn ); 182 } 183 184 void 185 callIOExceptionHook( 186 long id, 187 IOException e, 188 String operationName, 189 ObjectName objectName, 190 Object [] allArgs ) throws IOException 191 { 192 getHook().IOExceptionHook( id, e, operationName, objectName, allArgs ); 193 } 194 195 long 196 callPreHook( String name, Object [] args ) 197 { 198 return( getHook().preHook( name, args ) ); 199 } 200 201 long 202 callPreHook( String name ) 203 { 204 return( getHook().preHook( name ) ); 205 } 206 207 void 208 callPostHook( long id, String name, Object [] args, Object result ) 209 { 210 getHook().postHook( id, name, args, result ); 211 } 212 213 void 214 callPostHook( long id, String name, Object [] args ) 215 { 216 getHook().postHook( id, name, args ); 217 } 218 219 void 220 callPostHook( long id, String name ) 221 { 222 getHook().postHook( id, name ); 223 } 224 225 ObjectName 226 callNameHook( long id, ObjectName objectName ) 227 throws IOException 228 { 229 return( getHook().nameHook( id, objectName ) ); 230 } 231 232 public static final String CREATE_MBEAN = "createMBean"; 233 public static final String UNREGISTER_MBEAN = "unregisterMBean"; 234 public static final String REGISTER_MBEAN = "registerMBean"; 235 public static final String GET_OBJECT_INSTANCE = "getObjectInstance"; 236 public static final String QUERY_MBEANS = "queryMBeans"; 237 public static final String QUERY_NAMES = "queryNames"; 238 public final static String GET_DOMAINS = "getDomains"; 239 public static final String IS_REGISTERED = "isRegistered"; 240 public static final String GET_MBEAN_COUNT = "getMBeanCount"; 241 public static final String GET_ATTRIBUTE = "getAttribute"; 242 public static final String GET_ATTRIBUTES = "getAttributes"; 243 public static final String SET_ATTRIBUTE = "setAttribute"; 244 public static final String SET_ATTRIBUTES = "setAttributes"; 245 public static final String INVOKE = "invoke"; 246 public static final String GET_DEFAULT_DOMAIN = "getDefaultDomain"; 247 public final static String ADD_NOTIFICATION_LISTENER = "addNotificationListener"; 248 public final static String REMOVE_NOTIFICATION_LISTENER = "removeNotificationListener"; 249 public final static String GET_MBEAN_INFO = "getMBeanInfo"; 250 public final static String IS_INSTANCE_OF = "isInstanceOf"; 251 252 253 254 public ObjectInstance createMBean(String className, ObjectName name) 255 throws ReflectionException, InstanceAlreadyExistsException, 256 MBeanRegistrationException, MBeanException, 257 NotCompliantMBeanException, IOException 258 { 259 final Object [] args = new Object [] { className, name }; 260 261 final long id = callPreHook( CREATE_MBEAN, args ); 262 263 ObjectInstance result = null; 264 try 265 { 266 result = getConn().createMBean( className, name ); 267 callPostHook( id, CREATE_MBEAN, args, result ); 268 } 269 catch( IOException e ) 270 { 271 callIOExceptionHook( id, e, CREATE_MBEAN, name, args ); 272 } 273 274 return( result ); 275 } 276 277 public ObjectInstance createMBean(String className, ObjectName name, ObjectName loaderName) 278 throws ReflectionException, InstanceAlreadyExistsException, 279 MBeanRegistrationException, MBeanException, 280 NotCompliantMBeanException, InstanceNotFoundException, 281 IOException 282 { 283 final Object [] args = new Object [] { className, name, loaderName }; 284 final long id = callPreHook( CREATE_MBEAN, args ); 285 286 ObjectInstance result = null; 287 try 288 { 289 result = getConn().createMBean( className, name, loaderName ); 290 callPostHook( id, CREATE_MBEAN, args, result ); 291 } 292 catch( IOException e ) 293 { 294 callIOExceptionHook( id, e, CREATE_MBEAN, name, args ); 295 } 296 catch( InstanceNotFoundException e ) 297 { 298 getHook().InstanceNotFoundExceptionHook( CREATE_MBEAN, id, e ); 299 } 300 301 return( result ); 302 } 303 304 305 306 public ObjectInstance createMBean(String className, ObjectName name, 307 Object params[], String signature[]) 308 throws ReflectionException, InstanceAlreadyExistsException, 309 MBeanRegistrationException, MBeanException, 310 NotCompliantMBeanException, IOException 311 { 312 final Object [] args = new Object [] { className, name, params, signature }; 313 final long id = callPreHook( CREATE_MBEAN, args ); 314 315 ObjectInstance result = null; 316 try 317 { 318 result = getConn().createMBean( className, name, params, signature ); 319 callPostHook( id, CREATE_MBEAN, args, result ); 320 } 321 catch( IOException e ) 322 { 323 callIOExceptionHook( id, e, CREATE_MBEAN, name, args ); 324 } 325 326 return( result ); 327 } 328 329 330 public ObjectInstance createMBean(String className, ObjectName name, 331 ObjectName loaderName, Object params[], 332 String signature[]) 333 throws ReflectionException, InstanceAlreadyExistsException, 334 MBeanRegistrationException, MBeanException, 335 NotCompliantMBeanException, InstanceNotFoundException, 336 IOException 337 { 338 final Object [] args = new Object [] { className, name, loaderName, params, signature }; 339 final long id = callPreHook( CREATE_MBEAN, args ); 340 341 ObjectInstance result = null; 342 try 343 { 344 result = getConn().createMBean( className, name, loaderName, params, signature); 345 346 callPostHook( id, CREATE_MBEAN, args, result ); 347 } 348 catch( IOException e ) 349 { 350 callIOExceptionHook( id, e, CREATE_MBEAN, name, args ); 351 } 352 catch( InstanceNotFoundException e ) 353 { 354 getHook().InstanceNotFoundExceptionHook( CREATE_MBEAN, id, e ); 355 } 356 357 return( result ); 358 } 359 360 361 public void unregisterMBean(ObjectName name) 362 throws InstanceNotFoundException, MBeanRegistrationException, 363 IOException 364 { 365 final Object [] args = new Object [] { name }; 366 final long id = callPreHook( UNREGISTER_MBEAN, args ); 367 368 try 369 { 370 getConn().unregisterMBean( callNameHook( id, name ) ); 371 372 callPostHook( id, UNREGISTER_MBEAN, args ); 373 } 374 catch( IOException e ) 375 { 376 callIOExceptionHook( id, e, UNREGISTER_MBEAN, name, args ); 377 } 378 catch( InstanceNotFoundException e ) 379 { 380 getHook().InstanceNotFoundExceptionHook( UNREGISTER_MBEAN, id, e ); 381 } 382 } 383 384 385 public ObjectInstance getObjectInstance(ObjectName name) 386 throws InstanceNotFoundException, IOException 387 { 388 final Object [] args = new Object [] { name }; 389 final long id = callPreHook( GET_OBJECT_INSTANCE, args ); 390 391 ObjectInstance result = null; 392 try 393 { 394 result = getConn().getObjectInstance( callNameHook( id, name ) ); 395 396 callPostHook( id, GET_OBJECT_INSTANCE, args, result ); 397 } 398 catch( IOException e ) 399 { 400 callIOExceptionHook( id, e, GET_OBJECT_INSTANCE, name, args ); 401 } 402 catch( InstanceNotFoundException e ) 403 { 404 getHook().InstanceNotFoundExceptionHook( GET_OBJECT_INSTANCE, id, e ); 405 } 406 407 return( result ); 408 } 409 410 411 public Set <ObjectName> queryMBeans(ObjectName name, QueryExp query) 412 throws IOException 413 { 414 final Object [] args = new Object [] { name, query }; 415 final long id = callPreHook( QUERY_MBEANS, args ); 416 417 Set <ObjectName> result = null; 418 try 419 { 420 result = TypeCast.asSet( getConn().queryMBeans( name, query ) ); 421 } 422 catch( IOException e ) 423 { 424 callIOExceptionHook( id, e, QUERY_MBEANS, name, args ); 425 } 426 427 callPostHook( id, QUERY_MBEANS, args, result ); 428 429 return( result ); 430 } 431 432 433 public Set <ObjectName> queryNames(ObjectName name, QueryExp query) 434 throws IOException 435 { 436 final Object [] args = new Object [] { name, query }; 437 final long id = callPreHook( QUERY_NAMES, args ); 438 439 Set <ObjectName> result = null; 440 try 441 { 442 result = JMXUtil.queryNames( getConn(), name, query ); 443 } 444 catch( IOException e ) 445 { 446 callIOExceptionHook( id, e, QUERY_NAMES, name, args ); 447 } 448 449 callPostHook( id, QUERY_NAMES, args, result ); 450 451 return( result ); 452 } 453 454 455 456 public boolean isRegistered(ObjectName name) 457 throws IOException 458 { 459 final Object [] args = new Object [] { name }; 460 final long id = callPreHook( IS_REGISTERED, args ); 461 462 boolean registered = false; 463 464 try 465 { 466 registered = getConn().isRegistered( callNameHook( id, name) ); 467 468 callPostHook( id, IS_REGISTERED, args, registered ? Boolean.TRUE : Boolean.FALSE ); 469 } 470 catch( IOException e ) 471 { 472 callIOExceptionHook( id, e, IS_REGISTERED, name, args ); 473 } 474 475 return( registered ); 476 } 477 478 479 480 public Integer getMBeanCount() 481 throws IOException 482 { 483 final long id = callPreHook( GET_MBEAN_COUNT, null ); 484 485 Integer result = null; 486 try 487 { 488 result = getConn().getMBeanCount( ); 489 490 callPostHook( id, GET_MBEAN_COUNT, null, result ); 491 } 492 catch( IOException e ) 493 { 494 callIOExceptionHook( id, e, GET_MBEAN_COUNT, null, null ); 495 } 496 497 return( result ); 498 } 499 500 501 public Object getAttribute(ObjectName name, String attribute) 502 throws MBeanException, AttributeNotFoundException, 503 InstanceNotFoundException, ReflectionException, 504 IOException 505 { 506 final Object [] args = new Object [] { name, attribute }; 507 final long id = callPreHook( GET_ATTRIBUTE, args ); 508 509 Object result = null; 510 try 511 { 512 result = getConn().getAttribute( callNameHook( id, name ), attribute ); 513 514 callPostHook( id, GET_ATTRIBUTE, args, result ); 515 } 516 catch( IOException e ) 517 { 518 callIOExceptionHook( id, e, GET_ATTRIBUTE, name, args ); 519 } 520 521 return( result ); 522 } 523 524 525 526 public AttributeList getAttributes(ObjectName name, String [] attributes) 527 throws InstanceNotFoundException, ReflectionException, 528 IOException 529 { 530 final Object [] args = new Object [] { name, attributes }; 531 final long id = callPreHook( GET_ATTRIBUTES, args ); 532 533 AttributeList result = null; 534 try 535 { 536 final ObjectName actualName = callNameHook( id, name ); 537 538 result = getConn().getAttributes(actualName , attributes ); 539 540 callPostHook( id, GET_ATTRIBUTES, args, result ); 541 } 542 catch( IOException e ) 543 { 544 callIOExceptionHook( id, e, GET_ATTRIBUTES, name, args ); 545 } 546 547 return( result ); 548 } 549 550 551 public void setAttribute(ObjectName name, Attribute attribute) 552 throws InstanceNotFoundException, AttributeNotFoundException, 553 InvalidAttributeValueException, MBeanException, 554 ReflectionException, IOException 555 { 556 final Object [] args = new Object [] { name, attribute }; 557 final long id = callPreHook( SET_ATTRIBUTE, args ); 558 559 try 560 { 561 getConn().setAttribute( callNameHook( id, name ), attribute ); 562 563 callPostHook( id, SET_ATTRIBUTE, args ); 564 } 565 catch( IOException e ) 566 { 567 callIOExceptionHook( id, e, SET_ATTRIBUTE, name, args ); 568 } 569 } 570 571 572 573 574 public AttributeList setAttributes(ObjectName name, AttributeList attributes) 575 throws InstanceNotFoundException, ReflectionException, IOException 576 { 577 final Object [] args = new Object [] { name, attributes }; 578 final long id = callPreHook( SET_ATTRIBUTES, args ); 579 580 AttributeList result = null; 581 582 try 583 { 584 result = getConn().setAttributes( callNameHook( id, name ), attributes ); 585 586 callPostHook( id, SET_ATTRIBUTES, args ); 587 } 588 catch( IOException e ) 589 { 590 callIOExceptionHook( id, e, SET_ATTRIBUTES, name, args ); 591 } 592 593 return( result ); 594 } 595 596 public Object invoke(ObjectName name, String operationName, 597 Object params[], String signature[]) 598 throws InstanceNotFoundException, MBeanException, 599 ReflectionException, IOException 600 { 601 final Object [] args = new Object [] { name, operationName, params, signature }; 602 final long id = callPreHook( INVOKE, args ); 603 604 Object result = null; 605 try 606 { 607 result = getConn().invoke( callNameHook( id, name ), operationName, params, signature); 608 609 callPostHook( id, INVOKE, args ); 610 } 611 catch( IOException e ) 612 { 613 callIOExceptionHook( id, e, INVOKE, name, args ); 614 } 615 return( result ); 616 } 617 618 619 620 621 public String getDefaultDomain() 622 throws IOException 623 { 624 final long id = callPreHook( GET_DEFAULT_DOMAIN ); 625 626 String result = null; 627 try 628 { 629 result = getConn().getDefaultDomain(); 630 631 callPostHook( id, GET_DEFAULT_DOMAIN ); 632 } 633 catch( IOException e ) 634 { 635 callIOExceptionHook( id, e, GET_DEFAULT_DOMAIN, null, null ); 636 } 637 638 return( result ); 639 } 640 641 642 public String [] getDomains() 643 throws IOException 644 { 645 final long id = callPreHook( GET_DOMAINS ); 646 647 String [] result = null; 648 try 649 { 650 result = getConn().getDomains( ); 651 652 callPostHook( id, GET_DOMAINS, result ); 653 } 654 catch( IOException e ) 655 { 656 callIOExceptionHook( id, e, GET_DOMAINS, null, null ); 657 } 658 659 return( result ); 660 } 661 662 663 public void addNotificationListener(ObjectName name, 664 NotificationListener listener, 665 NotificationFilter filter, 666 Object handback) 667 throws InstanceNotFoundException, IOException 668 { 669 final Object [] args = new Object [] { name, listener, filter, handback }; 670 final long id = callPreHook( ADD_NOTIFICATION_LISTENER, args ); 671 672 try 673 { 674 getConn().addNotificationListener( callNameHook( id, name ), listener, filter, handback ); 675 676 callPostHook( id, ADD_NOTIFICATION_LISTENER, args ); 677 } 678 catch( IOException e ) 679 { 680 callIOExceptionHook( id, e, ADD_NOTIFICATION_LISTENER, name, args ); 681 } 682 } 683 684 685 686 public void addNotificationListener(ObjectName name, 687 ObjectName listener, 688 NotificationFilter filter, 689 Object handback) 690 throws InstanceNotFoundException, IOException 691 { 692 final Object [] args = new Object [] { name, listener, filter, handback }; 693 final long id = callPreHook( ADD_NOTIFICATION_LISTENER, args ); 694 695 try 696 { 697 getConn().addNotificationListener( callNameHook( id, name ), listener, filter, handback ); 698 699 callPostHook( id, ADD_NOTIFICATION_LISTENER, args ); 700 } 701 catch( IOException e ) 702 { 703 callIOExceptionHook( id, e, ADD_NOTIFICATION_LISTENER, name, args ); 704 } 705 } 706 707 708 709 public void removeNotificationListener(ObjectName name, 710 ObjectName listener) 711 throws InstanceNotFoundException, ListenerNotFoundException, 712 IOException 713 { 714 final Object [] args = new Object [] { listener }; 715 final long id = callPreHook( REMOVE_NOTIFICATION_LISTENER, args ); 716 717 try 718 { 719 getConn().removeNotificationListener( callNameHook( id, name ), listener ); 720 721 callPostHook( id, REMOVE_NOTIFICATION_LISTENER, args ); 722 } 723 catch( IOException e ) 724 { 725 callIOExceptionHook( id, e, REMOVE_NOTIFICATION_LISTENER, name, args ); 726 } 727 } 728 729 730 public void removeNotificationListener(ObjectName name, 731 ObjectName listener, 732 NotificationFilter filter, 733 Object handback) 734 throws InstanceNotFoundException, ListenerNotFoundException, 735 IOException 736 { 737 final Object [] args = new Object [] { name, listener, filter, handback }; 738 final long id = callPreHook( REMOVE_NOTIFICATION_LISTENER, args ); 739 740 try 741 { 742 getConn().removeNotificationListener( callNameHook( id, name ), listener, filter, handback ); 743 744 callPostHook( id, REMOVE_NOTIFICATION_LISTENER, args ); 745 } 746 catch( IOException e ) 747 { 748 callIOExceptionHook( id, e, REMOVE_NOTIFICATION_LISTENER, name, args ); 749 } 750 } 751 752 753 754 public void removeNotificationListener(ObjectName name, 755 NotificationListener listener) 756 throws InstanceNotFoundException, ListenerNotFoundException, 757 IOException 758 { 759 final Object [] args = new Object [] { name, listener }; 760 final long id = callPreHook( REMOVE_NOTIFICATION_LISTENER, args ); 761 762 try 763 { 764 getConn().removeNotificationListener( callNameHook( id, name ), listener ); 765 766 callPostHook( id, REMOVE_NOTIFICATION_LISTENER, args ); 767 } 768 catch( IOException e ) 769 { 770 callIOExceptionHook( id, e, REMOVE_NOTIFICATION_LISTENER, name, args ); 771 } 772 } 773 774 775 public void removeNotificationListener(ObjectName name, 776 NotificationListener listener, 777 NotificationFilter filter, 778 Object handback) 779 throws InstanceNotFoundException, ListenerNotFoundException, 780 IOException 781 { 782 final Object [] args = new Object [] { name, listener, filter, handback }; 783 final long id = callPreHook( REMOVE_NOTIFICATION_LISTENER, args ); 784 785 try 786 { 787 getConn().removeNotificationListener( callNameHook( id, name ), listener, filter, handback ); 788 789 callPostHook( id, REMOVE_NOTIFICATION_LISTENER, args ); 790 } 791 catch( IOException e ) 792 { 793 callIOExceptionHook( id, e, REMOVE_NOTIFICATION_LISTENER, name, args ); 794 } 795 } 796 797 798 public MBeanInfo getMBeanInfo(ObjectName name) 799 throws InstanceNotFoundException, IntrospectionException, 800 ReflectionException, IOException 801 { 802 final Object [] args = new Object [] { name }; 803 final long id = callPreHook( GET_MBEAN_INFO, args ); 804 805 MBeanInfo result = null; 806 try 807 { 808 result = getConn().getMBeanInfo( callNameHook( id, name ) ); 809 810 callPostHook( id, GET_MBEAN_INFO, args ); 811 } 812 catch( IOException e ) 813 { 814 callIOExceptionHook( id, e, GET_MBEAN_INFO, name, args ); 815 } 816 817 return( result ); 818 } 819 820 821 822 public boolean isInstanceOf(ObjectName name, String className) 823 throws InstanceNotFoundException, IOException 824 { 825 final Object [] args = new Object [] { name, className }; 826 final long id = callPreHook( IS_INSTANCE_OF, args ); 827 828 boolean isInstance = false; 829 try 830 { 831 isInstance = getConn().isInstanceOf( callNameHook( id, name ), className ); 832 833 callPostHook( id, IS_INSTANCE_OF, args, isInstance ? Boolean.TRUE : Boolean.FALSE ); 834 } 835 catch( IOException e ) 836 { 837 callIOExceptionHook( id, e, IS_INSTANCE_OF, name, args ); 838 } 839 840 return( isInstance ); 841 } 842 843 }; 844 845 | Popular Tags |