KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > actions > StartActionProvider


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.actions;
21
22 import com.sun.jdi.VirtualMachine;
23 import com.sun.jdi.event.Event;
24 import java.io.IOException JavaDoc;
25
26 import java.util.Collections JavaDoc;
27
28 import java.util.Set JavaDoc;
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 /**
49 *
50 * @author Jan Jancura
51 */

52 public class StartActionProvider extends ActionsProvider implements Cancellable {
53 // private static transient String [] stopMethodNames =
54
// {"main", "start", "init", "<init>"}; // NOI18N
55

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 JavaDoc 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 JavaDoc 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 JavaDoc getActions () {
84         return Collections.singleton (ActionsManager.ACTION_START);
85     }
86     
87     public void doAction (Object JavaDoc 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 JavaDoc action, final Runnable JavaDoc 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 (); // JS
132
if (startVerbose)
133             System.out.println ("\nS StartActionProvider." +
134                 "postAction () setStarting end"
135             );
136         
137         RequestProcessor.getDefault().post(new Runnable JavaDoc() {
138             public void run() {
139                 //debuggerImpl.setStartingThread(Thread.currentThread());
140
synchronized (StartActionProvider.this) {
141                     startingThread = Thread.currentThread();
142                 }
143                 try {
144                     doStartDebugger(cookie);
145                 //} catch (InterruptedException iex) {
146
// We've interrupted ourselves
147
} finally {
148                     synchronized (StartActionProvider.this) {
149                         startingThread = null;
150                     }
151                     //debuggerImpl.unsetStartingThread();
152
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 JavaDoc exception = null;
166         try {
167             VirtualMachine virtualMachine = cookie.getVirtualMachine ();
168             virtualMachine.setDebugTraceMode (jdiTrace);
169
170             final Object JavaDoc startLock = new Object JavaDoc();
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             // PATCH #46295 JSP breakpoint isn't reached during
188
// second debugging
189
// if (cookie instanceof AttachingDICookie) {
190
// synchronized (debuggerImpl.LOCK) {
191
// virtualMachine.resume ();
192
// }
193
// }
194
// PATCHEND Hanz
195

196             if (startVerbose)
197                 System.out.println ("\nS StartActionProvider." +
198                     "doAction ().thread end: success"
199                 );
200         } catch (InterruptedException JavaDoc iex) {
201             exception = iex;
202         } catch (IOException JavaDoc ioex) {
203             exception = ioex;
204         } catch (Exception JavaDoc ex) {
205             exception = ex;
206             // Notify! Otherwise bugs in the code can not be located!!!
207
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             // kill the session that did not start properly
216
final Session session = (Session) lookupProvider.lookupFirst(null, Session.class);
217             RequestProcessor.getDefault().post(new Runnable JavaDoc() {
218                 public void run() {
219                     // Kill it in a separate thread so that the startup sequence can be finished.
220
session.kill();
221                 }
222             });
223         }
224     }
225
226     public boolean isEnabled (Object JavaDoc 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 JavaDoc 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 JavaDoc () {
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