1 18 19 package org.apache.tools.ant; 20 21 import org.apache.tools.ant.dispatch.DispatchUtils; 22 23 import java.util.Enumeration ; 24 import java.io.IOException ; 25 26 34 public abstract class Task extends ProjectComponent { 35 42 protected Target target; 43 44 55 protected String taskName; 56 57 64 protected String taskType; 65 66 73 protected RuntimeConfigurable wrapper; 74 75 77 82 private boolean invalid; 83 84 85 public Task() { 86 } 87 88 94 public void setOwningTarget(Target target) { 95 this.target = target; 96 } 97 98 104 public Target getOwningTarget() { 105 return target; 106 } 107 108 114 public void setTaskName(String name) { 115 this.taskName = name; 116 } 117 118 123 public String getTaskName() { 124 return taskName; 125 } 126 127 133 public void setTaskType(String type) { 134 this.taskType = type; 135 } 136 137 143 public void init() throws BuildException { 144 } 145 146 155 public void execute() throws BuildException { 156 } 157 158 165 public RuntimeConfigurable getRuntimeConfigurableWrapper() { 166 if (wrapper == null) { 167 wrapper = new RuntimeConfigurable(this, getTaskName()); 168 } 169 return wrapper; 170 } 171 172 184 public void setRuntimeConfigurableWrapper(RuntimeConfigurable wrapper) { 185 this.wrapper = wrapper; 186 } 187 188 199 public void maybeConfigure() throws BuildException { 200 if (!invalid) { 201 if (wrapper != null) { 202 wrapper.maybeConfigure(getProject()); 203 } 204 } else { 205 getReplacement(); 206 } 207 } 208 209 212 public void reconfigure() { 213 if (wrapper != null) { 214 wrapper.reconfigure(getProject()); 215 } 216 } 217 218 223 protected void handleOutput(String output) { 224 log(output, Project.MSG_INFO); 225 } 226 227 234 protected void handleFlush(String output) { 235 handleOutput(output); 236 } 237 238 250 protected int handleInput(byte[] buffer, int offset, int length) 251 throws IOException { 252 return getProject().defaultInput(buffer, offset, length); 253 } 254 255 260 protected void handleErrorOutput(String output) { 261 log(output, Project.MSG_WARN); 262 } 263 264 271 protected void handleErrorFlush(String output) { 272 handleErrorOutput(output); 273 } 274 275 280 public void log(String msg) { 281 log(msg, Project.MSG_INFO); 282 } 283 284 292 public void log(String msg, int msgLevel) { 293 if (getProject() != null) { 294 getProject().log(this, msg, msgLevel); 295 } else { 296 super.log(msg, msgLevel); 297 } 298 } 299 300 309 public void log(Throwable t, int msgLevel) { 310 if (t != null) { 311 log(t.getMessage(), t, msgLevel); 312 } 313 } 314 315 325 public void log(String msg, Throwable t, int msgLevel) { 326 if (getProject() != null) { 327 getProject().log(this, msg, t, msgLevel); 328 } else { 329 super.log(msg, msgLevel); 330 } 331 } 332 333 342 public final void perform() { 343 if (!invalid) { 344 getProject().fireTaskStarted(this); 345 Throwable reason = null; 346 try { 347 maybeConfigure(); 348 DispatchUtils.execute(this); 349 } catch (BuildException ex) { 350 if (ex.getLocation() == Location.UNKNOWN_LOCATION) { 351 ex.setLocation(getLocation()); 352 } 353 reason = ex; 354 throw ex; 355 } catch (Exception ex) { 356 reason = ex; 357 BuildException be = new BuildException(ex); 358 be.setLocation(getLocation()); 359 throw be; 360 } catch (Error ex) { 361 reason = ex; 362 throw ex; 363 } finally { 364 getProject().fireTaskFinished(this, reason); 365 } 366 } else { 367 UnknownElement ue = getReplacement(); 368 Task task = ue.getTask(); 369 task.perform(); 370 } 371 } 372 373 377 final void markInvalid() { 378 invalid = true; 379 } 380 381 389 protected final boolean isInvalid() { 390 return invalid; 391 } 392 393 396 private UnknownElement replacement; 397 398 405 private UnknownElement getReplacement() { 406 if (replacement == null) { 407 replacement = new UnknownElement(taskType); 408 replacement.setProject(getProject()); 409 replacement.setTaskType(taskType); 410 replacement.setTaskName(taskName); 411 replacement.setLocation(location); 412 replacement.setOwningTarget(target); 413 replacement.setRuntimeConfigurableWrapper(wrapper); 414 wrapper.setProxy(replacement); 415 replaceChildren(wrapper, replacement); 416 target.replaceChild(this, replacement); 417 replacement.maybeConfigure(); 418 } 419 return replacement; 420 } 421 422 428 private void replaceChildren(RuntimeConfigurable wrapper, 429 UnknownElement parentElement) { 430 Enumeration e = wrapper.getChildren(); 431 while (e.hasMoreElements()) { 432 RuntimeConfigurable childWrapper = 433 (RuntimeConfigurable) e.nextElement(); 434 UnknownElement childElement = 435 new UnknownElement(childWrapper.getElementTag()); 436 parentElement.addChild(childElement); 437 childElement.setProject(getProject()); 438 childElement.setRuntimeConfigurableWrapper(childWrapper); 439 childWrapper.setProxy(childElement); 440 replaceChildren(childWrapper, childElement); 441 } 442 } 443 444 449 public String getTaskType() { 450 return taskType; 451 } 452 453 458 protected RuntimeConfigurable getWrapper() { 459 return wrapper; 460 } 461 462 474 public final void bindToOwner(Task owner) { 475 setProject(owner.getProject()); 476 setOwningTarget(owner.getOwningTarget()); 477 setTaskName(owner.getTaskName()); 478 setDescription(owner.getDescription()); 479 setLocation(owner.getLocation()); 480 setTaskType(owner.getTaskType()); 481 } 482 } 483 | Popular Tags |