1 22 package org.jboss.management.j2ee.cluster; 23 24 import java.rmi.RemoteException ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Set ; 28 29 import javax.management.Attribute ; 30 import javax.management.AttributeList ; 31 import javax.management.AttributeNotFoundException ; 32 import javax.management.InstanceAlreadyExistsException ; 33 import javax.management.InstanceNotFoundException ; 34 import javax.management.IntrospectionException ; 35 import javax.management.InvalidAttributeValueException ; 36 import javax.management.ListenerNotFoundException ; 37 import javax.management.MBeanException ; 38 import javax.management.MBeanInfo ; 39 import javax.management.MBeanRegistrationException ; 40 import javax.management.MBeanServer ; 41 import javax.management.NotCompliantMBeanException ; 42 import javax.management.NotificationFilter ; 43 import javax.management.ObjectInstance ; 44 import javax.management.ObjectName ; 45 import javax.management.QueryExp ; 46 import javax.management.ReflectionException ; 47 48 import org.jboss.ha.framework.interfaces.HAPartition; 49 import org.jboss.management.j2ee.J2EEDomain; 50 import org.jboss.mx.util.ObjectNameFactory; 51 import org.jboss.system.ServiceMBeanSupport; 52 import org.jboss.system.server.ServerConfigUtil; 53 54 65 public class HAManagementService 66 extends ServiceMBeanSupport 67 implements HAManagementServiceMBean 68 { 69 70 72 private final static String SERVICE_NAME = "HAManagementService"; 73 76 ObjectName OBJECT_NAME = ObjectNameFactory.create("jboss:service=HAManagement"); 77 78 80 private ObjectName mHAManagementName; 81 private ObjectName mClusterPartitionName; 82 private String mBackgroundPartition = ServerConfigUtil.getDefaultPartitionName(); 83 private HAPartition mPartition; 84 85 87 89 97 public HAManagementService() 98 { 99 } 100 101 103 105 public Object _getAttribute(ObjectName pName, String pAttribute) 106 { 107 Object lReturn = null; 108 try 109 { 110 lReturn = server.getAttribute(pName, pAttribute); 111 } 112 catch (Exception e) 113 { 114 } 116 return lReturn; 117 } 118 119 122 public Object getAttribute(ObjectName pName, String pAttribute) 123 throws 124 MBeanException , 125 AttributeNotFoundException , 126 InstanceNotFoundException , 127 ReflectionException , 128 RemoteException 129 { 130 Object lReturn = null; 135 try 136 { 137 lReturn = server.getAttribute(pName, pAttribute); 138 } 139 catch (InstanceNotFoundException infe) 140 { 141 Object [] lArguments = new Object []{ 143 pName, 144 pAttribute 145 }; 146 List lValues = null; 147 try 148 { 149 lValues = mPartition.callMethodOnCluster(SERVICE_NAME, 150 "_getAttribute", 151 lArguments, 152 new Class []{ObjectName .class, String .class}, 153 false 154 ); 155 } 156 catch (Exception e) 157 { 158 throw new RemoteException ("Could not get management attributes on the cluster", e); 159 } 160 Iterator i = lValues.iterator(); 161 while (i.hasNext()) 162 { 163 Object lValue = i.next(); 164 if (lValue != null) 165 { 166 lReturn = lValue; 167 break; 168 } 169 } 170 if (lReturn == null) 171 { 172 throw infe; 174 } 175 } 176 return lReturn; 177 } 178 179 181 public AttributeList _getAttributes(ObjectName pName, String [] pAttributes) 182 { 183 AttributeList lReturn = null; 184 try 185 { 186 lReturn = server.getAttributes(pName, pAttributes); 187 } 188 catch (Exception e) 189 { 190 } 192 return lReturn; 193 } 194 195 198 public AttributeList getAttributes(ObjectName pName, String [] pAttributes) 199 throws 200 InstanceNotFoundException , 201 ReflectionException , 202 RemoteException 203 { 204 AttributeList lReturn = null; 210 try 211 { 212 lReturn = server.getAttributes(pName, pAttributes); 213 } 214 catch (InstanceNotFoundException infe) 215 { 216 Object [] lArguments = new Object []{ 218 pName, 219 pAttributes 220 }; 221 List lValues = null; 222 try 223 { 224 lValues = mPartition.callMethodOnCluster(SERVICE_NAME, 225 "_getAttributes", 226 lArguments, 227 new Class []{ObjectName .class, String [].class}, 228 false 229 ); 230 } 231 catch (Exception e) 232 { 233 throw new RemoteException ("Could not get management attributes on the cluster", e); 234 } 235 Iterator i = lValues.iterator(); 236 while (i.hasNext()) 237 { 238 Object lValue = i.next(); 239 if (lValue != null) 240 { 241 lReturn = (AttributeList ) lValue; 242 break; 243 } 244 } 245 if (lReturn == null) 246 { 247 throw infe; 249 } 250 } 251 return lReturn; 252 } 253 254 257 public String getDefaultDomain() 258 throws RemoteException 259 { 260 return J2EEDomain.getDomainName(); 261 } 262 263 266 public Integer getMBeanCount() 267 throws RemoteException 268 { 269 try 270 { 271 return new Integer (queryNames(new ObjectName ("*:*"), 272 null).size()); 273 } 274 catch (Exception e) 275 { 276 } 277 return new Integer (0); 278 } 279 280 283 public MBeanInfo getMBeanInfo(ObjectName pName) 284 throws 285 IntrospectionException , 286 InstanceNotFoundException , 287 ReflectionException , 288 RemoteException 289 { 290 return server.getMBeanInfo(pName); 291 } 292 293 296 public javax.management.j2ee.ListenerRegistration getListenerRegistry() 297 throws RemoteException 298 { 299 return null; 300 306 } 307 308 310 public Object _invoke(ObjectName pName, String pOperationName, Object [] pParams, String [] pSignature) 311 { 312 Object lReturn = null; 313 try 314 { 315 log.info("_invoke(), name: " + pName + ", operation: " + pOperationName + 316 ", params: " + pParams + ", signature: " + pSignature); 317 lReturn = server.invoke(pName, 318 pOperationName, 319 pParams, 320 pSignature); 321 } 322 catch (Exception e) 323 { 324 lReturn = e; 326 } 327 return lReturn; 328 } 329 330 333 public Object invoke(ObjectName pName, String pOperationName, Object [] pParams, String [] pSignature) 334 throws 335 InstanceNotFoundException , 336 MBeanException , 337 ReflectionException , 338 RemoteException 339 { 340 Object lReturn = null; 341 InstanceNotFoundException lException = null; 342 log.info("invoke(), name: " + pName + ", operation: " + pOperationName + 343 ", params: " + pParams + ", signature: " + pSignature); 344 try 345 { 346 lReturn = server.invoke(pName, pOperationName, pParams, pSignature); 347 } 348 catch (InstanceNotFoundException infe) 349 { 350 lException = infe; 351 } 352 Object [] lArguments = new Object []{ 353 pName, 354 pOperationName, 355 pParams, 356 pSignature 357 }; 358 List lValues = null; 359 try 360 { 361 log.info("call _invoke()"); 362 lValues = mPartition.callMethodOnCluster(SERVICE_NAME, 363 "_invoke", 364 lArguments, 365 new Class []{ObjectName .class, String .class, Object [].class, String [].class}, 366 true 367 ); 368 } 369 catch (Exception e) 370 { 371 throw new RemoteException ("Could not get management attributes on the cluster", e); 372 } 373 Iterator i = lValues.iterator(); 374 while (i.hasNext()) 375 { 376 Object lValue = i.next(); 377 if (lValue instanceof Throwable ) 378 { 379 log.debug("invoke a method on the cluster caused an exception: " + lValue); 380 if (lValue instanceof InstanceNotFoundException ) 381 { 382 continue; 384 } 385 } 386 lException = null; 388 if (lValue != null) 389 { 390 lReturn = lValue; 391 break; 392 } 393 } 394 if (lException != null) 395 { 396 throw lException; 398 } 399 return lReturn; 400 } 401 402 405 public boolean isRegistered(ObjectName pName) 406 throws RemoteException 407 { 408 return server.isRegistered(pName); 409 } 410 411 414 public Set queryNames(ObjectName pName, QueryExp pQuery) 415 throws RemoteException 416 { 417 return server.queryNames(pName, pQuery); 418 } 419 420 422 public Object _setAttribute(ObjectName pName, Attribute pAttribute) 423 { 424 Object lReturn = null; 425 try 426 { 427 log.info("_setAttribute(), name: " + pName + ", attribute: " + pAttribute); 428 server.setAttribute(pName, pAttribute); 429 } 430 catch (Exception e) 431 { 432 lReturn = e; 434 } 435 return lReturn; 436 } 437 438 441 public void setAttribute(ObjectName pName, Attribute pAttribute) 442 throws 443 AttributeNotFoundException , 444 InstanceNotFoundException , 445 InvalidAttributeValueException , 446 MBeanException , 447 ReflectionException , 448 RemoteException 449 { 450 InstanceNotFoundException lException = null; 451 try 452 { 453 server.setAttribute(pName, pAttribute); 454 } 455 catch (InstanceNotFoundException infe) 456 { 457 lException = infe; } 459 Object [] lArguments = new Object []{ 460 pName, 461 pAttribute 462 }; 463 List lValues = null; 464 try 465 { 466 log.info("call _setAttribute()"); 467 lValues = mPartition.callMethodOnCluster(SERVICE_NAME, 468 "_setAttribute", 469 lArguments, 470 new Class []{ObjectName .class, Attribute .class}, 471 true 472 ); 473 } 474 catch (Exception e) 475 { 476 throw new RemoteException ("Could not set management attributes on the cluster", e); 477 } 478 Iterator i = lValues.iterator(); 479 while (i.hasNext()) 480 { 481 Object lValue = i.next(); 482 if (lValue instanceof Throwable ) 485 { 486 log.debug("invoke a method on the cluster caused an exception: " + lValue); 487 if (lValue instanceof InstanceNotFoundException ) 488 { 489 if (lException == null) 490 { 491 lException = (InstanceNotFoundException ) lValue; } 493 } 494 else 495 { 496 if (lValue instanceof AttributeNotFoundException ) 498 { 499 throw (AttributeNotFoundException ) lValue; 500 } 501 if (lValue instanceof InvalidAttributeValueException ) 502 { 503 throw (InvalidAttributeValueException ) lValue; 504 } 505 if (lValue instanceof MBeanException ) 506 { 507 throw (MBeanException ) lValue; 508 } 509 if (lValue instanceof ReflectionException ) 510 { 511 throw (ReflectionException ) lValue; 512 } 513 throw new RemoteException (lValue.toString()); 514 } 515 } 516 } 517 if (lException != null) 518 { 519 throw lException; } 521 } 522 523 525 public Object _setAttributes(ObjectName pName, AttributeList pAttributes) 526 { 527 Object lReturn = null; 528 try 529 { 530 log.info("_setAttributes(), name: " + pName + ", attribute: " + pAttributes); 531 server.setAttributes(pName, pAttributes); 532 } 533 catch (Exception e) 534 { 535 lReturn = e; 537 } 538 return lReturn; 539 } 540 541 544 public AttributeList setAttributes(ObjectName pName, AttributeList pAttributes) 545 throws 546 InstanceNotFoundException , 547 ReflectionException , 548 RemoteException 549 { 550 Object lReturn = null; 551 InstanceNotFoundException lException = null; 552 try 553 { 554 lReturn = server.setAttributes(pName, pAttributes); 555 } 556 catch (InstanceNotFoundException infe) 557 { 558 lException = infe; 559 } 560 Object [] lArguments = new Object []{ 561 pName, 562 pAttributes 563 }; 564 List lValues = null; 565 try 566 { 567 log.info("call _setAttributes()"); 568 lValues = mPartition.callMethodOnCluster(SERVICE_NAME, 569 "_setAttributes", 570 lArguments, 571 new Class []{ObjectName .class, AttributeList .class}, 572 true 573 ); 574 } 575 catch (Exception e) 576 { 577 throw new RemoteException ("Could not set management attributes on the cluster", e); 578 } 579 Iterator i = lValues.iterator(); 580 while (i.hasNext()) 581 { 582 Object lValue = i.next(); 583 if (lValue instanceof Throwable ) 586 { 587 log.debug("set Attributes on the cluster caused an exception: " + lValue); 588 if (lValue instanceof InstanceNotFoundException ) 589 { 590 if (lException == null) 591 { 592 lException = (InstanceNotFoundException ) lValue; } 594 } 595 else 596 { 597 if (lValue instanceof ReflectionException ) 598 { 599 throw (ReflectionException ) lValue; 600 } 601 throw new RemoteException (lValue.toString()); 602 } 603 } 604 } 605 if (lException != null) 606 { 607 throw lException; 609 } 610 return (AttributeList ) lReturn; 611 } 612 613 615 public Object _createMBean(String pClass, 616 ObjectName pName, 617 Object [] pParameters, 618 String [] pSignature) 619 { 620 Object lReturn = null; 621 try 622 { 623 log.info("_createMBean(), name: " + pName); 624 lReturn = server.createMBean(pClass, pName, pParameters, pSignature); 625 } 626 catch (Exception e) 627 { 628 lReturn = e; 630 } 631 return lReturn; 632 } 633 634 637 public ObjectInstance createMBean(String pClass, 638 ObjectName pName, 639 Object [] pParameters, 640 String [] pSignature) 641 throws 642 InstanceAlreadyExistsException , 643 MBeanException , 644 MBeanRegistrationException , 645 NotCompliantMBeanException , 646 ReflectionException , 647 RemoteException 648 { 649 List lValues = null; 650 Object [] lArguments = new Object []{ 651 pClass, 652 pName, 653 pParameters, 654 pSignature 655 }; 656 try 657 { 658 log.info("call _createMBean()"); 659 lValues = mPartition.callMethodOnCluster(SERVICE_NAME, 660 "_createMBean", 661 lArguments, 662 new Class []{String .class, ObjectName .class, Object [].class, String [].class}, 663 false 664 ); 665 } 666 catch (Exception e) 667 { 668 throw new RemoteException ("Could not create a MBean on the cluster", e); 670 } 671 Iterator i = lValues.iterator(); 672 ObjectInstance lInstance = null; 673 Throwable lException = null; 674 while (i.hasNext()) 675 { 676 Object lValue = i.next(); 677 if (lValue instanceof ObjectInstance ) 678 { 679 if (lInstance == null) 680 { 681 lInstance = (ObjectInstance ) lValue; 682 } 683 } 684 else if (lValue instanceof Throwable ) 685 { 686 if (lException == null) 687 { 688 lException = (Throwable ) lValue; 689 } 690 } 691 } 692 if (lException != null) 693 { 694 if (lInstance != null) 695 { 696 try 698 { 699 unregisterMBean(lInstance.getObjectName()); 700 } 701 catch (Exception e) 702 { 703 } 705 } 706 if (lException instanceof InstanceAlreadyExistsException ) 707 { 708 throw (InstanceAlreadyExistsException ) lException; 709 } 710 if (lException instanceof MBeanException ) 711 { 712 throw (MBeanException ) lException; 713 } 714 if (lException instanceof MBeanRegistrationException ) 715 { 716 throw (MBeanRegistrationException ) lException; 717 } 718 if (lException instanceof NotCompliantMBeanException ) 719 { 720 throw (NotCompliantMBeanException ) lException; 721 } 722 if (lException instanceof ReflectionException ) 723 { 724 throw (ReflectionException ) lException; 725 } 726 throw new RemoteException (lException.toString()); 727 } 728 return lInstance; 729 } 730 731 733 public Object _unregisterMBean(ObjectName pName) 734 { 735 Object lReturn = null; 736 try 737 { 738 log.info("_unregisterMBean(), name: " + pName); 739 server.unregisterMBean(pName); 740 } 741 catch (Exception e) 742 { 743 lReturn = e; 745 } 746 return lReturn; 747 } 748 749 752 public void unregisterMBean(ObjectName pName) 753 throws 754 InstanceNotFoundException , 755 MBeanRegistrationException , 756 RemoteException 757 { 758 List lValues = null; 759 Object [] lArguments = new Object []{ 760 pName 761 }; 762 try 763 { 764 log.info("call _unregisterMBean()"); 765 lValues = mPartition.callMethodOnCluster(SERVICE_NAME, 766 "_unregisterMBean", 767 lArguments, 768 new Class []{ObjectName .class}, 769 false 770 ); 771 } 772 catch (Exception e) 773 { 774 throw new RemoteException ("Could not unregister a MBean on the cluster", e); 776 } 777 Iterator i = lValues.iterator(); 778 Throwable lException = null; 779 while (i.hasNext()) 780 { 781 Object lValue = i.next(); 782 if (lValue instanceof Throwable ) 783 { 784 lException = (Throwable ) lValue; 785 break; 786 } 787 } 788 if (lException != null) 789 { 790 if (lException instanceof InstanceNotFoundException ) 791 { 792 throw (InstanceNotFoundException ) lException; 793 } 794 if (lException instanceof MBeanRegistrationException ) 795 { 796 throw (MBeanRegistrationException ) lException; 797 } 798 throw new RemoteException (lException.toString()); 799 } 800 } 801 802 805 public void addNotificationListener(ObjectName pBroadcaster, 806 ObjectName pListener, 807 NotificationFilter pFilter, 808 Object pHandback) 809 throws 810 InstanceNotFoundException , 811 RemoteException 812 { 813 server.addNotificationListener(pBroadcaster, pListener, pFilter, pHandback); 814 } 815 816 819 public void removeNotificationListener(ObjectName pBroadcaster, 820 ObjectName pListener) 821 throws 822 InstanceNotFoundException , 823 ListenerNotFoundException , 824 RemoteException 825 { 826 server.removeNotificationListener(pBroadcaster, pListener); 827 } 828 829 831 834 public ObjectName preRegister(MBeanServer pServer, ObjectName pName) 835 throws Exception 836 { 837 super.preRegister(pServer, pName); 838 log.info("HA Management Service MBean online"); 839 mHAManagementName = new ObjectName (OBJECT_NAME + ",Partition=" + mBackgroundPartition); 840 841 return mHAManagementName; 842 } 843 844 847 public void preDeregister() 848 throws Exception 849 { 850 super.preDeregister(); 851 } 852 853 855 public String getName() 856 { 857 return "HA Management Service"; 858 } 859 860 864 protected void createService() throws Exception 865 { 866 } 867 868 872 protected void startService() 873 throws Exception 874 { 875 mClusterPartitionName = new ObjectName ("jboss:service=" + mBackgroundPartition); 876 877 log.debug("registerRPCHandler"); 878 mPartition = (HAPartition) server.getAttribute(mClusterPartitionName, 879 "HAPartition"); 880 mPartition.registerRPCHandler(SERVICE_NAME, this); 881 } 882 883 886 protected void stopService() 887 { 888 } 889 890 892 898 private void logException(Throwable e) 899 { 900 Throwable t = org.jboss.mx.util.JMXExceptionDecoder.decode(e); 901 log.error("operation failed", t); 902 } 903 904 906 } 908 | Popular Tags |