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 com.sun.jdi.event.AccessWatchpointEvent;
013: import com.sun.jdi.event.BreakpointEvent;
014: import com.sun.jdi.event.ClassPrepareEvent;
015: import com.sun.jdi.event.ClassUnloadEvent;
016: import com.sun.jdi.event.Event;
017: import com.sun.jdi.event.ExceptionEvent;
018: import com.sun.jdi.event.MethodEntryEvent;
019: import com.sun.jdi.event.MethodExitEvent;
020: import com.sun.jdi.event.ModificationWatchpointEvent;
021: import com.sun.jdi.event.StepEvent;
022: import com.sun.jdi.event.ThreadDeathEvent;
023: import com.sun.jdi.event.ThreadStartEvent;
024: import com.sun.jdi.event.VMDeathEvent;
025: import com.sun.jdi.event.VMDisconnectEvent;
026: import com.sun.jdi.request.EventRequest;
027:
028: /**
029: * Listen for a specific kind of event.
030: */
031: public class EventWaiter implements EventListener {
032: protected EventRequest fRequest;
033: protected boolean fShouldGo;
034: protected Event fEvent;
035:
036: /**
037: * Creates a new EventWaiter for the given request. Sets whether it
038: * should let the VM go after it got the event.
039: * @param request
040: * @param shouldGo
041: */
042: public EventWaiter(EventRequest request, boolean shouldGo) {
043: fRequest = request;
044: fShouldGo = shouldGo;
045: }
046:
047: /**
048: * @see org.eclipse.debug.jdi.tests.EventListener#accessWatchpoint(com.sun.jdi.event.AccessWatchpointEvent)
049: */
050: public boolean accessWatchpoint(AccessWatchpointEvent event) {
051: return handleEvent(event);
052: }
053:
054: /**
055: * @see org.eclipse.debug.jdi.tests.EventListener#methodEntry(com.sun.jdi.event.MethodEntryEvent)
056: */
057: public boolean methodEntry(MethodEntryEvent event) {
058: return handleEvent(event);
059: }
060:
061: /**
062: * @see org.eclipse.debug.jdi.tests.EventListener#methodExit(com.sun.jdi.event.MethodExitEvent)
063: */
064: public boolean methodExit(MethodExitEvent event) {
065: return handleEvent(event);
066: }
067:
068: /**
069: * @see org.eclipse.debug.jdi.tests.EventListener#breakpoint(com.sun.jdi.event.BreakpointEvent)
070: */
071: public boolean breakpoint(BreakpointEvent event) {
072: return handleEvent(event);
073: }
074:
075: /**
076: * @see org.eclipse.debug.jdi.tests.EventListener#classPrepare(com.sun.jdi.event.ClassPrepareEvent)
077: */
078: public boolean classPrepare(ClassPrepareEvent event) {
079: return handleEvent(event);
080: }
081:
082: /**
083: * @see org.eclipse.debug.jdi.tests.EventListener#classUnload(com.sun.jdi.event.ClassUnloadEvent)
084: */
085: public boolean classUnload(ClassUnloadEvent event) {
086: return handleEvent(event);
087: }
088:
089: /**
090: * @see org.eclipse.debug.jdi.tests.EventListener#exception(com.sun.jdi.event.ExceptionEvent)
091: */
092: public boolean exception(ExceptionEvent event) {
093: return handleEvent(event);
094: }
095:
096: /**
097: * Handles an incoming event.
098: * Returns whether the VM should be resumed if it was suspended.
099: */
100: protected boolean handleEvent(Event event) {
101: if ((event.request() != null)
102: && (event.request().equals(fRequest))) {
103: notifyEvent(event);
104: return fShouldGo;
105: }
106: return true;
107: }
108:
109: /**
110: * @see org.eclipse.debug.jdi.tests.EventListener#modificationWatchpoint(com.sun.jdi.event.ModificationWatchpointEvent)
111: */
112: public boolean modificationWatchpoint(
113: ModificationWatchpointEvent event) {
114: return handleEvent(event);
115: }
116:
117: /**
118: * Notify any object that is waiting for an event.
119: */
120: synchronized protected void notifyEvent(Event event) {
121: notify();
122: fEvent = event;
123: }
124:
125: /**
126: * @see org.eclipse.debug.jdi.tests.EventListener#step(com.sun.jdi.event.StepEvent)
127: */
128: public boolean step(StepEvent event) {
129: return handleEvent(event);
130: }
131:
132: /**
133: * @see org.eclipse.debug.jdi.tests.EventListener#threadDeath(com.sun.jdi.event.ThreadDeathEvent)
134: */
135: public boolean threadDeath(ThreadDeathEvent event) {
136: return handleEvent(event);
137: }
138:
139: /**
140: * @see org.eclipse.debug.jdi.tests.EventListener#threadStart(com.sun.jdi.event.ThreadStartEvent)
141: */
142: public boolean threadStart(ThreadStartEvent event) {
143: return handleEvent(event);
144: }
145:
146: /**
147: * @see org.eclipse.debug.jdi.tests.EventListener#vmDeath(com.sun.jdi.event.VMDeathEvent)
148: */
149: public boolean vmDeath(VMDeathEvent event) {
150: if (fEvent == null) {
151: // This is the last event we can ever get an this was not the one we expected
152: notifyEvent(null);
153: return true;
154: }
155: return handleEvent(event);
156: }
157:
158: /**
159: * @see org.eclipse.debug.jdi.tests.EventListener#vmDisconnect(com.sun.jdi.event.VMDisconnectEvent)
160: */
161: public boolean vmDisconnect(VMDisconnectEvent event) {
162: return handleEvent(event);
163: }
164:
165: /**
166: * Waits for the first event corresponding to this waiter's request.
167: * @return if the vm should be restarted
168: * @throws InterruptedException
169: */
170: synchronized public Event waitEvent() throws InterruptedException {
171: if (fEvent == null) // If event didn't already come in
172: wait();
173: Event result = fEvent;
174: fEvent = null;
175: return result;
176: }
177:
178: /**
179: * Waits for the first event corresponding to this waiter's request
180: * for the given time (in ms). If it times out, return null.
181: * @param time
182: * @return if the vm should be restarted or not
183: * @throws InterruptedException
184: */
185: synchronized public Event waitEvent(long time)
186: throws InterruptedException {
187: if (fEvent == null) // If event didn't already come in
188: wait(time);
189: Event result = fEvent;
190: fEvent = null;
191: return result;
192: }
193: }
|