001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.wireprotocol;
042:
043: import org.netbeans.lib.profiler.client.RuntimeProfilingPoint;
044: import java.io.IOException;
045: import java.io.ObjectInputStream;
046: import java.io.ObjectOutputStream;
047: import java.util.*;
048:
049: /**
050: * Request from the client to the back end to initiate TA instrumentation of the given type.
051: *
052: * @author Tomas Hurka
053: * @author Misha Dmitriev
054: * @author Adrian Mos
055: * @author Ian Formanek
056: */
057: public class InitiateInstrumentationCommand extends Command {
058: //~ Inner Classes ------------------------------------------------------------------------------------------------------------
059:
060: private static final class ByIdComparator implements Comparator {
061: //~ Methods --------------------------------------------------------------------------------------------------------------
062:
063: public int compare(Object o1, Object o2) {
064: return ((RuntimeProfilingPoint) o1).getId()
065: - ((RuntimeProfilingPoint) o2).getId();
066: }
067: }
068:
069: //~ Instance fields ----------------------------------------------------------------------------------------------------------
070:
071: private String[] classNames;
072: private String[] profilingPointHandlers;
073: private int[] profilingPointIDs;
074: private String[] profilingPointInfos;
075: private boolean instrSpawnedThreads;
076: private boolean startProfilingPointsActive;
077: private int instrType;
078:
079: //~ Constructors -------------------------------------------------------------------------------------------------------------
080:
081: /** Legacy support for single root instrumentation */
082: public InitiateInstrumentationCommand(int instrType,
083: String className, boolean instrSpawnedThreads,
084: boolean startProfilingPointsActive) {
085: super (INITIATE_INSTRUMENTATION);
086: this .instrType = instrType;
087:
088: if (className == null) {
089: className = " "; // NOI18N
090: // We can have root class equal to null if e.g. we instrument object allocations in an app to which we attach at run time
091: }
092:
093: this .classNames = new String[1];
094: this .classNames[0] = className;
095: this .instrSpawnedThreads = instrSpawnedThreads;
096: this .startProfilingPointsActive = startProfilingPointsActive;
097: }
098:
099: public InitiateInstrumentationCommand(int instrType,
100: String[] classNames, boolean instrSpawnedThreads,
101: boolean startProfilingPointsActive) {
102: super (INITIATE_INSTRUMENTATION);
103:
104: if ((classNames == null)) {
105: classNames = new String[] { " " }; // NOI18N
106: } else if (classNames[0] == null) {
107: classNames[0] = " "; // NOI18N
108: }
109:
110: this .instrType = instrType;
111: this .classNames = classNames;
112: this .instrSpawnedThreads = instrSpawnedThreads;
113: this .startProfilingPointsActive = startProfilingPointsActive;
114: }
115:
116: /** This is a special method only called to setup the connection in ProfilerClient.connectToServer() - see comments there */
117: public InitiateInstrumentationCommand(int instrType,
118: String className, boolean startProfilingPointsActive) {
119: super (INITIATE_INSTRUMENTATION);
120: this .instrType = instrType;
121: this .classNames = new String[1]; //multiple roots, legacy support for one root
122: this .classNames[0] = className;
123: this .instrSpawnedThreads = false;
124: this .startProfilingPointsActive = startProfilingPointsActive;
125: }
126:
127: // Custom serialzation support
128: InitiateInstrumentationCommand() {
129: super (INITIATE_INSTRUMENTATION);
130: }
131:
132: //~ Methods ------------------------------------------------------------------------------------------------------------------
133:
134: public boolean getInstrSpawnedThreads() {
135: return instrSpawnedThreads;
136: }
137:
138: public void setInstrType(int t) {
139: instrType = t;
140: }
141:
142: public int getInstrType() {
143: return instrType;
144: }
145:
146: public String[] getProfilingPointHandlers() {
147: return profilingPointHandlers;
148: }
149:
150: public int[] getProfilingPointIDs() {
151: return profilingPointIDs;
152: }
153:
154: public String[] getProfilingPointInfos() {
155: return profilingPointInfos;
156: }
157:
158: public void setProfilingPoints(RuntimeProfilingPoint[] points) {
159: profilingPointHandlers = new String[points.length];
160: profilingPointInfos = new String[points.length];
161: profilingPointIDs = new int[points.length];
162: Arrays.sort(points, new ByIdComparator()); // ProfilerRuntime uses Arrays.binarySearch
163:
164: for (int i = 0; i < points.length; i++) {
165: RuntimeProfilingPoint point = points[i];
166: profilingPointIDs[i] = point.getId();
167: profilingPointHandlers[i] = point.getServerHandlerClass();
168: profilingPointInfos[i] = point.getServerInfo();
169: }
170: }
171:
172: public String getRootClassName() {
173: return classNames[0];
174: } // Legacy support for one root
175:
176: public String[] getRootClassNames() {
177: return classNames;
178: }
179:
180: public boolean isStartProfilingPointsActive() {
181: return startProfilingPointsActive;
182: }
183:
184: // for debugging
185: public String toString() {
186: return super .toString() + ", instrType = " + instrType; // NOI18N
187: }
188:
189: void readObject(ObjectInputStream in) throws IOException {
190: instrType = in.readInt();
191:
192: int len = in.readInt();
193: classNames = new String[len];
194:
195: for (int i = 0; i < len; i++) {
196: classNames[i] = in.readUTF().intern(); // Interning is important, since checks are through '=='
197: }
198:
199: instrSpawnedThreads = in.readBoolean();
200: startProfilingPointsActive = in.readBoolean();
201:
202: try {
203: profilingPointIDs = (int[]) in.readObject();
204: profilingPointHandlers = (String[]) in.readObject();
205: profilingPointInfos = (String[]) in.readObject();
206: } catch (ClassNotFoundException e) {
207: IOException ioe = new IOException();
208: ioe.initCause(e);
209: throw ioe;
210: }
211: }
212:
213: void writeObject(ObjectOutputStream out) throws IOException {
214: out.writeInt(instrType);
215: out.writeInt(classNames.length);
216:
217: for (int i = 0; i < classNames.length; i++) {
218: out.writeUTF(classNames[i]);
219: }
220:
221: out.writeBoolean(instrSpawnedThreads);
222: out.writeBoolean(startProfilingPointsActive);
223: out.writeObject(profilingPointIDs);
224: out.writeObject(profilingPointHandlers);
225: out.writeObject(profilingPointInfos);
226: }
227: }
|