1 8 9 package test.javax.management; 10 11 import java.io.IOException ; 12 import java.lang.reflect.UndeclaredThrowableException ; 13 import javax.management.InstanceNotFoundException ; 14 import javax.management.ListenerNotFoundException ; 15 import javax.management.MBeanException ; 16 import javax.management.MBeanNotificationInfo ; 17 import javax.management.MBeanServer ; 18 import javax.management.MBeanServerConnection ; 19 import javax.management.MBeanServerInvocationHandler ; 20 import javax.management.Notification ; 21 import javax.management.NotificationBroadcaster ; 22 import javax.management.NotificationBroadcasterSupport ; 23 import javax.management.NotificationEmitter ; 24 import javax.management.NotificationFilter ; 25 import javax.management.NotificationListener ; 26 import javax.management.ObjectName ; 27 import javax.management.remote.JMXConnector ; 28 import javax.management.remote.JMXConnectorFactory ; 29 import javax.management.remote.JMXConnectorServer ; 30 import javax.management.remote.JMXConnectorServerFactory ; 31 import javax.management.remote.JMXServiceURL ; 32 import javax.naming.NamingException ; 33 34 import test.MX4JTestCase; 35 36 39 public class MBeanServerInvocationHandlerTest extends MX4JTestCase 40 { 41 public MBeanServerInvocationHandlerTest(String s) 42 { 43 super(s); 44 } 45 46 public void testBadArguments() throws Exception 47 { 48 MBeanServer server = newMBeanServer(); 49 ObjectName name = new ObjectName ("domain:key=value"); 50 51 try 52 { 53 MBeanServerInvocationHandler.newProxyInstance(null, name, LocalServiceMBean.class, false); 54 fail("MBeanServerConnection cannot be null"); 55 } 56 catch (IllegalArgumentException x) 57 { 58 } 59 60 try 61 { 62 MBeanServerInvocationHandler.newProxyInstance(server, null, LocalServiceMBean.class, false); 63 fail("ObjectName cannot be null"); 64 } 65 catch (IllegalArgumentException x) 66 { 67 } 68 69 try 70 { 71 MBeanServerInvocationHandler.newProxyInstance(server, name, null, false); 72 fail("Class cannot be null"); 73 } 74 catch (IllegalArgumentException x) 75 { 76 } 77 78 try 79 { 80 MBeanServerInvocationHandler.newProxyInstance(server, name, LocalService.class, false); 81 fail("Class must be an interface"); 82 } 83 catch (IllegalArgumentException x) 84 { 85 } 86 } 87 88 public void testMBeanNotAnEmitter() throws Exception 89 { 90 MBeanServer server = newMBeanServer(); 91 ObjectName name = new ObjectName ("domain:key=value"); 92 LocalService mbean = new LocalService(); 93 server.registerMBean(mbean, name); 94 95 NotificationEmitter emitter = (NotificationEmitter )MBeanServerInvocationHandler.newProxyInstance(server, name, LocalServiceMBean.class, true); 96 97 try 98 { 99 emitter.addNotificationListener(new TestListener(), null, null); 100 fail("The MBean is not a NotificationEmitter"); 101 } 102 catch (IllegalArgumentException x) 103 { 104 } 105 } 106 107 public void testDeregisteredMBean() throws Exception 108 { 109 MBeanServer server = newMBeanServer(); 110 ObjectName name = new ObjectName ("domain:key=value"); 111 LocalService mbean = new LocalService(); 112 server.registerMBean(mbean, name); 113 114 LocalServiceMBean proxy = (LocalServiceMBean)MBeanServerInvocationHandler.newProxyInstance(server, name, LocalServiceMBean.class, false); 116 server.unregisterMBean(name); 117 try 118 { 119 proxy.throwCheckedException(); 120 fail(); 121 } 122 catch (NamingException x) 123 { 124 fail("Expecting an InstanceNotFoundException"); 125 } 126 catch (UndeclaredThrowableException x) 127 { 128 Throwable xx = x.getUndeclaredThrowable(); 129 if (!(xx instanceof InstanceNotFoundException )) 130 fail("Expecting an InstanceNotFoundException"); 131 } 132 } 133 134 public void testCheckedException() throws Exception 135 { 136 MBeanServer server = newMBeanServer(); 137 ObjectName name = new ObjectName ("domain:key=value"); 138 LocalService mbean = new LocalService(); 139 server.registerMBean(mbean, name); 140 141 LocalServiceMBean proxy = (LocalServiceMBean)MBeanServerInvocationHandler.newProxyInstance(server, name, LocalServiceMBean.class, false); 142 try 143 { 144 proxy.throwCheckedException(); 145 fail(); 146 } 147 catch (NamingException x) 148 { 149 } 150 } 151 152 public void testMBeanException() throws Exception 153 { 154 MBeanServer server = newMBeanServer(); 155 ObjectName name = new ObjectName ("domain:key=value"); 156 LocalService mbean = new LocalService(); 157 server.registerMBean(mbean, name); 158 159 LocalServiceMBean proxy = (LocalServiceMBean)MBeanServerInvocationHandler.newProxyInstance(server, name, LocalServiceMBean.class, false); 160 try 161 { 162 proxy.throwMBeanException(); 163 fail(); 164 } 165 catch (MBeanException x) 166 { 167 } 168 } 169 170 public void testRuntimeException() throws Exception 171 { 172 MBeanServer server = newMBeanServer(); 173 ObjectName name = new ObjectName ("domain:key=value"); 174 LocalService mbean = new LocalService(); 175 server.registerMBean(mbean, name); 176 177 LocalServiceMBean proxy = (LocalServiceMBean)MBeanServerInvocationHandler.newProxyInstance(server, name, LocalServiceMBean.class, false); 178 try 179 { 180 proxy.throwNullPointerException(); 181 fail(); 182 } 183 catch (NullPointerException x) 184 { 185 } 186 } 187 188 public void testError() throws Exception 189 { 190 MBeanServer server = newMBeanServer(); 191 ObjectName name = new ObjectName ("domain:key=value"); 192 LocalService mbean = new LocalService(); 193 server.registerMBean(mbean, name); 194 195 LocalServiceMBean proxy = (LocalServiceMBean)MBeanServerInvocationHandler.newProxyInstance(server, name, LocalServiceMBean.class, false); 196 try 197 { 198 proxy.throwError(); 199 fail(); 200 } 201 catch (Error x) 202 { 203 } 204 } 205 206 public void testNotificationBroadcasterProxy() throws Exception 207 { 208 MBeanServer server = newMBeanServer(); 209 ObjectName name = new ObjectName ("domain:key=value"); 210 LocalBroadcasterService mbean = new LocalBroadcasterService(); 211 server.registerMBean(mbean, name); 212 213 NotificationEmitter proxy = (NotificationEmitter )MBeanServerInvocationHandler.newProxyInstance(server, name, NotificationBroadcaster .class, true); 215 assertNotNull(proxy); 216 } 217 218 public void testAddNotificationListener() throws Exception 219 { 220 MBeanServer server = newMBeanServer(); 221 ObjectName name = new ObjectName ("domain:key=value"); 222 LocalEmitterService mbean = new LocalEmitterService(); 223 server.registerMBean(mbean, name); 224 225 NotificationEmitter proxy = (NotificationEmitter )MBeanServerInvocationHandler.newProxyInstance(server, name, NotificationEmitter .class, true); 226 TestListener listener = new TestListener(); 227 proxy.addNotificationListener(listener, null, null); 228 mbean.test(); 229 if (!listener.received) fail(); 230 } 231 232 public void testGetNotificationInfo() throws Exception 233 { 234 MBeanServer server = newMBeanServer(); 235 ObjectName name = new ObjectName ("domain:key=value"); 236 LocalEmitterService mbean = new LocalEmitterService(); 237 server.registerMBean(mbean, name); 238 239 NotificationEmitter proxy = (NotificationEmitter )MBeanServerInvocationHandler.newProxyInstance(server, name, NotificationEmitter .class, true); 240 MBeanNotificationInfo [] infos = proxy.getNotificationInfo(); 241 if (!infos[0].getDescription().equals(LocalEmitterService.DESC)) fail(); 242 } 243 244 public void testSimpleRemoveNotificationListener() throws Exception 245 { 246 MBeanServer server = newMBeanServer(); 247 ObjectName name = new ObjectName ("domain:key=value"); 248 LocalEmitterService mbean = new LocalEmitterService(); 249 server.registerMBean(mbean, name); 250 251 NotificationEmitter proxy = (NotificationEmitter )MBeanServerInvocationHandler.newProxyInstance(server, name, NotificationEmitter .class, true); 252 TestListener listener = new TestListener(); 253 proxy.addNotificationListener(listener, null, null); 254 proxy.removeNotificationListener(listener); 255 mbean.test(); 256 if (listener.received) fail(); 257 } 258 259 public void testRemoveNotificationListener() throws Exception 260 { 261 MBeanServer server = newMBeanServer(); 262 ObjectName name = new ObjectName ("domain:key=value"); 263 LocalEmitterService mbean = new LocalEmitterService(); 264 server.registerMBean(mbean, name); 265 266 NotificationEmitter proxy = (NotificationEmitter )MBeanServerInvocationHandler.newProxyInstance(server, name, NotificationEmitter .class, true); 267 TestListener listener = new TestListener(); 268 TestFilter filter = new TestFilter(); 269 Object handback = new Object (); 270 proxy.addNotificationListener(listener, filter, handback); 271 proxy.removeNotificationListener(listener, filter, handback); 272 mbean.test(); 273 if (listener.received) fail(); 274 } 275 276 public void testRemoveMultiNotificationListener() throws Exception 277 { 278 MBeanServer server = newMBeanServer(); 279 ObjectName name = new ObjectName ("domain:key=value"); 280 LocalEmitterService mbean = new LocalEmitterService(); 281 server.registerMBean(mbean, name); 282 283 NotificationEmitter proxy = (NotificationEmitter )MBeanServerInvocationHandler.newProxyInstance(server, name, NotificationEmitter .class, true); 284 TestListener listener1 = new TestListener(); 285 TestFilter filter = new TestFilter(); 286 Object handback = new Object (); 287 proxy.addNotificationListener(listener1, filter, handback); 288 289 TestListener listener2 = new TestListener(); 290 proxy.addNotificationListener(listener2, null, null); 291 292 mbean.test(); 293 if (!listener1.received) fail(); 294 if (!listener2.received) fail(); 295 296 try 297 { 298 proxy.removeNotificationListener(listener2, filter, handback); 299 fail("Listener is not registered"); 300 } 301 catch (ListenerNotFoundException x) 302 { 303 } 304 305 proxy.removeNotificationListener(listener2, null, null); 306 listener1.received = false; 307 listener2.received = false; 308 309 mbean.test(); 310 if (!listener1.received) fail(); 311 if (listener2.received) fail(); 312 } 313 314 public void testRemoteExceptionWithRemoteInterface() throws Exception 315 { 316 MBeanServer server = newMBeanServer(); 317 ObjectName name = new ObjectName ("domain:key=value"); 318 LocalService mbean = new LocalService(); 319 server.registerMBean(mbean, name); 320 321 JMXServiceURL url = new JMXServiceURL ("service:jmx:rmi://localhost"); 322 JMXConnectorServer cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server); 323 cntorServer.start(); 324 JMXConnector cntor = JMXConnectorFactory.connect(cntorServer.getAddress()); 325 MBeanServerConnection mbsc = cntor.getMBeanServerConnection(); 326 327 RemoteService remoteMBean = (RemoteService)MBeanServerInvocationHandler.newProxyInstance(mbsc, name, RemoteService.class, false); 328 329 cntor.close(); 331 cntorServer.stop(); 332 333 try 334 { 335 remoteMBean.throwCheckedException(); 336 fail("Must not be able to connect"); 337 } 338 catch (IOException x) 339 { 340 } 341 } 342 343 public void testRemoteExceptionWithLocalInterface() throws Exception 344 { 345 MBeanServer server = newMBeanServer(); 346 ObjectName name = new ObjectName ("domain:key=value"); 347 LocalService mbean = new LocalService(); 348 server.registerMBean(mbean, name); 349 350 JMXServiceURL url = new JMXServiceURL ("service:jmx:rmi://localhost"); 351 JMXConnectorServer cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server); 352 cntorServer.start(); 353 JMXConnector cntor = JMXConnectorFactory.connect(cntorServer.getAddress()); 354 MBeanServerConnection mbsc = cntor.getMBeanServerConnection(); 355 356 LocalServiceMBean remoteMBean = (LocalServiceMBean)MBeanServerInvocationHandler.newProxyInstance(mbsc, name, LocalServiceMBean.class, false); 357 358 cntor.close(); 360 cntorServer.stop(); 361 362 try 364 { 365 remoteMBean.throwCheckedException(); 366 fail("Must not be able to connect"); 367 } 368 catch (UndeclaredThrowableException x) 369 { 370 Throwable xx = x.getUndeclaredThrowable(); 371 if (!(xx instanceof IOException )) fail(); 372 } 373 } 374 375 public void testRemoteCheckedException() throws Exception 376 { 377 MBeanServer server = newMBeanServer(); 378 ObjectName name = new ObjectName ("domain:key=value"); 379 LocalService mbean = new LocalService(); 380 server.registerMBean(mbean, name); 381 382 JMXServiceURL url = new JMXServiceURL ("service:jmx:rmi://localhost"); 383 JMXConnectorServer cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server); 384 cntorServer.start(); 385 JMXConnector cntor = JMXConnectorFactory.connect(cntorServer.getAddress()); 386 MBeanServerConnection mbsc = cntor.getMBeanServerConnection(); 387 388 LocalServiceMBean remoteMBean = (LocalServiceMBean)MBeanServerInvocationHandler.newProxyInstance(mbsc, name, LocalServiceMBean.class, false); 389 try 390 { 391 remoteMBean.throwCheckedException(); 392 fail(); 393 } 394 catch (NamingException x) 395 { 396 } 397 } 398 399 public interface LocalServiceMBean 400 { 401 public void throwCheckedException() throws NamingException ; 402 403 public void throwMBeanException() throws MBeanException ; 404 405 public void throwNullPointerException(); 406 407 public void throwError(); 408 } 409 410 public interface RemoteService 411 { 412 public void throwCheckedException() throws NamingException , IOException ; 413 414 public void throwMBeanException() throws MBeanException , IOException ; 415 416 public void throwNullPointerException() throws IOException ; 417 418 public void throwError() throws IOException ; 419 } 420 421 public class LocalService implements LocalServiceMBean 422 { 423 public void throwCheckedException() throws NamingException 424 { 425 throw new NamingException (); 426 } 427 428 public void throwMBeanException() throws MBeanException 429 { 430 throw new MBeanException (new Exception ()); 431 } 432 433 public void throwNullPointerException() 434 { 435 throw new NullPointerException (); 436 } 437 438 public void throwError() 439 { 440 throw new Error (); 441 } 442 } 443 444 public class LocalBroadcasterService extends LocalService implements NotificationBroadcaster 445 { 446 private NotificationBroadcasterSupport support = new NotificationBroadcasterSupport (); 447 static final String TYPE = "test.notification"; 448 static final String DESC = "Test Notification"; 449 private long sequence; 450 451 454 public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) 455 throws IllegalArgumentException 456 { 457 support.addNotificationListener(listener, filter, handback); 458 } 459 460 463 public MBeanNotificationInfo [] getNotificationInfo() 464 { 465 return new MBeanNotificationInfo []{ 466 new MBeanNotificationInfo (new String []{TYPE}, Notification .class.getName(), DESC), 467 }; 468 } 469 470 473 public void removeNotificationListener(NotificationListener listener) throws ListenerNotFoundException 474 { 475 support.removeNotificationListener(listener); 476 } 477 478 public void test() 479 { 480 Notification notification = new Notification (TYPE, this, ++sequence, System.currentTimeMillis(), DESC); 481 support.sendNotification(notification); 482 } 483 } 484 485 public class LocalEmitterService extends LocalService implements NotificationEmitter 486 { 487 private NotificationBroadcasterSupport support = new NotificationBroadcasterSupport (); 488 static final String TYPE = "test.notification"; 489 static final String DESC = "Test Notification"; 490 private long sequence; 491 492 public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) 493 throws IllegalArgumentException 494 { 495 support.addNotificationListener(listener, filter, handback); 496 } 497 498 public MBeanNotificationInfo [] getNotificationInfo() 499 { 500 return new MBeanNotificationInfo []{ 501 new MBeanNotificationInfo (new String []{TYPE}, Notification .class.getName(), DESC), 502 }; 503 } 504 505 public void removeNotificationListener(NotificationListener listener) throws ListenerNotFoundException 506 { 507 support.removeNotificationListener(listener); 508 } 509 510 public void removeNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws ListenerNotFoundException 511 { 512 support.removeNotificationListener(listener, filter, handback); 513 } 514 515 public void test() 516 { 517 Notification notification = new Notification (TYPE, this, ++sequence, System.currentTimeMillis(), DESC); 518 support.sendNotification(notification); 519 } 520 } 521 522 public class TestListener implements NotificationListener 523 { 524 boolean received = false; 525 526 public void handleNotification(Notification notification, Object handback) 527 { 528 received = true; 529 } 530 } 531 532 public class TestFilter implements NotificationFilter 533 { 534 public boolean isNotificationEnabled(Notification notification) 535 { 536 return notification.getType().equals(LocalEmitterService.TYPE); 537 } 538 } 539 } 540 | Popular Tags |