KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > debugger > jpda > LaunchingDICookie


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.debugger.jpda;
21
22 import com.sun.jdi.Bootstrap;
23 import com.sun.jdi.VirtualMachine;
24 import com.sun.jdi.connect.IllegalConnectorArgumentsException;
25 import com.sun.jdi.connect.LaunchingConnector;
26 import com.sun.jdi.connect.VMStartException;
27 import com.sun.jdi.connect.Connector.Argument;
28
29 import java.io.IOException JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32
33
34 /**
35  * Launches a new JVM in debug mode and returns VirtualMachine for it.
36  *
37  * <br><br>
38  * <b>How to use it:</b>
39  * <pre style="background-color: rgb(255, 255, 153);">
40  * DebuggerInfo di = DebuggerInfo.create (
41  * "My First Launching Debugger Info",
42  * new Object [] {
43  * LaunchingDICookie.create (
44  * "examples.texteditor.Ted",
45  * new String [] {},
46  * "c:\\nb\\settings\\sampledir",
47  * true
48  * )
49  * }
50  * );
51  * DebuggerManager.getDebuggerManager ().startDebugging (di);</pre>
52  *
53  * @author Jan Jancura
54  */

55 public final class LaunchingDICookie extends AbstractDICookie {
56
57     /**
58      * Public ID used for registration in Meta-inf/debugger.
59      */

60     public static final String JavaDoc ID = "netbeans-jpda-LaunchingDICookie";
61
62     private LaunchingConnector launchingConnector;
63     private Map JavaDoc<String JavaDoc, ? extends Argument> args;
64
65     private String JavaDoc mainClassName;
66     private boolean suspend;
67
68
69     private LaunchingDICookie (
70         LaunchingConnector launchingConnector,
71         Map JavaDoc<String JavaDoc, ? extends Argument> args,
72         String JavaDoc mainClassName,
73         boolean suspend
74     ) {
75         this.launchingConnector = launchingConnector;
76         this.args = args;
77         this.mainClassName = mainClassName;
78         this.suspend = suspend;
79     }
80
81     /**
82      * Creates a new instance of LaunchingDICookie for given parameters.
83      *
84      * @param mainClassName a name or main class
85      * @param commandLine command line of debugged JVM
86      * @param address a address to listen on
87      * @param suspend if true session will be suspended
88      * @return a new instance of LaunchingDICookie for given parameters
89      */

90     public static LaunchingDICookie create (
91         String JavaDoc mainClassName,
92         String JavaDoc commandLine,
93         String JavaDoc address,
94         boolean suspend
95     ) {
96         return new LaunchingDICookie (
97             findLaunchingConnector (),
98             getArgs (commandLine, address),
99             mainClassName,
100             suspend
101         );
102     }
103
104     /**
105      * Creates a new instance of LaunchingDICookie for given parameters.
106      *
107      * @param mainClassName a name or main class
108      * @param args command line arguments
109      * @param classPath a classPath
110      * @param suspend if true session will be suspended
111      * @return a new instance of LaunchingDICookie for given parameters
112      */

113     public static LaunchingDICookie create (
114         String JavaDoc mainClassName,
115         String JavaDoc[] args,
116         String JavaDoc classPath,
117         boolean suspend
118     ) {
119         String JavaDoc argss = "";
120         int i, k = args.length;
121         for (i = 0; i < k; i++) {
122             argss += " \"" + args [i] + "\"";
123         }
124         // TODO: This code is likely wrong, we need to run debuggee on JDK that
125
// is set on the project.
126
// XXX: This method is likely not called from anywhere.
127
// But it's an impl. of API method JPDADebugger.launch().
128
String JavaDoc commandLine = System.getProperty ("java.home") +
129             "\\bin\\java -Xdebug -Xnoagent -Xrunjdwp:transport=" +
130             getTransportName () +
131             ",address=name,suspend=" +
132             (suspend ? "y" : "n") +
133             " -classpath \"" +
134             classPath +
135             "\" " +
136             mainClassName +
137             argss;
138         String JavaDoc address = "name";
139         return new LaunchingDICookie (
140             findLaunchingConnector (),
141             getArgs (commandLine, address),
142             mainClassName,
143             suspend
144         );
145     }
146     
147     /**
148      * Returns type of transport to be used.
149      *
150      * @return type of transport to be used
151      */

152     public static String JavaDoc getTransportName () {
153         return findLaunchingConnector ().transport ().name ();
154     }
155
156
157     // main methods ............................................................
158

159     /**
160      * Returns main class name.
161      *
162      * @return main class name
163      */

164     public String JavaDoc getClassName () {
165         return mainClassName;
166     }
167
168     /**
169      * Returns suspended state.
170      *
171      * @return suspended state
172      */

173     public boolean getSuspend () {
174         return suspend;
175     }
176
177     /**
178      * Returns command line to be used.
179      *
180      * @return command line to be used
181      */

182     public String JavaDoc getCommandLine () {
183         Argument a = (Argument) args.get ("command");
184         if (a == null) return null;
185         return a.value ();
186     }
187     
188     /**
189      * Creates a new instance of VirtualMachine for this DebuggerInfo Cookie.
190      *
191      * @return a new instance of VirtualMachine for this DebuggerInfo Cookie
192      */

193     public VirtualMachine getVirtualMachine () throws IOException JavaDoc,
194     IllegalConnectorArgumentsException, VMStartException {
195         return launchingConnector.launch (args);
196     }
197     
198     
199     // private helper methods ..................................................
200

201     private static Map JavaDoc<String JavaDoc, ? extends Argument> getArgs (
202         String JavaDoc commandLine,
203         String JavaDoc address
204     ) {
205         Map JavaDoc<String JavaDoc, ? extends Argument> args = findLaunchingConnector ().defaultArguments ();
206         args.get ("command").setValue (commandLine);
207         args.get ("address").setValue (address);
208         return args;
209     }
210     
211     private static LaunchingConnector findLaunchingConnector () {
212         Iterator JavaDoc<LaunchingConnector> iter = Bootstrap.virtualMachineManager ().
213             launchingConnectors ().iterator ();
214         while (iter.hasNext ()) {
215             LaunchingConnector lc = iter.next ();
216             if (lc.name ().indexOf ("RawCommandLineLaunch") > -1)
217                 return lc;
218         }
219         return null;
220     }
221 }
222
Popular Tags