KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdi > internal > ThreadGroupReferenceImpl


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.jdi.internal;
12
13
14 import java.io.DataInputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.eclipse.jdi.internal.jdwp.JdwpCommandPacket;
21 import org.eclipse.jdi.internal.jdwp.JdwpID;
22 import org.eclipse.jdi.internal.jdwp.JdwpReplyPacket;
23 import org.eclipse.jdi.internal.jdwp.JdwpThreadGroupID;
24
25 import com.sun.jdi.ThreadGroupReference;
26
27 /**
28  * this class implements the corresponding interfaces
29  * declared by the JDI specification. See the com.sun.jdi package
30  * for more information.
31  *
32  */

33 public class ThreadGroupReferenceImpl extends ObjectReferenceImpl implements ThreadGroupReference {
34     /** JDWP Tag. */
35     public static final byte tag = JdwpID.THREAD_GROUP_TAG;
36     /**
37      * The cached name of this thread group. This value is safe to cache because
38      * there is no API for changing the name of a ThreadGroup.
39      */

40     private String JavaDoc fName;
41     /**
42      * The cached parent of this thread group. Once set, this value cannot be changed
43      */

44     private ThreadGroupReference fParent= fgUnsetParent;
45     private static ThreadGroupReferenceImpl fgUnsetParent= new ThreadGroupReferenceImpl(null, null);
46     
47     /**
48      * Creates new ThreadGroupReferenceImpl.
49      */

50     public ThreadGroupReferenceImpl(VirtualMachineImpl vmImpl, JdwpThreadGroupID threadGroupID) {
51         super("ThreadGroupReference", vmImpl, threadGroupID); //$NON-NLS-1$
52
}
53
54     /**
55      * @returns Value tag.
56      */

57     public byte getTag() {
58         return tag;
59     }
60     
61     /**
62      * @return Returns the name of this thread group.
63      */

64     public String JavaDoc name() {
65         if (fName != null) {
66             return fName;
67         }
68         initJdwpRequest();
69         try {
70             JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.TGR_NAME, this);
71             defaultReplyErrorHandler(replyPacket.errorCode());
72             DataInputStream JavaDoc replyData = replyPacket.dataInStream();
73             fName= readString("name", replyData); //$NON-NLS-1$
74
return fName;
75         } catch (IOException JavaDoc e) {
76             defaultIOExceptionHandler(e);
77             return null;
78         } finally {
79             handledJdwpRequest();
80         }
81     }
82     
83     /**
84      * @return Returns the parent of this thread group., or null if there isn't.
85      */

86     public ThreadGroupReference parent() {
87         if (fParent != fgUnsetParent) {
88             return fParent;
89         }
90         initJdwpRequest();
91         try {
92             JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.TGR_PARENT, this);
93             defaultReplyErrorHandler(replyPacket.errorCode());
94             DataInputStream JavaDoc replyData = replyPacket.dataInStream();
95             fParent= ThreadGroupReferenceImpl.read(this, replyData);
96             return fParent;
97         } catch (IOException JavaDoc e) {
98             defaultIOExceptionHandler(e);
99             return null;
100         } finally {
101             handledJdwpRequest();
102         }
103     }
104         
105     /**
106      * Resumes all threads in this thread group (including subgroups).
107      */

108     public void resume() {
109         Iterator JavaDoc iter = allThreads().iterator();
110         while (iter.hasNext()) {
111             ThreadReferenceImpl thr = (ThreadReferenceImpl)iter.next();
112             thr.resume();
113         }
114     }
115     
116     /**
117      * Suspends all threads in this thread group (including subgroups).
118      */

119     public void suspend() {
120         Iterator JavaDoc iter = allThreads().iterator();
121         while (iter.hasNext()) {
122             ThreadReferenceImpl thr = (ThreadReferenceImpl)iter.next();
123             thr.suspend();
124         }
125     }
126     
127     /**
128      * Inner class used to return children info.
129      */

130     private class ChildrenInfo {
131         List JavaDoc childThreads;
132         List JavaDoc childThreadGroups;
133     }
134         
135     /**
136      * @return Returns a List containing each ThreadReference in this thread group.
137      */

138     public ChildrenInfo childrenInfo() {
139         // Note that this information should not be cached.
140
initJdwpRequest();
141         try {
142             JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.TGR_CHILDREN, this);
143             defaultReplyErrorHandler(replyPacket.errorCode());
144             DataInputStream JavaDoc replyData = replyPacket.dataInStream();
145             ChildrenInfo result = new ChildrenInfo();
146             int nrThreads = readInt("nr threads", replyData); //$NON-NLS-1$
147
result.childThreads = new ArrayList JavaDoc(nrThreads);
148             for (int i = 0; i < nrThreads; i++)
149                 result.childThreads.add(ThreadReferenceImpl.read(this, replyData));
150             int nrThreadGroups = readInt("nr thread groups", replyData); //$NON-NLS-1$
151
result.childThreadGroups = new ArrayList JavaDoc(nrThreadGroups);
152             for (int i = 0; i < nrThreadGroups; i++)
153                 result.childThreadGroups.add(ThreadGroupReferenceImpl.read(this, replyData));
154             return result;
155         } catch (IOException JavaDoc e) {
156             defaultIOExceptionHandler(e);
157             return null;
158         } finally {
159             handledJdwpRequest();
160         }
161     }
162
163     /**
164      * @return Returns a List containing each ThreadGroupReference in this thread group.
165      */

166     public List JavaDoc threadGroups() {
167         return childrenInfo().childThreadGroups;
168     }
169     
170     /**
171      * @return Returns a List containing each ThreadReference in this thread group.
172      */

173     public List JavaDoc threads() {
174         return childrenInfo().childThreads;
175     }
176         
177     /**
178      * @return Returns a List containing each ThreadGroupReference in this thread group and all of
179      * its subgroups.
180      */

181     private List JavaDoc allThreads() {
182         ChildrenInfo info = childrenInfo();
183         List JavaDoc result = info.childThreads;
184         Iterator JavaDoc iter = info.childThreadGroups.iterator();
185         while (iter.hasNext()) {
186             ThreadGroupReferenceImpl tg = (ThreadGroupReferenceImpl)iter.next();
187             result.addAll(tg.allThreads());
188         }
189         return result;
190     }
191     
192     /**
193      * @return Returns description of Mirror object.
194      */

195     public String JavaDoc toString() {
196         try {
197             return name();
198         } catch (Exception JavaDoc e) {
199             return fDescription;
200         }
201     }
202
203     /**
204      * @return Reads JDWP representation and returns new instance.
205      */

206     public static ThreadGroupReferenceImpl read(MirrorImpl target, DataInputStream JavaDoc in) throws IOException JavaDoc {
207         VirtualMachineImpl vmImpl = target.virtualMachineImpl();
208         JdwpThreadGroupID ID = new JdwpThreadGroupID(vmImpl);
209         ID.read(in);
210         if (target.fVerboseWriter != null)
211             target.fVerboseWriter.println("threadGroupReference", ID.value()); //$NON-NLS-1$
212

213         if (ID.isNull())
214             return null;
215             
216         ThreadGroupReferenceImpl mirror = (ThreadGroupReferenceImpl)vmImpl.getCachedMirror(ID);
217         if (mirror == null) {
218             mirror = new ThreadGroupReferenceImpl(vmImpl, ID);
219             vmImpl.addCachedMirror(mirror);
220         }
221         return mirror;
222     }
223 }
224
Popular Tags