KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > debugger > request > ThreadRequestAction


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.request;
24
25 import org.aspectj.debugger.base.*;
26
27 import com.sun.jdi.*;
28 import com.sun.jdi.request.*;
29 import com.sun.jdi.event.*;
30 import java.util.*;
31
32 /**
33  * ThreadRequestAction.java
34  *
35  *
36  * Created: Mon Aug 28 09:41:48 2000
37  *
38  * @author <a HREF="mailto:palm@parc.xerox.com"Jeffrey Palm</a>
39  */

40
41 public abstract class ThreadRequestAction extends RequestActionSupport implements ThreadListener {
42
43     protected String JavaDoc threadName;
44     
45     public ThreadRequestAction(Debugger debugger, String JavaDoc threadName) {
46         super(debugger);
47         this.threadName = threadName;
48     }
49     
50     void resolveAll() {
51         resolveAgainstAllThreads();
52     }
53
54     protected void addListeners() {
55         super.addListeners();
56         debugger.addThreadListener(this);
57     }
58
59     protected void removeListeners() {
60         super.removeListeners();
61         debugger.removeThreadListener(this);
62     }
63
64     abstract boolean isEnter();
65
66     protected EventRequest resolve(ThreadReference threadRef) {
67         try {
68             if (isSame(threadRef)) {
69                 EventRequestManager em = vm().eventRequestManager();
70                 if (isEnter()) {
71                     MethodEntryRequest entry = em.createMethodEntryRequest();
72                     entry.addThreadFilter(threadRef);
73                     entry.setSuspendPolicy(EventRequest.SUSPEND_NONE); //TODO: Change to ALL
74
entry.enable();
75                     debugger.addExcludesTo(entry);
76                     return entry;
77                 } else {
78                     MethodExitRequest exit = em.createMethodExitRequest();
79                     exit.addThreadFilter(threadRef);
80                     exit.setSuspendPolicy(EventRequest.SUSPEND_NONE); //TODO: Change to ALL
81
exit.enable();
82                     debugger.addExcludesTo(exit);
83                     return exit;
84                 }
85             }
86         } catch (NoVMException e) {
87         }
88         return null;
89     }
90
91     protected boolean isSame(ThreadReference threadRef) {
92         if (threadRef.name().equals(threadName)) {
93             return true;
94         } else {
95             try {
96                 long ourID = Long.parseLong(threadName);
97                 long refID = threadRef.uniqueID();
98                 if (ourID == refID) {
99                     return true;
100                 }
101             } catch (NumberFormatException JavaDoc e) {
102             }
103         }
104         return false;
105     }
106
107     protected boolean resolveAgainstAllThreads() {
108         try {
109             Iterator iter = debugger.getVM().allThreads().iterator();
110             while (iter.hasNext()) {
111                 if (setRequest(resolve((ThreadReference) iter.next())) != null) {
112                     return success();
113                 }
114             }
115         } catch (NoVMException e) {
116         }
117         return failure(true);
118     }
119     
120     protected boolean resolveAgainstThread(ThreadReference threadRef) {
121         if (setRequest(resolve(threadRef)) != null) {
122             return success();
123         }
124         return failure(false);
125     }
126     
127     /* ThreadListener */
128     public void threadStartEvent(ThreadStartEvent e) {
129         if (!isSet()) {
130             resolve(e.thread());
131         }
132     }
133     public void threadDeathEvent(ThreadDeathEvent e) {
134     }
135 }
136
Popular Tags