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: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.api.debugger.jpda;
043:
044: import com.sun.jdi.Bootstrap;
045: import com.sun.jdi.VirtualMachine;
046: import com.sun.jdi.connect.IllegalConnectorArgumentsException;
047: import com.sun.jdi.connect.LaunchingConnector;
048: import com.sun.jdi.connect.VMStartException;
049: import com.sun.jdi.connect.Connector.Argument;
050:
051: import java.io.IOException;
052: import java.util.Iterator;
053: import java.util.Map;
054:
055: /**
056: * Launches a new JVM in debug mode and returns VirtualMachine for it.
057: *
058: * <br><br>
059: * <b>How to use it:</b>
060: * <pre style="background-color: rgb(255, 255, 153);">
061: * DebuggerInfo di = DebuggerInfo.create (
062: * "My First Launching Debugger Info",
063: * new Object [] {
064: * LaunchingDICookie.create (
065: * "examples.texteditor.Ted",
066: * new String [] {},
067: * "c:\\nb\\settings\\sampledir",
068: * true
069: * )
070: * }
071: * );
072: * DebuggerManager.getDebuggerManager ().startDebugging (di);</pre>
073: *
074: * @author Jan Jancura
075: */
076: public final class LaunchingDICookie extends AbstractDICookie {
077:
078: /**
079: * Public ID used for registration in Meta-inf/debugger.
080: */
081: public static final String ID = "netbeans-jpda-LaunchingDICookie";
082:
083: private LaunchingConnector launchingConnector;
084: private Map<String, ? extends Argument> args;
085:
086: private String mainClassName;
087: private boolean suspend;
088:
089: private LaunchingDICookie(LaunchingConnector launchingConnector,
090: Map<String, ? extends Argument> args, String mainClassName,
091: boolean suspend) {
092: this .launchingConnector = launchingConnector;
093: this .args = args;
094: this .mainClassName = mainClassName;
095: this .suspend = suspend;
096: }
097:
098: /**
099: * Creates a new instance of LaunchingDICookie for given parameters.
100: *
101: * @param mainClassName a name or main class
102: * @param commandLine command line of debugged JVM
103: * @param address a address to listen on
104: * @param suspend if true session will be suspended
105: * @return a new instance of LaunchingDICookie for given parameters
106: */
107: public static LaunchingDICookie create(String mainClassName,
108: String commandLine, String address, boolean suspend) {
109: return new LaunchingDICookie(findLaunchingConnector(), getArgs(
110: commandLine, address), mainClassName, suspend);
111: }
112:
113: /**
114: * Creates a new instance of LaunchingDICookie for given parameters.
115: *
116: * @param mainClassName a name or main class
117: * @param args command line arguments
118: * @param classPath a classPath
119: * @param suspend if true session will be suspended
120: * @return a new instance of LaunchingDICookie for given parameters
121: */
122: public static LaunchingDICookie create(String mainClassName,
123: String[] args, String classPath, boolean suspend) {
124: String argss = "";
125: int i, k = args.length;
126: for (i = 0; i < k; i++) {
127: argss += " \"" + args[i] + "\"";
128: }
129: // TODO: This code is likely wrong, we need to run debuggee on JDK that
130: // is set on the project.
131: // XXX: This method is likely not called from anywhere.
132: // But it's an impl. of API method JPDADebugger.launch().
133: String commandLine = System.getProperty("java.home")
134: + "\\bin\\java -Xdebug -Xnoagent -Xrunjdwp:transport="
135: + getTransportName() + ",address=name,suspend="
136: + (suspend ? "y" : "n") + " -classpath \"" + classPath
137: + "\" " + mainClassName + argss;
138: String address = "name";
139: return new LaunchingDICookie(findLaunchingConnector(), getArgs(
140: commandLine, address), mainClassName, suspend);
141: }
142:
143: /**
144: * Returns type of transport to be used.
145: *
146: * @return type of transport to be used
147: */
148: public static String getTransportName() {
149: return findLaunchingConnector().transport().name();
150: }
151:
152: // main methods ............................................................
153:
154: /**
155: * Returns main class name.
156: *
157: * @return main class name
158: */
159: public String getClassName() {
160: return mainClassName;
161: }
162:
163: /**
164: * Returns suspended state.
165: *
166: * @return suspended state
167: */
168: public boolean getSuspend() {
169: return suspend;
170: }
171:
172: /**
173: * Returns command line to be used.
174: *
175: * @return command line to be used
176: */
177: public String getCommandLine() {
178: Argument a = (Argument) args.get("command");
179: if (a == null)
180: return null;
181: return a.value();
182: }
183:
184: /**
185: * Creates a new instance of VirtualMachine for this DebuggerInfo Cookie.
186: *
187: * @return a new instance of VirtualMachine for this DebuggerInfo Cookie
188: */
189: public VirtualMachine getVirtualMachine() throws IOException,
190: IllegalConnectorArgumentsException, VMStartException {
191: return launchingConnector.launch(args);
192: }
193:
194: // private helper methods ..................................................
195:
196: private static Map<String, ? extends Argument> getArgs(
197: String commandLine, String address) {
198: Map<String, ? extends Argument> args = findLaunchingConnector()
199: .defaultArguments();
200: args.get("command").setValue(commandLine);
201: args.get("address").setValue(address);
202: return args;
203: }
204:
205: private static LaunchingConnector findLaunchingConnector() {
206: Iterator<LaunchingConnector> iter = Bootstrap
207: .virtualMachineManager().launchingConnectors()
208: .iterator();
209: while (iter.hasNext()) {
210: LaunchingConnector lc = iter.next();
211: if (lc.name().indexOf("RawCommandLineLaunch") > -1)
212: return lc;
213: }
214: return null;
215: }
216: }
|