1 7 package org.jboss.mx.remoting.rmi; 8 9 import java.io.IOException ; 10 import java.io.NotSerializableException ; 11 import java.rmi.MarshalledObject ; 12 import java.util.Set ; 13 import javax.management.Attribute ; 14 import javax.management.AttributeList ; 15 import javax.management.AttributeNotFoundException ; 16 import javax.management.InstanceAlreadyExistsException ; 17 import javax.management.InstanceNotFoundException ; 18 import javax.management.IntrospectionException ; 19 import javax.management.InvalidAttributeValueException ; 20 import javax.management.ListenerNotFoundException ; 21 import javax.management.MBeanException ; 22 import javax.management.MBeanInfo ; 23 import javax.management.MBeanRegistrationException ; 24 import javax.management.MBeanServerConnection ; 25 import javax.management.NotCompliantMBeanException ; 26 import javax.management.NotificationFilter ; 27 import javax.management.NotificationListener ; 28 import javax.management.ObjectInstance ; 29 import javax.management.ObjectName ; 30 import javax.management.QueryExp ; 31 import javax.management.ReflectionException ; 32 import javax.management.remote.rmi.RMIConnection ; 33 import javax.security.auth.Subject ; 34 35 42 public class ClientMBeanServerConnection implements MBeanServerConnection 43 { 44 private RMIConnection connection = null; 45 private Subject subject = null; 46 private transient ClassLoader defaultClassLoader = null; 47 private ClientNotifier clientNotifier = null; 48 49 50 public ClientMBeanServerConnection(RMIConnection rmiConnection, ClientNotifier clientNotifier, 51 ClassLoader classLoader, Subject subject) 52 { 53 this.connection = rmiConnection; 54 this.clientNotifier = clientNotifier; 55 this.defaultClassLoader = classLoader; 56 this.subject = subject; 57 } 58 59 private ClassLoader activateDefaultClassLoader() 60 { 61 final ClassLoader current = Thread.currentThread().getContextClassLoader(); 62 Thread.currentThread().setContextClassLoader(defaultClassLoader); 63 return current; 64 } 65 66 private void deActivateDefaultClassLoader(ClassLoader previousClassLoader) 67 { 68 Thread.currentThread().setContextClassLoader(previousClassLoader); 69 } 70 71 98 public ObjectInstance createMBean(String className, ObjectName name) 99 throws ReflectionException , InstanceAlreadyExistsException , MBeanRegistrationException , 100 MBeanException , NotCompliantMBeanException , IOException 101 { 102 final ClassLoader current = activateDefaultClassLoader(); 103 104 try 105 { 106 return connection.createMBean(className, name, subject); 107 } 108 finally 109 { 110 deActivateDefaultClassLoader(current); 111 } 112 } 113 114 145 public ObjectInstance createMBean(String className, ObjectName name, ObjectName loaderName) 146 throws ReflectionException , InstanceAlreadyExistsException , MBeanRegistrationException , 147 MBeanException , NotCompliantMBeanException , InstanceNotFoundException , IOException 148 { 149 final ClassLoader current = activateDefaultClassLoader(); 150 151 try 152 { 153 return connection.createMBean(className, name, loaderName, subject); 154 } 155 finally 156 { 157 deActivateDefaultClassLoader(current); 158 } 159 160 } 161 162 191 public ObjectInstance createMBean(String className, ObjectName name, Object [] params, String [] signature) 192 throws ReflectionException , InstanceAlreadyExistsException , MBeanRegistrationException , 193 MBeanException , NotCompliantMBeanException , IOException 194 { 195 MarshalledObject args = new MarshalledObject (params); 196 final ClassLoader current = activateDefaultClassLoader(); 197 198 try 199 { 200 return connection.createMBean(className, name, args, signature, subject); 201 } 202 finally 203 { 204 deActivateDefaultClassLoader(current); 205 } 206 } 207 208 241 public ObjectInstance createMBean(String className, ObjectName name, ObjectName loaderName, 242 Object [] params, String [] signature) 243 throws ReflectionException , InstanceAlreadyExistsException , MBeanRegistrationException , 244 MBeanException , NotCompliantMBeanException , InstanceNotFoundException , IOException 245 { 246 MarshalledObject args = new MarshalledObject (params); 247 final ClassLoader current = activateDefaultClassLoader(); 248 249 try 250 { 251 return connection.createMBean(className, name, loaderName, args, signature, subject); 252 } 253 finally 254 { 255 deActivateDefaultClassLoader(current); 256 } 257 } 258 259 274 public void unregisterMBean(ObjectName name) throws InstanceNotFoundException , MBeanRegistrationException , IOException 275 { 276 final ClassLoader current = activateDefaultClassLoader(); 277 278 try 279 { 280 connection.unregisterMBean(name, subject); 281 } 282 finally 283 { 284 deActivateDefaultClassLoader(current); 285 } 286 } 287 288 297 public ObjectInstance getObjectInstance(ObjectName name) throws InstanceNotFoundException , IOException 298 { 299 final ClassLoader current = activateDefaultClassLoader(); 300 301 try 302 { 303 return connection.getObjectInstance(name, subject); 304 } 305 finally 306 { 307 deActivateDefaultClassLoader(current); 308 } 309 } 310 311 319 public Set queryMBeans(ObjectName name, QueryExp query) throws IOException 320 { 321 MarshalledObject args = new MarshalledObject (query); 322 final ClassLoader current = activateDefaultClassLoader(); 323 324 try 325 { 326 return connection.queryMBeans(name, args, subject); 327 } 328 finally 329 { 330 deActivateDefaultClassLoader(current); 331 } 332 } 333 334 342 public Set queryNames(ObjectName name, QueryExp query) throws IOException 343 { 344 MarshalledObject args = new MarshalledObject (query); 345 final ClassLoader current = activateDefaultClassLoader(); 346 347 try 348 { 349 return connection.queryNames(name, args, subject); 350 } 351 finally 352 { 353 deActivateDefaultClassLoader(current); 354 } 355 } 356 357 367 public boolean isRegistered(ObjectName name) throws IOException 368 { 369 final ClassLoader current = activateDefaultClassLoader(); 370 371 try 372 { 373 return connection.isRegistered(name, subject); 374 } 375 finally 376 { 377 deActivateDefaultClassLoader(current); 378 } 379 } 380 381 387 public Integer getMBeanCount() throws IOException 388 { 389 final ClassLoader current = activateDefaultClassLoader(); 390 391 try 392 { 393 return connection.getMBeanCount(subject); 394 } 395 finally 396 { 397 deActivateDefaultClassLoader(current); 398 } 399 } 400 401 420 public Object getAttribute(ObjectName name, String attribute) 421 throws MBeanException , AttributeNotFoundException , InstanceNotFoundException , ReflectionException , IOException 422 { 423 final ClassLoader current = activateDefaultClassLoader(); 424 425 try 426 { 427 return connection.getAttribute(name, attribute, subject); 428 } 429 finally 430 { 431 deActivateDefaultClassLoader(current); 432 } 433 } 434 435 450 public AttributeList getAttributes(ObjectName name, String [] attributes) 451 throws InstanceNotFoundException , ReflectionException , IOException 452 { 453 final ClassLoader current = activateDefaultClassLoader(); 454 455 try 456 { 457 return connection.getAttributes(name, attributes, subject); 458 } 459 finally 460 { 461 deActivateDefaultClassLoader(current); 462 } 463 } 464 465 485 public void setAttribute(ObjectName name, Attribute attribute) 486 throws InstanceNotFoundException , AttributeNotFoundException , 487 InvalidAttributeValueException , MBeanException , ReflectionException , IOException 488 { 489 MarshalledObject args = new MarshalledObject (attribute); 490 final ClassLoader current = activateDefaultClassLoader(); 491 492 try 493 { 494 connection.setAttribute(name, args, subject); 495 } 496 finally 497 { 498 deActivateDefaultClassLoader(current); 499 } 500 } 501 502 517 public AttributeList setAttributes(ObjectName name, AttributeList attributes) 518 throws InstanceNotFoundException , ReflectionException , IOException 519 { 520 MarshalledObject args = new MarshalledObject (attributes); 521 final ClassLoader current = activateDefaultClassLoader(); 522 523 try 524 { 525 return connection.setAttributes(name, args, subject); 526 } 527 finally 528 { 529 deActivateDefaultClassLoader(current); 530 } 531 532 } 533 534 550 public Object invoke(ObjectName name, String operationName, Object [] params, String [] signature) 551 throws InstanceNotFoundException , MBeanException , ReflectionException , IOException 552 { 553 MarshalledObject args = new MarshalledObject (params); 554 final ClassLoader current = activateDefaultClassLoader(); 555 556 try 557 { 558 return connection.invoke(name, operationName, args, signature, subject); 559 } 560 finally 561 { 562 deActivateDefaultClassLoader(current); 563 } 564 565 } 566 567 573 public String getDefaultDomain() throws IOException 574 { 575 final ClassLoader current = activateDefaultClassLoader(); 576 577 try 578 { 579 return connection.getDefaultDomain(subject); 580 } 581 finally 582 { 583 deActivateDefaultClassLoader(current); 584 } 585 } 586 587 593 public String [] getDomains() throws IOException 594 { 595 final ClassLoader current = activateDefaultClassLoader(); 596 597 try 598 { 599 return connection.getDomains(subject); 600 } 601 finally 602 { 603 deActivateDefaultClassLoader(current); 604 } 605 } 606 607 618 public void addNotificationListener(ObjectName name, NotificationListener listener, 619 NotificationFilter filter, Object handback) 620 throws InstanceNotFoundException , IOException 621 { 622 ClientListenerHolder holder = new ClientListenerHolder(name, listener, filter, handback); 623 if(clientNotifier.exists(holder)) 626 { 627 return; 628 } 629 else 630 { 631 MarshalledObject marshalledFilter = null; 632 633 if(filter != null) 634 { 635 try 636 { 637 marshalledFilter = new MarshalledObject (filter); 638 } 639 catch(IOException e) 640 { 641 holder.setFilterOnClient(true); 642 } 643 } 644 645 Integer [] listenerIDs = connection.addNotificationListeners(new ObjectName [] {name}, new MarshalledObject []{marshalledFilter}, 646 new Subject []{subject}); 647 clientNotifier.addNotificationListener(listenerIDs[0], holder); 648 } 649 650 } 651 652 666 public void addNotificationListener(ObjectName name, ObjectName listener, 667 NotificationFilter filter, Object handback) 668 throws InstanceNotFoundException , IOException 669 { 670 MarshalledObject marshalledFilter = filter != null ? new MarshalledObject (filter) : null; 671 MarshalledObject marshalledHandback = handback != null ? new MarshalledObject (handback) : null; 672 connection.addNotificationListener(name, listener, marshalledFilter, marshalledHandback, subject); 673 } 674 675 688 public void removeNotificationListener(ObjectName name, ObjectName listener) 689 throws InstanceNotFoundException , ListenerNotFoundException , IOException 690 { 691 connection.removeNotificationListener(name, listener, subject); 692 } 693 694 709 public void removeNotificationListener(ObjectName name, ObjectName listener, 710 NotificationFilter filter, Object handback) 711 throws InstanceNotFoundException , ListenerNotFoundException , IOException 712 { 713 MarshalledObject marshalledFilter = filter != null ? new MarshalledObject (filter) : null; 714 MarshalledObject marshalledHandback = handback != null ? new MarshalledObject (handback) : null; 715 connection.removeNotificationListener(name, listener, marshalledFilter, marshalledHandback, subject); 716 } 717 718 731 public void removeNotificationListener(ObjectName name, NotificationListener listener) 732 throws InstanceNotFoundException , ListenerNotFoundException , IOException 733 { 734 Integer [] ids = clientNotifier.getListeners(name, listener); 735 if(ids == null || ids.length == 0) 736 { 737 throw new ListenerNotFoundException ("Listener (" + listener + ") not found as a registered listener."); 738 } 739 connection.removeNotificationListeners(name, ids, subject); 740 clientNotifier.removeListeners(ids); 741 } 742 743 758 public void removeNotificationListener(ObjectName name, NotificationListener listener, 759 NotificationFilter filter, Object handback) 760 throws InstanceNotFoundException , ListenerNotFoundException , IOException 761 { 762 Integer id = clientNotifier.getListener(new ClientListenerHolder(name, listener, filter, handback)); 763 if(id == null) 764 { 765 throw new ListenerNotFoundException ("Listener (" + listener + ") could not be found as registered listener."); 766 } 767 768 Integer [] ids = new Integer []{id}; 769 connection.removeNotificationListeners(name, ids, subject); 770 clientNotifier.removeListeners(ids); 771 } 772 773 786 public MBeanInfo getMBeanInfo(ObjectName name) throws InstanceNotFoundException , IntrospectionException , ReflectionException , IOException 787 { 788 final ClassLoader current = activateDefaultClassLoader(); 789 790 try 791 { 792 return connection.getMBeanInfo(name, subject); 793 } 794 finally 795 { 796 deActivateDefaultClassLoader(current); 797 } 798 799 } 800 801 811 public boolean isInstanceOf(ObjectName name, String className) throws InstanceNotFoundException , IOException 812 { 813 final ClassLoader current = activateDefaultClassLoader(); 814 815 try 816 { 817 return connection.isInstanceOf(name, className, subject); 818 } 819 finally 820 { 821 deActivateDefaultClassLoader(current); 822 } 823 } 824 } | Popular Tags |