KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > ant > JPDAConnect


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.modules.debugger.jpda.ant;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.Task;
28 import org.apache.tools.ant.types.Path;
29
30 import org.openide.util.RequestProcessor;
31
32 import org.netbeans.api.debugger.jpda.JPDADebugger;
33 import org.netbeans.api.java.classpath.ClassPath;
34
35
36 /**
37  * Ant task to attach the NetBeans JPDA debugger to a remote process.
38  * @see "#18708"
39  * @author Jesse Glick
40  */

41 public class JPDAConnect extends Task {
42
43     private static final Logger JavaDoc logger = Logger.getLogger("org.netbeans.modules.debugger.jpda.ant"); // NOI18N
44

45     private String JavaDoc host = "localhost"; // NOI18N
46

47     private String JavaDoc address;
48     
49     /** Explicit sourcepath of the debugged process. */
50     private Path sourcepath = null;
51     
52     /** Explicit classpath of the debugged process. */
53     private Path classpath = null;
54     
55     /** Explicit bootclasspath of the debugged process. */
56     private Path bootclasspath = null;
57         
58     /** Name which will represent this debugging session in debugger UI.
59      * If known in advance it should be name of the app which will be debugged.
60      */

61     private String JavaDoc name;
62
63     /** Default transport is socket*/
64     private String JavaDoc transport = "dt_socket"; // NOI18N
65

66     
67     /**
68      * Host to connect to.
69      * By default, localhost.
70      */

71     public void setHost (String JavaDoc h) {
72         host = h;
73     }
74     
75     public void setAddress (String JavaDoc address) {
76         this.address = address;
77     }
78     
79     private String JavaDoc getAddress () {
80         return address;
81     }
82     
83     public void addClasspath (Path path) {
84         if (classpath != null)
85             throw new BuildException ("Only one classpath subelement is supported");
86         classpath = path;
87     }
88     
89     public void addBootclasspath (Path path) {
90         if (bootclasspath != null)
91             throw new BuildException ("Only one bootclasspath subelement is supported");
92         bootclasspath = path;
93     }
94     
95     public void addSourcepath (Path path) {
96         if (sourcepath != null)
97             throw new BuildException ("Only one sourcepath subelement is supported");
98         sourcepath = path;
99     }
100     
101     public void setTransport (String JavaDoc transport) {
102         this.transport = transport;
103     }
104     
105     private String JavaDoc getTransport () {
106         return transport;
107     }
108     
109     public void setName (String JavaDoc name) {
110         this.name = name;
111     }
112     
113     private String JavaDoc getName () {
114         return name;
115     }
116     
117     public void execute () throws BuildException {
118         logger.fine("JPDAConnect.execute ()"); // NOI18N
119

120         JPDAStart.verifyPaths(getProject(), classpath);
121         //JPDAStart.verifyPaths(getProject(), bootclasspath); Do not check the paths on bootclasspath (see issue #70930).
122
JPDAStart.verifyPaths(getProject(), sourcepath);
123         
124         if (name == null)
125             throw new BuildException (
126                 "name attribute must specify name of this debugging session",
127                 getLocation ()
128             );
129         if (address == null)
130             throw new BuildException (
131                 "address attribute must specify port number or memory " +
132                 "allocation unit name of connection",
133                 getLocation ()
134             );
135         if (transport == null)
136             transport = "dt_socket"; // NOI18N
137

138         final Object JavaDoc[] lock = new Object JavaDoc [1];
139
140         ClassPath sourcePath = JPDAStart.createSourcePath (
141             getProject (),
142             classpath,
143             sourcepath
144         );
145         ClassPath jdkSourcePath = JPDAStart.createJDKSourcePath (
146             getProject (),
147             bootclasspath
148         );
149         if (logger.isLoggable(Level.FINE)) {
150             logger.fine("Create sourcepath:"); // NOI18N
151
logger.fine(" classpath : " + classpath); // NOI18N
152
logger.fine(" sourcepath : " + sourcepath); // NOI18N
153
logger.fine(" bootclasspath : " + bootclasspath); // NOI18N
154
logger.fine(" >> sourcePath : " + sourcePath); // NOI18N
155
logger.fine(" >> jdkSourcePath : " + jdkSourcePath); // NOI18N
156
}
157         final Map JavaDoc properties = new HashMap JavaDoc ();
158         properties.put ("sourcepath", sourcePath); // NOI18N
159
properties.put ("name", getName ()); // NOI18N
160
properties.put ("jdksources", jdkSourcePath); // NOI18N
161

162
163         synchronized(lock) {
164             RequestProcessor.getDefault ().post (new Runnable JavaDoc () {
165                 public void run() {
166                     synchronized(lock) {
167                         try {
168                             if (logger.isLoggable(Level.FINE)) {
169                                 logger.fine(
170                                     "JPDAConnect.execute ().synchronized: " // NOI18N
171
+ "host = " + host + " port = " + address + // NOI18N
172
" transport = " + transport // NOI18N
173
);
174                             }
175                             // VirtualMachineManagerImpl can be initialized
176
// here, so needs to be inside RP thread.
177
if (transport.equals ("dt_socket")) // NOI18N
178
try {
179                                     JPDADebugger.attach (
180                                         host,
181                                         Integer.parseInt (address),
182                                         new Object JavaDoc[] {properties}
183                                     );
184                                 } catch (NumberFormatException JavaDoc e) {
185                                     throw new BuildException (
186                                         "address attribute must specify port " +
187                                         "number for dt_socket connection",
188                                         getLocation ()
189                                     );
190                                 }
191                             else
192                                 JPDADebugger.attach (
193                                     address,
194                                     new Object JavaDoc[] {properties}
195                                 );
196                             logger.fine(
197                                     "JPDAConnect.execute ().synchronized " + // NOI18N
198
"end: success" // NOI18N
199
);
200                         } catch (Throwable JavaDoc e) {
201                             logger.fine(
202                                     "JPDAConnect.execute().synchronized " + // NOI18N
203
"end: exception " + e // NOI18N
204
);
205                             lock[0] = e;
206                         } finally {
207                             lock.notify();
208                         }
209                     }
210                 }
211             });
212             try {
213                 lock.wait();
214             } catch (InterruptedException JavaDoc e) {
215                 logger.fine("JPDAConnect.execute() " + "end: exception " + e); // NOI18N
216
throw new BuildException(e);
217             }
218             if (lock[0] != null) {
219                 logger.fine("JPDAConnect.execute() " + "end: exception " + lock[0]); // NOI18N
220
throw new BuildException((Throwable JavaDoc) lock[0]);
221             }
222
223         }
224         if (host == null)
225             log ("Attached JPDA debugger to " + address);
226         else
227             log ("Attached JPDA debugger to " + host + ":" + address);
228         logger.fine("JPDAConnect.execute () " + "end: success"); // NOI18N
229
}
230 }
231
Popular Tags