1 7 8 9 package javax.management.remote; 10 11 import java.io.IOException ; 12 import java.util.ArrayList ; 13 import java.util.List ; 14 import java.util.Map ; 15 import java.util.SortedMap ; 16 17 import javax.management.MBeanNotificationInfo ; 18 import javax.management.MBeanServer ; 19 import javax.management.MBeanRegistration ; 20 import javax.management.Notification ; 21 import javax.management.NotificationBroadcasterSupport ; 22 import javax.management.ObjectName ; 23 24 49 public abstract class JMXConnectorServer 50 extends NotificationBroadcasterSupport 51 implements JMXConnectorServerMBean , MBeanRegistration { 52 53 59 public static final String AUTHENTICATOR = 60 "jmx.remote.authenticator"; 61 62 69 public JMXConnectorServer() { 70 this(null); 71 } 72 73 82 public JMXConnectorServer(MBeanServer mbeanServer) { 83 this.mbeanServer = mbeanServer; 84 } 85 86 93 public synchronized MBeanServer getMBeanServer() { 94 return mbeanServer; 95 } 96 97 public synchronized void setMBeanServerForwarder(MBeanServerForwarder mbsf) 98 { 99 if (mbsf == null) 100 throw new IllegalArgumentException ("Invalid null argument: mbsf"); 101 102 if (mbeanServer != null) mbsf.setMBeanServer(mbeanServer); 103 mbeanServer = mbsf; 104 } 105 106 public String [] getConnectionIds() { 107 synchronized (connectionIds) { 108 return (String []) 109 connectionIds.toArray(new String [connectionIds.size()]); 110 } 111 } 112 113 155 public JMXConnector toJMXConnector(Map <String ,?> env) 156 throws IOException 157 { 158 if (!isActive()) throw new 159 IllegalStateException ("Connector is not active"); 160 JMXServiceURL addr = getAddress(); 161 return JMXConnectorFactory.newJMXConnector(addr, env); 162 } 163 164 175 public MBeanNotificationInfo [] getNotificationInfo() { 176 final String [] types = { 177 JMXConnectionNotification.OPENED, 178 JMXConnectionNotification.CLOSED, 179 JMXConnectionNotification.FAILED, 180 }; 181 final String className = JMXConnectionNotification .class.getName(); 182 final String description = 183 "A client connection has been opened or closed"; 184 return new MBeanNotificationInfo [] { 185 new MBeanNotificationInfo (types, className, description), 186 }; 187 } 188 189 211 protected void connectionOpened(String connectionId, 212 String message, 213 Object userData) { 214 215 if (connectionId == null) 216 throw new NullPointerException ("Illegal null argument"); 217 218 synchronized (connectionIds) { 219 connectionIds.add(connectionId); 220 } 221 222 sendNotification(JMXConnectionNotification.OPENED, connectionId, 223 message, userData); 224 } 225 226 246 protected void connectionClosed(String connectionId, 247 String message, 248 Object userData) { 249 250 if (connectionId == null) 251 throw new NullPointerException ("Illegal null argument"); 252 253 synchronized (connectionIds) { 254 connectionIds.remove(connectionId); 255 } 256 257 sendNotification(JMXConnectionNotification.CLOSED, connectionId, 258 message, userData); 259 } 260 261 281 protected void connectionFailed(String connectionId, 282 String message, 283 Object userData) { 284 285 if (connectionId == null) 286 throw new NullPointerException ("Illegal null argument"); 287 288 synchronized (connectionIds) { 289 connectionIds.remove(connectionId); 290 } 291 292 sendNotification(JMXConnectionNotification.FAILED, connectionId, 293 message, userData); 294 } 295 296 private void sendNotification(String type, String connectionId, 297 String message, Object userData) { 298 Notification notif = 299 new JMXConnectionNotification (type, 300 getNotificationSource(), 301 connectionId, 302 nextSequenceNumber(), 303 message, 304 userData); 305 sendNotification(notif); 306 } 307 308 private synchronized Object getNotificationSource() { 309 if (myName != null) 310 return myName; 311 else 312 return this; 313 } 314 315 private static long nextSequenceNumber() { 316 synchronized (sequenceNumberLock) { 317 return sequenceNumber++; 318 } 319 } 320 321 343 public synchronized ObjectName preRegister(MBeanServer mbs, 344 ObjectName name) { 345 if (mbs == null || name == null) 346 throw new NullPointerException ("Null MBeanServer or ObjectName"); 347 if (mbeanServer == null) { 348 mbeanServer = mbs; 349 myName = name; 350 } 351 return name; 352 } 353 354 public void postRegister(Boolean registrationDone) { 355 } 357 358 371 public synchronized void preDeregister() throws Exception { 372 if (myName != null && isActive()) { 373 stop(); 374 myName = null; } 376 } 377 378 public void postDeregister() { 379 myName = null; 380 } 381 382 385 private MBeanServer mbeanServer = null; 386 387 391 private ObjectName myName; 392 393 private final int[] lock = new int[0]; 394 395 private List connectionIds = new ArrayList (); 396 397 private static final int[] sequenceNumberLock = new int[0]; 398 private static long sequenceNumber; 399 } 400 | Popular Tags |