1 25 package org.objectweb.jonas.mejb; 26 27 import java.io.IOException ; 28 import java.rmi.RemoteException ; 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 import java.util.Set ; 32 33 import javax.ejb.CreateException ; 34 import javax.ejb.SessionBean ; 35 import javax.ejb.SessionContext ; 36 import javax.management.Attribute ; 37 import javax.management.AttributeList ; 38 import javax.management.AttributeNotFoundException ; 39 import javax.management.InstanceAlreadyExistsException ; 40 import javax.management.InstanceNotFoundException ; 41 import javax.management.IntrospectionException ; 42 import javax.management.InvalidAttributeValueException ; 43 import javax.management.MBeanAttributeInfo ; 44 import javax.management.MBeanException ; 45 import javax.management.MBeanInfo ; 46 import javax.management.MBeanOperationInfo ; 47 import javax.management.MBeanServer ; 48 import javax.management.MBeanServerConnection ; 49 import javax.management.MalformedObjectNameException ; 50 import javax.management.ObjectName ; 51 import javax.management.QueryExp ; 52 import javax.management.ReflectionException ; 53 import javax.management.j2ee.ListenerRegistration ; 54 55 import org.objectweb.jonas.common.Log; 56 import org.objectweb.jonas.jmx.J2eeObjectName; 57 import org.objectweb.jonas.jmx.JmxService; 58 import org.objectweb.jonas.management.j2eemanagement.ManagementListener; 59 import org.objectweb.jonas.management.j2eemanagement.ManagementListenerMBean; 60 import org.objectweb.jonas.service.ServiceManager; 61 import org.objectweb.util.monolog.api.BasicLevel; 62 import org.objectweb.util.monolog.api.Logger; 63 64 81 public class ManagementBean implements SessionBean , ManagementEndpoint { 82 83 86 private static Logger logger = Log.getLogger(Log.JONAS_MEJB); 87 90 private SessionContext sessionContext = null; 91 95 private JmxService jmxService = null; 96 99 private MBeanServerConnection jmxServerConnection = null; 100 103 private String serverName = null; 104 107 private String domainName = null; 108 111 private String proxyName = "MEJB_listener"; 112 113 118 public void ejbCreate() throws CreateException { 119 try { 120 jmxService = (JmxService) ServiceManager.getInstance().getJmxService(); 121 serverName = jmxService.getJonasServerName(); 122 domainName = jmxService.getDomainName(); 123 jmxServerConnection = (MBeanServerConnection ) jmxService.getJmxServer(); 124 } catch (Exception e) { 125 throw new CreateException ("Could not create Management bean: " + e.getMessage()); 126 } 127 } 128 129 130 131 public void ejbActivate() { 132 } 134 public void ejbPassivate() { 135 } 137 public void ejbRemove() { 138 } 140 144 public void setSessionContext(SessionContext sessionContext) { 145 this.sessionContext = sessionContext; 146 } 147 148 149 150 151 152 public Object getAttribute(ObjectName name, String attribute) throws 153 MBeanException , 154 AttributeNotFoundException , 155 InstanceNotFoundException , 156 ReflectionException , 157 RemoteException { 158 159 try { 160 return jmxServerConnection.getAttribute(name, attribute); 161 } catch (java.io.IOException ioe) { 162 throw new RemoteException ("Object getAttribute(ObjectName, String) failed", ioe); 163 } 164 } 165 166 public AttributeList getAttributes(ObjectName name, String [] attributes) throws 167 InstanceNotFoundException , 168 ReflectionException , 169 RemoteException { 170 171 try { 172 return jmxServerConnection.getAttributes(name, attributes); 173 } catch (java.io.IOException ioe) { 174 throw new RemoteException ("AttributeList getAttributes(ObjectName, String[]) failed", ioe); 175 } 176 } 177 178 public String getDefaultDomain() throws RemoteException { 179 180 try { 181 return jmxServerConnection.getDefaultDomain(); 182 } catch (java.io.IOException ioe) { 183 throw new RemoteException ("String getDefaultDomain() failed", ioe); 184 } 185 } 186 187 public Integer getMBeanCount() throws RemoteException { 188 189 try { 190 return jmxServerConnection.getMBeanCount(); 191 } catch (java.io.IOException ioe) { 192 throw new RemoteException ("Integer getMBeanCount() failed", ioe); 193 } 194 } 195 196 public MBeanInfo getMBeanInfo(ObjectName name) throws 197 IntrospectionException , 198 InstanceNotFoundException , 199 ReflectionException , 200 RemoteException { 201 202 try { 203 return jmxServerConnection.getMBeanInfo(name); 204 } catch (java.io.IOException ioe) { 205 throw new RemoteException ("MBeanInfo getMBeanInfo(ObjectName) failed", ioe); 206 } 207 } 208 209 public Object invoke(ObjectName name, String operationName, Object [] params, String [] signature) throws 210 MBeanException , 211 InstanceNotFoundException , 212 ReflectionException , 213 RemoteException { 214 215 try { 216 return jmxServerConnection.invoke(name, operationName, params, signature); 217 } catch (java.io.IOException ioe) { 218 throw new RemoteException ("Object invoke(ObjectName, String, Object[], String[]) failed", ioe); 219 } 220 } 221 222 public boolean isRegistered(ObjectName name) throws RemoteException { 223 224 try { 225 return jmxServerConnection.isRegistered(name); 226 } catch (java.io.IOException ioe) { 227 throw new RemoteException ("boolean isRegistered(ObjectName) failed", ioe); 228 } 229 } 230 231 public Set queryNames(ObjectName name, QueryExp query) throws RemoteException { 232 try { 233 return jmxServerConnection.queryNames(name, query); 234 } catch (java.io.IOException ioe) { 235 throw new RemoteException ("Set queryNames(ObjectName, QueryExp) failed", ioe); 236 } 237 } 238 239 public void setAttribute(ObjectName name, Attribute attribute) throws 240 MBeanException , 241 AttributeNotFoundException , 242 InstanceNotFoundException , 243 InvalidAttributeValueException , 244 ReflectionException , 245 RemoteException { 246 247 try { 248 jmxServerConnection.setAttribute(name, attribute); 249 } catch (java.io.IOException ioe) { 250 throw new RemoteException ("void setAttribute(ObjectName, Attribute) failed", ioe); 251 } 252 } 253 254 public AttributeList setAttributes(ObjectName name, AttributeList attributes) throws 255 InstanceNotFoundException , 256 ReflectionException , 257 RemoteException { 258 259 try { 260 return jmxServerConnection.setAttributes(name, attributes); 261 } catch (java.io.IOException ioe) { 262 throw new RemoteException ("AttributeList setAttributes(ObjectName, AttributeList) failed", ioe); 263 } 264 } 265 266 273 public ListenerRegistration getListenerRegistry() throws RemoteException { 274 ObjectName listenerOn = J2eeObjectName.ManagementListener(domainName, serverName); 276 boolean isRegisteredListener; 277 isRegisteredListener = jmxService.getJmxServer().isRegistered(listenerOn); 278 if (!isRegisteredListener) { 279 ManagementListenerMBean listenerMBean = new ManagementListener(proxyName); 280 try { 281 jmxService.getJmxServer().registerMBean(listenerMBean, listenerOn); 282 } catch (InstanceAlreadyExistsException ae) { 283 } catch (Exception e) { 285 throw new RemoteException ("Can not return ListenerRegistration implementation", e); 287 } 288 } 289 return new ListenerRegistrationImpl(listenerOn, proxyName); 291 } 292 293 294 295 301 private MBeanServerConnection getServerConnection (String domainServerName) { 302 if (domainServerName == null || domainServerName.equals(serverName)) { 304 return jmxServerConnection; 305 } 306 return jmxService.getServerConnection(domainServerName); 307 } 308 309 public Object getAttribute (String domainServerName, ObjectName name, String attribute) throws 310 AttributeNotFoundException , 311 InstanceNotFoundException , 312 MBeanException , 313 ReflectionException , 314 RemoteException { 315 316 MBeanServerConnection connection = getServerConnection(domainServerName); 317 if (connection == null) { 318 throw new RemoteException ("Could not connect to server " + domainServerName); 319 } try { 320 return connection.getAttribute(name, attribute); 321 } catch (IOException ioe) { 322 throw new RemoteException ("Object getAttribute(String, ObjectName, String) failed", ioe); 323 } 324 } 325 326 327 public AttributeList getAttributes(String domainServerName, ObjectName name, String [] attributes) throws 328 InstanceNotFoundException , 329 ReflectionException , 330 RemoteException { 331 332 MBeanServerConnection connection = getServerConnection(domainServerName); 333 if (connection == null) { 334 throw new RemoteException ("Could not connect to server " + domainServerName); 335 } try { 336 return connection.getAttributes(name, attributes); 337 } catch (IOException ioe) { 338 throw new RemoteException ("AttributeList getAttributes(String, ObjectName, String[]) failed", ioe); 339 } 340 } 341 342 343 344 public Integer getMBeanCount(String domainServerName) throws RemoteException { 345 346 MBeanServerConnection connection = getServerConnection(domainServerName); 347 if (connection == null) { 348 throw new RemoteException ("Could not connect to server " + domainServerName); 349 } try { 350 return connection.getMBeanCount(); 351 } catch (IOException ioe) { 352 throw new RemoteException ("Integer getMBeanCount(String) failed", ioe); 353 } 354 } 355 356 357 public MBeanInfo getMBeanInfo(String domainServerName, ObjectName name) throws 358 IntrospectionException , 359 InstanceNotFoundException , 360 ReflectionException , 361 RemoteException { 362 363 MBeanServerConnection connection = getServerConnection (domainServerName); 364 if (connection == null) { 365 throw new RemoteException ("Could not connect to server " + domainServerName); 366 } try { 367 return connection.getMBeanInfo(name); 368 } catch (IOException ioe) { 369 throw new RemoteException ("MBeanInfo getMbeanInfo(String, ObjectName) failed", ioe); 370 } 371 } 372 373 public Object invoke(String domainServerName, ObjectName name, String operationName, Object [] params, String [] signature) throws 374 MBeanException , 375 InstanceNotFoundException , 376 ReflectionException , 377 RemoteException { 378 379 MBeanServerConnection connection = getServerConnection (domainServerName); 380 if (connection == null) { 381 throw new RemoteException ("Could not connect to server " + domainServerName); 382 } try { 383 return connection.invoke (name, operationName, params, signature); 384 } catch (IOException ioe) { 385 throw new RemoteException ("Object invoke(String, ObjectName, String, Object[], String[]) failed", ioe); 386 } 387 } 388 389 public boolean isRegistered(String domainServerName, ObjectName name) throws RemoteException { 390 391 MBeanServerConnection connection = getServerConnection(domainServerName); 392 if (connection == null) { 393 throw new RemoteException ("Could not connect to server " + domainServerName); 394 } try { 395 return connection.isRegistered(name); 396 } catch (java.io.IOException ioe) { 397 throw new RemoteException ("boolean isRegistered(String, ObjectName) failed", ioe); 398 } 399 } 400 401 public Set queryNames(String domainServerName, ObjectName name, QueryExp query) throws RemoteException { 402 403 MBeanServerConnection connection = getServerConnection(domainServerName); 404 if (connection == null) { 405 throw new RemoteException ("Could not connect to server " + domainServerName); 406 } try { 407 return connection.queryNames (name, query); 408 } catch (java.io.IOException ioe) { 409 throw new RemoteException ("Set queryNames(String, ObjectName, QueryExp) failed", ioe); 410 } 411 } 412 413 public void setAttribute(String domainServerName, ObjectName name, Attribute attribute) throws 414 MBeanException , 415 AttributeNotFoundException , 416 InstanceNotFoundException , 417 InvalidAttributeValueException , 418 ReflectionException , 419 RemoteException { 420 421 MBeanServerConnection connection = getServerConnection(domainServerName); 422 if (connection == null) { 423 throw new RemoteException ("Could not connect to server " + domainServerName); 424 } try { 425 connection.setAttribute (name, attribute); 426 } catch (java.io.IOException ioe) { 427 throw new RemoteException ("void setAttribute(String, ObjectName, Attribute) failed", ioe); 428 } 429 } 430 431 public AttributeList setAttributes(String domainServerName, ObjectName name, AttributeList attributes) throws 432 InstanceNotFoundException , 433 ReflectionException , 434 RemoteException { 435 436 MBeanServerConnection connection = getServerConnection(domainServerName); 437 if (connection == null) { 438 throw new RemoteException ("Could not connect to server " + domainServerName); 439 } try { 440 return connection.setAttributes(name, attributes); 441 } catch (java.io.IOException ioe) { 442 throw new RemoteException ("AttributeList setAttributes(String, ObjectName, AttributeList) failed", ioe); 443 } 444 } 445 446 447 448 449 450 464 465 471 public String [] getServers() throws ManagementEndpointException, RemoteException { 472 String j2eeDomainName = domainName + ":j2eeType=J2EEDomain,name=" + this.domainName; 473 return getAttribute(serverName, j2eeDomainName, "serverNames"); 474 } 475 476 477 480 public String [] getAttribute(String domainServerName, String objectName, String attribute) 481 throws ManagementEndpointException { 482 try { 483 return getObjectValue(getAttribute(domainServerName, new ObjectName (objectName), attribute)); 484 } catch (Exception e) { 485 ManagementEndpointException mex = new ManagementEndpointException(); 486 mex.setExceptionType(e.getClass().toString()); 487 mex.setMessage("Problem in getAttribute service call for objectname: " + objectName 488 + " - the request was not completed: " + e.getMessage()); 489 logger.log(BasicLevel.ERROR, mex.getMessage(), e); 490 throw (ManagementEndpointException) mex.initCause(e); 491 } 492 } 493 494 497 public boolean isRegistered(String domainServerName, String objectName) throws ManagementEndpointException { 498 try { 499 return isRegistered(domainServerName, new ObjectName (objectName)); 500 } catch (Exception e) { 501 ManagementEndpointException mex = new ManagementEndpointException(); 502 mex.setExceptionType(e.getClass().toString()); 503 mex.setMessage("Problem in isRegistered service call for objectname: " + objectName 504 + " - the request was not completed: " + e.getMessage()); 505 logger.log(BasicLevel.ERROR, mex.getMessage(), e); 506 throw (ManagementEndpointException) mex.initCause(e); 507 } 508 } 509 510 513 public String [] queryNames(String domainServerName, String objectName, String query) throws ManagementEndpointException { 514 try { 515 516 return getObjectValue(queryNames(domainServerName, new ObjectName (objectName), null)); 518 519 } catch (Exception e) { 520 ManagementEndpointException mex = new ManagementEndpointException(); 521 mex.setExceptionType(e.getClass().toString()); 522 mex.setMessage("Problem in queryNames service call for objectname: " + objectName 523 + " - the request was not completed: " + e.getMessage()); 524 logger.log(BasicLevel.ERROR, mex.getMessage(), e); 525 throw (ManagementEndpointException) mex.initCause(e); 526 } 527 } 528 529 532 public String [] getAttributesList(String domainServerName, String objectName) throws ManagementEndpointException { 533 try { 534 MBeanInfo info = getMBeanInfo(domainServerName, new ObjectName (objectName)); 535 MBeanAttributeInfo [] attrInfo = info.getAttributes(); 536 String [] attrs = new String [attrInfo.length]; 537 for (int i = 0; i < attrs.length; i++) { 538 attrs[i] = attrInfo[i].getName(); 539 } 540 return attrs; 541 542 } catch (Exception e) { 543 ManagementEndpointException mex = new ManagementEndpointException(); 544 mex.setExceptionType(e.getClass().toString()); 545 mex.setMessage("Problem in getAttributeList service call for objectname: " + objectName 546 + " - the request was not completed: " + e.getMessage()); 547 logger.log(BasicLevel.ERROR, mex.getMessage(), e); 548 throw (ManagementEndpointException) mex.initCause(e); 549 } 550 } 551 552 555 public String getDescription(String domainServerName, String objectName) 556 throws ManagementEndpointException { 557 try { 558 MBeanInfo info = getMBeanInfo(domainServerName, new ObjectName (objectName)); 559 return info.getDescription(); 560 } catch (Exception e) { 561 ManagementEndpointException mex = new ManagementEndpointException(); 562 mex.setExceptionType(e.getClass().toString()); 563 mex.setMessage("Problem in getDescription service call for objectname: " + objectName 564 + " - the request was not completed: " + e.getMessage()); 565 logger.log(BasicLevel.ERROR, mex.getMessage(), e); 566 throw (ManagementEndpointException) mex.initCause(e); 567 } 568 569 } 570 571 574 public String [] getOperations(String domainServerName, String objectName) 575 throws ManagementEndpointException { 576 try { 577 MBeanInfo info = getMBeanInfo(domainServerName, new ObjectName (objectName)); 578 MBeanOperationInfo [] operationInfo = info.getOperations(); 579 String [] operations = new String [operationInfo.length]; 580 for (int i = 0; i < operationInfo.length; i++) { 581 operations[i] = operationInfo[i].getName(); 582 } 583 return operations; 584 585 } catch (Exception e) { 586 ManagementEndpointException mex = new ManagementEndpointException(); 587 mex.setExceptionType(e.getClass().toString()); 588 mex.setMessage("Problem in getOperations service call for objectname: " + objectName 589 + " - the request was not completed: " + e.getMessage()); 590 logger.log(BasicLevel.ERROR, mex.getMessage(), e); 591 throw (ManagementEndpointException) mex.initCause(e); 592 } 593 } 594 595 602 603 610 private String [] getObjectValue(Object objectValue) { 611 String [] value = null; 612 613 if (objectValue == null) { 614 value = new String [1]; 615 value[0] = "null"; 616 } else { 617 if (objectValue.getClass().isArray()) { 619 value = arrayToString((Object []) objectValue); 621 } else { 622 try { 623 value = collectionToString((Collection ) objectValue); 625 } catch (Exception e) { 626 value = new String [1]; 628 value[0] = objectValue.toString(); 629 } 630 } 631 } 632 return value; 633 } 634 635 640 private String [] arrayToString(Object [] pArray) { 641 String [] retStringArr = new String [pArray.length]; 642 for (int i = 0; i < pArray.length; i++) { 643 if (pArray[i] == null) { 644 retStringArr[i] = "null"; 645 } else { 646 retStringArr[i] = pArray[i].toString(); 647 } 648 649 } 650 return retStringArr; 651 } 652 653 658 private String [] collectionToString(Collection pCollection) { 659 String [] retStringArr = new String [pCollection.size()]; 660 Iterator it = pCollection.iterator(); 661 int i = 0; 662 while (it.hasNext()) { 663 retStringArr[i++] = it.next().toString(); 664 } 665 return retStringArr; 666 } 667 668 } | Popular Tags |