KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > debug > model > AntDebugTarget


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ant.internal.ui.debug.model;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.ant.internal.ui.debug.IAntDebugConstants;
18 import org.eclipse.ant.internal.ui.debug.IAntDebugController;
19 import org.eclipse.core.resources.IMarkerDelta;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.variables.VariablesPlugin;
22 import org.eclipse.debug.core.DebugEvent;
23 import org.eclipse.debug.core.DebugException;
24 import org.eclipse.debug.core.DebugPlugin;
25 import org.eclipse.debug.core.IBreakpointManager;
26 import org.eclipse.debug.core.IBreakpointManagerListener;
27 import org.eclipse.debug.core.IDebugEventSetListener;
28 import org.eclipse.debug.core.ILaunch;
29 import org.eclipse.debug.core.model.IBreakpoint;
30 import org.eclipse.debug.core.model.IDebugTarget;
31 import org.eclipse.debug.core.model.ILineBreakpoint;
32 import org.eclipse.debug.core.model.IMemoryBlock;
33 import org.eclipse.debug.core.model.IProcess;
34 import org.eclipse.debug.core.model.IThread;
35 import org.eclipse.ui.externaltools.internal.model.IExternalToolConstants;
36
37 /**
38  * Ant Debug Target
39  */

40 public class AntDebugTarget extends AntDebugElement implements IDebugTarget, IDebugEventSetListener, IBreakpointManagerListener {
41     
42     // associated system process (Ant Build)
43
private IProcess fProcess;
44     
45     // containing launch object
46
private ILaunch fLaunch;
47     
48     // Build file name
49
private String JavaDoc fName;
50
51     // suspend state
52
private boolean fSuspended= false;
53     
54     // terminated state
55
private boolean fTerminated= false;
56     
57     // threads
58
private AntThread fThread;
59     private IThread[] fThreads;
60     
61     private IAntDebugController fController;
62     
63     private List JavaDoc fRunToLineBreakpoints;
64
65     /**
66      * Constructs a new debug target in the given launch for the
67      * associated Ant build process.
68      *
69      * @param launch containing launch
70      * @param process Ant build process
71      * @param controller the controller to communicate to the Ant build
72      */

73     public AntDebugTarget(ILaunch launch, IProcess process, IAntDebugController controller) {
74         super(null);
75         fLaunch = launch;
76         fProcess = process;
77         
78         fController= controller;
79         
80         fThread = new AntThread(this);
81         fThreads = new IThread[] {fThread};
82         
83         DebugPlugin.getDefault().getBreakpointManager().addBreakpointManagerListener(this);
84         DebugPlugin.getDefault().getBreakpointManager().addBreakpointListener(this);
85         DebugPlugin.getDefault().addDebugEventListener(this);
86     }
87     
88     /* (non-Javadoc)
89      * @see org.eclipse.debug.core.model.IDebugTarget#getProcess()
90      */

91     public IProcess getProcess() {
92         return fProcess;
93     }
94     
95     /* (non-Javadoc)
96      * @see org.eclipse.debug.core.model.IDebugTarget#getThreads()
97      */

98     public IThread[] getThreads() {
99         return fThreads;
100     }
101     
102     /* (non-Javadoc)
103      * @see org.eclipse.debug.core.model.IDebugTarget#hasThreads()
104      */

105     public boolean hasThreads() throws DebugException {
106         return !fTerminated && fThreads.length > 0;
107     }
108     
109     /* (non-Javadoc)
110      * @see org.eclipse.debug.core.model.IDebugTarget#getName()
111      */

112     public String JavaDoc getName() throws DebugException {
113         if (fName == null) {
114             try {
115                 fName= getLaunch().getLaunchConfiguration().getAttribute(IExternalToolConstants.ATTR_LOCATION, DebugModelMessages.AntDebugTarget_0);
116                 fName= VariablesPlugin.getDefault().getStringVariableManager().performStringSubstitution(fName);
117             } catch (CoreException e) {
118                 fName = DebugModelMessages.AntDebugTarget_0;
119             }
120         }
121         return fName;
122     }
123     
124     /* (non-Javadoc)
125      * @see org.eclipse.debug.core.model.IDebugTarget#supportsBreakpoint(org.eclipse.debug.core.model.IBreakpoint)
126      */

127     public boolean supportsBreakpoint(IBreakpoint breakpoint) {
128         if (breakpoint.getModelIdentifier().equals(IAntDebugConstants.ID_ANT_DEBUG_MODEL)) {
129             //need to consider all breakpoints as no way to tell which set
130
//of buildfiles will be executed (ant task)
131
return true;
132         }
133         return false;
134     }
135     
136     /* (non-Javadoc)
137      * @see org.eclipse.debug.core.model.IDebugElement#getDebugTarget()
138      */

139     public IDebugTarget getDebugTarget() {
140         return this;
141     }
142     
143     /* (non-Javadoc)
144      * @see org.eclipse.debug.core.model.IDebugElement#getLaunch()
145      */

146     public ILaunch getLaunch() {
147         return fLaunch;
148     }
149     
150     /* (non-Javadoc)
151      * @see org.eclipse.debug.core.model.ITerminate#canTerminate()
152      */

153     public boolean canTerminate() {
154         return !fTerminated;
155     }
156     
157     /* (non-Javadoc)
158      * @see org.eclipse.debug.core.model.ITerminate#isTerminated()
159      */

160     public boolean isTerminated() {
161         return fTerminated;
162     }
163     
164     /* (non-Javadoc)
165      * @see org.eclipse.debug.core.model.ITerminate#terminate()
166      */

167     public void terminate() throws DebugException {
168         terminated();
169     }
170     
171     /* (non-Javadoc)
172      * @see org.eclipse.debug.core.model.ISuspendResume#canResume()
173      */

174     public boolean canResume() {
175         return !isTerminated() && isSuspended();
176     }
177     
178     /* (non-Javadoc)
179      * @see org.eclipse.debug.core.model.ISuspendResume#canSuspend()
180      */

181     public boolean canSuspend() {
182         return !isTerminated() && !isSuspended();
183     }
184     
185     /* (non-Javadoc)
186      * @see org.eclipse.debug.core.model.ISuspendResume#isSuspended()
187      */

188     public boolean isSuspended() {
189         return fSuspended;
190     }
191     
192     /* (non-Javadoc)
193      * @see org.eclipse.debug.core.model.ISuspendResume#resume()
194      */

195     public void resume() throws DebugException {
196         fSuspended= false;
197         fController.resume();
198     }
199     
200     /**
201      * Notification the target has suspended for the given reason
202      *
203      * @param detail reason for the suspend
204      */

205     public void suspended(int detail) {
206         fSuspended = true;
207         fThread.setStepping(false);
208         fThread.fireSuspendEvent(detail);
209     }
210     
211     /* (non-Javadoc)
212      * @see org.eclipse.debug.core.model.ISuspendResume#suspend()
213      */

214     public void suspend() throws DebugException {
215         fController.suspend();
216     }
217     
218     /* (non-Javadoc)
219      * @see org.eclipse.debug.core.IBreakpointListener#breakpointAdded(org.eclipse.debug.core.model.IBreakpoint)
220      */

221     public void breakpointAdded(IBreakpoint breakpoint) {
222         fController.handleBreakpoint(breakpoint, true);
223         if (breakpoint instanceof AntLineBreakpoint) {
224             if (((AntLineBreakpoint) breakpoint).isRunToLine()) {
225                 if (fRunToLineBreakpoints == null) {
226                     fRunToLineBreakpoints= new ArrayList JavaDoc();
227                 }
228                 fRunToLineBreakpoints.add(breakpoint);
229             }
230         }
231     }
232
233     /* (non-Javadoc)
234      * @see org.eclipse.debug.core.IBreakpointListener#breakpointRemoved(org.eclipse.debug.core.model.IBreakpoint, org.eclipse.core.resources.IMarkerDelta)
235      */

236     public void breakpointRemoved(IBreakpoint breakpoint, IMarkerDelta delta) {
237         fController.handleBreakpoint(breakpoint, false);
238         if (fRunToLineBreakpoints != null) {
239             if (fRunToLineBreakpoints.remove(breakpoint) && fRunToLineBreakpoints.isEmpty()) {
240                 fRunToLineBreakpoints= null;
241             }
242         }
243     }
244     
245     /* (non-Javadoc)
246      * @see org.eclipse.debug.core.IBreakpointListener#breakpointChanged(org.eclipse.debug.core.model.IBreakpoint, org.eclipse.core.resources.IMarkerDelta)
247      */

248     public void breakpointChanged(IBreakpoint breakpoint, IMarkerDelta delta) {
249         if (supportsBreakpoint(breakpoint)) {
250             try {
251                 if (breakpoint.isEnabled() && DebugPlugin.getDefault().getBreakpointManager().isEnabled()) {
252                     breakpointAdded(breakpoint);
253                 } else {
254                     breakpointRemoved(breakpoint, null);
255                 }
256             } catch (CoreException e) {
257             }
258         }
259     }
260     
261     /* (non-Javadoc)
262      * @see org.eclipse.debug.core.model.IDisconnect#canDisconnect()
263      */

264     public boolean canDisconnect() {
265         return false;
266     }
267     
268     /* (non-Javadoc)
269      * @see org.eclipse.debug.core.model.IDisconnect#disconnect()
270      */

271     public void disconnect() throws DebugException {
272     }
273     
274     /* (non-Javadoc)
275      * @see org.eclipse.debug.core.model.IDisconnect#isDisconnected()
276      */

277     public boolean isDisconnected() {
278         return false;
279     }
280     
281     /* (non-Javadoc)
282      * @see org.eclipse.debug.core.model.IMemoryBlockRetrieval#supportsStorageRetrieval()
283      */

284     public boolean supportsStorageRetrieval() {
285         return false;
286     }
287     
288     /* (non-Javadoc)
289      * @see org.eclipse.debug.core.model.IMemoryBlockRetrieval#getMemoryBlock(long, long)
290      */

291     public IMemoryBlock getMemoryBlock(long startAddress, long length) throws DebugException {
292         return null;
293     }
294
295     /**
296      * Notification we have connected to the Ant build logger and it has started.
297      * Resume the build.
298      */

299     public void buildStarted() {
300         fireCreationEvent();
301         installDeferredBreakpoints();
302         try {
303             resume();
304         } catch (DebugException e) {
305         }
306     }
307     
308     /**
309      * Install breakpoints that are already registered with the breakpoint
310      * manager if the breakpoint manager is enabled and the breakpoint is enabled.
311      */

312     private void installDeferredBreakpoints() {
313         IBreakpointManager manager= DebugPlugin.getDefault().getBreakpointManager();
314         if (!manager.isEnabled()) {
315             return;
316         }
317         IBreakpoint[] breakpoints = manager.getBreakpoints(IAntDebugConstants.ID_ANT_DEBUG_MODEL);
318         for (int i = 0; i < breakpoints.length; i++) {
319             IBreakpoint breakpoint= breakpoints[i];
320             try {
321                 if (breakpoint.isEnabled()) {
322                     breakpointAdded(breakpoints[i]);
323                 }
324             } catch (CoreException e) {
325             }
326         }
327     }
328     
329     /**
330      * Called when this debug target terminates.
331      */

332     protected void terminated() {
333         fThreads= new IThread[0];
334         fTerminated = true;
335         fSuspended = false;
336         if (DebugPlugin.getDefault() != null) {
337             DebugPlugin.getDefault().getBreakpointManager().removeBreakpointListener(this);
338             DebugPlugin.getDefault().removeDebugEventListener(this);
339             DebugPlugin.getDefault().getBreakpointManager().removeBreakpointManagerListener(this);
340         }
341         if (!getProcess().isTerminated()) {
342             try {
343                 fProcess.terminate();
344                 resume();
345             } catch (DebugException e) {
346             }
347         }
348         if (DebugPlugin.getDefault() != null) {
349             fireTerminateEvent();
350         }
351     }
352     
353     /**
354      * Single step the Ant build.
355      *
356      * @throws DebugException if the request fails
357      */

358     protected void stepOver() {
359         fSuspended= false;
360         fController.stepOver();
361     }
362     
363     /**
364      * Step-into the Ant build.
365      *
366      * @throws DebugException if the request fails
367      */

368     protected void stepInto() {
369         fSuspended= false;
370         fController.stepInto();
371     }
372     
373     /**
374      * Notification a breakpoint was encountered. Determine
375      * which breakpoint was hit and fire a suspend event.
376      *
377      * @param event debug event
378      */

379     protected void breakpointHit(String JavaDoc event) {
380         // determine which breakpoint was hit, and set the thread's breakpoint
381
String JavaDoc[] datum= event.split(DebugMessageIds.MESSAGE_DELIMITER);
382         String JavaDoc fileName= datum[1];
383         int lineNumber = Integer.parseInt(datum[2]);
384         IBreakpoint[] breakpoints = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints(IAntDebugConstants.ID_ANT_DEBUG_MODEL);
385         boolean found= false;
386         for (int i = 0; i < breakpoints.length; i++) {
387            ILineBreakpoint lineBreakpoint = (ILineBreakpoint)breakpoints[i];
388            if (setThreadBreakpoint(lineBreakpoint, lineNumber, fileName)) {
389                found= true;
390                break;
391            }
392         }
393         if (!found && fRunToLineBreakpoints != null) {
394             Iterator JavaDoc iter= fRunToLineBreakpoints.iterator();
395             while (iter.hasNext()) {
396                 ILineBreakpoint lineBreakpoint = (ILineBreakpoint) iter.next();
397                 if (setThreadBreakpoint(lineBreakpoint, lineNumber, fileName)) {
398                     break;
399                 }
400             }
401         }
402         suspended(DebugEvent.BREAKPOINT);
403     }
404     
405     private boolean setThreadBreakpoint(ILineBreakpoint lineBreakpoint, int lineNumber, String JavaDoc fileName) {
406         try {
407             if (lineBreakpoint.getLineNumber() == lineNumber &&
408                     fileName.equals(lineBreakpoint.getMarker().getResource().getLocation().toOSString())) {
409                 fThread.setBreakpoints(new IBreakpoint[]{lineBreakpoint});
410                 return true;
411             }
412         } catch (CoreException e) {
413         }
414         return false;
415     }
416     
417     public void breakpointHit (IBreakpoint breakpoint) {
418         fThread.setBreakpoints(new IBreakpoint[]{breakpoint});
419         suspended(DebugEvent.BREAKPOINT);
420     }
421     
422     protected void getStackFrames() {
423         fController.getStackFrames();
424     }
425     
426     protected void getProperties() {
427         fController.getProperties();
428     }
429
430     /* (non-Javadoc)
431      * @see org.eclipse.debug.core.IDebugEventSetListener#handleDebugEvents(org.eclipse.debug.core.DebugEvent[])
432      */

433     public void handleDebugEvents(DebugEvent[] events) {
434         for (int i = 0; i < events.length; i++) {
435             DebugEvent event = events[i];
436             if (event.getKind() == DebugEvent.TERMINATE && event.getSource().equals(fProcess)) {
437                 terminated();
438             }
439         }
440     }
441     
442     /**
443      * When the breakpoint manager disables, remove all registered breakpoints
444      * requests from the VM. When it enables, reinstall them.
445      *
446      * @see org.eclipse.debug.core.IBreakpointManagerListener#breakpointManagerEnablementChanged(boolean)
447      */

448     public void breakpointManagerEnablementChanged(boolean enabled) {
449         IBreakpoint[] breakpoints = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints(IAntDebugConstants.ID_ANT_DEBUG_MODEL);
450         for (int i = 0; i < breakpoints.length; i++) {
451             IBreakpoint breakpoint = breakpoints[i];
452             if (enabled) {
453                 breakpointAdded(breakpoint);
454             } else {
455                 breakpointRemoved(breakpoint, null);
456             }
457         }
458     }
459
460     public IAntDebugController getAntDebugController() {
461         return fController;
462     }
463 }
464
Popular Tags