KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > ui > DebuggerOutput


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.ui;
21
22 import com.sun.jdi.AbsentInformationException;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.net.ConnectException JavaDoc;
25 import java.net.UnknownHostException JavaDoc;
26 import java.text.MessageFormat JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.MissingResourceException JavaDoc;
30 import java.util.Set JavaDoc;
31 import org.netbeans.api.debugger.ActionsManager;
32
33
34 import org.netbeans.api.debugger.DebuggerEngine;
35 import org.netbeans.api.debugger.ActionsManagerListener;
36 import org.netbeans.api.debugger.DebuggerManager;
37 import org.netbeans.api.debugger.LazyActionsManagerListener;
38 import org.netbeans.api.debugger.Session;
39 import org.netbeans.api.debugger.jpda.AbstractDICookie;
40 import org.netbeans.api.debugger.jpda.AttachingDICookie;
41 import org.netbeans.api.debugger.jpda.CallStackFrame;
42 import org.netbeans.api.debugger.jpda.DebuggerStartException;
43 import org.netbeans.api.debugger.jpda.JPDADebugger;
44 import org.netbeans.api.debugger.jpda.JPDAThread;
45 import org.netbeans.api.debugger.jpda.LaunchingDICookie;
46 import org.netbeans.api.debugger.jpda.ListeningDICookie;
47 import org.netbeans.spi.debugger.ContextProvider;
48
49 import org.openide.util.NbBundle;
50
51
52 /**
53  * Listens on
54  * {@link org.netbeans.api.debugger.ActionsManagerListener#PROP_ACTION_PERFORMED} and
55  * {@link org.netbeans.api.debugger.jpda.JPDADebugger#PROP_STATE}
56  * properties and writes some messages to Debugger Console.
57  *
58  * @author Jan Jancura
59  */

