1 11 package org.eclipse.jdi.internal; 12 13 14 import java.io.DataInputStream ; 15 import java.io.IOException ; 16 import java.util.ArrayList ; 17 import java.util.Iterator ; 18 import java.util.List ; 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 33 public class ThreadGroupReferenceImpl extends ObjectReferenceImpl implements ThreadGroupReference { 34 35 public static final byte tag = JdwpID.THREAD_GROUP_TAG; 36 40 private String fName; 41 44 private ThreadGroupReference fParent= fgUnsetParent; 45 private static ThreadGroupReferenceImpl fgUnsetParent= new ThreadGroupReferenceImpl(null, null); 46 47 50 public ThreadGroupReferenceImpl(VirtualMachineImpl vmImpl, JdwpThreadGroupID threadGroupID) { 51 super("ThreadGroupReference", vmImpl, threadGroupID); } 53 54 57 public byte getTag() { 58 return tag; 59 } 60 61 64 public String 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 replyData = replyPacket.dataInStream(); 73 fName= readString("name", replyData); return fName; 75 } catch (IOException e) { 76 defaultIOExceptionHandler(e); 77 return null; 78 } finally { 79 handledJdwpRequest(); 80 } 81 } 82 83 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 replyData = replyPacket.dataInStream(); 95 fParent= ThreadGroupReferenceImpl.read(this, replyData); 96 return fParent; 97 } catch (IOException e) { 98 defaultIOExceptionHandler(e); 99 return null; 100 } finally { 101 handledJdwpRequest(); 102 } 103 } 104 105 108 public void resume() { 109 Iterator iter = allThreads().iterator(); 110 while (iter.hasNext()) { 111 ThreadReferenceImpl thr = (ThreadReferenceImpl)iter.next(); 112 thr.resume(); 113 } 114 } 115 116 119 public void suspend() { 120 Iterator iter = allThreads().iterator(); 121 while (iter.hasNext()) { 122 ThreadReferenceImpl thr = (ThreadReferenceImpl)iter.next(); 123 thr.suspend(); 124 } 125 } 126 127 130 private class ChildrenInfo { 131 List childThreads; 132 List childThreadGroups; 133 } 134 135 138 public ChildrenInfo childrenInfo() { 139 initJdwpRequest(); 141 try { 142 JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.TGR_CHILDREN, this); 143 defaultReplyErrorHandler(replyPacket.errorCode()); 144 DataInputStream replyData = replyPacket.dataInStream(); 145 ChildrenInfo result = new ChildrenInfo(); 146 int nrThreads = readInt("nr threads", replyData); result.childThreads = new ArrayList (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); result.childThreadGroups = new ArrayList (nrThreadGroups); 152 for (int i = 0; i < nrThreadGroups; i++) 153 result.childThreadGroups.add(ThreadGroupReferenceImpl.read(this, replyData)); 154 return result; 155 } catch (IOException e) { 156 defaultIOExceptionHandler(e); 157 return null; 158 } finally { 159 handledJdwpRequest(); 160 } 161 } 162 163 166 public List threadGroups() { 167 return childrenInfo().childThreadGroups; 168 } 169 170 173 public List threads() { 174 return childrenInfo().childThreads; 175 } 176 177 181 private List allThreads() { 182 ChildrenInfo info = childrenInfo(); 183 List result = info.childThreads; 184 Iterator 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 195 public String toString() { 196 try { 197 return name(); 198 } catch (Exception e) { 199 return fDescription; 200 } 201 } 202 203 206 public static ThreadGroupReferenceImpl read(MirrorImpl target, DataInputStream in) throws IOException { 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()); 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 |