KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > core > model > JDIThreadGroup


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.jdt.internal.debug.core.model;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.debug.core.DebugException;
18 import org.eclipse.debug.core.model.ITerminate;
19 import org.eclipse.jdt.debug.core.IJavaThread;
20 import org.eclipse.jdt.debug.core.IJavaThreadGroup;
21
22 import com.sun.jdi.ThreadGroupReference;
23 import com.sun.jdi.ThreadReference;
24
25 /**
26  * @since 3.2
27  *
28  */

29 public class JDIThreadGroup extends JDIDebugElement implements IJavaThreadGroup, ITerminate {
30     
31     private ThreadGroupReference fGroup = null;
32     private String JavaDoc fName = null;
33
34     /**
35      * Constructs a new thread group in the given target based on the underlying
36      * thread group reference.
37      *
38      * @param target debug target
39      * @param group thread group reference
40      */

41     public JDIThreadGroup(JDIDebugTarget target, ThreadGroupReference group) {
42         super(target);
43         fGroup = group;
44     }
45
46     /* (non-Javadoc)
47      * @see org.eclipse.jdt.debug.core.IJavaThreadGroup#getThreads()
48      */

49     public synchronized IJavaThread[] getThreads() throws DebugException {
50         try {
51             List JavaDoc threads = fGroup.threads();
52             List JavaDoc modelThreads = new ArrayList JavaDoc(threads.size());
53             Iterator JavaDoc iterator = threads.iterator();
54             while (iterator.hasNext()) {
55                 ThreadReference ref = (ThreadReference) iterator.next();
56                 JDIThread thread = getJavaDebugTarget().findThread(ref);
57                 if (thread != null) {
58                     modelThreads.add(thread);
59                 }
60             }
61             return (IJavaThread[]) modelThreads.toArray(new IJavaThread[modelThreads.size()]);
62         } catch (RuntimeException JavaDoc e) {
63             targetRequestFailed(JDIDebugModelMessages.JDIThreadGroup_0, e);
64         }
65         return null;
66     }
67
68     /* (non-Javadoc)
69      * @see org.eclipse.jdt.debug.core.IJavaThreadGroup#getThreadGroup()
70      */

71     public IJavaThreadGroup getThreadGroup() throws DebugException {
72         try {
73             ThreadGroupReference reference = fGroup.parent();
74             if (reference != null) {
75                 return getJavaDebugTarget().findThreadGroup(reference);
76             }
77         } catch (RuntimeException JavaDoc e) {
78             targetRequestFailed(JDIDebugModelMessages.JDIThreadGroup_1, e);
79         }
80         return null;
81     }
82
83     /* (non-Javadoc)
84      * @see org.eclipse.jdt.debug.core.IJavaThreadGroup#getThreadGroups()
85      */

86     public IJavaThreadGroup[] getThreadGroups() throws DebugException {
87         try {
88             List JavaDoc groups = fGroup.threadGroups();
89             List JavaDoc modelGroups = new ArrayList JavaDoc(groups.size());
90             Iterator JavaDoc iterator = groups.iterator();
91             while (iterator.hasNext()) {
92                 ThreadGroupReference ref = (ThreadGroupReference) iterator.next();
93                 JDIThreadGroup group = getJavaDebugTarget().findThreadGroup(ref);
94                 if (group != null) {
95                     modelGroups.add(group);
96                 }
97             }
98             return (IJavaThreadGroup[]) modelGroups.toArray(new IJavaThreadGroup[modelGroups.size()]);
99         } catch (RuntimeException JavaDoc e) {
100             targetRequestFailed(JDIDebugModelMessages.JDIThreadGroup_2, e);
101         }
102         return null;
103     }
104
105     /* (non-Javadoc)
106      * @see org.eclipse.jdt.debug.core.IJavaThreadGroup#getName()
107      */

108     public synchronized String JavaDoc getName() throws DebugException {
109         if (fName == null) {
110             try {
111                 fName = fGroup.name();
112             } catch (RuntimeException JavaDoc e) {
113                 targetRequestFailed(JDIDebugModelMessages.JDIThreadGroup_3, e);
114             }
115         }
116         return fName;
117     }
118     
119     ThreadGroupReference getUnderlyingThreadGroup() {
120         return fGroup;
121     }
122
123     /* (non-Javadoc)
124      * @see org.eclipse.jdt.debug.core.IJavaThreadGroup#hasThreadGroups()
125      */

126     public boolean hasThreadGroups() throws DebugException {
127         try {
128             List JavaDoc groups = fGroup.threadGroups();
129             return groups.size() > 0;
130         } catch (RuntimeException JavaDoc e) {
131             targetRequestFailed(JDIDebugModelMessages.JDIThreadGroup_4, e);
132         }
133         return false;
134     }
135
136     /* (non-Javadoc)
137      * @see org.eclipse.jdt.debug.core.IJavaThreadGroup#hasThreads()
138      */

139     public boolean hasThreads() throws DebugException {
140         try {
141             List JavaDoc threads = fGroup.threads();
142             return threads.size() > 0;
143         } catch (RuntimeException JavaDoc e) {
144             targetRequestFailed(JDIDebugModelMessages.JDIThreadGroup_5, e);
145         }
146         return false;
147     }
148
149     /**
150      * @see org.eclipse.debug.core.model.ITerminate#canTerminate()
151      */

152     public boolean canTerminate() {
153         //the group can terminate if the target can terminate
154
return getDebugTarget().canTerminate();
155     }
156
157     /**
158      * @see org.eclipse.debug.core.model.ITerminate#isTerminated()
159      */

160     public boolean isTerminated() {
161         return getDebugTarget().isTerminated();
162     }
163
164     /**
165      * @see org.eclipse.debug.core.model.ITerminate#terminate()
166      */

167     public void terminate() throws DebugException {
168         getDebugTarget().terminate();
169     }
170
171 }
172
Popular Tags