KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > example > debug > bdi > JDIEventSource


1 /*
2  * @(#)JDIEventSource.java 1.10 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Copyright (c) 1997-1999 by Sun Microsystems, Inc. All Rights Reserved.
9  *
10  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
11  * modify and redistribute this software in source and binary code form,
12  * provided that i) this copyright notice and license appear on all copies of
13  * the software; and ii) Licensee does not utilize the software in a manner
14  * which is disparaging to Sun.
15  *
16  * This software is provided "AS IS," without a warranty of any kind. ALL
17  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
18  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
19  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
20  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
21  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
22  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
23  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
24  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
25  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGES.
27  *
28  * This software is not designed or intended for use in on-line control of
29  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
30  * the design, construction, operation or maintenance of any nuclear
31  * facility. Licensee represents and warrants that it will not use or
32  * redistribute the Software for such purposes.
33  */

34 package com.sun.tools.example.debug.bdi;
35
36 import com.sun.jdi.*;
37 import com.sun.jdi.event.*;
38
39 import java.util.*;
40
41 import com.sun.tools.example.debug.event.*;
42
43 import javax.swing.SwingUtilities JavaDoc;
44
45 /**
46  */

47 class JDIEventSource extends Thread JavaDoc {
48
49     private /*final*/ EventQueue queue;
50     private /*final*/ Session session;
51     private /*final*/ ExecutionManager runtime;
52     private final JDIListener firstListener = new FirstListener();
53
54     private boolean wantInterrupt; //### Hack
55

56     /**
57      * Create event source.
58      */

59     JDIEventSource(Session session) {
60         super("JDI Event Set Dispatcher");
61     this.session = session;
62         this.runtime = session.runtime;
63         this.queue = session.vm.eventQueue();
64     }
65     
66     public void run() {
67         try {
68             runLoop();
69         } catch (Exception JavaDoc exc) {
70             //### Do something different for InterruptedException???
71
// just exit
72
}
73         session.running = false;
74     }
75
76     private void runLoop() throws InterruptedException JavaDoc {
77         AbstractEventSet es;
78         do {
79             EventSet jdiEventSet = queue.remove();
80             es = AbstractEventSet.toSpecificEventSet(jdiEventSet);
81         session.interrupted = es.suspendedAll();
82         dispatchEventSet(es);
83         } while(!(es instanceof VMDisconnectEventSet));
84     }
85
86     //### Gross foul hackery!
87
private void dispatchEventSet(final AbstractEventSet es) {
88         SwingUtilities.invokeLater(new Runnable JavaDoc() {
89             public void run() {
90                 boolean interrupted = es.suspendedAll();
91                 es.notify(firstListener);
92                 boolean wantInterrupt = JDIEventSource.this.wantInterrupt;
93                 for (Iterator it = session.runtime.jdiListeners.iterator();
94                      it.hasNext(); ) {
95                     JDIListener jl = (JDIListener)it.next();
96                     es.notify(jl);
97                 }
98                 if (interrupted && !wantInterrupt) {
99                     session.interrupted = false;
100                     //### Catch here is a hack
101
try {
102                         session.vm.resume();
103                     } catch (VMDisconnectedException ee) {}
104                 }
105                 if (es instanceof ThreadDeathEventSet) {
106                     ThreadReference t = ((ThreadDeathEventSet)es).getThread();
107                     session.runtime.removeThreadInfo(t);
108                 }
109             }
110         });
111     }
112
113     private void finalizeEventSet(AbstractEventSet es) {
114         if (session.interrupted && !wantInterrupt) {
115             session.interrupted = false;
116             //### Catch here is a hack
117
try {
118                 session.vm.resume();
119             } catch (VMDisconnectedException ee) {}
120         }
121         if (es instanceof ThreadDeathEventSet) {
122             ThreadReference t = ((ThreadDeathEventSet)es).getThread();
123             session.runtime.removeThreadInfo(t);
124         }
125     }
126
127     //### This is a Hack, deal with it
128
private class FirstListener implements JDIListener {
129
130         public void accessWatchpoint(AccessWatchpointEventSet e) {
131             session.runtime.validateThreadInfo();
132             wantInterrupt = true;
133         }
134
135         public void classPrepare(ClassPrepareEventSet e) {
136             wantInterrupt = false;
137             runtime.resolve(e.getReferenceType());
138         }
139
140         public void classUnload(ClassUnloadEventSet e) {
141             wantInterrupt = false;
142         }
143
144         public void exception(ExceptionEventSet e) {
145             wantInterrupt = true;
146         }
147
148         public void locationTrigger(LocationTriggerEventSet e) {
149             session.runtime.validateThreadInfo();
150             wantInterrupt = true;
151         }
152
153         public void modificationWatchpoint(ModificationWatchpointEventSet e) {
154             session.runtime.validateThreadInfo();
155             wantInterrupt = true;
156         }
157
158         public void threadDeath(ThreadDeathEventSet e) {
159             wantInterrupt = false;
160         }
161
162         public void threadStart(ThreadStartEventSet e) {
163             wantInterrupt = false;
164         }
165
166         public void vmDeath(VMDeathEventSet e) {
167             //### Should have some way to notify user
168
//### that VM died before the session ended.
169
wantInterrupt = false;
170         }
171
172         public void vmDisconnect(VMDisconnectEventSet e) {
173             //### Notify user?
174
wantInterrupt = false;
175             session.runtime.endSession();
176         }
177
178         public void vmStart(VMStartEventSet e) {
179             //### Do we need to do anything with it?
180
wantInterrupt = false;
181         }
182     }
183 }
184
Popular Tags