1 11 package org.eclipse.jdt.internal.debug.core; 12 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.debug.core.DebugEvent; 21 import org.eclipse.debug.core.DebugException; 22 import org.eclipse.debug.core.DebugPlugin; 23 import org.eclipse.jdt.debug.core.IJavaLineBreakpoint; 24 import org.eclipse.jdt.internal.debug.core.model.JDIDebugTarget; 25 26 import com.sun.jdi.VMDisconnectedException; 27 import com.sun.jdi.VirtualMachine; 28 import com.sun.jdi.event.Event; 29 import com.sun.jdi.event.EventIterator; 30 import com.sun.jdi.event.EventQueue; 31 import com.sun.jdi.event.EventSet; 32 import com.sun.jdi.event.VMDeathEvent; 33 import com.sun.jdi.event.VMDisconnectEvent; 34 import com.sun.jdi.event.VMStartEvent; 35 import com.sun.jdi.request.EventRequest; 36 37 57 58 public class EventDispatcher implements Runnable { 59 62 private JDIDebugTarget fTarget; 63 66 private boolean fShutdown; 67 72 private HashMap fEventHandlers; 73 74 78 private List fDebugEvents = new ArrayList (5); 79 80 86 private List fDeferredEvents = new ArrayList (5); 87 88 95 public EventDispatcher(JDIDebugTarget target) { 96 fEventHandlers = new HashMap (10); 97 fTarget= target; 98 fShutdown = false; 99 } 100 101 106 protected void dispatch(EventSet eventSet) { 107 if (isShutdown()) { 108 return; 109 } 110 EventIterator iter= eventSet.eventIterator(); 111 boolean vote = false; 112 boolean resume = true; 113 int voters = 0; 114 Event winningEvent = null; 115 IJDIEventListener winner = null; 116 while (iter.hasNext()) { 117 if (isShutdown()) { 118 return; 119 } 120 Event event= iter.nextEvent(); 121 if (event == null) { 122 continue; 123 } 124 IJDIEventListener listener = (IJDIEventListener)fEventHandlers.get(event.request()); 126 if (listener != null) { 127 if (listener instanceof IJavaLineBreakpoint) { 128 try { 131 if (((IJavaLineBreakpoint)listener).isConditionEnabled()) { 132 defer(event); 133 continue; 134 } 135 } catch (CoreException exception) { 136 JDIDebugPlugin.log(exception); 137 } 138 } 139 vote = true; 140 resume = listener.handleEvent(event, fTarget) && resume; 141 voters++; 142 if (!resume && winner == null) { 143 winner = listener; 144 winningEvent = event; 145 } 146 continue; 147 } 148 149 if (event instanceof VMDeathEvent) { 151 fTarget.handleVMDeath((VMDeathEvent) event); 152 shutdown(); } else if (event instanceof VMDisconnectEvent) { 154 fTarget.handleVMDisconnect((VMDisconnectEvent) event); 155 shutdown(); } else if (event instanceof VMStartEvent) { 157 fTarget.handleVMStart((VMStartEvent)event); 158 } else { 159 } 161 } 162 163 if (resume) { 164 if (!getDeferredEvents().isEmpty()) { 167 Iterator deferredIter= getDeferredEvents().iterator(); 168 while (deferredIter.hasNext()) { 169 if (isShutdown()) { 170 return; 171 } 172 Event event= (Event)deferredIter.next(); 173 if (event == null) { 174 continue; 175 } 176 IJDIEventListener listener = (IJDIEventListener)fEventHandlers.get(event.request()); 178 if (listener != null) { 179 vote = true; 180 resume = listener.handleEvent(event, fTarget) && resume; 181 continue; 182 } 183 } 184 } 185 } 186 getDeferredEvents().clear(); 188 189 if (winner != null && voters > 1) { 191 winner.wonSuspendVote(winningEvent, fTarget); 192 } 193 194 fireEvents(); 195 196 if (vote && resume) { 197 try { 198 eventSet.resume(); 199 } catch (VMDisconnectedException e) { 200 } catch (RuntimeException e) { 201 try { 202 fTarget.targetRequestFailed(JDIDebugMessages.EventDispatcher_0, e); 203 } catch (DebugException de) { 204 JDIDebugPlugin.log(de); 205 } 206 } 207 } 208 } 209 210 217 public void run() { 218 VirtualMachine vm = fTarget.getVM(); 219 if (vm != null) { 220 EventQueue q= vm.eventQueue(); 221 EventSet eventSet= null; 222 while (!isShutdown()) { 223 try { 224 try { 225 eventSet= q.remove(1000); 227 } catch (VMDisconnectedException e) { 228 break; 229 } 230 231 if(!isShutdown() && eventSet != null) { 232 dispatch(eventSet); 233 } 234 } catch (InterruptedException e) { 235 break; 236 } 237 } 238 } 239 } 240 241 247 public void shutdown() { 248 fShutdown= true; 249 } 250 251 258 protected boolean isShutdown() { 259 return fShutdown; 260 } 261 262 272 public void addJDIEventListener(IJDIEventListener listener, EventRequest request) { 273 fEventHandlers.put(request, listener); 274 } 275 276 285 public void removeJDIEventListener(IJDIEventListener listener, EventRequest request) { 286 fEventHandlers.remove(request); 287 } 288 289 295 public void queue(DebugEvent event) { 296 fDebugEvents.add(event); 297 } 298 299 302 protected void fireEvents() { 303 DebugPlugin plugin= DebugPlugin.getDefault(); 304 if (plugin != null) { DebugEvent[] events = (DebugEvent[])fDebugEvents.toArray(new DebugEvent[fDebugEvents.size()]); 306 fDebugEvents.clear(); 307 plugin.fireDebugEventSet(events); 308 } 309 } 310 311 317 protected void defer(Event event) { 318 fDeferredEvents.add(event); 319 } 320 321 326 protected List getDeferredEvents() { 327 return fDeferredEvents; 328 } 329 330 } 331 332 | Popular Tags |