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.common;
042:
043: import org.netbeans.lib.profiler.ProfilerEngineSettings;
044: import org.netbeans.lib.profiler.global.Platform;
045: import java.util.Map;
046: import java.util.ResourceBundle;
047:
048: /**
049: * A Class holding transient data for a single profiling session. Typical usage is creating this class based on context
050: * and then call applySettings () on ProfilingEngineSettings. Not used for Attach.
051: *
052: * The class travels through Ant, and thus can store/load itself to/from Properties.
053: *
054: * @author Tomas Hurka
055: * @author Ian Formanek
056: */
057: public final class SessionSettings {
058: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
059:
060: // -----
061: // I18N String constants
062: private static final ResourceBundle bundle = ResourceBundle
063: .getBundle("org.netbeans.lib.profiler.common.Bundle"); // NOI18N
064: private static final String INCORRECT_PORT_MSG = bundle
065: .getString("SessionSettings_IncorrectPortMsg"); // NOI18N
066: private static final String INCORRECT_ARCH_MSG = bundle
067: .getString("SessionSettings_IncorrectArchMsg"); // NOI18N
068: // -----
069: public static final String PROP_CLASS_NAME = "profiler.session.class.name"; //NOI18N
070: public static final String PROP_CLASS_PATH = "profiler.session.class.path"; //NOI18N
071: public static final String PROP_ARGS = "profiler.session.args"; //NOI18N
072: public static final String PROP_JVM_ARGS = "profiler.session.jvm.args"; //NOI18N
073: public static final String PROP_WORKING_DIR = "profiler.session.working.dir"; //NOI18N
074: public static final String PROP_JAVA_EXECUTABLE = "profiler.session.java.executable"; //NOI18N
075: public static final String PROP_JAVA_VERSION = "profiler.session.java.version"; //NOI18N
076: public static final String PROP_ARCHITECTURE = "profiler.session.java.architecture"; //NOI18N
077: public static final String PROP_PORT_NO = "profiler.session.port.no"; //NOI18N
078:
079: //~ Instance fields ----------------------------------------------------------------------------------------------------------
080:
081: private String javaExecutable = ""; //NOI18N
082: private String javaVersionString = ""; //NOI18N
083: private String jvmArgs = ""; //NOI18N // Only used for Profile, not for Attach
084: private String mainArgs = ""; //NOI18N // Only used for Profile, not for Attach
085: private String mainClass = ""; //NOI18N
086: private String mainClassPath = ""; //NOI18N
087: private String workingDir = System.getProperty("user.dir"); //NOI18N // Only used for Profile, not for Attach
088: private int architecture = Platform.ARCH_32;
089: private int portNo = 5140;
090:
091: //~ Constructors -------------------------------------------------------------------------------------------------------------
092:
093: public SessionSettings() {
094: }
095:
096: //~ Methods ------------------------------------------------------------------------------------------------------------------
097:
098: public void setJVMArgs(final String value) {
099: if (value == null) {
100: throw new IllegalArgumentException();
101: }
102:
103: jvmArgs = value;
104: }
105:
106: public String getJVMArgs() {
107: return jvmArgs;
108: }
109:
110: public void setJavaExecutable(String value) {
111: if (value == null) {
112: throw new IllegalArgumentException();
113: }
114:
115: javaExecutable = value;
116: }
117:
118: public String getJavaExecutable() {
119: return javaExecutable;
120: }
121:
122: public void setJavaVersionString(String value) {
123: if (value == null) {
124: throw new IllegalArgumentException();
125: }
126:
127: javaVersionString = value;
128: }
129:
130: public String getJavaVersionString() {
131: return javaVersionString;
132: }
133:
134: public void setMainArgs(final String value) {
135: if (value == null) {
136: throw new IllegalArgumentException();
137: }
138:
139: mainArgs = value;
140: }
141:
142: public String getMainArgs() {
143: return mainArgs;
144: }
145:
146: public void setMainClass(final String value) {
147: if (value == null) {
148: throw new IllegalArgumentException();
149: }
150:
151: mainClass = value;
152: }
153:
154: // -- Session-related settings ---------------------------------------------------------------------------------------
155: public String getMainClass() {
156: return mainClass;
157: }
158:
159: public void setMainClassPath(final String value) {
160: if (value == null) {
161: throw new IllegalArgumentException();
162: }
163:
164: mainClassPath = value;
165: }
166:
167: public String getMainClassPath() {
168: return mainClassPath;
169: }
170:
171: public void setPortNo(int value) {
172: portNo = value;
173: }
174:
175: public int getPortNo() {
176: return portNo;
177: }
178:
179: public void setSystemArchitecture(int value) {
180: architecture = value;
181: }
182:
183: public int getSystemArchitecture() {
184: return architecture;
185: }
186:
187: public void setWorkingDir(final String value) {
188: if (value == null) {
189: throw new IllegalArgumentException();
190: }
191:
192: workingDir = value;
193: }
194:
195: public String getWorkingDir() {
196: return workingDir;
197: }
198:
199: public void applySettings(final ProfilerEngineSettings settings) {
200: settings.setMainClass(mainClass);
201: settings.setMainClassPath(mainClassPath);
202: settings.setMainArgs(mainArgs);
203: settings.setJVMArgs(jvmArgs);
204: settings.setWorkingDir(workingDir);
205: settings.setTargetJVMExeFile(javaExecutable);
206: settings.setTargetJDKVersionString(javaVersionString);
207: settings.setSystemArchitecture(architecture);
208: settings.setPortNo(portNo);
209: }
210:
211: public String debug() {
212: final StringBuffer sb = new StringBuffer();
213: sb.append("mainClass: " + mainClass); //NOI18N
214: sb.append('\n'); //NOI18N
215: sb.append("mainClassPath: " + mainClassPath); //NOI18N
216: sb.append('\n'); //NOI18N
217: sb.append("mainArgs: " + mainArgs); //NOI18N
218: sb.append('\n'); //NOI18N
219: sb.append("jvmArgs =" + jvmArgs); //NOI18N
220: sb.append('\n'); //NOI18N
221: sb.append("workingDir =" + workingDir); //NOI18N
222: sb.append('\n'); //NOI18N
223: sb.append("javaExecutable =" + javaExecutable); //NOI18N
224: sb.append('\n'); //NOI18N
225: sb.append("javaVersionString =" + javaVersionString); //NOI18N
226: sb.append('\n'); //NOI18N
227: sb.append("architecture =" + architecture);
228: sb.append('\n');
229: sb.append("portNo =" + portNo); //NOI18N
230: sb.append('\n');
231:
232: return sb.toString();
233: }
234:
235: public void load(Map props) {
236: setMainClass(getProperty(props, PROP_CLASS_NAME, "")); //NOI18N
237: setMainClassPath(getProperty(props, PROP_CLASS_PATH, "")); //NOI18N
238: setMainArgs(getProperty(props, PROP_ARGS, "")); //NOI18N
239: setJVMArgs(getProperty(props, PROP_JVM_ARGS, "")); //NOI18N
240: setWorkingDir(getProperty(props, PROP_WORKING_DIR, System
241: .getProperty("user.home"))); //NOI18N
242: setJavaExecutable(getProperty(props, PROP_JAVA_EXECUTABLE, "")); //NOI18N
243: setJavaVersionString(getProperty(props, PROP_JAVA_VERSION, "")); //NOI18N
244:
245: String arch = getProperty(props, PROP_ARCHITECTURE, String
246: .valueOf(Platform.ARCH_32)); //NOI18N
247:
248: try {
249: setSystemArchitecture(Integer.parseInt(arch));
250: } catch (NumberFormatException e) {
251: Profiler.getDefault().displayWarning(INCORRECT_ARCH_MSG);
252: architecture = 32;
253: }
254:
255: String port = getProperty(props, PROP_PORT_NO, "5140"); //NOI18N
256:
257: try {
258: setPortNo(Integer.parseInt(port));
259: } catch (NumberFormatException e) {
260: Profiler.getDefault().displayWarning(INCORRECT_PORT_MSG);
261: portNo = 5140;
262: }
263: }
264:
265: public void store(Map props) {
266: props.put(PROP_CLASS_NAME, mainClass);
267: props.put(PROP_CLASS_PATH, mainClassPath);
268: props.put(PROP_ARGS, mainArgs);
269: props.put(PROP_JVM_ARGS, jvmArgs);
270: props.put(PROP_WORKING_DIR, workingDir);
271: props.put(PROP_JAVA_EXECUTABLE, javaExecutable);
272: props.put(PROP_JAVA_VERSION, javaVersionString);
273: props.put(PROP_ARCHITECTURE, Integer.toString(architecture));
274: props.put(PROP_PORT_NO, Integer.toString(portNo));
275: }
276:
277: private static String getProperty(Map props, String key,
278: String defaultValue) {
279: String ret = (String) props.get(key);
280:
281: if (ret == null) {
282: ret = defaultValue;
283: }
284:
285: return ret;
286: }
287: }
|