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.global;
042:
043: import java.io.*;
044: import java.text.MessageFormat;
045: import java.util.ResourceBundle;
046:
047: /**
048: * Reading and saving calibration data file.
049: *
050: * @author Misha Dmitriev
051: */
052: public class CalibrationDataFileIO {
053: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
054:
055: // -----
056: // I18N String constants
057: private static final ResourceBundle messages = ResourceBundle
058: .getBundle("org.netbeans.lib.profiler.global.Bundle"); // NOI18N
059: private static final String CALIBRATION_FILE_NOT_EXIST_MSG = messages
060: .getString("CalibrationDataFileIO_CalibrationFileNotExistMsg"); // NOI18N
061: private static final String CALIBRATION_FILE_NOT_READABLE_MSG = messages
062: .getString("CalibrationDataFileIO_CalibrationFileNotReadableMsg"); // NOI18N
063: private static final String CALIBRATION_DATA_CORRUPTED_PREFIX = messages
064: .getString("CalibrationDataFileIO_CalibrationDataCorruptedPrefix"); // NOI18N
065: private static final String SHORTER_THAN_EXPECTED_STRING = messages
066: .getString("CalibrationDataFileIO_ShorterThanExpectedString"); // NOI18N
067: private static final String ORIGINAL_MESSAGE_STRING = messages
068: .getString("CalibrationDataFileIO_OriginalMessageString"); // NOI18N
069: private static final String RERUN_CALIBRATION_MSG = messages
070: .getString("CalibrationDataFileIO_ReRunCalibrationMsg"); // NOI18N
071: private static final String ERROR_WRITING_CALIBRATION_FILE_PREFIX = messages
072: .getString("CalibrationDataFileIO_ErrorWritingCalibrationFilePrefix"); // NOI18N
073: private static final String REEXECUTE_CALIBRATION_MSG = messages
074: .getString("CalibrationDataFileIO_ReExecuteCalibrationMsg"); // NOI18N
075: // -----
076: private static String errorMessage;
077:
078: //~ Methods ------------------------------------------------------------------------------------------------------------------
079:
080: public static String getErrorMessage() {
081: String res = errorMessage;
082: errorMessage = null;
083:
084: return res;
085: }
086:
087: /**
088: * Reads the saved calibration data file.
089: * Returns -1 in case of a fatal error (cannot read the calibration file), -2 if file exists but data is corrupted,
090: * 1 if file does not exist, and 0 if file has been read successfully.
091: */
092: public static int readSavedCalibrationData(
093: ProfilingSessionStatus status) {
094: String fn = null;
095:
096: try {
097: fn = getCalibrationDataFileName(status.targetJDKVersionString);
098: } catch (IOException e) {
099: errorMessage = e.getMessage();
100:
101: return -1;
102: }
103:
104: File savedDataFile = new File(fn);
105:
106: if (!savedDataFile.exists()) {
107: errorMessage = CALIBRATION_FILE_NOT_EXIST_MSG;
108:
109: return 1;
110: }
111:
112: if (!savedDataFile.canRead()) {
113: errorMessage = MessageFormat.format(
114: CALIBRATION_FILE_NOT_READABLE_MSG,
115: new Object[] { savedDataFile.toString() });
116:
117: return -1;
118: }
119:
120: try {
121: FileInputStream fiStream = new FileInputStream(
122: savedDataFile);
123: ObjectInputStream oiStream = new ObjectInputStream(fiStream);
124:
125: status.methodEntryExitCallTime = (double[]) oiStream
126: .readObject();
127: status.methodEntryExitInnerTime = (double[]) oiStream
128: .readObject();
129: status.methodEntryExitOuterTime = (double[]) oiStream
130: .readObject();
131: status.timerCountsInSecond = (long[]) oiStream.readObject();
132:
133: fiStream.close();
134: } catch (Exception e) {
135: String errorMessage = e.getMessage();
136: String prefix = CALIBRATION_DATA_CORRUPTED_PREFIX;
137:
138: if (errorMessage == null) {
139: if (e instanceof java.io.EOFException) {
140: errorMessage = prefix + " "
141: + SHORTER_THAN_EXPECTED_STRING; // NOI18N
142: }
143: } else {
144: errorMessage = prefix + "\n" + ORIGINAL_MESSAGE_STRING
145: + " " + errorMessage; // NOI18N
146: }
147:
148: errorMessage += ("\n" + RERUN_CALIBRATION_MSG + "\n"); // NOI18N
149:
150: return -2;
151: }
152:
153: return 0;
154: }
155:
156: public static boolean saveCalibrationData(
157: ProfilingSessionStatus status) {
158: try {
159: FileOutputStream foStream = new FileOutputStream(
160: getCalibrationDataFileName(status.targetJDKVersionString));
161: ObjectOutputStream ooStream = new ObjectOutputStream(
162: foStream);
163:
164: ooStream.writeObject(status.methodEntryExitCallTime);
165: ooStream.writeObject(status.methodEntryExitInnerTime);
166: ooStream.writeObject(status.methodEntryExitOuterTime);
167: ooStream.writeObject(status.timerCountsInSecond);
168:
169: foStream.close();
170: } catch (IOException e) {
171: String errorMessage = e.getMessage();
172: String prefix = ERROR_WRITING_CALIBRATION_FILE_PREFIX;
173: errorMessage = prefix + "\n" + ORIGINAL_MESSAGE_STRING
174: + "\n" + errorMessage; // NOI18N
175: // status.remoteProfiling below means that we actually perform off-line calibration on the remote target machine.
176: // In that case, the message that follows, which is meaningful in case of local machine calibration, doesn't make sense.
177:
178: if (!status.remoteProfiling) {
179: errorMessage += ("\n" + REEXECUTE_CALIBRATION_MSG + "\n"); // NOI18N
180: }
181:
182: return false;
183: }
184:
185: return true;
186: }
187:
188: public static boolean validateCalibrationInput(
189: String javaVersionString, String javaExecutable) {
190: if ((javaVersionString != null) && (javaExecutable != null)) {
191: if (CommonConstants.JDK_15_STRING.equals(javaVersionString)
192: || CommonConstants.JDK_16_STRING
193: .equals(javaVersionString)
194: || CommonConstants.JDK_17_STRING
195: .equals(javaVersionString)) {
196: if (new File(javaExecutable).exists()) {
197: return true;
198: }
199: }
200: }
201:
202: return false;
203: }
204:
205: private static String getCalibrationDataFileName(
206: String targetJDKVerString) throws IOException {
207: String fileName = "machinedata" + "." + targetJDKVerString; // NOI18N
208:
209: return Platform.getProfilerUserDir() + File.separator
210: + fileName;
211: }
212: }
|