001: // ResourceEventQueue.java
002: // $Id: ResourceEventQueue.java,v 1.5 2000/10/13 16:37:58 bmahe Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1997.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.tools.resources.event;
007:
008: import org.w3c.tools.resources.FramedResource;
009: import org.w3c.tools.resources.InvalidResourceException;
010: import org.w3c.tools.resources.Resource;
011: import org.w3c.tools.resources.ResourceReference;
012:
013: public class ResourceEventQueue {
014:
015: public final static boolean debug = false;
016:
017: class QueueCell {
018: ResourceEvent event;
019: QueueCell next;
020: int id;
021:
022: QueueCell(ResourceEvent revt) {
023: this .event = revt;
024: this .next = null;
025: this .id = revt.getID();
026: }
027: }
028:
029: private QueueCell queue = null;
030: private QueueCell last = null;
031:
032: /**
033: * Send a ResourceEvent in the queue.
034: * @param evt The Resource Event to put in the queue.
035: */
036: public synchronized void sendEvent(ResourceEvent evt) {
037: if (debug)
038: System.out.println("[QUEUE] : sendEvent " + evt.getID());
039: // => lock()
040: QueueCell ncell = new QueueCell(evt);
041: if (queue == null) {
042: queue = ncell;
043: last = ncell;
044: notifyAll();
045: } else {
046: if ((ncell.id == Events.RESOURCE_MODIFIED)
047: || (ncell.id == Events.FRAME_MODIFIED)) {
048: if (debug)
049: System.out
050: .println("[QUEUE] : remove old modified event");
051: QueueCell cell = queue;
052: while (cell != null) {
053: if ((cell.id == ncell.id)
054: && (cell.event.getSource() == ncell.event
055: .getSource())) {
056: if (debug)
057: System.out.println("[QUEUE] : Found one!");
058: cell.event = ncell.event;
059: break;
060: }
061: cell = cell.next;
062: }
063: if (cell == null) {
064: if (debug)
065: System.out.println("[QUEUE] : add new event.");
066: last.next = ncell;
067: last = ncell;
068: }
069: } else {
070: last.next = ncell;
071: last = ncell;
072: }
073: }
074: }
075:
076: /**
077: * Get the next Event in the queue, wait if there is no event
078: * available in the queue.
079: * @return a ResourceEvent instance.
080: * @exception InterruptedException Is unable to get the next event due to
081: * some interruption.
082: */
083: public synchronized ResourceEvent getNextEvent()
084: throws InterruptedException {
085: while (queue == null)
086: wait();
087: QueueCell next = queue;
088: queue = queue.next;
089: if (queue == null)
090: last = null;
091: return next.event;
092: }
093:
094: /**
095: * Remove all the Events comming from the given source object.
096: * @param source The Object where the events to remove are comming from.
097: */
098: public synchronized void removeSourceEvents(Object source) {
099: QueueCell current = queue;
100: QueueCell prev = null;
101: while (current != null) {
102: if (current.event.getSource() == source) {
103: if (prev == null)
104: queue = current.next;
105: else {
106: prev.next = current.next;
107: if (prev.next == null)
108: last = prev;
109: }
110: }
111: prev = current;
112: current = current.next;
113: }
114: }
115:
116: public ResourceEventQueue() {
117: queue = null;
118: new Dispatcher("ResourceEventQueue Dispatcher", this ).start();
119: }
120:
121: }
122:
123: class Dispatcher extends Thread {
124: private ResourceEventQueue queue = null;
125: private boolean dispatch = true;
126:
127: public void stopDispatching() {
128: dispatch = false;
129: }
130:
131: public void run() {
132: while (dispatch) {
133: try {
134: ResourceEvent event = queue.getNextEvent();
135: if (queue.debug)
136: System.out.println("[QUEUE] : getNextEvent()");
137: Object src = event.getSource();
138: ResourceReference rr = null;
139: try {
140: FramedResource source = null;
141: if (src instanceof ResourceReference) {
142: rr = (ResourceReference) src;
143: Resource res = rr.lock();
144: if (res instanceof FramedResource)
145: source = (FramedResource) res;
146: else
147: source = null;
148: } else if (src instanceof FramedResource) {
149: source = (FramedResource) src;
150: }
151:
152: if (source != null) {
153: if (queue.debug)
154: System.out
155: .println("[QUEUE] : processEvent "
156: + event);
157: source.processEvent(event);
158: }
159: } catch (InvalidResourceException ex) {
160: if (queue.debug) {
161: System.err.println("Exception occurred "
162: + "in EventDispatcher:");
163: ex.printStackTrace();
164: }
165: } finally {
166: if (rr != null)
167: rr.unlock();
168: }
169: } catch (ThreadDeath death) {
170: return;
171: } catch (Throwable e) {
172: if (queue.debug) {
173: System.err.println("Exception occurred "
174: + "during event dispatching:");
175: e.printStackTrace();
176: }
177: }
178: }
179: }
180:
181: Dispatcher(String name, ResourceEventQueue queue) {
182: super(name);
183: this.queue = queue;
184: }
185:
186: }
|