KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > debugger > base > EventSpinner


1 /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the debugger and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  */

22
23 package org.aspectj.debugger.base;
24
25 import com.sun.jdi.*;
26 import com.sun.jdi.event.*;
27 import com.sun.jdi.request.*;
28 import java.util.*;
29
30 /**
31  * EventSpinner.java
32  *
33  * The event list.
34  *
35  * Created: Mon Aug 21 11:43:00 2000
36  *
37  * @author <a HREF="mailto:palm@parc.xerox.com"Jeffrey Palm</a>
38  */

39
40 public class EventSpinner implements Runnable JavaDoc {
41
42     private Thread JavaDoc runner;
43     private EventQueue eventQueue;
44     private Debugger debugger;
45     private boolean isRunning;
46     private boolean isDone;
47
48     /**
49      * Create a new EventSpinner out of the object that should be notified.
50      */

51     public EventSpinner(Debugger debugger) {
52         this.debugger = debugger;
53     }
54
55     public void start() {
56         Debug.debug(this, "Starting the event queue");
57         try {
58             eventQueue = debugger.getVM().eventQueue();
59             isRunning = true;
60             (runner = new Thread JavaDoc(this)).start();
61         } catch (NoVMException e) {
62         }
63     }
64
65     public void run() {
66         EventIterator iter = null;
67         boolean disconnect = false;
68         try {
69             while (isRunning) {
70                 EventSet eventSet = eventQueue.remove();
71                 handle(eventSet);
72             }
73         } catch (InterruptedException JavaDoc ie) {
74         } catch (VMDisconnectedException vmde) {
75             disconnect = true;
76             vmde.getMessage(); // wait a bit
77
} catch (Exception JavaDoc e) {
78             debugger.app().handleInternalException(e);
79         } finally {
80             try {
81                 EventSet eventSet = null;
82                 while ((eventSet = eventQueue.remove(10)) != null) {
83                     handle(eventSet);
84                 }
85             } catch (InterruptedException JavaDoc ie) {
86             } catch (VMDisconnectedException vmde) {
87                 disconnect = true;
88             }
89         }
90
91         try {
92             isDone = true;
93             notifyAll();
94         } catch (IllegalMonitorStateException JavaDoc e) {
95         }
96     }
97
98     private void handle(EventSet set) throws VMDisconnectedException {
99         if (set == null) {
100             return;
101         }
102         try {
103             EventIterator iter = set.eventIterator();
104             boolean resume = false;
105             printThreads(true);
106             while (iter.hasNext()) {
107                 Event event = iter.nextEvent();
108                 resume |= debugger.handle(event);
109             }
110             if (resume) {
111                 debugger.getVM().resume();
112             }
113             printThreads(false);
114         } catch (NoVMException nvme) {
115         }
116     }
117
118     boolean printThreads = false;
119     void printThreads(boolean before) {
120         try {
121             if (!printThreads) return;
122             Iterator threadIter = debugger.getVM().allThreads().iterator();
123             System.out.print((before ? "BEFORE" : "AFTER") + ": ");
124             while (threadIter.hasNext()) {
125                 ThreadReference threadRef = (ThreadReference) threadIter.next();
126                 System.out.print("(" + threadRef.uniqueID() + "):" + threadRef.suspendCount() + ", ");
127             }
128             System.out.println();
129         } catch (NoVMException nvme) {
130         }
131     }
132
133     public void shutDown() {
134         isRunning = false;
135         if (runner != null) runner.interrupt();
136         while (!isDone) {
137             try {wait();} catch (InterruptedException JavaDoc ie) {}
138                           catch (IllegalMonitorStateException JavaDoc imse) { return; }
139         }
140     }
141
142     class DummyVMDisconnectEvent implements VMDisconnectEvent {
143         public String JavaDoc toString() {
144             return "Dummy VMDisconnectEvent: vm=" + debugger.vm();
145         }
146
147         public VirtualMachine virtualMachine() {
148             return debugger.vm();
149         }
150
151         public EventRequest request() {
152             return null;
153         }
154     }
155 }
156
Popular Tags