KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdi > internal > event > EventQueueImpl


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdi.internal.event;
12
13
14 import java.io.IOException JavaDoc;
15
16 import org.eclipse.jdi.TimeoutException;
17 import org.eclipse.jdi.internal.MirrorImpl;
18 import org.eclipse.jdi.internal.VirtualMachineImpl;
19 import org.eclipse.jdi.internal.connect.PacketReceiveManager;
20 import org.eclipse.jdi.internal.jdwp.JdwpCommandPacket;
21 import org.eclipse.jdi.internal.request.RequestID;
22
23 import com.sun.jdi.VMDisconnectedException;
24 import com.sun.jdi.event.EventQueue;
25 import com.sun.jdi.event.EventSet;
26
27 /**
28  * this class implements the corresponding interfaces
29  * declared by the JDI specification. See the com.sun.jdi package
30  * for more information.
31  *
32  */

33 public class EventQueueImpl extends MirrorImpl implements EventQueue {
34     /** Flag used to see if a VMDisconnectEvent has already been generated. */
35     private boolean genereatedVMDisconnectEvent = false;
36     
37     /**
38      * Creates new EventQueueImpl.
39      */

40     public EventQueueImpl(VirtualMachineImpl vmImpl) {
41         super("EventQueue", vmImpl); //$NON-NLS-1$
42
}
43
44     /*
45      * @return Returns next EventSet from Virtual Machine.
46      */

47     public EventSet remove() throws InterruptedException JavaDoc {
48         return remove(PacketReceiveManager.TIMEOUT_INFINITE);
49     }
50
51     /*
52      * @return Returns next EventSet from Virtual Machine, returns null if times out.
53      */

54     public EventSet remove(long timeout) throws InterruptedException JavaDoc {
55         // Return a received EventSet or null if no EventSet is received in time.
56
// Note that handledJdwpEventSet() is not don in a 'finally' clause because
57
// it must also be done when an 'empty' set is read (i.e. a set composed of internal
58
// events only).
59
try {
60             // We remove elements from event sets that are generated from inside, therefore the set may become empty.
61
EventSetImpl set;
62             do {
63                 JdwpCommandPacket packet = getCommandVM(JdwpCommandPacket.E_COMPOSITE, timeout);
64                 initJdwpEventSet(packet);
65                 set = EventSetImpl.read(this, packet.dataInStream());
66                 handledJdwpEventSet();
67             } while (set.isEmpty());
68             return set;
69         } catch (TimeoutException e) {
70             // Timeout in getCommand, JDI spec says return null.
71
handledJdwpEventSet();
72             return null;
73         } catch (IOException JavaDoc e) {
74             // This means the already received data is invalid.
75
handledJdwpEventSet();
76             defaultIOExceptionHandler(e);
77             return null;
78         } catch(VMDisconnectedException e) {
79             // JDI spec says that a VMDisconnectedException must always be preceeded by a VMDisconnectEvent.
80
handledJdwpEventSet();
81             if (!genereatedVMDisconnectEvent) {
82                 genereatedVMDisconnectEvent = true;
83                 return new EventSetImpl(virtualMachineImpl(), new VMDisconnectEventImpl(virtualMachineImpl(), RequestID.nullID));
84             }
85             throw e;
86         }
87     }
88 }
89
Popular Tags