1 23 24 28 29 31 32 package com.sun.enterprise.management.support; 33 34 import java.util.List ; 35 import java.util.Set ; 36 import java.util.Iterator ; 37 import java.util.ArrayList ; 38 import java.util.Collections ; 39 import java.util.logging.Logger ; 40 41 42 import javax.management.ObjectName ; 43 import javax.management.MBeanRegistration ; 44 import javax.management.NotificationListener ; 45 46 import com.sun.appserv.management.util.misc.ExceptionUtil; 47 48 final class LoaderRegThread extends Thread 49 { 50 private boolean mQuit; 51 private List <ObjectName > mRegistrationQueue; 52 private List <ObjectName > mUnregistrationQueue; 53 private final Logger mLogger; 54 private final LoaderRegHandler mRegHandler; 55 56 public 57 LoaderRegThread( 58 final LoaderRegHandler regHandler, 59 final Logger logger ) 60 { 61 mRegHandler = regHandler; 62 mLogger = logger; 63 mQuit = false; 64 65 mRegistrationQueue = Collections.synchronizedList( new ArrayList <ObjectName >() ); 66 mUnregistrationQueue = Collections.synchronizedList( new ArrayList <ObjectName >() ); 67 } 68 69 private final Logger 70 getLogger( ) 71 { 72 return( mLogger ); 73 } 74 75 private final void 76 trace( final Object o ) 77 { 78 if ( mLogger != null ) 79 { 80 mLogger.finer( o.toString() ); 81 } 82 } 83 84 public void 85 quit() 86 { 87 mQuit = true; 88 synchronized( this ) 89 { 90 this.notify(); 91 } 92 } 93 94 public void 95 enqueue( 96 final boolean register, 97 final List candidates ) 98 { 99 final Iterator iter = candidates.iterator(); 100 while ( iter.hasNext() ) 101 { 102 enqueue( register, (ObjectName )iter.next() ); 103 } 104 } 105 106 public void 107 enqueue( 108 final boolean register, 109 final ObjectName theObject ) 110 { 111 final List <ObjectName > theQueue; 112 113 if ( register ) 114 { 115 theQueue = mRegistrationQueue; 116 } 117 else 118 { 119 theQueue = mUnregistrationQueue; 120 } 121 122 theQueue.add( theObject ); 123 124 synchronized( this ) 125 { 126 this.notify(); 127 } 128 } 129 130 private ObjectName 131 dequeue( final List <ObjectName > theQueue ) 132 { 133 ObjectName theObject = null; 134 135 synchronized( theQueue ) 136 { 137 if ( theQueue.size() != 0 ) 138 { 139 theObject = (ObjectName )theQueue.remove( 0 ); 140 } 141 } 142 return( theObject ); 143 } 144 145 146 public void 147 run() 148 { 149 final int INTERVAL = 1024 * 1024 * 1024; 151 152 mQuit = false; 153 154 trace( "Loader.RegThread.run(): start" ); 155 while ( ! mQuit ) 156 { 157 try 158 { 159 trace( "Loader.RegThread.run(): waiting" ); 160 synchronized( this ) 161 { 162 wait(); 163 } 164 } 165 catch( InterruptedException e ) 166 { 167 trace( "Loader.RegThread.run(): interrupted from wait" ); 168 } 169 170 if ( mQuit ) 171 { 172 break; 173 } 174 175 processRegistrations(); 176 processUnregistrations(); 177 } 178 } 179 180 181 private void 182 mySleep( final long millis ) 183 { 184 try 185 { 186 Thread.sleep( millis ); 187 } 188 catch( InterruptedException e ) 189 { 190 } 191 } 192 193 public void 194 waitEmpty() 195 { 196 int queueSize = 0; 197 198 while ( (queueSize = mRegistrationQueue.size() ) != 0 ) 199 { 200 mySleep( 100 ); 201 } 202 trace( "RegThread.waitEmpty: queue is empty" ); 203 } 204 205 public int 206 getRegistrationQueueSize() 207 { 208 return( mRegistrationQueue.size() ); 209 } 210 211 212 public synchronized void 213 processRegistration( final ObjectName objectName ) 214 { 215 try 216 { 217 mRegHandler.handleMBeanRegistered( objectName ); 218 getLogger().finer( "LoaderRegThread.processRegistration: processed mbean: " + objectName ); 219 } 220 catch( Throwable t ) 221 { 222 getLogger().warning( "LoaderRegThread.processRegistration: " + 223 "registration of MBean failed for: " + 224 objectName + " = " + t.toString() + ", " + t.getMessage() + "\n" + 225 ExceptionUtil.getStackTrace( t ) ); 226 } 227 } 228 229 230 public synchronized void 231 processUnregistration( final ObjectName objectName ) 232 { 233 try 234 { 235 mRegHandler.handleMBeanUnregistered( objectName ); 236 } 237 catch( Throwable t ) 238 { 239 getLogger().warning( "LoaderRegThread.processUnregistration: " + 240 "unregistration of MBean failed for: " + 241 objectName + " = " + t.toString() ); 242 } 243 } 244 245 private void 246 processRegistrations() 247 { 248 ObjectName objectName = null; 249 250 int numDone = 0; 251 while ( (objectName = dequeue( mRegistrationQueue )) != null ) 252 { 253 processRegistration( objectName ); 254 ++numDone; 255 } 256 getLogger().fine( "LoaderRegThread: processed mbeans: " + numDone ); 257 } 258 259 private void 260 processUnregistrations() 261 { 262 ObjectName objectName = null; 263 264 while ( (objectName = dequeue( mUnregistrationQueue )) != null ) 265 { 266 processUnregistration( objectName ); 267 } 268 } 269 } 270 271 272 273 274 | Popular Tags |