1 23 24 29 30 package com.sun.enterprise.admin.jmx.remote.internal; 31 32 import java.util.Set ; 33 34 import java.util.Map ; 35 36 import java.util.logging.Logger ; 37 import java.io.IOException ; 38 39 import com.sun.enterprise.admin.jmx.remote.DefaultConfiguration; 40 41 import com.sun.enterprise.admin.jmx.remote.comm.IConnection; 42 import com.sun.enterprise.admin.jmx.remote.comm.HttpConnectorAddress; 43 import com.sun.enterprise.admin.jmx.remote.comm.ConnectionFactory; 44 import com.sun.enterprise.admin.jmx.remote.comm.MBeanServerMessageConductor; 45 import com.sun.enterprise.admin.jmx.remote.internal.MBeanServerConnectionExceptionThrower; 46 47 import com.sun.enterprise.admin.jmx.remote.notification.ListenerInfo; 48 import com.sun.enterprise.admin.jmx.remote.notification.ClientNotificationManager; 49 50 51 import javax.management.*; 52 import javax.management.remote.message.MBeanServerRequestMessage; 53 import javax.management.remote.message.MBeanServerResponseMessage; 54 import com.sun.enterprise.admin.jmx.remote.protocol.Version; 55 56 71 72 public class RemoteMBeanServerConnection implements MBeanServerConnection { 73 74 private IConnection physicalConnection = null; 75 private MBeanServerMessageConductor conductor = null; 76 private HttpConnectorAddress ad = null; 77 78 private ClientNotificationManager notifMgr = null; 79 private Map env = null; 80 81 82 private static Version cv; 83 static { 84 try { 85 cv = (Version)Class.forName(Version.CLASS_NAME).newInstance(); 86 } 87 catch(Exception e) { 88 throw new RuntimeException (e); 89 } 90 } 91 private final static Object EMPTY = new String (); 92 96 97 private final Logger logger = Logger.getLogger( 98 DefaultConfiguration.JMXCONNECTOR_LOGGER); 100 101 public RemoteMBeanServerConnection(IConnection connectionToServer) { 102 } 105 106 109 110 public RemoteMBeanServerConnection(HttpConnectorAddress ad, Map env) throws Exception { 112 113 this.ad = ad; 114 115 this.env = env; 116 117 connect(); 118 119 Boolean enabled = (Boolean ) env.get(DefaultConfiguration.NOTIF_ENABLED_PROPERTY_NAME); 120 121 if (enabled != null && enabled.booleanValue() == true) { 122 notifMgr = new ClientNotificationManager(ad, env); 123 } 124 125 logger.finer("Connected to: Address = " + ad.getHost() + ":" + ad.getPort()); 126 } 127 128 129 public ClientNotificationManager getNotificationManager() { 130 return notifMgr; 131 } 132 133 private void checkNotifInit() throws IOException { 134 if (notifMgr == null) 135 return; 136 notifMgr.reinit(); 137 } 138 139 140 private void connect() throws java.io.IOException { 141 physicalConnection = ConnectionFactory.createConnection(ad); 142 conductor = new MBeanServerMessageConductor(physicalConnection); 143 } 144 145 public void addNotificationListener(ObjectName objectName, 146 NotificationListener notificationListener, 147 NotificationFilter notificationFilter, Object obj) throws 148 InstanceNotFoundException, IOException { 149 try { 150 151 if (notifMgr == null) 152 return; checkNotifInit(); 154 String id = notifMgr.addNotificationListener( 155 objectName, 156 notificationListener, 157 notificationFilter, 158 obj); 159 160 connect(); 161 final MBeanServerResponseMessage response = conductor.invoke( 162 MBeanServerRequestMessage.ADD_NOTIFICATION_LISTENERS, 163 164 toArray(objectName, notifMgr.getId(), id, null) ); 165 167 MBeanServerResponseActor.voidOrThrow(response); 168 } 169 catch (Exception e) { 170 MBeanServerConnectionExceptionThrower.addNotificationListenerObjectName(e); 171 } 172 } 173 174 public void addNotificationListener(ObjectName objectName, 175 ObjectName objectName1, NotificationFilter notificationFilter, 176 Object obj) throws InstanceNotFoundException, IOException { 177 try { 178 179 checkNotifInit(); 180 181 connect(); 182 ListenerInfo info = new ListenerInfo(null, notificationFilter, obj); 183 final MBeanServerResponseMessage response = conductor.invoke( 184 MBeanServerRequestMessage.ADD_NOTIFICATION_LISTENER_OBJECTNAME, 185 toArray(objectName, objectName1, notificationFilter, obj, info.computeId()) ); 186 MBeanServerResponseActor.voidOrThrow(response); 187 } 188 catch(Exception e) { 189 MBeanServerConnectionExceptionThrower.addNotificationListeners(e); 190 } 191 } 192 193 public ObjectInstance createMBean(String str, ObjectName objectName) throws 194 ReflectionException, InstanceAlreadyExistsException, 195 MBeanRegistrationException, MBeanException, NotCompliantMBeanException, 196 IOException { 197 try { 198 199 checkNotifInit(); 200 201 connect(); 202 final MBeanServerResponseMessage response = conductor.invoke( 203 MBeanServerRequestMessage.CREATE_MBEAN, toArray(str, objectName) ); 204 return ( (ObjectInstance) MBeanServerResponseActor.returnOrThrow(response) ); 206 } 207 catch (Exception e) { 208 MBeanServerConnectionExceptionThrower.createMBean(e); 209 return null; 210 } 211 } 212 213 public ObjectInstance createMBean(String str, ObjectName objectName, ObjectName loaderName) 214 throws ReflectionException, InstanceAlreadyExistsException, 215 MBeanRegistrationException, MBeanException, NotCompliantMBeanException, 216 InstanceNotFoundException, IOException { 217 try { 218 219 checkNotifInit(); 220 221 connect(); 222 final MBeanServerResponseMessage response = conductor.invoke( 223 MBeanServerRequestMessage.CREATE_MBEAN_LOADER, toArray(str, objectName, loaderName) ); 224 return ( (ObjectInstance) MBeanServerResponseActor.returnOrThrow(response) ); 226 } 227 catch(Exception e) { 228 MBeanServerConnectionExceptionThrower.createMBeanLoader(e); 229 return null; 230 } 231 } 232 233 public ObjectInstance createMBean(String str, ObjectName objectName, 234 Object [] params, String [] signature) throws ReflectionException, 235 InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, 236 NotCompliantMBeanException, IOException { 237 try { 238 239 checkNotifInit(); 240 241 connect(); 242 final MBeanServerResponseMessage response = conductor.invoke( 243 MBeanServerRequestMessage.CREATE_MBEAN_PARAMS, 244 toArray(str, objectName, params, signature) ); 245 return ( (ObjectInstance) MBeanServerResponseActor.returnOrThrow(response)); 247 } 248 catch(Exception e) { 249 MBeanServerConnectionExceptionThrower.createMBeanParams(e); 250 return null; 251 } 252 } 253 254 public ObjectInstance createMBean(String str, ObjectName objectName, 255 ObjectName loaderName, Object [] params, String [] signature) 256 throws ReflectionException, InstanceAlreadyExistsException, 257 MBeanRegistrationException, MBeanException, NotCompliantMBeanException, 258 InstanceNotFoundException, IOException { 259 try { 260 261 checkNotifInit(); 262 263 connect(); 264 final MBeanServerResponseMessage response = conductor.invoke( 265 MBeanServerRequestMessage.CREATE_MBEAN_LOADER_PARAMS, 266 toArray(str, objectName, loaderName, params, signature) ); 267 return ( (ObjectInstance)MBeanServerResponseActor.returnOrThrow(response) ); 269 } 270 catch(Exception e) { 271 MBeanServerConnectionExceptionThrower.createMBeanLoaderParams(e); 272 return null; 273 } 274 } 275 276 public Object getAttribute(ObjectName objectName, String str) throws 277 MBeanException, AttributeNotFoundException, InstanceNotFoundException, 278 ReflectionException, IOException { 279 try { 280 281 checkNotifInit(); 282 283 connect(); 284 final MBeanServerResponseMessage response = conductor.invoke( 285 MBeanServerRequestMessage.GET_ATTRIBUTE, toArray(objectName, str) ); 286 return ( MBeanServerResponseActor.returnOrThrow(response) ); 287 } 288 catch(Exception e) { 289 MBeanServerConnectionExceptionThrower.getAttribute(e); 290 return null; 291 } 292 } 293 294 public AttributeList getAttributes(ObjectName objectName, String [] attributes) 295 throws InstanceNotFoundException, ReflectionException, IOException { 296 try { 297 298 checkNotifInit(); 299 300 connect(); 301 final MBeanServerResponseMessage response = conductor.invoke( 302 MBeanServerRequestMessage.GET_ATTRIBUTES, 303 toArray(objectName, attributes) ); 304 return ( (AttributeList) MBeanServerResponseActor.returnOrThrow(response) ); 306 } 307 catch(Exception e) { 308 MBeanServerConnectionExceptionThrower.getAttributes(e); 309 return null; 310 } 311 } 312 313 public String getDefaultDomain() throws IOException { 314 try { 315 316 checkNotifInit(); 317 318 connect(); 319 final MBeanServerResponseMessage response = conductor.invoke( 320 MBeanServerRequestMessage.GET_DEFAULT_DOMAIN, toArray(EMPTY)); 321 return ( (String ) MBeanServerResponseActor.returnOrThrow(response) ); 323 } 324 catch(Exception e) { 325 MBeanServerConnectionExceptionThrower.getDefaultDomain(e); 326 return null; 327 } 328 } 329 330 public String [] getDomains() throws IOException { 331 try { 332 333 checkNotifInit(); 334 335 connect(); 336 final MBeanServerResponseMessage response = conductor.invoke( 337 MBeanServerRequestMessage.GET_DOMAINS, toArray(EMPTY)); 338 return ( (String []) MBeanServerResponseActor.returnOrThrow(response) ); 340 } 341 catch(Exception e) { 342 MBeanServerConnectionExceptionThrower.getDomains(e); 343 return null; 344 } 345 } 346 347 public Integer getMBeanCount() throws IOException { 348 try { 349 350 checkNotifInit(); 351 352 connect(); 353 final MBeanServerResponseMessage response = conductor.invoke( 354 MBeanServerRequestMessage.GET_MBEAN_COUNT, toArray(EMPTY)); 355 return ( (Integer ) MBeanServerResponseActor.returnOrThrow(response) ); 357 } 358 catch(Exception e) { 359 MBeanServerConnectionExceptionThrower.getMBeanCount(e); 360 return null; 361 } 362 } 363 364 public MBeanInfo getMBeanInfo(ObjectName objectName) throws 365 InstanceNotFoundException, IntrospectionException, ReflectionException, IOException { 366 try { 367 368 checkNotifInit(); 369 370 connect(); 371 final MBeanServerResponseMessage response = conductor.invoke( 372 MBeanServerRequestMessage.GET_MBEAN_INFO, toArray(objectName)); 373 return ( (MBeanInfo) MBeanServerResponseActor.returnOrThrow(response) ); 375 } 376 catch(Exception e) { 377 MBeanServerConnectionExceptionThrower.getMBeanInfo(e); 378 return null; 379 } 380 } 381 382 public ObjectInstance getObjectInstance(ObjectName objectName) throws 383 InstanceNotFoundException, IOException { 384 try { 385 386 checkNotifInit(); 387 388 connect(); 389 final MBeanServerResponseMessage response = conductor.invoke( 390 MBeanServerRequestMessage.GET_OBJECT_INSTANCE, toArray(objectName)); 391 return ( (ObjectInstance) MBeanServerResponseActor.returnOrThrow(response) ); 393 } 394 catch(Exception e) { 395 MBeanServerConnectionExceptionThrower.getObjectInstance(e); 396 return null; 397 } 398 } 399 400 public Object invoke(ObjectName objectName, String methodName, 401 Object [] params, String [] signature) throws InstanceNotFoundException, 402 MBeanException, ReflectionException, IOException { 403 try { 404 405 checkNotifInit(); 406 407 connect(); 408 final MBeanServerResponseMessage response = conductor.invoke( 409 MBeanServerRequestMessage.INVOKE, 410 toArray(objectName, methodName, params, signature)); 411 return ( MBeanServerResponseActor.returnOrThrow(response) ); 412 } 413 catch(Exception e) { 414 MBeanServerConnectionExceptionThrower.invoke(e); 415 return null; 416 } 417 } 418 419 public boolean isInstanceOf(ObjectName objectName, String className) throws 420 InstanceNotFoundException, IOException { 421 try { 422 423 checkNotifInit(); 424 425 connect(); 426 final MBeanServerResponseMessage response = conductor.invoke( 427 MBeanServerRequestMessage.IS_INSTANCE_OF, 428 toArray(objectName, className)); 429 return ( ((Boolean ) MBeanServerResponseActor.returnOrThrow(response)).booleanValue() ); 431 } 432 catch(Exception e) { 433 MBeanServerConnectionExceptionThrower.isInstanceOf(e); 434 return false; 435 } 436 } 437 438 public boolean isRegistered(ObjectName objectName) throws IOException { 439 try { 440 441 checkNotifInit(); 442 443 connect(); 444 final MBeanServerResponseMessage response = conductor.invoke( 445 MBeanServerRequestMessage.IS_REGISTERED, 446 toArray(objectName)); 447 return ( ((Boolean ) MBeanServerResponseActor.returnOrThrow(response)).booleanValue() ); 449 } 450 catch(Exception e) { 451 MBeanServerConnectionExceptionThrower.isRegistered(e); 452 return false; 453 } 454 } 455 456 public Set queryMBeans(ObjectName objectName, QueryExp queryExp) 457 throws IOException { 458 try { 459 460 checkNotifInit(); 461 462 connect(); 463 final MBeanServerResponseMessage response = conductor.invoke( 464 MBeanServerRequestMessage.QUERY_MBEANS, toArray(objectName, queryExp)); 465 return ( (Set ) MBeanServerResponseActor.returnOrThrow(response)); 467 } 468 catch(Exception e) { 469 MBeanServerConnectionExceptionThrower.queryMBeans(e); 470 return null; 471 } 472 } 473 474 public Set queryNames(ObjectName objectName, QueryExp queryExp) 475 throws IOException { 476 try { 477 478 checkNotifInit(); 479 480 connect(); 481 final MBeanServerResponseMessage response = conductor.invoke( 482 MBeanServerRequestMessage.QUERY_NAMES, toArray(objectName, queryExp)); 483 return ( (Set ) MBeanServerResponseActor.returnOrThrow(response)); 485 } 486 catch(Exception e) { 487 MBeanServerConnectionExceptionThrower.queryNames(e); 488 return null; 489 } 490 } 491 492 public void removeNotificationListener(ObjectName objectName, NotificationListener notificationListener) 493 throws InstanceNotFoundException, ListenerNotFoundException, java.io.IOException { 494 495 if (notifMgr == null) 496 return; 498 try { 499 500 checkNotifInit(); 501 String [] ids = notifMgr.removeNotificationListener( 502 objectName, 503 notificationListener); 504 505 connect(); 506 final MBeanServerResponseMessage response = conductor.invoke( 507 MBeanServerRequestMessage.REMOVE_NOTIFICATION_LISTENER, 508 509 toArray(objectName, notifMgr.getId(), ids) ); 510 512 MBeanServerResponseActor.voidOrThrow(response); 513 } 514 catch(Exception e) { 515 MBeanServerConnectionExceptionThrower.removeNotificationListener(e); 516 } 517 } 518 519 public void removeNotificationListener(ObjectName objectName, ObjectName objectName1) 520 throws InstanceNotFoundException, ListenerNotFoundException, java.io.IOException { 521 try { 522 523 checkNotifInit(); 524 525 connect(); 526 final MBeanServerResponseMessage response = conductor.invoke( 527 528 MBeanServerRequestMessage.REMOVE_NOTIFICATION_LISTENER_OBJECTNAME, 530 531 toArray(objectName, objectName1) ); 532 MBeanServerResponseActor.voidOrThrow(response); 533 } 534 catch(Exception e) { 535 MBeanServerConnectionExceptionThrower.removeNotificationListenerObjectName(e); 536 } 537 } 538 539 public void removeNotificationListener(ObjectName objectName, ObjectName objectName1, 540 NotificationFilter notificationFilter, Object obj) 541 throws InstanceNotFoundException, ListenerNotFoundException, java.io.IOException { 542 try { 543 544 checkNotifInit(); 545 546 connect(); 547 ListenerInfo info = new ListenerInfo(null, notificationFilter, obj); 548 final MBeanServerResponseMessage response = conductor.invoke( 549 550 554 MBeanServerRequestMessage.REMOVE_NOTIFICATION_LISTENER_OBJECTNAME_FILTER_HANDBACK, 555 toArray(objectName, objectName1, null, null, info.computeId())); 556 557 MBeanServerResponseActor.voidOrThrow(response); 558 } 559 catch(Exception e) { 560 MBeanServerConnectionExceptionThrower.removeNotificationListenerObjectNameFilterHandback(e); 561 } 562 } 563 564 public void removeNotificationListener(ObjectName objectName, 565 NotificationListener notificationListener, NotificationFilter notificationFilter, Object obj) 566 throws InstanceNotFoundException, ListenerNotFoundException, java.io.IOException { 567 568 if (notifMgr == null) 569 return; 571 try { 572 573 checkNotifInit(); 574 String [] ids = notifMgr.removeNotificationListener( 575 objectName, 576 notificationListener, 577 notificationFilter, 578 obj); 579 580 connect(); 581 final MBeanServerResponseMessage response = conductor.invoke( 582 583 MBeanServerRequestMessage.REMOVE_NOTIFICATION_LISTENER_FILTER_HANDBACK, 585 toArray(objectName, notifMgr.getId(), ids.length > 0 ? ids[0] : null, null) ); 586 588 MBeanServerResponseActor.voidOrThrow(response); 589 } 590 catch(Exception e) { 591 MBeanServerConnectionExceptionThrower.removeNotificationListenerFilterHandback(e); 592 } 593 } 594 595 public void setAttribute(ObjectName objectName, Attribute attribute) throws 596 InstanceNotFoundException, AttributeNotFoundException, 597 InvalidAttributeValueException, MBeanException, ReflectionException, IOException { 598 try { 599 600 checkNotifInit(); 601 602 connect(); 603 final MBeanServerResponseMessage response = conductor.invoke( 604 MBeanServerRequestMessage.SET_ATTRIBUTE, toArray(objectName, attribute)); 605 MBeanServerResponseActor.voidOrThrow(response); 607 } 608 catch(Exception e) { 609 MBeanServerConnectionExceptionThrower.setAttribute(e); 610 } 611 } 612 613 public AttributeList setAttributes(ObjectName objectName, AttributeList 614 list) throws InstanceNotFoundException, ReflectionException, IOException { 615 try { 616 617 checkNotifInit(); 618 619 connect(); 620 final MBeanServerResponseMessage response = conductor.invoke( 621 MBeanServerRequestMessage.SET_ATTRIBUTES, toArray(objectName, list)); 622 return ( (AttributeList) MBeanServerResponseActor.returnOrThrow(response) ); 624 } 625 catch(Exception e) { 626 MBeanServerConnectionExceptionThrower.setAttributes(e); 627 return null; 628 } 629 } 630 631 public void unregisterMBean(ObjectName objectName) throws 632 InstanceNotFoundException, MBeanRegistrationException, IOException { 633 try { 634 635 checkNotifInit(); 636 637 connect(); 638 final MBeanServerResponseMessage response = conductor.invoke( 639 MBeanServerRequestMessage.UNREGISTER_MBEAN, toArray(objectName)); 640 MBeanServerResponseActor.voidOrThrow(response); 642 } 643 catch(Exception e) { 644 MBeanServerConnectionExceptionThrower.unregisterMBean(e); 645 } 646 } 647 648 private Object [] toArray(Object param1) { 649 final Shifter s = new Shifter (new Object []{param1}); 650 s.shiftRight(cv); 651 return ( s.state() ); 652 } 653 private Object [] toArray(Object param1, Object param2) { 654 final Shifter s = new Shifter (new Object []{param1, param2}); 655 s.shiftRight(cv); 656 return ( s.state() ); 657 } 658 private Object [] toArray(Object param1, Object param2, Object param3) { 659 final Shifter s = new Shifter (new Object []{param1, param2, param3}); 660 s.shiftRight(cv); 661 return ( s.state() ); 662 } 663 private Object [] toArray(Object param1, Object param2, Object param3, 664 Object param4) { 665 final Shifter s = new Shifter (new Object []{param1, param2, param3, param4}); 666 s.shiftRight(cv); 667 return ( s.state() ); 668 } 669 private Object [] toArray(Object param1, Object param2, Object param3, 670 Object param4, Object param5) { 671 final Shifter s = new Shifter (new Object []{param1, param2, param3, param4, param5}); 672 s.shiftRight(cv); 673 return ( s.state() ); 674 } 675 } 676 | Popular Tags |