01: package org.mactor.framework;
02:
03: import java.util.Iterator;
04: import java.util.LinkedList;
05:
06: public class EventReporter implements Runnable, TestFeedbackListener {
07: LinkedList<EI> eventQueue = new LinkedList<EI>();
08: private Object lock = new Object();
09: private TestFeedbackListener listener;
10: private boolean stopped = false;
11:
12: public EventReporter(TestFeedbackListener listener) {
13: super ();
14: this .listener = listener;
15: }
16:
17: public void onEvent(TestEvent event, TestContext context) {
18: EI e = new EI();
19: e.te = event;
20: e.context = context;
21: synchronized (lock) {
22: eventQueue.add(e);
23: lock.notifyAll();
24: }
25: }
26:
27: public void run() {
28: while (!stopped) {
29: LinkedList<EI> current = null;
30: synchronized (lock) {
31: if (eventQueue.size() == 0) {
32: try {
33: lock.wait();
34: } catch (InterruptedException ie) {
35: }
36: }
37: if (eventQueue.size() > 0) {
38: current = eventQueue;
39: eventQueue = new LinkedList<EI>();
40: }
41: }
42: if (current != null) {
43: Iterator<EI> it = current.iterator();
44: while (it.hasNext()) {
45: EI e = (EI) it.next();
46: listener.onEvent(e.te, e.context);
47: }
48: }
49: }
50: }
51:
52: public void start() {
53: stopped = false;
54: new Thread(this ).start();
55: }
56:
57: public void stop() {
58: stopped = true;
59: synchronized (lock) {
60: lock.notifyAll();
61: }
62: }
63:
64: private static class EI {
65: TestEvent te;
66: TestContext context;
67: }
68: }
|