1 8 9 package test.javax.management.remote; 10 11 import java.io.ByteArrayInputStream ; 12 import java.io.ByteArrayOutputStream ; 13 import java.io.IOException ; 14 import java.io.ObjectInputStream ; 15 import java.io.ObjectOutputStream ; 16 import java.net.MalformedURLException ; 17 import java.util.ArrayList ; 18 import java.util.HashMap ; 19 import java.util.List ; 20 import java.util.Map ; 21 import javax.management.ListenerNotFoundException ; 22 import javax.management.MBeanNotificationInfo ; 23 import javax.management.MBeanServer ; 24 import javax.management.MBeanServerConnection ; 25 import javax.management.Notification ; 26 import javax.management.NotificationEmitter ; 27 import javax.management.NotificationFilter ; 28 import javax.management.NotificationListener ; 29 import javax.management.ObjectName ; 30 import javax.management.remote.JMXAuthenticator ; 31 import javax.management.remote.JMXConnector ; 32 import javax.management.remote.JMXConnectorFactory ; 33 import javax.management.remote.JMXConnectorServer ; 34 import javax.management.remote.JMXConnectorServerFactory ; 35 import javax.management.remote.JMXPrincipal ; 36 import javax.management.remote.JMXServiceURL ; 37 import javax.security.auth.Subject ; 38 39 import test.MX4JTestCase; 40 41 44 public abstract class JMXConnectorTestCase extends MX4JTestCase 45 { 46 public JMXConnectorTestCase(String name) 47 { 48 super(name); 49 } 50 51 protected void tearDown() throws Exception 52 { 53 sleep(5000); 54 } 55 56 public abstract JMXServiceURL createJMXConnectorServerAddress() throws MalformedURLException ; 57 58 public abstract Map getEnvironment(); 59 60 public void testNewJMXConnectorWithNullURL() throws Exception 61 { 62 try 63 { 64 JMXConnectorFactory.connect(null); 65 fail(); 66 } 67 catch (NullPointerException x) 68 { 69 } 70 } 71 72 public void testConnectionId() throws Exception 73 { 74 78 JMXConnectorServer cntorServer = null; 79 JMXConnector cntor = null; 80 try 81 { 82 JMXServiceURL url = createJMXConnectorServerAddress(); 83 cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), newMBeanServer()); 84 cntorServer.start(); 85 sleep(5000); 86 87 cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment()); 88 String connectionId = cntor.getConnectionId(); 89 String protocol = connectionId.substring(0, connectionId.indexOf(':')); 90 assertEquals(protocol, url.getProtocol()); 91 92 int space = connectionId.indexOf(' '); 94 String remaining = connectionId.substring(space + 1); 95 space = remaining.indexOf(' '); 97 String arbitrary = remaining.substring(space + 1); 98 if (arbitrary.length() < 1) fail("Missing MX4J arbitrary test"); 99 } 100 finally 101 { 102 if (cntor != null) cntor.close(); 103 if (cntorServer != null) cntorServer.stop(); 104 } 105 } 106 107 public void testConnectionWithNoPath() throws Exception 108 { 109 JMXConnectorServer cntorServer = null; 110 JMXConnector cntor = null; 111 try 112 { 113 JMXServiceURL url = createJMXConnectorServerAddress(); 114 cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), newMBeanServer()); 115 cntorServer.start(); 116 sleep(5000); 117 118 cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment()); 119 } 120 finally 121 { 122 if (cntor != null) cntor.close(); 123 if (cntorServer != null) cntorServer.stop(); 124 } 125 } 126 127 public void testJMXAuthenticator() throws Exception 128 { 129 final String password = "mx4j"; 130 JMXAuthenticator authenticator = new JMXAuthenticator () 131 { 132 public Subject authenticate(Object credentials) throws SecurityException 133 { 134 if (password.equals(credentials)) 135 { 136 JMXPrincipal principal = new JMXPrincipal ("mx4j"); 137 Subject subject = new Subject (); 138 subject.getPrincipals().add(principal); 139 subject.setReadOnly(); 140 return subject; 141 } 142 throw new SecurityException ("Authentication Failed"); 143 } 144 }; 145 146 JMXConnectorServer cntorServer = null; 147 JMXConnector cntor = null; 148 try 149 { 150 JMXServiceURL url = createJMXConnectorServerAddress(); 151 MBeanServer server = newMBeanServer(); 152 Map serverEnv = getEnvironment(); 153 serverEnv.put(JMXConnectorServer.AUTHENTICATOR, authenticator); 154 cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, serverEnv, server); 155 cntorServer.start(); 156 sleep(5000); 157 158 Map clientEnv = getEnvironment(); 160 try 161 { 162 testJMXAuthenticatorConnect(cntorServer.getAddress(), clientEnv); 163 fail(); 164 } 165 catch (SecurityException x) 166 { 167 } 168 169 clientEnv.put(JMXConnector.CREDENTIALS, password); 171 testJMXAuthenticatorConnect(cntorServer.getAddress(), clientEnv); 172 } 173 finally 174 { 175 if (cntor != null) cntor.close(); 176 if (cntorServer != null) cntorServer.stop(); 177 } 178 } 179 180 protected void testJMXAuthenticatorConnect(JMXServiceURL url, Map environment) throws SecurityException , IOException 181 { 182 JMXConnectorFactory.connect(url, environment); 183 } 184 185 public void testStopServerBeforeClosingClient() throws Exception 186 { 187 JMXServiceURL url = createJMXConnectorServerAddress(); 188 JMXConnectorServer cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), newMBeanServer()); 189 cntorServer.start(); 190 sleep(5000); 191 192 JMXConnector cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment()); 193 MBeanServerConnection mbsc = cntor.getMBeanServerConnection(); 194 195 cntorServer.stop(); 196 197 try 198 { 199 mbsc.getDefaultDomain(); 200 fail(); 201 } 202 catch (IOException x) 203 { 204 } 205 } 206 207 public void testStopServerAndCloseClientThenInvoke() throws Exception 208 { 209 JMXServiceURL url = createJMXConnectorServerAddress(); 210 JMXConnectorServer cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), newMBeanServer()); 211 cntorServer.start(); 212 sleep(5000); 213 214 JMXConnector cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment()); 215 MBeanServerConnection mbsc = cntor.getMBeanServerConnection(); 216 217 cntor.close(); 218 cntorServer.stop(); 219 220 try 221 { 222 mbsc.getDefaultDomain(); 223 fail(); 224 } 225 catch (IOException x) 226 { 227 } 228 } 229 230 public void testSerializedConnectorCanConnect() throws Exception 231 { 232 JMXConnectorServer cntorServer = null; 233 JMXConnector cntor = null; 234 try 235 { 236 JMXServiceURL url = createJMXConnectorServerAddress(); 237 cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), newMBeanServer()); 238 cntorServer.start(); 239 sleep(5000); 240 241 cntor = JMXConnectorFactory.newJMXConnector(cntorServer.getAddress(), getEnvironment()); 242 243 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 245 ObjectOutputStream oos = new ObjectOutputStream (baos); 246 oos.writeObject(cntor); 247 oos.close(); 248 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 249 ObjectInputStream ois = new ObjectInputStream (bais); 250 cntor = (JMXConnector )ois.readObject(); 251 ois.close(); 252 253 cntor.connect(); 254 MBeanServerConnection mbsc = cntor.getMBeanServerConnection(); 255 mbsc.getDefaultDomain(); 256 257 cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment()); 259 260 baos = new ByteArrayOutputStream (); 262 oos = new ObjectOutputStream (baos); 263 oos.writeObject(cntor); 264 oos.close(); 265 bais = new ByteArrayInputStream (baos.toByteArray()); 266 ois = new ObjectInputStream (bais); 267 cntor = (JMXConnector )ois.readObject(); 268 ois.close(); 269 270 cntor.connect(); 271 mbsc = cntor.getMBeanServerConnection(); 272 mbsc.getDefaultDomain(); 273 } 274 finally 275 { 276 if (cntor != null) cntor.close(); 277 if (cntorServer != null) cntorServer.stop(); 278 } 279 } 280 281 public void testDefaultClassLoader() throws Exception 282 { 283 JMXServiceURL url = createJMXConnectorServerAddress(); 284 Map environment = new HashMap (getEnvironment()); 285 environment.put(JMXConnectorFactory.DEFAULT_CLASS_LOADER, new Object ()); 286 try 287 { 288 JMXConnectorFactory.newJMXConnector(url, environment); 289 fail(); 290 } 291 catch (IllegalArgumentException x) 292 { 293 } 294 295 JMXConnector cntor = JMXConnectorFactory.newJMXConnector(url, getEnvironment()); 296 try 297 { 298 cntor.connect(environment); 299 fail(); 300 } 301 catch (IllegalArgumentException x) 302 { 303 } 304 } 305 306 public void testListenersAreRemovedOnConnectorClose() throws Exception 307 { 308 JMXConnectorServer cntorServer = null; 309 JMXConnector cntor = null; 310 try 311 { 312 JMXServiceURL url = createJMXConnectorServerAddress(); 313 MBeanServer server = newMBeanServer(); 314 cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), server); 315 cntorServer.start(); 316 sleep(5000); 317 318 Emitter emitter = new Emitter (); 319 ObjectName emitterName = ObjectName.getInstance(":name=emitter"); 320 server.registerMBean(emitter, emitterName); 321 322 cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment()); 323 MBeanServerConnection mbsc = cntor.getMBeanServerConnection(); 324 325 NotificationListener listener = new NotificationListener () 326 { 327 public void handleNotification(Notification notification, Object handback) 328 { 329 } 330 }; 331 332 mbsc.addNotificationListener(emitterName, listener, null, null); 334 assertEquals(emitter.getSize(), 1); 335 mbsc.removeNotificationListener(emitterName, listener, null, null); 336 assertEquals(emitter.getSize(), 0); 337 338 mbsc.addNotificationListener(emitterName, listener, null, null); 340 assertEquals(emitter.getSize(), 1); 341 cntor.close(); 342 assertEquals(emitter.getSize(), 0); 343 } 344 finally 345 { 346 if (cntorServer != null) cntorServer.stop(); 347 } 348 } 349 350 public void testConnectWithProviderClassLoader() throws Exception 351 { 352 JMXConnectorServer cntorServer = null; 353 JMXConnector cntor = null; 354 try 355 { 356 JMXServiceURL url = createJMXConnectorServerAddress(); 357 MBeanServer server = newMBeanServer(); 358 Map serverEnv = getEnvironment(); 359 serverEnv.put(JMXConnectorServerFactory.PROTOCOL_PROVIDER_CLASS_LOADER, getClass().getClassLoader()); 360 ClassLoader old = Thread.currentThread().getContextClassLoader(); 361 Thread.currentThread().setContextClassLoader(getClass().getClassLoader().getParent()); 362 cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, serverEnv, server); 363 cntorServer.start(); 364 Thread.currentThread().setContextClassLoader(old); 365 sleep(5000); 366 367 cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment()); 368 MBeanServerConnection mbsc = cntor.getMBeanServerConnection(); 369 assertNotNull(mbsc); 370 } 371 finally 372 { 373 if (cntor != null) cntor.close(); 374 if (cntorServer != null) cntorServer.stop(); 375 } 376 } 377 378 public interface EmitterMBean 379 { 380 } 381 382 private static class Emitter implements NotificationEmitter , EmitterMBean 383 { 384 private List listeners = new ArrayList (); 385 386 public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws IllegalArgumentException 387 { 388 listeners.add(listener); 389 } 390 391 public void removeNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws ListenerNotFoundException 392 { 393 listeners.remove(listener); 394 } 395 396 public MBeanNotificationInfo [] getNotificationInfo() 397 { 398 return new MBeanNotificationInfo [0]; 399 } 400 401 public void removeNotificationListener(NotificationListener listener) throws ListenerNotFoundException 402 { 403 } 404 405 public int getSize() 406 { 407 return listeners.size(); 408 } 409 } 410 } 411 | Popular Tags |