KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
15
16 import org.eclipse.debug.core.DebugEvent;
17 import org.eclipse.debug.core.DebugException;
18 import org.eclipse.debug.core.model.IBreakpoint;
19 import org.eclipse.debug.core.model.IStackFrame;
20 import org.eclipse.debug.core.model.IThread;
21 import org.eclipse.debug.core.model.IVariable;
22
23 /**
24  * An Ant build thread.
25  */

26 public class AntThread extends AntDebugElement implements IThread {
27     
28     /**
29      * Breakpoints this thread is suspended at or <code>null</code>
30      * if none.
31      */

32     private IBreakpoint[] fBreakpoints;
33     
34     /**
35      * The stackframes associated with this thread
36      */

37     private List JavaDoc fFrames= new ArrayList JavaDoc(1);
38     
39     /**
40      * The stackframes to be reused on suspension
41      */

42     private List JavaDoc fOldFrames;
43     
44     /**
45      * Whether this thread is stepping
46      */

47     private boolean fStepping = false;
48     
49     private boolean fRefreshProperties= true;
50     
51     /**
52      * The user properties associated with this thread
53      */

54     private AntProperties fUserProperties;
55     
56     /**
57      * The system properties associated with this thread
58      */

59     private AntProperties fSystemProperties;
60     
61     /**
62      * The properties set during the build associated with this thread
63      */

64     private AntProperties fRuntimeProperties;
65     
66     private Object JavaDoc fPropertiesLock= new Object JavaDoc();
67     
68     /**
69      * Constructs a new thread for the given target
70      *
71      * @param target the Ant Build
72      */

73     public AntThread(AntDebugTarget target) {
74         super(target);
75     }
76     
77     /* (non-Javadoc)
78      * @see org.eclipse.debug.core.model.IThread#getStackFrames()
79      */

80     public synchronized IStackFrame[] getStackFrames() throws DebugException {
81         if (isSuspended()) {
82             if (fFrames.size() == 0) {
83                 getStackFrames0();
84             }
85         }
86         
87         return (IStackFrame[]) fFrames.toArray(new IStackFrame[fFrames.size()]);
88     }
89     
90     /**
91      * Retrieves the current stack frames in the thread
92      * possibly waiting until the frames are populated
93      *
94      */

95     private void getStackFrames0() throws DebugException {
96         synchronized (fFrames) {
97             getAntDebugTarget().getStackFrames();
98             if (fFrames.size() > 0) {
99                 //frames set..no need to wait
100
return;
101             }
102             int attempts= 0;
103             try {
104                 while (fFrames.size() == 0 && !isTerminated()) {
105                     fFrames.wait(50);
106                     if (attempts == 20 && fFrames.size() == 0 && !isTerminated()) {
107                         throwDebugException(DebugModelMessages.AntThread_3);
108                     }
109                     attempts++;
110                 }
111             } catch (InterruptedException JavaDoc e) {
112             }
113         }
114     }
115     
116     /* (non-Javadoc)
117      * @see org.eclipse.debug.core.model.IThread#hasStackFrames()
118      */

119     public boolean hasStackFrames() throws DebugException {
120         return isSuspended();
121     }
122     
123     /* (non-Javadoc)
124      * @see org.eclipse.debug.core.model.IThread#getPriority()
125      */

126     public int getPriority() throws DebugException {
127         return 0;
128     }
129     
130     /* (non-Javadoc)
131      * @see org.eclipse.debug.core.model.IThread#getTopStackFrame()
132      */

133     public synchronized IStackFrame getTopStackFrame() throws DebugException {
134         if (isSuspended()) {
135             if (fFrames.size() == 0) {
136                 getStackFrames0();
137             }
138             if (fFrames.size() > 0) {
139                 return (IStackFrame)fFrames.get(0);
140             }
141         }
142         return null;
143     }
144     
145     /* (non-Javadoc)
146      * @see org.eclipse.debug.core.model.IThread#getName()
147      */

148     public String JavaDoc getName() {
149         return "Thread [Ant Build]"; //$NON-NLS-1$
150
}
151     
152     /* (non-Javadoc)
153      * @see org.eclipse.debug.core.model.IThread#getBreakpoints()
154      */

155     public IBreakpoint[] getBreakpoints() {
156         if (fBreakpoints == null) {
157             return new IBreakpoint[0];
158         }
159         return fBreakpoints;
160     }
161     
162     /**
163      * Sets the breakpoints this thread is suspended at, or <code>null</code>
164      * if none.
165      *
166      * @param breakpoints the breakpoints this thread is suspended at, or <code>null</code>
167      * if none
168      */

169     protected void setBreakpoints(IBreakpoint[] breakpoints) {
170         fBreakpoints = breakpoints;
171     }
172     
173     /* (non-Javadoc)
174      * @see org.eclipse.debug.core.model.ISuspendResume#canResume()
175      */

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

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

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

197     public synchronized void resume() throws DebugException {
198         aboutToResume(DebugEvent.CLIENT_REQUEST, false);
199         getDebugTarget().resume();
200     }
201     
202     /* (non-Javadoc)
203      * @see org.eclipse.debug.core.model.ISuspendResume#suspend()
204      */

205     public synchronized void suspend() throws DebugException {
206         getDebugTarget().suspend();
207     }
208     
209     /* (non-Javadoc)
210      * @see org.eclipse.debug.core.model.IStep#canStepInto()
211      */

212     public boolean canStepInto() {
213         return isSuspended();
214     }
215     
216     /* (non-Javadoc)
217      * @see org.eclipse.debug.core.model.IStep#canStepOver()
218      */

219     public boolean canStepOver() {
220         return isSuspended();
221     }
222     
223     /* (non-Javadoc)
224      * @see org.eclipse.debug.core.model.IStep#canStepReturn()
225      */

226     public boolean canStepReturn() {
227         return false;
228     }
229     
230     /* (non-Javadoc)
231      * @see org.eclipse.debug.core.model.IStep#isStepping()
232      */

233     public boolean isStepping() {
234         return fStepping;
235     }
236     
237     /* (non-Javadoc)
238      * @see org.eclipse.debug.core.model.IStep#stepInto()
239      */

240     public synchronized void stepInto() throws DebugException {
241         aboutToResume(DebugEvent.STEP_INTO, true);
242         ((AntDebugTarget)getDebugTarget()).stepInto();
243     }
244     
245     private void aboutToResume(int detail, boolean stepping) {
246         fRefreshProperties= true;
247         fOldFrames= new ArrayList JavaDoc(fFrames);
248         fFrames.clear();
249         setPropertiesValid(false);
250         setStepping(stepping);
251         setBreakpoints(null);
252         fireResumeEvent(detail);
253     }
254
255     private void setPropertiesValid(boolean valid) {
256         if (fUserProperties != null) {
257             fUserProperties.setValid(valid);
258             fSystemProperties.setValid(valid);
259             fRuntimeProperties.setValid(valid);
260         }
261     }
262
263     /* (non-Javadoc)
264      * @see org.eclipse.debug.core.model.IStep#stepOver()
265      */

266     public synchronized void stepOver() throws DebugException {
267         aboutToResume(DebugEvent.STEP_OVER, true);
268         ((AntDebugTarget)getDebugTarget()).stepOver();
269     }
270     
271     /* (non-Javadoc)
272      * @see org.eclipse.debug.core.model.IStep#stepReturn()
273      */

274     public synchronized void stepReturn() throws DebugException {
275     }
276     
277     /* (non-Javadoc)
278      * @see org.eclipse.debug.core.model.ITerminate#canTerminate()
279      */

280     public boolean canTerminate() {
281         return !isTerminated();
282     }
283     
284     /* (non-Javadoc)
285      * @see org.eclipse.debug.core.model.ITerminate#isTerminated()
286      */

287     public boolean isTerminated() {
288         return getDebugTarget().isTerminated();
289     }
290     
291     /* (non-Javadoc)
292      * @see org.eclipse.debug.core.model.ITerminate#terminate()
293      */

294     public void terminate() throws DebugException {
295         fFrames.clear();
296         getDebugTarget().terminate();
297     }
298     
299     /**
300      * Sets whether this thread is stepping
301      *
302      * @param stepping whether stepping
303      */

304     protected void setStepping(boolean stepping) {
305         fStepping = stepping;
306     }
307
308     public void buildStack(String JavaDoc data) {
309         synchronized (fFrames) {
310             String JavaDoc[] strings= data.split(DebugMessageIds.MESSAGE_DELIMITER);
311             //0 STACK message
312
//1 targetName
313
//2 taskName
314
//3 filePath
315
//4 lineNumber
316
//5 ...
317
if (fOldFrames != null && (strings.length - 1)/ 4 != fOldFrames.size()) {
318                 fOldFrames= null; //stack size changed..do not preserve
319
}
320             StringBuffer JavaDoc name;
321             String JavaDoc filePath;
322             int lineNumber;
323             int stackFrameId= 0;
324             String JavaDoc taskName;
325             for (int i = 1; i < strings.length; i++) {
326                 if (strings[i].length() > 0) {
327                     name= new StringBuffer JavaDoc(strings[i]);
328                     taskName= strings[++i];
329                     if (taskName.length() > 0) {
330                         name.append(": "); //$NON-NLS-1$
331
name.append(taskName);
332                     }
333                 } else {
334                     name= new StringBuffer JavaDoc(strings[++i]);
335                 }
336                 filePath= strings[++i];
337                 lineNumber= Integer.parseInt(strings[++i]);
338                 addFrame(stackFrameId++, name.toString(), filePath, lineNumber);
339             }
340             //wake up the call from getStackFrames
341
fFrames.notifyAll();
342         }
343     }
344     
345     private void addFrame(int stackFrameId, String JavaDoc name, String JavaDoc filePath, int lineNumber) {
346         AntStackFrame frame= getOldFrame();
347         
348         if (frame == null || !frame.getFilePath().equals(filePath)) {
349             frame= new AntStackFrame(this, stackFrameId, name, filePath, lineNumber);
350         } else {
351             frame.setFilePath(filePath);
352             frame.setId(stackFrameId);
353             frame.setLineNumber(lineNumber);
354             frame.setName(name);
355         }
356         fFrames.add(frame);
357     }
358     
359     private AntStackFrame getOldFrame() {
360         if (fOldFrames == null) {
361             return null;
362         }
363         AntStackFrame frame= (AntStackFrame) fOldFrames.remove(0);
364         if (fOldFrames.isEmpty()) {
365             fOldFrames= null;
366         }
367         return frame;
368     }
369     
370     public void newProperties(String JavaDoc data) {
371         synchronized (fPropertiesLock) {
372             try {
373                 String JavaDoc[] datum= data.split(DebugMessageIds.MESSAGE_DELIMITER);
374                 if (fUserProperties == null) {
375                     initializePropertyGroups();
376                 }
377
378                 List JavaDoc userProperties= ((AntPropertiesValue)fUserProperties.getLastValue()).getProperties();
379                 List JavaDoc systemProperties= ((AntPropertiesValue)fSystemProperties.getLastValue()).getProperties();
380                 List JavaDoc runtimeProperties= ((AntPropertiesValue)fRuntimeProperties.getLastValue()).getProperties();
381                 //0 PROPERTIES message
382
//1 propertyName length
383
//2 propertyName
384
//3 propertyValue length
385
//3 propertyValue
386
//4 propertyType
387
//5 ...
388
if (datum.length > 1) { //new properties
389
StringBuffer JavaDoc propertyName;
390                     StringBuffer JavaDoc propertyValue;
391                     int propertyNameLength;
392                     int propertyValueLength;
393                     for (int i = 1; i < datum.length; i++) {
394                         propertyNameLength= Integer.parseInt(datum[i]);
395                         propertyName= new StringBuffer JavaDoc(datum[++i]);
396                         while (propertyName.length() != propertyNameLength) {
397                             propertyName.append(DebugMessageIds.MESSAGE_DELIMITER);
398                             propertyName.append(datum[++i]);
399                         }
400
401                         propertyName= getAntDebugTarget().getAntDebugController().unescapeString(propertyName);
402
403                         propertyValueLength= Integer.parseInt(datum[++i]);
404                         if (propertyValueLength == 0 && i + 1 == datum.length) { //bug 81299
405
propertyValue= new StringBuffer JavaDoc(""); //$NON-NLS-1$
406
} else {
407                             propertyValue= new StringBuffer JavaDoc(datum[++i]);
408                         }
409                         while (propertyValue.length() != propertyValueLength) {
410                             propertyValue.append(DebugMessageIds.MESSAGE_DELIMITER);
411                             propertyValue.append(datum[++i]);
412                         }
413
414                         propertyValue= getAntDebugTarget().getAntDebugController().unescapeString(propertyValue);
415
416                         int propertyType= Integer.parseInt(datum[++i]);
417                         addProperty(userProperties, systemProperties, runtimeProperties, propertyName.toString(), propertyValue.toString(), propertyType);
418                     }
419                 }
420             } finally {
421                 fRefreshProperties= false;
422                 setPropertiesValid(true);
423                 //wake up the call from getVariables
424
fPropertiesLock.notifyAll();
425             }
426         }
427     }
428
429     private void addProperty(List JavaDoc userProperties, List JavaDoc systemProperties, List JavaDoc runtimeProperties, String JavaDoc propertyName, String JavaDoc propertyValue, int propertyType) {
430         AntProperty property= new AntProperty((AntDebugTarget) getDebugTarget(), propertyName, propertyValue);
431         switch (propertyType) {
432             case DebugMessageIds.PROPERTY_SYSTEM:
433                 systemProperties.add(property);
434                 break;
435             case DebugMessageIds.PROPERTY_USER:
436                 userProperties.add(property);
437                 break;
438             case DebugMessageIds.PROPERTY_RUNTIME:
439                 runtimeProperties.add(property);
440                 break;
441         }
442     }
443
444     private void initializePropertyGroups() {
445         AntDebugTarget target= getAntDebugTarget();
446         fUserProperties= new AntProperties(target, DebugModelMessages.AntThread_0);
447         fUserProperties.setValue(new AntPropertiesValue(target));
448         fSystemProperties= new AntProperties(target, DebugModelMessages.AntThread_1);
449         fSystemProperties.setValue(new AntPropertiesValue(target));
450         fRuntimeProperties= new AntProperties(target, DebugModelMessages.AntThread_2);
451         fRuntimeProperties.setValue(new AntPropertiesValue(target));
452     }
453     
454     protected IVariable[] getVariables() throws DebugException {
455         synchronized (fPropertiesLock) {
456             if (fRefreshProperties) {
457                 getAntDebugTarget().getProperties();
458                 if (fRefreshProperties) {
459                     //properties have not been set; need to wait
460
try {
461                         int attempts= 0;
462                         while (fRefreshProperties && !isTerminated()) {
463                             fPropertiesLock.wait(50);
464                             if (attempts == 20 && fRefreshProperties && !isTerminated()) {
465                                 throwDebugException(DebugModelMessages.AntThread_4);
466                             }
467                             attempts++;
468                         }
469                     } catch (InterruptedException JavaDoc ie) {
470                     }
471                 }
472             }
473             if (fSystemProperties == null) {
474                 return new IVariable[0];
475             }
476             return new IVariable[]{fSystemProperties, fUserProperties, fRuntimeProperties};
477         }
478     }
479 }
Popular Tags