1 11 package org.eclipse.ant.internal.ui.debug.model; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 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 26 public class AntThread extends AntDebugElement implements IThread { 27 28 32 private IBreakpoint[] fBreakpoints; 33 34 37 private List fFrames= new ArrayList (1); 38 39 42 private List fOldFrames; 43 44 47 private boolean fStepping = false; 48 49 private boolean fRefreshProperties= true; 50 51 54 private AntProperties fUserProperties; 55 56 59 private AntProperties fSystemProperties; 60 61 64 private AntProperties fRuntimeProperties; 65 66 private Object fPropertiesLock= new Object (); 67 68 73 public AntThread(AntDebugTarget target) { 74 super(target); 75 } 76 77 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 95 private void getStackFrames0() throws DebugException { 96 synchronized (fFrames) { 97 getAntDebugTarget().getStackFrames(); 98 if (fFrames.size() > 0) { 99 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 e) { 112 } 113 } 114 } 115 116 119 public boolean hasStackFrames() throws DebugException { 120 return isSuspended(); 121 } 122 123 126 public int getPriority() throws DebugException { 127 return 0; 128 } 129 130 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 148 public String getName() { 149 return "Thread [Ant Build]"; } 151 152 155 public IBreakpoint[] getBreakpoints() { 156 if (fBreakpoints == null) { 157 return new IBreakpoint[0]; 158 } 159 return fBreakpoints; 160 } 161 162 169 protected void setBreakpoints(IBreakpoint[] breakpoints) { 170 fBreakpoints = breakpoints; 171 } 172 173 176 public boolean canResume() { 177 return isSuspended(); 178 } 179 180 183 public boolean canSuspend() { 184 return !isSuspended(); 185 } 186 187 190 public boolean isSuspended() { 191 return getDebugTarget().isSuspended(); 192 } 193 194 197 public synchronized void resume() throws DebugException { 198 aboutToResume(DebugEvent.CLIENT_REQUEST, false); 199 getDebugTarget().resume(); 200 } 201 202 205 public synchronized void suspend() throws DebugException { 206 getDebugTarget().suspend(); 207 } 208 209 212 public boolean canStepInto() { 213 return isSuspended(); 214 } 215 216 219 public boolean canStepOver() { 220 return isSuspended(); 221 } 222 223 226 public boolean canStepReturn() { 227 return false; 228 } 229 230 233 public boolean isStepping() { 234 return fStepping; 235 } 236 237 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 (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 266 public synchronized void stepOver() throws DebugException { 267 aboutToResume(DebugEvent.STEP_OVER, true); 268 ((AntDebugTarget)getDebugTarget()).stepOver(); 269 } 270 271 274 public synchronized void stepReturn() throws DebugException { 275 } 276 277 280 public boolean canTerminate() { 281 return !isTerminated(); 282 } 283 284 287 public boolean isTerminated() { 288 return getDebugTarget().isTerminated(); 289 } 290 291 294 public void terminate() throws DebugException { 295 fFrames.clear(); 296 getDebugTarget().terminate(); 297 } 298 299 304 protected void setStepping(boolean stepping) { 305 fStepping = stepping; 306 } 307 308 public void buildStack(String data) { 309 synchronized (fFrames) { 310 String [] strings= data.split(DebugMessageIds.MESSAGE_DELIMITER); 311 if (fOldFrames != null && (strings.length - 1)/ 4 != fOldFrames.size()) { 318 fOldFrames= null; } 320 StringBuffer name; 321 String filePath; 322 int lineNumber; 323 int stackFrameId= 0; 324 String taskName; 325 for (int i = 1; i < strings.length; i++) { 326 if (strings[i].length() > 0) { 327 name= new StringBuffer (strings[i]); 328 taskName= strings[++i]; 329 if (taskName.length() > 0) { 330 name.append(": "); name.append(taskName); 332 } 333 } else { 334 name= new StringBuffer (strings[++i]); 335 } 336 filePath= strings[++i]; 337 lineNumber= Integer.parseInt(strings[++i]); 338 addFrame(stackFrameId++, name.toString(), filePath, lineNumber); 339 } 340 fFrames.notifyAll(); 342 } 343 } 344 345 private void addFrame(int stackFrameId, String name, String 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 data) { 371 synchronized (fPropertiesLock) { 372 try { 373 String [] datum= data.split(DebugMessageIds.MESSAGE_DELIMITER); 374 if (fUserProperties == null) { 375 initializePropertyGroups(); 376 } 377 378 List userProperties= ((AntPropertiesValue)fUserProperties.getLastValue()).getProperties(); 379 List systemProperties= ((AntPropertiesValue)fSystemProperties.getLastValue()).getProperties(); 380 List runtimeProperties= ((AntPropertiesValue)fRuntimeProperties.getLastValue()).getProperties(); 381 if (datum.length > 1) { StringBuffer propertyName; 390 StringBuffer 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 (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) { propertyValue= new StringBuffer (""); } else { 407 propertyValue= new StringBuffer (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 fPropertiesLock.notifyAll(); 425 } 426 } 427 } 428 429 private void addProperty(List userProperties, List systemProperties, List runtimeProperties, String propertyName, String 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 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 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 |