KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > antsupport > logger > AntProcessDebugBuildLogger


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.antsupport.logger;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.apache.tools.ant.BuildEvent;
18 import org.apache.tools.ant.Location;
19 import org.eclipse.ant.internal.ui.antsupport.AntSupportMessages;
20 import org.eclipse.ant.internal.ui.antsupport.logger.util.AntDebugState;
21 import org.eclipse.ant.internal.ui.antsupport.logger.util.IDebugBuildLogger;
22 import org.eclipse.ant.internal.ui.debug.IAntDebugController;
23 import org.eclipse.ant.internal.ui.debug.model.AntDebugTarget;
24 import org.eclipse.ant.internal.ui.debug.model.AntThread;
25 import org.eclipse.ant.internal.ui.launchConfigurations.AntProcess;
26 import org.eclipse.core.resources.IFile;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.OperationCanceledException;
29 import org.eclipse.debug.core.DebugEvent;
30 import org.eclipse.debug.core.ILaunch;
31 import org.eclipse.debug.core.model.IBreakpoint;
32 import org.eclipse.debug.core.model.ILineBreakpoint;
33 import org.eclipse.debug.core.model.IProcess;
34
35 public class AntProcessDebugBuildLogger extends AntProcessBuildLogger implements IAntDebugController, IDebugBuildLogger {
36     
37     private AntDebugState fDebugState= null;
38     
39     private List JavaDoc fBreakpoints= null;
40     
41     private AntDebugTarget fAntDebugTarget;
42     private boolean fResumed= false;
43     
44     /* (non-Javadoc)
45      * @see org.apache.tools.ant.BuildListener#buildStarted(org.apache.tools.ant.BuildEvent)
46      */

47     public void buildStarted(BuildEvent event) {
48         fDebugState= new AntDebugState(this);
49         super.buildStarted(event);
50         IProcess process= getAntProcess(fProcessId);
51         ILaunch launch= process.getLaunch();
52         fAntDebugTarget= new AntDebugTarget(launch, process, this);
53         launch.addDebugTarget(fAntDebugTarget);
54         
55         fAntDebugTarget.buildStarted();
56         fDebugState.buildStarted();
57     }
58
59     /* (non-Javadoc)
60      * @see org.apache.tools.ant.BuildListener#taskFinished(org.apache.tools.ant.BuildEvent)
61      */

62     public void taskFinished(BuildEvent event) {
63         super.taskFinished(event);
64         fDebugState.taskFinished();
65     }
66     
67     /* (non-Javadoc)
68      * @see org.apache.tools.ant.BuildListener#taskStarted(org.apache.tools.ant.BuildEvent)
69      */

70     public void taskStarted(BuildEvent event) {
71         super.taskStarted(event);
72         fDebugState.taskStarted(event);
73     }
74     
75     /* (non-Javadoc)
76      * @see org.eclipse.ant.internal.ui.antsupport.logger.util.IDebugBuildLogger#waitIfSuspended()
77      */

78     public synchronized void waitIfSuspended() {
79         fResumed= false;
80         IBreakpoint breakpoint= breakpointAtLineNumber(fDebugState.getBreakpointLocation());
81         if (breakpoint != null) {
82              fAntDebugTarget.breakpointHit(breakpoint);
83              try {
84                  while (!fResumed) {
85                      wait(500);
86                      checkCancelled();
87                  }
88              } catch (InterruptedException JavaDoc e) {
89              }
90         } else if (fDebugState.getCurrentTask() != null) {
91             int detail= -1;
92             boolean shouldSuspend= true;
93             if (fDebugState.isStepIntoSuspend()) {
94                 detail= DebugEvent.STEP_END;
95                 fDebugState.setStepIntoSuspend(false);
96             } else if ((fDebugState.getLastTaskFinished() != null && fDebugState.getLastTaskFinished() == fDebugState.getStepOverTask()) || fDebugState.shouldSuspend()) {
97                 detail= DebugEvent.STEP_END;
98                 fDebugState.setShouldSuspend(false);
99                 fDebugState.setStepOverTask(null);
100             } else if (fDebugState.isClientSuspend()) {
101                 detail= DebugEvent.CLIENT_REQUEST;
102                 fDebugState.setClientSuspend(false);
103             } else {
104                 shouldSuspend= false;
105             }
106             if (shouldSuspend) {
107                 fAntDebugTarget.suspended(detail);
108                 try {
109                     while (!fResumed) {
110                         wait(500);
111                         checkCancelled();
112                     }
113                 } catch (InterruptedException JavaDoc e) {
114                 }
115             }
116         }
117     }
118
119     private void checkCancelled() {
120         AntProcess process= getAntProcess(fProcessId);
121         if (process != null && process.isCanceled()) {
122             throw new OperationCanceledException(AntSupportMessages.AntProcessDebugBuildLogger_1);
123         }
124     }
125
126     /* (non-Javadoc)
127      * @see org.eclipse.ant.internal.ui.debug.IAntDebugController#resume()
128      */

129     public synchronized void resume() {
130         fResumed= true;
131         notifyAll();
132     }
133
134     /* (non-Javadoc)
135      * @see org.eclipse.ant.internal.ui.debug.IAntDebugController#suspend()
136      */

137     public synchronized void suspend() {
138         fDebugState.setClientSuspend(true);
139     }
140
141     /* (non-Javadoc)
142      * @see org.eclipse.ant.internal.ui.debug.IAntDebugController#stepInto()
143      */

144     public synchronized void stepInto() {
145         fDebugState.setStepIntoSuspend(true);
146         fResumed= true;
147         notifyAll();
148     }
149
150     /* (non-Javadoc)
151      * @see org.eclipse.ant.internal.ui.debug.IAntDebugController#stepOver()
152      */

153     public synchronized void stepOver() {
154         fResumed= true;
155         fDebugState.stepOver();
156     }
157
158     /* (non-Javadoc)
159      * @see org.eclipse.ant.internal.ui.debug.IAntDebugController#handleBreakpoint(org.eclipse.debug.core.model.IBreakpoint, boolean)
160      */

161     public void handleBreakpoint(IBreakpoint breakpoint, boolean added) {
162         if (added) {
163             if (fBreakpoints == null) {
164                 fBreakpoints= new ArrayList JavaDoc();
165             }
166             if (!fBreakpoints.contains(breakpoint)) {
167                 fBreakpoints.add(breakpoint);
168             }
169         } else {
170             if (fBreakpoints != null) {
171                 fBreakpoints.remove(breakpoint);
172             }
173         }
174     }
175
176     /* (non-Javadoc)
177      * @see org.eclipse.ant.internal.ui.debug.IAntDebugController#getProperties()
178      */

179     public void getProperties() {
180         if (!fAntDebugTarget.isSuspended()) {
181             return;
182         }
183         StringBuffer JavaDoc propertiesRepresentation= new StringBuffer JavaDoc();
184         fDebugState.marshallProperties(propertiesRepresentation, false);
185         if (fAntDebugTarget.getThreads().length > 0) {
186             ((AntThread) fAntDebugTarget.getThreads()[0]).newProperties(propertiesRepresentation.toString());
187         }
188     }
189
190     /* (non-Javadoc)
191      * @see org.eclipse.ant.internal.ui.debug.IAntDebugController#getStackFrames()
192      */

193     public void getStackFrames() {
194         StringBuffer JavaDoc stackRepresentation= new StringBuffer JavaDoc();
195         fDebugState.marshalStack(stackRepresentation);
196         ((AntThread) fAntDebugTarget.getThreads()[0]).buildStack(stackRepresentation.toString());
197     }
198     
199     private IBreakpoint breakpointAtLineNumber(Location location) {
200         if (fBreakpoints == null || location == null || location == Location.UNKNOWN_LOCATION) {
201             return null;
202         }
203         int lineNumber= fDebugState.getLineNumber(location);
204         File JavaDoc locationFile= new File JavaDoc(fDebugState.getFileName(location));
205         for (int i = 0; i < fBreakpoints.size(); i++) {
206             ILineBreakpoint breakpoint = (ILineBreakpoint) fBreakpoints.get(i);
207             int breakpointLineNumber;
208             try {
209                 if (!breakpoint.isEnabled()) {
210                     continue;
211                 }
212                 breakpointLineNumber = breakpoint.getLineNumber();
213             } catch (CoreException e) {
214                return null;
215             }
216             IFile resource= (IFile) breakpoint.getMarker().getResource();
217             if (breakpointLineNumber == lineNumber && resource.getLocation().toFile().equals(locationFile)) {
218                 return breakpoint;
219             }
220         }
221         return null;
222     }
223     
224     /* (non-Javadoc)
225      * @see org.apache.tools.ant.BuildListener#targetStarted(org.apache.tools.ant.BuildEvent)
226      */

227     public void targetStarted(BuildEvent event) {
228         fDebugState.targetStarted(event);
229         waitIfSuspended();
230         super.targetStarted(event);
231     }
232     
233     /* (non-Javadoc)
234      * @see org.apache.tools.ant.BuildListener#targetFinished(org.apache.tools.ant.BuildEvent)
235      */

236     public void targetFinished(BuildEvent event) {
237         super.targetFinished(event);
238         fDebugState.setTargetExecuting(null);
239     }
240
241     /* (non-Javadoc)
242      * @see org.eclipse.ant.internal.ui.debug.IAntDebugController#unescapeString(java.lang.StringBuffer)
243      */

244     public StringBuffer JavaDoc unescapeString(StringBuffer JavaDoc propertyValue) {
245         return propertyValue;
246     }
247 }
248
Popular Tags