Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 71afdcb0b374cdae6e5de360726b18ac83fac107 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*******************************************************************************
 * Copyright (c) 2000, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.debug.jdi.tests;

import java.util.Vector;

import com.sun.jdi.VMDisconnectedException;
import com.sun.jdi.event.AccessWatchpointEvent;
import com.sun.jdi.event.BreakpointEvent;
import com.sun.jdi.event.ClassPrepareEvent;
import com.sun.jdi.event.ClassUnloadEvent;
import com.sun.jdi.event.Event;
import com.sun.jdi.event.EventIterator;
import com.sun.jdi.event.EventQueue;
import com.sun.jdi.event.EventSet;
import com.sun.jdi.event.ExceptionEvent;
import com.sun.jdi.event.MethodEntryEvent;
import com.sun.jdi.event.MethodExitEvent;
import com.sun.jdi.event.ModificationWatchpointEvent;
import com.sun.jdi.event.StepEvent;
import com.sun.jdi.event.ThreadDeathEvent;
import com.sun.jdi.event.ThreadStartEvent;
import com.sun.jdi.event.VMDeathEvent;
import com.sun.jdi.event.VMDisconnectEvent;
import com.sun.jdi.request.EventRequest;

/**
 * An event reader that continuously reads events coming from the VM
 * and dispatch them to the registered listeners.
 */

public class EventReader extends AbstractReader {
	private EventQueue fEventQueue;
	private Vector fEventListeners = new Vector(); // A Vector of EventListener

	/**
	 * Constructor
	 * @param name
	 * @param queue
	 */
	public EventReader(String name, EventQueue queue) {
		super(name);
		fEventQueue = queue;
	}
	/**
	 * Registers the given event listener.
	 * @param listener
	 */
	public synchronized void addEventListener(EventListener listener) {
		fEventListeners.addElement(listener);
	}
	/**
	 * Dispatches the given event to the given listener.
	 * Returns whether the VM should be resumed.
	 */
	private boolean dispath(Event event, EventListener listener) {
		if (event instanceof AccessWatchpointEvent)
			return listener.accessWatchpoint((AccessWatchpointEvent) event);
		if (event instanceof BreakpointEvent)
			return listener.breakpoint((BreakpointEvent) event);
		if (event instanceof ClassPrepareEvent)
			return listener.classPrepare((ClassPrepareEvent) event);
		if (event instanceof ClassUnloadEvent)
			return listener.classUnload((ClassUnloadEvent) event);
		if (event instanceof ExceptionEvent)
			return listener.exception((ExceptionEvent) event);
		if (event instanceof MethodEntryEvent)
			return listener.methodEntry((MethodEntryEvent) event);
		if (event instanceof MethodExitEvent)
			return listener.methodExit((MethodExitEvent) event);
		if (event instanceof ModificationWatchpointEvent)
			return listener.modificationWatchpoint(
				(ModificationWatchpointEvent) event);
		if (event instanceof StepEvent)
			return listener.step((StepEvent) event);
		if (event instanceof ThreadDeathEvent)
			return listener.threadDeath((ThreadDeathEvent) event);
		if (event instanceof ThreadStartEvent)
			return listener.threadStart((ThreadStartEvent) event);
		if (event instanceof VMDisconnectEvent)
			return listener.vmDisconnect((VMDisconnectEvent) event);
		if (event instanceof VMDeathEvent)
			return listener.vmDeath((VMDeathEvent) event);
		return true;
	}
	/**
	 * Continuously reads events that are coming from the event queue.
	 */
	@Override
	protected void readerLoop() {
		while (!fIsStopping) {
			try {
				if (!fIsStopping) {
					// Get the next event
					EventSet eventSet = fEventQueue.remove();

					// Dispatch the events
					boolean shouldGo = true;
					EventIterator iterator = eventSet.eventIterator();
					while (iterator.hasNext()) {
						Event event = iterator.nextEvent();
						for (int i = 0; i < fEventListeners.size(); i++) {
							EventListener listener =
								(EventListener) fEventListeners.elementAt(i);
							shouldGo = shouldGo & dispath(event, listener);
						}
						if (event instanceof VMDeathEvent)
							stop();
					}

					// Let the VM go if it was interrupted
					if ((!fIsStopping)
						&& (eventSet != null)
						&& (eventSet.suspendPolicy() == EventRequest.SUSPEND_ALL)
						&& shouldGo)
						synchronized (this) {
							fEventQueue.virtualMachine().resume();
						}
				}
			} catch (InterruptedException e) {
				if (!fIsStopping) {
					System.out.println("Event reader loop was interrupted");
					return;
				}
			} catch (VMDisconnectedException e) {
				return;
			}
		}
	}
	/**
	 * De-registers the given event listener.
	 * @param listener
	 */
	public synchronized void removeEventListener(EventListener listener) {
		fEventListeners.removeElement(listener);
	}
}

Back to the top