KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
29
30 /**
31  * ThreadsRequest.java
32  *
33  *
34  * Created: Thu Aug 24 13:18:28 2000
35  *
36  * @author <a HREF="mailto:palm@parc.xerox.com"Jeffrey Palm</a>
37  */

38
39 public class ThreadsRequest extends Request {
40
41     private String JavaDoc threadGroupName;
42
43     public ThreadsRequest(Debugger debugger, String JavaDoc threadGroupName) {
44         super(debugger);
45         this.threadGroupName = threadGroupName.trim();
46     }
47
48     public Object JavaDoc go() throws NoVMException, DebuggerException {
49         List list = new ArrayList();
50         if (threadGroupName == null || threadGroupName.length() == 0) {
51             list = getAllThreads();
52         } else {
53             list = getThreads();
54         }
55         return list;
56     }
57
58     private List getAllThreads() throws NoVMException, DebuggerException {
59         return vm().allThreads();
60     }
61
62     private List getThreads() throws NoVMException, DebuggerException {
63         List result = getThreads(null);
64         if (result == null) {
65             throw new ThreadGroupNotFoundException();
66         }
67         return result;
68     }
69
70     private List getThreads(ThreadGroupReference ref) throws NoVMException, DebuggerException {
71         if (ref != null) {
72             if (ref.name().equals(threadGroupName)) {
73                 return ref.threads();
74             }
75
76             try {
77                 long ourID = Long.parseLong(threadGroupName);
78                 long refID = ref.uniqueID();
79                 if (ourID == refID) {
80                     return ref.threads();
81                 }
82             } catch (NumberFormatException JavaDoc e) {
83             }
84         }
85
86         List threadGroups = null;
87         if (ref == null) {
88             threadGroups = vm().topLevelThreadGroups();
89         } else {
90             threadGroups = ref.threadGroups();
91         }
92         Iterator iter = threadGroups.iterator();
93         while (iter.hasNext()) {
94             List threads = getThreads((ThreadGroupReference) iter.next());
95             if (threads != null) {
96                 return threads;
97             }
98         }
99         return null;
100     }
101
102     class ThreadGroupNotFoundException extends DebuggerException {
103         public ThreadGroupNotFoundException() {
104             super(ThreadsRequest.this.threadGroupName +
105                   " is not a valid thread group.");
106         }
107     }
108 }
109
Popular Tags