private void dispatch(EventSet eventSet) {
if (isShutdown()) {
return;
}
if (JDIDebugOptions.DEBUG_JDI_EVENTS) {
EventIterator eventIter = eventSet.eventIterator();
StringBuffer buf = new StringBuffer("JDI Event Set: {\n"); //$NON-NLS-1$
while (eventIter.hasNext()) {
buf.append(eventIter.next());
if (eventIter.hasNext()) {
buf.append(", "); //$NON-NLS-1$
}
}
buf.append("}\n"); //$NON-NLS-1$
JDIDebugOptions.trace(buf.toString());
}
EventIterator iter = eventSet.eventIterator();
IJDIEventListener[] listeners = new IJDIEventListener[eventSet.size()];
boolean vote = false;
boolean resume = true;
int index = -1;
List<Event> deferredEvents = null;
while (iter.hasNext()) {
index++;
if (isShutdown()) {
return;
}
Event event = iter.nextEvent();
if (event == null) {
continue;
}
// Dispatch events to registered listeners, if any
IJDIEventListener listener = fEventHandlers.get(event.request());
listeners[index] = listener;
if (listener != null) {
if (listener instanceof IJavaLineBreakpoint) {
// Event dispatch to conditional breakpoints is deferred
// until after
// other listeners vote.
try {
if (((IJavaLineBreakpoint) listener).isConditionEnabled()) {
if (deferredEvents == null) {
deferredEvents = new ArrayList<Event>(5);
}
deferredEvents.add(event);
continue;
}
} catch (CoreException exception) {
JDIDebugPlugin.log(exception);
}
}
vote = true;
resume = listener.handleEvent(event, fTarget, !resume, eventSet) && resume;
continue;
}
// Dispatch VM start/end events
if (event instanceof VMDeathEvent) {
fTarget.handleVMDeath((VMDeathEvent) event);
shutdown(); // stop listening for events
} else if (event instanceof VMDisconnectEvent) {
fTarget.handleVMDisconnect((VMDisconnectEvent) event);
shutdown(); // stop listening for events
} else if (event instanceof VMStartEvent) {
fTarget.handleVMStart((VMStartEvent) event);
} else {
// not handled
}
}
// process deferred conditional breakpoint events
if (deferredEvents != null) {
Iterator<Event> deferredIter = deferredEvents.iterator();
while (deferredIter.hasNext()) {
if (isShutdown()) {
return;
}
Event event = deferredIter.next();
if (event == null) {
continue;
}
// Dispatch events to registered listeners, if any
IJDIEventListener listener = fEventHandlers
.get(event.request());
if (listener != null) {
vote = true;
resume = listener.handleEvent(event, fTarget, !resume, eventSet) && resume;
continue;
}
}
}
// notify handlers of the end result
index = -1;
iter = eventSet.eventIterator();
while (iter.hasNext()) {
index++;
Event event = iter.nextEvent();
// notify registered listener, if any
IJDIEventListener listener = listeners[index];
if (listener != null) {
listener.eventSetComplete(event, fTarget, !resume, eventSet);
}