001: /*
002: * Copyright 1998-1999 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.jdi;
027:
028: import com.sun.jdi.*;
029: import java.util.*;
030:
031: public class ThreadGroupReferenceImpl extends ObjectReferenceImpl
032: implements ThreadGroupReference, VMListener {
033: // Cached components that cannot change
034: String name;
035: ThreadGroupReference parent;
036: boolean triedParent;
037:
038: // This is cached only while the VM is suspended
039: private static class Cache extends ObjectReferenceImpl.Cache {
040: JDWP.ThreadGroupReference.Children kids = null;
041: }
042:
043: protected ObjectReferenceImpl.Cache newCache() {
044: return new Cache();
045: }
046:
047: ThreadGroupReferenceImpl(VirtualMachine aVm, long aRef) {
048: super (aVm, aRef);
049: vm.state().addListener(this );
050: }
051:
052: protected String description() {
053: return "ThreadGroupReference " + uniqueID();
054: }
055:
056: public String name() {
057: if (name == null) {
058: // Does not need synchronization, since worst-case
059: // static info is fetched twice (Thread group name
060: // cannot change)
061: try {
062: name = JDWP.ThreadGroupReference.Name.process(vm, this ).groupName;
063: } catch (JDWPException exc) {
064: throw exc.toJDIException();
065: }
066: }
067: return name;
068: }
069:
070: public ThreadGroupReference parent() {
071: if (!triedParent) {
072: // Does not need synchronization, since worst-case
073: // static info is fetched twice (Thread group parent cannot
074: // change)
075: try {
076: parent = JDWP.ThreadGroupReference.Parent.process(vm,
077: this ).parentGroup;
078: triedParent = true;
079: } catch (JDWPException exc) {
080: throw exc.toJDIException();
081: }
082: }
083: return parent;
084: }
085:
086: public void suspend() {
087: List threads = threads();
088: Iterator iter = threads.iterator();
089: while (iter.hasNext()) {
090: ((ThreadReference) iter.next()).suspend();
091: }
092:
093: List groups = threadGroups();
094: iter = groups.iterator();
095: while (iter.hasNext()) {
096: ((ThreadGroupReference) iter.next()).suspend();
097: }
098: }
099:
100: public void resume() {
101: List threads = threads();
102: Iterator iter = threads.iterator();
103: while (iter.hasNext()) {
104: ((ThreadReference) iter.next()).resume();
105: }
106:
107: List groups = threadGroups();
108: iter = groups.iterator();
109: while (iter.hasNext()) {
110: ((ThreadGroupReference) iter.next()).resume();
111: }
112: }
113:
114: private JDWP.ThreadGroupReference.Children kids() {
115: JDWP.ThreadGroupReference.Children kids = null;
116: try {
117: Cache local = (Cache) getCache();
118:
119: if (local != null) {
120: kids = local.kids;
121: }
122: if (kids == null) {
123: kids = JDWP.ThreadGroupReference.Children.process(vm,
124: this );
125: if (local != null) {
126: local.kids = kids;
127: if ((vm.traceFlags & vm.TRACE_OBJREFS) != 0) {
128: vm.printTrace(description()
129: + " temporarily caching children ");
130: }
131: }
132: }
133: } catch (JDWPException exc) {
134: throw exc.toJDIException();
135: }
136: return kids;
137: }
138:
139: public List<ThreadReference> threads() {
140: return Arrays.asList((ThreadReference[]) kids().childThreads);
141: }
142:
143: public List<ThreadGroupReference> threadGroups() {
144: return Arrays
145: .asList((ThreadGroupReference[]) kids().childGroups);
146: }
147:
148: public String toString() {
149: return "instance of " + referenceType().name() + "(name='"
150: + name() + "', " + "id=" + uniqueID() + ")";
151: }
152:
153: byte typeValueKey() {
154: return JDWP.Tag.THREAD_GROUP;
155: }
156: }
|