001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.debug.jdi.tests;
011:
012: import java.util.Vector;
013:
014: import com.sun.jdi.VMDisconnectedException;
015: import com.sun.jdi.event.AccessWatchpointEvent;
016: import com.sun.jdi.event.BreakpointEvent;
017: import com.sun.jdi.event.ClassPrepareEvent;
018: import com.sun.jdi.event.ClassUnloadEvent;
019: import com.sun.jdi.event.Event;
020: import com.sun.jdi.event.EventIterator;
021: import com.sun.jdi.event.EventQueue;
022: import com.sun.jdi.event.EventSet;
023: import com.sun.jdi.event.ExceptionEvent;
024: import com.sun.jdi.event.MethodEntryEvent;
025: import com.sun.jdi.event.MethodExitEvent;
026: import com.sun.jdi.event.ModificationWatchpointEvent;
027: import com.sun.jdi.event.StepEvent;
028: import com.sun.jdi.event.ThreadDeathEvent;
029: import com.sun.jdi.event.ThreadStartEvent;
030: import com.sun.jdi.event.VMDeathEvent;
031: import com.sun.jdi.event.VMDisconnectEvent;
032: import com.sun.jdi.request.EventRequest;
033:
034: /**
035: * An event reader that continuously reads events coming from the VM
036: * and dispatch them to the registered listeners.
037: */
038:
039: public class EventReader extends AbstractReader {
040: private EventQueue fEventQueue;
041: private Vector fEventListeners = new Vector(); // A Vector of EventListener
042:
043: /**
044: * Constructor
045: * @param name
046: * @param queue
047: */
048: public EventReader(String name, EventQueue queue) {
049: super (name);
050: fEventQueue = queue;
051: }
052:
053: /**
054: * Registers the given event listener.
055: * @param listener
056: */
057: public synchronized void addEventListener(EventListener listener) {
058: fEventListeners.addElement(listener);
059: }
060:
061: /**
062: * Dispatches the given event to the given listener.
063: * Returns whether the VM should be resumed.
064: */
065: private boolean dispath(Event event, EventListener listener) {
066: if (event instanceof AccessWatchpointEvent)
067: return listener
068: .accessWatchpoint((AccessWatchpointEvent) event);
069: if (event instanceof BreakpointEvent)
070: return listener.breakpoint((BreakpointEvent) event);
071: if (event instanceof ClassPrepareEvent)
072: return listener.classPrepare((ClassPrepareEvent) event);
073: if (event instanceof ClassUnloadEvent)
074: return listener.classUnload((ClassUnloadEvent) event);
075: if (event instanceof ExceptionEvent)
076: return listener.exception((ExceptionEvent) event);
077: if (event instanceof MethodEntryEvent)
078: return listener.methodEntry((MethodEntryEvent) event);
079: if (event instanceof MethodExitEvent)
080: return listener.methodExit((MethodExitEvent) event);
081: if (event instanceof ModificationWatchpointEvent)
082: return listener
083: .modificationWatchpoint((ModificationWatchpointEvent) event);
084: if (event instanceof StepEvent)
085: return listener.step((StepEvent) event);
086: if (event instanceof ThreadDeathEvent)
087: return listener.threadDeath((ThreadDeathEvent) event);
088: if (event instanceof ThreadStartEvent)
089: return listener.threadStart((ThreadStartEvent) event);
090: if (event instanceof VMDisconnectEvent)
091: return listener.vmDisconnect((VMDisconnectEvent) event);
092: if (event instanceof VMDeathEvent)
093: return listener.vmDeath((VMDeathEvent) event);
094: return true;
095: }
096:
097: /**
098: * Continuously reads events that are coming from the event queue.
099: */
100: protected void readerLoop() {
101: while (!fIsStopping) {
102: try {
103: if (!fIsStopping) {
104: // Get the next event
105: EventSet eventSet = fEventQueue.remove();
106:
107: // Dispatch the events
108: boolean shouldGo = true;
109: EventIterator iterator = eventSet.eventIterator();
110: while (iterator.hasNext()) {
111: Event event = iterator.nextEvent();
112: for (int i = 0; i < fEventListeners.size(); i++) {
113: EventListener listener = (EventListener) fEventListeners
114: .elementAt(i);
115: shouldGo = shouldGo
116: & dispath(event, listener);
117: }
118: if (event instanceof VMDeathEvent)
119: stop();
120: }
121:
122: // Let the VM go if it was interrupted
123: if ((!fIsStopping)
124: && (eventSet != null)
125: && (eventSet.suspendPolicy() == EventRequest.SUSPEND_ALL)
126: && shouldGo)
127: synchronized (this ) {
128: fEventQueue.virtualMachine().resume();
129: }
130: }
131: } catch (InterruptedException e) {
132: if (!fIsStopping) {
133: System.out
134: .println("Event reader loop was interrupted");
135: return;
136: }
137: } catch (VMDisconnectedException e) {
138: return;
139: }
140: }
141: }
142:
143: /**
144: * De-registers the given event listener.
145: * @param listener
146: */
147: public synchronized void removeEventListener(EventListener listener) {
148: fEventListeners.removeElement(listener);
149: }
150: }
|