1 28 29 package com.idaremedia.antx.flowcontrol.call; 30 31 import java.util.Iterator ; 32 import java.util.List ; 33 34 import org.apache.tools.ant.BuildException; 35 import org.apache.tools.ant.Project; 36 import org.apache.tools.ant.Task; 37 38 import com.idaremedia.antx.AntX; 39 import com.idaremedia.antx.AntXFixture; 40 import com.idaremedia.antx.AssertableTask; 41 import com.idaremedia.antx.ExportedProperties; 42 import com.idaremedia.antx.helpers.Strings; 43 import com.idaremedia.antx.ownhelpers.TaskExaminer; 44 import com.idaremedia.antx.parameters.RecoveryEnabled; 45 46 72 73 public abstract class CallerTask extends AssertableTask 74 implements RecoveryEnabled 75 { 76 79 protected CallerTask() 80 { 81 super(AntX.flow+"call"); 82 } 83 84 85 86 90 protected CallerTask(String iam) 91 { 92 super(iam); 93 } 94 95 96 102 public void maybeConfigure() 103 throws BuildException 104 { 105 if (!TaskExaminer.ignoreConfigure(this)) { super.maybeConfigure(); 107 } 108 } 109 110 111 115 119 public void setFailProperty(String property) 120 { 121 require_(property!=null,"setFailProperty- nonzro name"); 122 m_failProperty = property; 123 } 124 125 126 131 public final String getFailProperty() 132 { 133 return m_failProperty; 134 } 135 136 137 143 public void setFailVariable(String variable) 144 { 145 require_(variable!=null,"setFailVariable- nonzro name"); 146 m_failVariable = variable; 147 } 148 149 150 151 156 public final String getFailVariable() 157 { 158 return m_failVariable; 159 } 160 161 162 168 public void setFailValue(String value) 169 { 170 require_(value!=null,"setFailValu- nonzro valu"); 171 m_failValue = value; 172 } 173 174 175 180 public final String getFailValue() 181 { 182 return m_failValue; 183 } 184 185 186 187 192 public void setHaltIfError(boolean balk) 193 { 194 m_haltIfError = balk; 195 } 196 197 198 202 public final boolean isHaltIfError() 203 { 204 return m_haltIfError; 205 } 206 207 208 212 public void setTryEach(boolean tryEach) 213 { 214 m_tryEach = tryEach; 215 } 216 217 218 223 public final boolean isTryEach() 224 { 225 return m_tryEach; 226 } 227 228 229 233 234 239 protected OverlayParametersHelper getParametersKeeper() 240 { 241 if (m_paramsHelper==null) { 242 m_paramsHelper= new OverlayParametersHelper(getProject()); 243 } 244 return m_paramsHelper; 245 } 246 247 248 249 254 protected final boolean haveOverlayParameters() 255 { 256 if (m_paramsHelper==null) { 257 return false; 258 } 259 return !m_paramsHelper.getParameters().isEmpty(); 260 } 261 262 263 264 269 protected final void transferOverlayParameters(TargetCaller caller) 270 { 271 if (m_paramsHelper!=null) { 272 m_paramsHelper.transferParameters(caller); 273 } 274 } 275 276 277 278 286 protected abstract List copyOfOrderedTargetCallers() 287 throws BuildException; 288 289 290 291 298 protected void runTargetCallers(TargetCaller.RunSelector selector) 299 throws BuildException 300 { 301 List callers = copyOfOrderedTargetCallers(); 302 verify_(callers!=null,"runcalrs- nonzro calrs to run"); 303 304 if (!callers.isEmpty()) { 305 Iterator itr= callers.listIterator(); 306 boolean started=false; 307 TargetCaller caller=null; 308 309 while (itr.hasNext()) { 310 caller = (TargetCaller)itr.next(); 311 try { 312 313 callerStarted(caller); 314 started=true; 315 log("Caller for '"+callerName(caller)+ 316 "' about to run.",Project.MSG_DEBUG); 317 318 if (selector!=null) { 319 selector.run(caller); 320 } else { 321 caller.run(); 322 } 323 324 started=false; 325 callerFinished(caller,null); 326 327 } catch(RuntimeException rtX) { 328 callerFailed(caller, rtX, callers); 329 if (started) { 330 callerFinished(caller,rtX); 331 } 332 if (isHaltIfError()) { 333 throw rtX; 334 } else if (!isTryEach()) { 335 break; 336 } 337 } 338 } 340 callers.clear(); 341 342 } } 344 345 346 347 353 public void execute() throws BuildException 354 { 355 verifyCanExecute_("execute"); 356 runTargetCallers(null); 357 } 358 359 360 361 366 protected void callerStarted(TargetCaller caller) 367 { 368 } 370 371 372 373 379 protected void callerFinished(TargetCaller caller, Throwable cause) 380 { 381 } 383 384 385 386 394 protected void callerFailed(TargetCaller caller, Throwable failure, 395 List callers) 396 { 397 String targetName = callerName(caller); 398 399 if (getMsgId()!=null) { 400 log(getMsg(newMsgGetter(targetName)), Project.MSG_ERR); 401 } else { 402 log(uistrs().get("flow.runsteps.failure",getTaskName(),targetName), 403 Project.MSG_ERR); 404 } 405 406 String what = getFailValue(); 407 408 if (getFailProperty()!=null) { 409 checkIfProperty_(getFailProperty(),true); 410 getProject().setNewProperty(getFailProperty(), what); 411 } 412 if (getFailVariable()!=null) { 413 ExportedProperties.set(getFailVariable(), what); 414 } 415 } 416 417 418 422 423 428 protected boolean targetExists(String targetName) 429 { 430 return getProject().getTargets().get(targetName)!=null; 431 } 432 433 434 440 protected Task[] getAllPossibleTasks() 441 { 442 return getOwningTarget().getTasks(); 443 } 444 445 446 447 462 protected List getAllPossibleStepsLike(Class thisClass) 463 { 464 require_(InlineStep.class.isAssignableFrom(thisClass), 465 "getStpsLik- kindof(inlineStep)"); 466 Task[] tasks = getAllPossibleTasks(); 467 List l = AntXFixture.newList(tasks.length); 468 for (int i=0;i<tasks.length;i++) { 469 Class taskClass = TaskExaminer.trueClass(tasks[i]); if (taskClass!=null && thisClass.isAssignableFrom(taskClass)) { 471 l.add(tasks[i]); 472 } 473 } 474 return l; 475 } 476 477 478 479 487 private String getStepName(Object t) 488 { 489 if (t instanceof InlineStep) { 490 return ((InlineStep)t).getName(); 491 } 492 return TaskExaminer.trueAttr((Task)t,"name"); 493 } 494 495 496 497 508 protected final List getFilteredStepsLike(Class thisClass, List filterList) 509 { 510 require_(filterList!=null,"filterlst- nonzro list"); 511 List steps = getAllPossibleStepsLike(thisClass); 512 if (!steps.isEmpty()) { 513 Iterator itr= steps.iterator(); 514 while (itr.hasNext()) { 515 String sn = getStepName(itr.next()); 516 if (!filterList.contains(sn)) { 517 itr.remove(); 518 } 519 } 520 } 521 return steps; 522 } 523 524 525 526 530 static final String callerName(TargetCaller caller) 531 { 532 String name = caller.getStepName(); 533 if (name==null) { 534 name = caller.getTargetName(); 535 } 536 if (name==null) { 537 name = "n/d"; 538 } 539 return name; 540 } 541 542 543 544 OverlayParametersHelper m_paramsHelper; private boolean m_haltIfError= true; 546 private boolean m_tryEach=false; 547 private String m_failProperty, m_failVariable; 548 private String m_failValue = Strings.TRUE; 549 } 550 551 552 | Popular Tags |