1 23 package com.sun.appserv.management.util.jmx; 24 25 import java.util.Set ; 26 import java.util.HashSet ; 27 import java.util.Collections ; 28 29 import java.io.IOException ; 30 31 import javax.management.ObjectName ; 32 import javax.management.Notification ; 33 import javax.management.NotificationListener ; 34 import javax.management.NotificationFilter ; 35 import javax.management.MBeanServerNotification ; 36 import javax.management.MBeanServer ; 37 import javax.management.MBeanServerConnection ; 38 import javax.management.InstanceNotFoundException ; 39 import javax.management.JMException ; 40 41 import com.sun.appserv.management.util.misc.GSetUtil; 42 43 54 public abstract class NotificationListenerBase 55 implements NotificationListener 56 { 57 private final MBeanServerConnection mConn; 58 59 60 private final Set <ObjectName > mListenees; 61 62 63 private final ObjectName mPattern; 64 private final NotificationFilter mFilter; 65 private final Object mHandback; 66 67 private RegistrationListener mDelegateListener; 68 69 72 public 73 NotificationListenerBase( 74 final MBeanServerConnection conn, 75 final ObjectName pattern ) 76 throws InstanceNotFoundException , IOException 77 { 78 this( conn, pattern, null ); 79 } 80 81 88 public 89 NotificationListenerBase( 90 final MBeanServerConnection conn, 91 final ObjectName pattern, 92 final NotificationFilter filter ) 93 throws InstanceNotFoundException , IOException 94 { 95 mConn = conn; 96 mPattern = pattern; 97 mFilter = filter; 98 mHandback = null; 99 mDelegateListener = null; 100 101 mListenees = Collections.synchronizedSet( new HashSet <ObjectName >() ); 102 103 if ( ! conn.isRegistered( JMXUtil.getMBeanServerDelegateObjectName() ) ) 105 { 106 throw new IllegalArgumentException (); 107 } 108 109 setupListening( ); 110 } 111 112 115 public abstract void 116 handleNotification( final Notification notif, final Object handback); 117 118 119 protected synchronized void 120 listenToMBean( final ObjectName objectName ) 121 throws InstanceNotFoundException , IOException 122 { 123 if ( ! mListenees.contains( objectName ) ) 124 { 125 mListenees.add( objectName ); 126 getMBeanServerConnection().addNotificationListener( 127 objectName, this, mFilter, null ); 128 } 129 } 130 131 private void 132 setupListening() 133 throws InstanceNotFoundException , IOException 134 { 135 if ( mPattern.isPattern() ) 136 { 137 mDelegateListener = new RegistrationListener(); 142 JMXUtil.listenToMBeanServerDelegate( mConn, 143 mDelegateListener, null, null ); 144 } 145 146 147 Set <ObjectName > s = null; 148 149 if ( mPattern.isPattern() ) 150 { 151 s = JMXUtil.queryNames( getConn(), mPattern, null ); 152 } 153 else 154 { 155 s = GSetUtil.newSet( mPattern ); 156 } 157 158 synchronized( this ) 159 { 160 for( final ObjectName objectName : s ) 161 { 162 listenToMBean( objectName ); 163 } 164 } 165 } 166 167 170 public final NotificationFilter 171 getNotificationFilter( final ObjectName objectName) 172 { 173 return mFilter; 174 } 175 176 177 protected synchronized void 178 listenToIfMatch( final ObjectName objectName ) 179 throws IOException , InstanceNotFoundException 180 { 181 if ( ! mListenees.contains( objectName ) ) 182 { 183 final String defaultDomain = getConn().getDefaultDomain(); 184 185 if ( JMXUtil.matchesPattern( defaultDomain, mPattern, objectName ) ) 186 { 187 listenToMBean( objectName ); 188 } 189 } 190 } 191 195 private final class RegistrationListener implements NotificationListener 196 { 197 public RegistrationListener() {} 198 199 public void 200 handleNotification( 201 final Notification notifIn, 202 final Object handback) 203 { 204 if ( notifIn instanceof MBeanServerNotification ) 205 { 206 final MBeanServerNotification notif = (MBeanServerNotification )notifIn; 207 208 final ObjectName objectName = notif.getMBeanName(); 209 final String type = notif.getType(); 210 211 try 212 { 213 if ( type.equals( MBeanServerNotification.REGISTRATION_NOTIFICATION ) ) 214 { 215 listenToIfMatch( objectName ); 216 } 217 else if ( type.equals( MBeanServerNotification.UNREGISTRATION_NOTIFICATION ) ) 218 { 219 mListenees.remove( objectName ); 220 } 221 } 222 catch( Exception e ) 223 { 224 } 226 } 227 } 228 } 229 230 234 public synchronized void 235 cleanup() 236 { 237 try 238 { 239 if ( mDelegateListener != null ) 240 { 241 getConn().removeNotificationListener( 244 JMXUtil.getMBeanServerDelegateObjectName(), 245 mDelegateListener, null, null ); 246 mDelegateListener = null; 247 } 248 249 for( final ObjectName objectName : mListenees ) 250 { 251 getConn().removeNotificationListener( 252 objectName, this, mFilter, null); 253 } 254 } 255 catch( JMException e ) 256 { 257 } 258 catch( IOException e ) 259 { 260 } 261 262 mListenees.clear(); 263 } 264 265 268 public synchronized Set <ObjectName > 269 getListenees() 270 { 271 final Set <ObjectName > objectNames = new HashSet <ObjectName >(); 272 273 synchronized( mListenees ) 274 { 275 objectNames.addAll( mListenees ); 276 } 277 278 return( objectNames ); 279 } 280 281 282 286 public final MBeanServerConnection 287 getMBeanServerConnection() 288 { 289 return getConn(); 290 } 291 292 protected final MBeanServerConnection 293 getConn() 294 { 295 return mConn; 296 } 297 298 299 protected final void 300 checkAlive() 301 throws IOException 302 { 303 if ( ! isAlive() ) 304 { 305 throw new IOException ( "MBeanServerConnection failed" ); 306 } 307 } 308 309 312 public boolean 313 isAlive() 314 { 315 boolean isAlive = true; 316 317 if ( ! (mConn instanceof MBeanServer ) ) 318 { 319 try 321 { 322 mConn.isRegistered( JMXUtil.getMBeanServerDelegateObjectName() ); 323 } 324 catch( Exception e ) 325 { 326 isAlive = false; 327 } 328 } 329 return isAlive; 330 } 331 332 } 333 334 335 336 337 338 339 | Popular Tags |