1 11 12 package org.eclipse.osgi.framework.eventmgr; 13 14 import org.eclipse.osgi.framework.eventmgr.EventListeners.ListElement; 15 16 98 99 public class EventManager { 100 static final boolean DEBUG = false; 101 102 106 private EventThread thread; 107 108 111 protected final String threadName; 112 113 118 public EventManager() { 119 this(null); 120 } 121 122 129 public EventManager(String threadName) { 130 thread = null; 131 this.threadName = threadName; 132 } 133 134 142 public synchronized void close() { 143 if (thread != null) { 144 thread.close(); 145 thread = null; 146 } 147 } 148 149 156 synchronized EventThread getEventThread() { 157 if (thread == null) { 158 159 if (threadName == null) { 160 thread = new EventThread(); 161 } 162 else { 163 thread = new EventThread(threadName); 164 } 165 thread.start(); 166 } 167 168 return thread; 169 } 170 171 186 static void dispatchEvent(ListElement[] listeners, EventDispatcher dispatcher, int eventAction, Object eventObject) { 187 int size = listeners.length; 188 for (int i = 0; i < size; i++) { 189 ListElement listener = listeners[i]; 190 if (listener == null) { 191 break; 192 } 193 try { 194 195 dispatcher.dispatchEvent(listener.primary, listener.companion, eventAction, eventObject); 196 } 197 catch (Throwable t) { 198 199 if (DEBUG) { 200 System.out.println("Exception in " + listener.primary); t.printStackTrace(); 202 } 203 } 204 } 205 } 206 207 210 211 static class EventThread extends Thread { 212 217 private static class Queued { 218 219 final ListElement[] listeners; 220 221 final EventDispatcher dispatcher; 222 223 final int action; 224 225 final Object object; 226 227 Queued next; 228 229 237 Queued(ListElement[] l, EventDispatcher d, int a, Object o) { 238 listeners = l; 239 dispatcher = d; 240 action = a; 241 object = o; 242 next = null; 243 } 244 } 245 246 247 private Queued head; 248 249 private Queued tail; 250 251 private volatile boolean running; 252 253 257 EventThread(String threadName) { 258 super(threadName); 259 init(); 260 } 261 262 265 EventThread() { 266 super(); 267 init(); 268 } 269 270 private void init() { 271 running = true; 272 head = null; 273 tail = null; 274 275 setDaemon(true); 276 } 277 278 281 void close() { 282 running = false; 283 interrupt(); 284 } 285 286 290 public void run() { 291 try { 292 while (true) { 293 Queued item = getNextEvent(); 294 if (item == null) { 295 return; 296 } 297 EventManager.dispatchEvent(item.listeners, item.dispatcher, item.action, item.object); 298 } 299 } 300 catch (RuntimeException e) { 301 if (EventManager.DEBUG) { 302 e.printStackTrace(); 303 } 304 throw e; 305 } 306 catch (Error e) { 307 if (EventManager.DEBUG) { 308 e.printStackTrace(); 309 } 310 throw e; 311 } 312 } 313 314 324 synchronized void postEvent(ListElement[] l, EventDispatcher d, int a, Object o) { 325 if (!isAlive()) { 326 throw new IllegalStateException (); 327 } 328 329 Queued item = new Queued(l, d, a, o); 330 331 if (head == null) 332 { 333 head = item; 334 tail = item; 335 } else 336 { 337 tail.next = item; 338 tail = item; 339 } 340 341 notify(); 342 } 343 344 352 private synchronized Queued getNextEvent() { 353 while (running && (head == null)) { 354 try { 355 wait(); 356 } 357 catch (InterruptedException e) { 358 } 359 } 360 361 if (!running) { 362 return null; 363 } 364 365 Queued item = head; 366 head = item.next; 367 if (head == null) { 368 tail = null; 369 } 370 371 return item; 372 } 373 } 374 } 375 | Popular Tags |