1 22 package org.jboss.management.mejb; 23 24 import org.jboss.logging.Logger; 25 import org.jboss.management.j2ee.J2EEDomain; 26 import org.jboss.mx.util.MBeanServerLocator; 27 28 import javax.ejb.CreateException ; 29 import javax.ejb.EJBException ; 30 import javax.ejb.SessionBean ; 31 import javax.ejb.SessionContext ; 32 import javax.management.*; 33 import javax.management.j2ee.ManagementHome ; 34 import javax.naming.Context ; 35 import javax.naming.InitialContext ; 36 import javax.naming.NamingException ; 37 import java.rmi.RemoteException ; 38 import java.util.Set ; 39 40 61 public class ManagementBean 62 implements SessionBean 63 { 64 70 private static Logger log = Logger.getLogger(ManagementBean.class); 71 72 76 private SessionContext mContext; 77 78 82 private MBeanServer mbeanServer; 83 86 private ObjectName mManagementService; 87 88 92 96 public Object getAttribute(ObjectName pName, String pAttribute) 97 throws 98 MBeanException, 99 AttributeNotFoundException, 100 InstanceNotFoundException, 101 ReflectionException, 102 RemoteException 103 { 104 try 105 { 106 if (mManagementService == null) 107 { 108 return mbeanServer.getAttribute(pName, pAttribute); 109 } 110 else 111 { 112 return mbeanServer.invoke(mManagementService, 113 "getAttribute", 114 new Object []{ 115 pName, 116 pAttribute 117 }, 118 new String []{ 119 ObjectName.class.getName(), 120 String .class.getName() 121 }); 122 } 123 } 124 catch (RuntimeOperationsException e) 127 { 128 if (e.getTargetException() instanceof IllegalArgumentException ) 129 { 130 throw new AttributeNotFoundException("MBean attribute not found: " + pAttribute); 131 } 132 throw e; 133 } 134 } 135 136 140 public AttributeList getAttributes(ObjectName pName, String [] pAttributes) 141 throws 142 InstanceNotFoundException, 143 ReflectionException, 144 RemoteException 145 { 146 if (mManagementService == null) 147 { 148 return mbeanServer.getAttributes(pName, pAttributes); 149 } 150 else 151 { 152 try 153 { 154 return (AttributeList) mbeanServer.invoke(mManagementService, 155 "getAttributes", 156 new Object []{ 157 pName, 158 pAttributes 159 }, 160 new String []{ 161 ObjectName.class.getName(), 162 String [].class.getName() 163 }); 164 } 165 catch (MBeanException me) 166 { 167 log.error("getAttributes() got exception from cluster service", me); 168 return null; 170 } 171 } 172 } 173 174 178 public String getDefaultDomain() 179 throws RemoteException 180 { 181 if (mManagementService == null) 182 { 183 return J2EEDomain.getDomainName(); 184 } 185 else 186 { 187 try 188 { 189 return (String ) mbeanServer.getAttribute(mManagementService, 190 "DefaultDomain"); 191 } 192 catch (JMException jme) 193 { 194 log.error("getDefaultDomain() got exception from cluster service", jme); 196 return null; 197 } 198 } 199 } 200 201 205 public Integer getMBeanCount() 206 throws RemoteException 207 { 208 if (mManagementService == null) 209 { 210 try 211 { 212 Set mbeans = this.queryNames(new ObjectName("*:*"), null); 213 return new Integer (mbeans.size()); 214 } 215 catch (Exception e) 216 { 217 } 218 return new Integer (0); 219 } 220 else 221 { 222 try 223 { 224 return (Integer ) mbeanServer.invoke(mManagementService, 225 "getMBeanCount", 226 new Object []{}, 227 new String []{}); 228 } 229 catch (JMException jme) 230 { 231 log.error("getMBeanCount() got exception from cluster service", jme); 232 return null; 234 } 235 } 236 } 237 238 242 public MBeanInfo getMBeanInfo(ObjectName pName) 243 throws 244 IntrospectionException, 245 InstanceNotFoundException, 246 ReflectionException, 247 RemoteException 248 { 249 if (mManagementService == null) 250 { 251 return mbeanServer.getMBeanInfo(pName); 252 } 253 else 254 { 255 try 256 { 257 return (MBeanInfo) mbeanServer.invoke(mManagementService, 258 "getMBeanInfo", 259 new Object []{ 260 pName 261 }, 262 new String []{ 263 ObjectName.class.getName() 264 }); 265 } 266 catch (MBeanException me) 267 { 268 log.error("getMBeanInfo() got exception from cluster service", me); 269 return null; 271 } 272 } 273 } 274 275 279 public javax.management.j2ee.ListenerRegistration getListenerRegistry() 280 throws RemoteException 281 { 282 return new ListenerRegistration((ManagementHome ) mContext.getEJBObject().getEJBHome(), 283 new String []{}); 284 } 285 286 290 public Object invoke(ObjectName pName, String pOperationName, Object [] pParams, String [] pSignature) 291 throws 292 InstanceNotFoundException, 293 MBeanException, 294 ReflectionException, 295 RemoteException 296 { 297 if (pOperationName.equals("start")) 300 { 301 pOperationName = "mejbStart"; 302 } 303 else if (pOperationName.equals("startRecursive")) 304 { 305 pOperationName = "mejbStartRecursive"; 306 } 307 else if (pOperationName.equals("stop")) 308 { 309 pOperationName = "mejbStop"; 310 } 311 if (mManagementService == null) 312 { 313 return mbeanServer.invoke(pName, 314 pOperationName, 315 pParams, 316 pSignature); 317 } 318 else 319 { 320 return mbeanServer.invoke(mManagementService, 321 "invoke", 322 new Object []{ 323 pName, 324 pOperationName, 325 pParams, 326 pSignature 327 }, 328 new String []{ 329 ObjectName.class.getName(), 330 String .class.getName(), 331 Object [].class.getName(), 332 String [].class.getName() 333 }); 334 } 335 } 336 337 341 public boolean isRegistered(ObjectName pName) 342 throws RemoteException 343 { 344 if (mManagementService == null) 345 { 346 return mbeanServer.isRegistered(pName); 347 } 348 else 349 { 350 try 351 { 352 Boolean lCheck = (Boolean ) mbeanServer.invoke(mManagementService, 353 "isRegistered", 354 new Object []{ 355 pName 356 }, 357 new String []{ 358 ObjectName.class.getName() 359 }); 360 if (lCheck != null) 361 { 362 return lCheck.booleanValue(); 363 } 364 } 365 catch (JMException jme) 366 { 367 log.error("isRegistered() got exception from cluster service", jme); 368 } 370 return false; 371 } 372 } 373 374 378 public Set queryNames(ObjectName pName, QueryExp pQuery) 379 throws RemoteException 380 { 381 if (mManagementService == null) 382 { 383 return mbeanServer.queryNames(pName, pQuery); 384 } 385 else 386 { 387 try 388 { 389 return (Set ) mbeanServer.invoke(mManagementService, 390 "queryNames", 391 new Object []{ 392 pName, 393 pQuery 394 }, 395 new String []{ 396 ObjectName.class.getName(), 397 QueryExp.class.getName() 398 }); 399 } 400 catch (JMException jme) 401 { 402 log.error("queryNames() got exception from cluster service", jme); 403 return null; 405 } 406 } 407 } 408 409 413 public void setAttribute(ObjectName pName, Attribute pAttribute) 414 throws 415 AttributeNotFoundException, 416 InstanceNotFoundException, 417 InvalidAttributeValueException, 418 MBeanException, 419 ReflectionException, 420 RemoteException 421 { 422 if (mManagementService == null) 423 { 424 mbeanServer.setAttribute(pName, pAttribute); 425 } 426 else 427 { 428 mbeanServer.invoke(mManagementService, 429 "setAttribute", 430 new Object []{ 431 pName, 432 pAttribute 433 }, 434 new String []{ 435 ObjectName.class.getName(), 436 String .class.getName() 437 }); 438 } 439 } 440 441 445 public AttributeList setAttributes(ObjectName pName, AttributeList pAttributes) 446 throws 447 InstanceNotFoundException, 448 ReflectionException, 449 RemoteException 450 { 451 if (mManagementService == null) 452 { 453 return mbeanServer.setAttributes(pName, pAttributes); 454 } 455 else 456 { 457 try 458 { 459 return (AttributeList) mbeanServer.invoke(mManagementService, 460 "setAttributes", 461 new Object []{ 462 pName, 463 pAttributes 464 }, 465 new String []{ 466 ObjectName.class.getName(), 467 AttributeList.class.getName() 468 }); 469 } 470 catch (MBeanException me) 471 { 472 log.error("setAttributes() got exception from cluster service", me); 473 return null; 475 } 476 } 477 } 478 479 483 public ObjectInstance createMBean(String pClass, 484 ObjectName pName, 485 Object [] pParameters, 486 String [] pSignature) 487 throws 488 InstanceAlreadyExistsException, 489 MBeanException, 490 MBeanRegistrationException, 491 NotCompliantMBeanException, 492 ReflectionException, 493 RemoteException 494 { 495 if (mManagementService == null) 496 { 497 return mbeanServer.createMBean(pClass, pName, pParameters, pSignature); 498 } 499 else 500 { 501 try 502 { 503 return (ObjectInstance) mbeanServer.invoke(mManagementService, 504 "createMBean", 505 new Object []{ 506 pClass, 507 pName, 508 pParameters, 509 pSignature 510 }, 511 new String []{ 512 String .class.getName(), 513 ObjectName.class.getName(), 514 Object [].class.getName(), 515 String [].class.getName() 516 }); 517 } 518 catch (InstanceNotFoundException infe) 519 { 520 log.error("createMBean() got exception from cluster service", infe); 521 return null; 523 } 524 } 525 } 526 527 531 public void unregisterMBean(ObjectName pName) 532 throws 533 InstanceNotFoundException, 534 MBeanRegistrationException, 535 RemoteException 536 { 537 if (mManagementService == null) 538 { 539 mbeanServer.unregisterMBean(pName); 540 } 541 else 542 { 543 try 544 { 545 mbeanServer.invoke(mManagementService, 546 "unregisterMBean", 547 new Object []{ 548 pName 549 }, 550 new String []{ 551 ObjectName.class.getName() 552 }); 553 } 554 catch (MBeanException me) 556 { 557 log.error("unregisterMBean() got exception from cluster service", me); 558 } 559 catch (ReflectionException re) 560 { 561 log.error("unregisterMBean() got exception from cluster service", re); 562 } 563 } 564 } 565 566 570 public void addNotificationListener(ObjectName pBroadcaster, 571 ObjectName pListener, 572 NotificationFilter pFilter, 573 Object pHandback) 574 throws 575 InstanceNotFoundException, 576 RemoteException 577 { 578 if (mManagementService == null) 579 { 580 mbeanServer.addNotificationListener(pBroadcaster, pListener, pFilter, pHandback); 581 } 582 else 583 { 584 try 585 { 586 mbeanServer.invoke(mManagementService, 587 "addNotificationListener", 588 new Object []{ 589 pBroadcaster, 590 pListener, 591 pFilter, 592 pHandback 593 }, 594 new String []{ 595 ObjectName.class.getName(), 596 ObjectName.class.getName(), 597 NotificationFilter.class.getName(), 598 Object .class.getName() 599 }); 600 } 601 catch (MBeanException me) 603 { 604 log.error("addNotificationListener() got exception from cluster service", me); 605 } 606 catch (ReflectionException re) 607 { 608 log.error("addNotificationListener() got exception from cluster service", re); 609 } 610 } 611 } 612 613 617 public void removeNotificationListener(ObjectName pBroadcaster, 618 ObjectName pListener) 619 throws 620 InstanceNotFoundException, 621 ListenerNotFoundException, 622 RemoteException 623 { 624 if (mManagementService == null) 625 { 626 mbeanServer.removeNotificationListener(pBroadcaster, pListener); 627 } 628 else 629 { 630 try 631 { 632 mbeanServer.invoke(mManagementService, 633 "removeNotificationListener", 634 new Object []{ 635 pBroadcaster, 636 pListener 637 }, 638 new String []{ 639 ObjectName.class.getName(), 640 ObjectName.class.getName() 641 }); 642 } 643 catch (MBeanException me) 645 { 646 log.error("removeNotificationListener() got exception from cluster service", me); 647 } 648 catch (ReflectionException re) 649 { 650 log.error("removeNotificationListener() got exception from cluster service", re); 651 } 652 } 653 } 654 655 662 public void ejbCreate() 663 throws 664 CreateException 665 { 666 if (mbeanServer == null) 667 { 668 try 669 { 670 Context jndiCtx = new InitialContext (); 671 String serverName = (String ) jndiCtx.lookup("java:comp/env/Server-Name"); 672 serverName = serverName.trim(); 673 if (serverName == null || serverName.length() == 0 || serverName.equals("null")) 674 { 675 try 676 { 677 mbeanServer = MBeanServerLocator.locateJBoss(); 678 } 679 catch (IllegalStateException e) 680 { 681 throw new CreateException ("No local JMX MBeanServer available"); 682 } 683 } 684 else 685 { 686 Object lServer = jndiCtx.lookup(serverName); 687 if (lServer != null) 688 { 689 if (lServer instanceof MBeanServer) 690 { 691 mbeanServer = (MBeanServer) lServer; 692 } 693 else 694 { 695 throw new CreateException ("Server: " + lServer + " reference by Server-Name: " + serverName + 696 " is not of type MBeanServer"); 697 } 698 } 699 else 700 { 701 throw new CreateException ("Server-Name " + serverName 702 + " does not reference an Object in JNDI"); 703 } 704 } 705 } 706 catch (NamingException ne) 707 { 708 throw new EJBException (ne); 709 } 710 } 711 712 try 714 { 715 ObjectName haManagement = new ObjectName("jboss:service=HAManagement"); 716 ObjectInstance oi = mbeanServer.getObjectInstance(haManagement); 717 mManagementService = oi.getObjectName(); 718 } 719 catch (Exception e) 720 { 721 log.debug("ejbCreate() failed to locate jboss:service=HAManagement", e); 722 } 723 } 724 725 730 public String toString() 731 { 732 return "Management [ " + " ]"; 733 } 734 735 739 749 public void setSessionContext(SessionContext aContext) 750 throws 751 EJBException 752 { 753 mContext = aContext; 754 } 755 756 757 766 public void ejbActivate() 767 throws 768 EJBException 769 { 770 } 771 772 773 785 public void ejbPassivate() 786 throws 787 EJBException 788 { 789 } 790 791 792 802 public void ejbRemove() 803 throws 804 EJBException 805 { 806 } 807 808 } 809 | Popular Tags |