1 19 20 package org.netbeans.modules.debugger.jpda.ui; 21 22 import com.sun.jdi.AbsentInformationException; 23 import java.beans.PropertyChangeListener ; 24 import java.net.ConnectException ; 25 import java.net.UnknownHostException ; 26 import java.text.MessageFormat ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.MissingResourceException ; 30 import java.util.Set ; 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 60 public class DebuggerOutput extends LazyActionsManagerListener implements 61 PropertyChangeListener { 62 63 64 private static Set managers = new HashSet (); 66 67 private JPDADebugger debugger; 68 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 engineContext = (SourcePath) contextProvider.lookupFirst 81 (null, SourcePath.class); 82 83 if (DebuggerManager.getDebuggerManager ().getSessions ().length == 1) { 85 Iterator i = managers.iterator (); 86 while (i.hasNext ()) 87 ((IOManager) i.next ()).close (); 88 managers = new HashSet (); 89 } 90 91 String title = (String ) contextProvider.lookupFirst (null, String .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 engineContext = null; 113 ioManager = null; 114 } 115 116 public String [] getProperties () { 117 return new String [] {ActionsManagerListener.PROP_ACTION_PERFORMED}; 118 } 119 120 public void propertyChange (java.beans.PropertyChangeEvent 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 new String [] { 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 new String [] { 150 c.getSharedMemoryName () 151 }, 152 null 153 ); 154 } else if (c.getArgs().get("pid") != null) { 155 print ( 156 "CTL_Attaching_pid", 157 new String [] { 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 new String [] { 178 c.getSharedMemoryName () 179 }, 180 null 181 ); 182 else 183 print ( 184 "CTL_Listening_socket", 185 new String [] { 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 new String [] { 198 c.getCommandLine () 199 }, 200 null 201 ); 202 } 203 } else 204 if (debuggerState == JPDADebugger.STATE_RUNNING) { 205 print ( 206 "CTL_Debugger_running", 207 new String [] { 209 }, 210 null 211 ); 212 } else 213 if (debuggerState == JPDADebugger.STATE_DISCONNECTED) { 214 Throwable 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 message = e.getMessage (); 228 if (e instanceof ConnectException ) 229 message = NbBundle.getBundle (DebuggerOutput.class). 230 getString ("CTL_Connection_refused"); 231 if (e instanceof UnknownHostException ) 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 } 246 ioManager.closeStream (); 247 } else 248 if (debuggerState == JPDADebugger.STATE_STOPPED) { 249 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 language = (session != null) ? session.getCurrentLanguage() : null; 264 String threadName = t.getName (); 265 String methodName = t.getMethodName (); 266 String className = t.getClassName (); 267 int lineNumber = t.getLineNumber (language); 268 try { 269 String sourceName = t.getSourceName (language); 270 String relativePath = EditorContextBridge.getRelativePath 271 (t, language); 272 String 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 new String [] { 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 new String [] { 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 [] { threadName }, 313 line 314 ); 315 } catch (AbsentInformationException ex) { 316 if (lineNumber > 0) 317 print ( 318 "CTL_Thread_stopped_no_info", 319 new String [] { 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 new String [] { 333 threadName, 334 className, 335 methodName 336 }, 337 null 338 ); 339 } 340 } 341 } 342 343 public void actionPerformed (Object action, boolean success) { 344 if (!success) return; 345 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 365 private void print ( 366 String message, 367 String [] args, 369 IOManager.Line line 370 ) { 371 String text = (args == null) ? 372 NbBundle.getMessage ( 373 DebuggerOutput.class, 374 message 375 ) : 376 new MessageFormat (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 line 390 ); 391 } 392 } 393 | Popular Tags |