60 public class DebuggerOutput extends LazyActionsManagerListener implements
61 PropertyChangeListener JavaDoc {
62
63
64     // set of all IOManagers
65
private static Set JavaDoc managers = new HashSet JavaDoc ();
66     
67     private JPDADebugger debugger;
68     //private DebuggerEngine engine;
69
private SourcePath engineContext;
70     private IOManager ioManager;
71     private ContextProvider contextProvider;
72
73
74     public DebuggerOutput (ContextProvider contextProvider) {
75         this.contextProvider = contextProvider;
76         this.debugger = (JPDADebugger) contextProvider.lookupFirst
77             (null, JPDADebugger.class);
78         //this.engine = (DebuggerEngine) contextProvider.lookupFirst
79
// (null, DebuggerEngine.class);
80
engineContext = (SourcePath) contextProvider.lookupFirst
81             (null, SourcePath.class);
82         
83         // close old tabs
84
if (DebuggerManager.getDebuggerManager ().getSessions ().length == 1) {
85             Iterator JavaDoc i = managers.iterator ();
86             while (i.hasNext ())
87                 ((IOManager) i.next ()).close ();
88             managers = new HashSet JavaDoc ();
89         }
90         
91         // open new tab
92
String JavaDoc title = (String JavaDoc) contextProvider.lookupFirst (null, String JavaDoc.class);
93         if (title == null)
94             title = NbBundle.getBundle (IOManager.class).getString
95                 ("CTL_DebuggerConsole_Title");
96         ioManager = new IOManager (title);
97         managers.add (ioManager);
98         
99         debugger.addPropertyChangeListener (
100             JPDADebugger.PROP_STATE,
101             this
102         );
103     }
104
105     protected synchronized void destroy () {
106         debugger.removePropertyChangeListener (
107             JPDADebugger.PROP_STATE,
108             this
109         );
110         debugger = null;
111         //engine = null;
112
engineContext = null;
113         ioManager = null;
114     }
115
116     public String JavaDoc[] getProperties () {
117         return new String JavaDoc[] {ActionsManagerListener.PROP_ACTION_PERFORMED};
118     }
119
120     public void propertyChange (java.beans.PropertyChangeEvent JavaDoc evt) {
121         JPDAThread t;
122         int debuggerState;
123         IOManager ioManager;
124         synchronized (this) {
125             if (debugger == null) return ;
126             t = debugger.getCurrentThread ();
127             debuggerState = debugger.getState();
128             ioManager = this.ioManager;
129         }
130         if (debuggerState == JPDADebugger.STATE_STARTING) {
131             AbstractDICookie cookie = (AbstractDICookie) contextProvider.
132                 lookupFirst (null, AbstractDICookie.class);
133             if (cookie instanceof AttachingDICookie) {
134                 AttachingDICookie c = (AttachingDICookie) cookie;
135                 if (c.getHostName () != null) {
136                     print (
137                         "CTL_Attaching_socket",
138 // where,
139
new String JavaDoc[] {
140                             c.getHostName (),
141                             String.valueOf(c.getPortNumber ())
142                         },
143                         null
144                     );
145                 } else if (c.getSharedMemoryName() != null) {
146                     print (
147                         "CTL_Attaching_shmem",
148 // where,
149
new String JavaDoc[] {
150                             c.getSharedMemoryName ()
151                         },
152                         null
153                     );
154                 } else if (c.getArgs().get("pid") != null) {
155                     print (
156                         "CTL_Attaching_pid",
157 // where,
158
new String JavaDoc[] {
159                             c.getArgs().get("pid").toString()
160                         },
161                         null
162                     );
163                 } else {
164                     print (
165                         "CTL_Attaching",
166                         null,
167                         null
168                     );
169                 }
170             } else
171             if (cookie instanceof ListeningDICookie) {
172                 ListeningDICookie c = (ListeningDICookie) cookie;
173                 if (c.getSharedMemoryName () != null)
174                     print (
175                         "CTL_Listening_shmem",
176 // where,
177
new String JavaDoc[] {
178                             c.getSharedMemoryName ()
179                         },
180                         null
181                     );
182                 else
183                     print (
184                         "CTL_Listening_socket",
185 // where,
186
new String JavaDoc[] {
187                             String.valueOf(c.getPortNumber ())
188                         },
189                         null
190                     );
191             } else
192             if (cookie instanceof LaunchingDICookie) {
193                 LaunchingDICookie c = (LaunchingDICookie) cookie;
194                     print (
195                         "CTL_Launching",
196 // where,
197
new String JavaDoc[] {
198                             c.getCommandLine ()
199                         },
200                         null
201                     );
202             }
203         } else
204         if (debuggerState == JPDADebugger.STATE_RUNNING) {
205             print (
206                 "CTL_Debugger_running",
207 // where,
208
new String JavaDoc[] {
209                 },
210                 null
211             );
212         } else
213         if (debuggerState == JPDADebugger.STATE_DISCONNECTED) {
214             Throwable JavaDoc e = null;
215             try {
216                 synchronized (this) {
217                     if (debugger != null) {
218                         debugger.waitRunning ();
219                     }
220                 }
221             } catch (DebuggerStartException ex) {
222                 e = ex.getTargetException ();
223             }
224             if (e == null)
225                 print ("CTL_Debugger_finished", null, null);
226             else {
227                 String JavaDoc message = e.getMessage ();
228                 if (e instanceof ConnectException JavaDoc)
229                     message = NbBundle.getBundle (DebuggerOutput.class).
230                         getString ("CTL_Connection_refused");
231                 if (e instanceof UnknownHostException JavaDoc)
232                     message = NbBundle.getBundle (DebuggerOutput.class).
233                         getString ("CTL_Unknown_host");
234                 if (message != null) {
235                     ioManager.println (
236                         message,
237                         null
238                     );
239                 } else
240                     ioManager.println (
241                         e.toString (),
242                         null
243                     );
244                 //e.printStackTrace ();
245
}
246             ioManager.closeStream ();
247         } else
248         if (debuggerState == JPDADebugger.STATE_STOPPED) {
249             //DebuggerEngine engine = debugger.getEngine ();
250
//S ystem.out.println("State Stopped " + debugger.getLastAction ());
251
if (t == null) {
252                 print ("CTL_Debugger_stopped", null, null);
253                 return;
254             }
255             Session session = null;
256             Session[] sessions = DebuggerManager.getDebuggerManager().getSessions();
257             for (int i = 0; i < sessions.length; i++) {
258                 if (sessions[i].lookupFirst(null, JPDADebugger.class) == debugger) {
259                     session = sessions[i];
260                     break;
261                 }
262             }
263             String JavaDoc language = (session != null) ? session.getCurrentLanguage() : null;
264             String JavaDoc threadName = t.getName ();
265             String JavaDoc methodName = t.getMethodName ();
266             String JavaDoc className = t.getClassName ();
267             int lineNumber = t.getLineNumber (language);
268             try {
269                 String JavaDoc sourceName = t.getSourceName (language);
270                 String JavaDoc relativePath = EditorContextBridge.getRelativePath
271                     (t, language);
272                 String JavaDoc url = null;
273                 synchronized (this) {
274                     if (relativePath != null && engineContext != null) {
275                         url = engineContext.getURL(relativePath, true);
276                     }
277                 }
278                 IOManager.Line line = null;
279                 if (lineNumber > 0 && url != null)
280                     line = new IOManager.Line (
281                         url,
282                         lineNumber,
283                         debugger
284                     );
285
286                 if (lineNumber > 0)
287                     print (
288                         "CTL_Thread_stopped",
289                       // IOManager.DEBUGGER_OUT + IOManager.STATUS_OUT,
290
new String JavaDoc[] {
291                             threadName,
292                             sourceName,
293                             methodName,
294                             String.valueOf(lineNumber)
295                         },
296                         line
297                     );
298                 else if (sourceName.length() > 0 && methodName.length() > 0)
299                     print (
300                         "CTL_Thread_stopped_no_line",
301                     // IOManager.DEBUGGER_OUT + IOManager.STATUS_OUT,
302
new String JavaDoc[] {
303                             threadName,
304                             sourceName,
305                             methodName
306                         },
307                         line
308                     );
309                 else
310                     print (
311                         "CTL_Thread_stopped_no_line_no_source",
312                         new String JavaDoc[] { threadName },
313                         line
314                     );
315             } catch (AbsentInformationException ex) {
316                 if (lineNumber > 0)
317                     print (
318                         "CTL_Thread_stopped_no_info",
319                      // IOManager.DEBUGGER_OUT + IOManager.STATUS_OUT,
320
new String JavaDoc[] {
321                             threadName,
322                             className,
323                             methodName,
324                             lineNumber > 0 ? String.valueOf(lineNumber) : ""
325                         },
326                         null
327                     );
328                 else
329                     print (
330                         "CTL_Thread_stopped_no_info_no_line",
331                         //IOManager.DEBUGGER_OUT + IOManager.STATUS_OUT,
332
new String JavaDoc[] {
333                             threadName,
334                             className,
335                             methodName
336                         },
337                         null
338                     );
339             }
340         }
341     }
342
343     public void actionPerformed (Object JavaDoc action, boolean success) {
344         if (!success) return;
345         //print ("CTL_Debugger_running", where, null, null);
346
if (action == ActionsManager.ACTION_CONTINUE)
347             print ("CTL_Continue", null, null);
348         else
349         if (action == ActionsManager.ACTION_STEP_INTO)
350             print ("CTL_Step_Into", null, null);
351         else
352         if (action == ActionsManager.ACTION_STEP_OUT)
353             print ("CTL_Step_Out", null, null);
354         else
355         if (action == ActionsManager.ACTION_STEP_OVER)
356             print ("CTL_Step_Over", null, null);
357     }
358
359     IOManager getIOManager() {
360         return ioManager;
361     }
362
363     // helper methods ..........................................................
364

365     private void print (
366         String JavaDoc message,
367 // int where,
368
String JavaDoc[] args,
369         IOManager.Line line
370     ) {
371         String JavaDoc text = (args == null) ?
372             NbBundle.getMessage (
373                 DebuggerOutput.class,
374                 message
375             ) :
376             new MessageFormat JavaDoc (NbBundle.getMessage (
377                 DebuggerOutput.class,
378                 message
379             )).format (args);
380
381         IOManager ioManager;
382         synchronized (this) {
383             ioManager = this.ioManager;
384             if (ioManager == null) return ;
385         }
386         ioManager.println (
387             text,
388 // where,
389
line
390         );
391     }
392 }
393
Popular Tags