1 19 20 package org.netbeans.modules.debugger.jpda.actions; 21 22 import com.sun.jdi.VirtualMachine; 23 import com.sun.jdi.event.Event; 24 import java.io.IOException ; 25 26 import java.util.Collections ; 27 28 import java.util.Set ; 29 import org.netbeans.api.debugger.ActionsManager; 30 31 32 import org.netbeans.api.debugger.jpda.AttachingDICookie; 33 import org.netbeans.spi.debugger.ContextProvider; 34 import org.netbeans.api.debugger.Session; 35 import org.netbeans.api.debugger.jpda.AbstractDICookie; 36 import org.netbeans.api.debugger.jpda.JPDADebugger; 37 import org.netbeans.api.debugger.jpda.ListeningDICookie; 38 import org.netbeans.modules.debugger.jpda.JPDADebuggerImpl; 39 import org.netbeans.modules.debugger.jpda.util.Operator; 40 import org.netbeans.modules.debugger.jpda.util.Executor; 41 import org.netbeans.spi.debugger.ActionsProvider; 42 import org.netbeans.spi.debugger.ActionsProviderListener; 43 import org.openide.ErrorManager; 44 import org.openide.util.Cancellable; 45 import org.openide.util.RequestProcessor; 46 47 48 52 public class StartActionProvider extends ActionsProvider implements Cancellable { 53 56 private static final boolean startVerbose = 57 System.getProperty ("netbeans.debugger.start") != null; 58 private static int jdiTrace; 59 static { 60 if (System.getProperty ("netbeans.debugger.jditrace") != null) { 61 try { 62 jdiTrace = Integer.parseInt ( 63 System.getProperty ("netbeans.debugger.jditrace") 64 ); 65 } catch (NumberFormatException ex) { 66 jdiTrace = VirtualMachine.TRACE_NONE; 67 } 68 } else 69 jdiTrace = VirtualMachine.TRACE_NONE; 70 } 71 72 private JPDADebuggerImpl debuggerImpl; 73 private ContextProvider lookupProvider; 74 private Thread startingThread; 75 76 77 public StartActionProvider (ContextProvider lookupProvider) { 78 debuggerImpl = (JPDADebuggerImpl) lookupProvider.lookupFirst 79 (null, JPDADebugger.class); 80 this.lookupProvider = lookupProvider; 81 } 82 83 public Set getActions () { 84 return Collections.singleton (ActionsManager.ACTION_START); 85 } 86 87 public void doAction (Object action) { 88 if (startVerbose) 89 System.out.println ("\nS StartActionProvider.doAction ()"); 90 JPDADebuggerImpl debugger = (JPDADebuggerImpl) lookupProvider. 91 lookupFirst (null, JPDADebugger.class); 92 if ( debugger != null && 93 debugger.getVirtualMachine () != null 94 ) return; 95 96 97 debuggerImpl.setStarting (); 98 final AbstractDICookie cookie = (AbstractDICookie) lookupProvider. 99 lookupFirst (null, AbstractDICookie.class); 100 doStartDebugger(cookie); 101 if (startVerbose) 102 System.out.println ("\nS StartActionProvider." + 103 "doAction () setStarting" 104 ); 105 if (startVerbose) 106 System.out.println ("\nS StartActionProvider." + 107 "doAction () end" 108 ); 109 } 110 111 public void postAction(Object action, final Runnable actionPerformedNotifier) { 112 if (startVerbose) 113 System.out.println ("\nS StartActionProvider.postAction ()"); 114 JPDADebuggerImpl debugger = (JPDADebuggerImpl) lookupProvider. 115 lookupFirst (null, JPDADebugger.class); 116 if ( debugger != null && 117 debugger.getVirtualMachine () != null 118 ) { 119 actionPerformedNotifier.run(); 120 return; 121 } 122 123 124 final AbstractDICookie cookie = (AbstractDICookie) lookupProvider. 125 lookupFirst (null, AbstractDICookie.class); 126 127 if (startVerbose) 128 System.out.println ("\nS StartActionProvider." + 129 "postAction () setStarting" 130 ); 131 debuggerImpl.setStarting (); if (startVerbose) 133 System.out.println ("\nS StartActionProvider." + 134 "postAction () setStarting end" 135 ); 136 137 RequestProcessor.getDefault().post(new Runnable () { 138 public void run() { 139 synchronized (StartActionProvider.this) { 141 startingThread = Thread.currentThread(); 142 } 143 try { 144 doStartDebugger(cookie); 145 } finally { 148 synchronized (StartActionProvider.this) { 149 startingThread = null; 150 } 151 actionPerformedNotifier.run(); 153 } 154 155 } 156 }); 157 158 } 159 160 private void doStartDebugger(AbstractDICookie cookie) { 161 if (startVerbose) 162 System.out.println ("\nS StartActionProvider." + 163 "doAction ().thread" 164 ); 165 Exception exception = null; 166 try { 167 VirtualMachine virtualMachine = cookie.getVirtualMachine (); 168 virtualMachine.setDebugTraceMode (jdiTrace); 169 170 final Object startLock = new Object (); 171 Operator o = createOperator (virtualMachine, startLock); 172 synchronized (startLock) { 173 if (startVerbose) System.out.println ( 174 "\nS StartActionProvider.doAction () - " + 175 "starting operator thread" 176 ); 177 o.start (); 178 if (cookie instanceof ListeningDICookie) 179 startLock.wait(1500); 180 } 181 182 debuggerImpl.setRunning ( 183 virtualMachine, 184 o 185 ); 186 187 196 if (startVerbose) 197 System.out.println ("\nS StartActionProvider." + 198 "doAction ().thread end: success" 199 ); 200 } catch (InterruptedException iex) { 201 exception = iex; 202 } catch (IOException ioex) { 203 exception = ioex; 204 } catch (Exception ex) { 205 exception = ex; 206 ErrorManager.getDefault().notify(ex); 208 } 209 if (exception != null) { 210 if (startVerbose) 211 System.out.println ("\nS StartActionProvider." + 212 "doAction ().thread end: exception " + exception 213 ); 214 debuggerImpl.setException (exception); 215 final Session session = (Session) lookupProvider.lookupFirst(null, Session.class); 217 RequestProcessor.getDefault().post(new Runnable () { 218 public void run() { 219 session.kill(); 221 } 222 }); 223 } 224 } 225 226 public boolean isEnabled (Object action) { 227 return true; 228 } 229 230 public void addActionsProviderListener (ActionsProviderListener l) {} 231 public void removeActionsProviderListener (ActionsProviderListener l) {} 232 233 private Operator createOperator ( 234 VirtualMachine virtualMachine, 235 final Object startLock 236 ) { 237 return new Operator ( 238 virtualMachine, 239 debuggerImpl, 240 new Executor () { 241 public boolean exec(Event event) { 242 synchronized(startLock) { 243 startLock.notify(); 244 } 245 return false; 246 } 247 }, 248 new Runnable () { 249 public void run () { 250 debuggerImpl.finish(); 251 } 252 }, 253 debuggerImpl.LOCK 254 ); 255 } 256 257 public boolean cancel() { 258 synchronized (StartActionProvider.this) { 259 if (startingThread != null) { 260 startingThread.interrupt(); 261 } else { 262 return true; 263 } 264 } 265 return true; 266 } 267 } 268 | Popular Tags |