1 28 29 package com.idaremedia.antx.starters; 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.RuntimeConfigurable; 37 import org.apache.tools.ant.Target; 38 import org.apache.tools.ant.Task; 39 import org.apache.tools.ant.TaskContainer; 40 import org.apache.tools.ant.UnknownElement; 41 import org.apache.tools.ant.util.CollectionUtils; 42 43 import com.idaremedia.antx.AntX; 44 import com.idaremedia.antx.AntXFixture; 45 import com.idaremedia.antx.AssertableTask; 46 import com.idaremedia.antx.helpers.Empties; 47 import com.idaremedia.antx.helpers.TaskHandle; 48 import com.idaremedia.antx.ownhelpers.TaskExaminer; 49 50 67 68 public class TaskSet extends AssertableTask implements TaskContainer 69 { 70 74 public static final boolean DEFAULT_DELAY_CONFIGURE= true; 75 76 77 81 private static final int INFINITE_TASKS = Integer.MAX_VALUE; 82 83 84 85 88 public TaskSet() 89 { 90 super(AntX.starters); 91 } 92 93 94 98 protected TaskSet(String iam) 99 { 100 super(iam); 101 } 102 103 104 105 111 public void unconfigure() 112 { 113 super.unconfigure(); 114 getTasksList().clear(); 115 } 116 117 118 119 125 protected TaskSet(String iam, boolean delayConfigure) 126 { 127 super(iam); 128 delayChildrenConfigure(delayConfigure); 129 } 130 131 132 133 138 protected final void delayChildrenConfigure(boolean delay) 139 { 140 m_delayConfigure = delay; 141 } 142 143 144 145 149 protected final boolean isDelayChildrenConfigure() 150 { 151 return m_delayConfigure; 152 } 153 154 155 156 161 public void maybeConfigure() 162 throws BuildException 163 { 164 if (!TaskExaminer.ignoreConfigure(this)) { if (isInvalid() || !isDelayChildrenConfigure()) { 166 super.maybeConfigure(); 167 } else { 168 getRuntimeConfigurableWrapper().maybeConfigure(getProject(),false); 169 if (AntX.ANT_PRE17) { maybeConfigureSpecialTasks(); 171 } 172 } 173 } 174 } 175 176 177 182 public void setRuntimeConfigurableWrapper(RuntimeConfigurable wrapper) 183 { 184 if (!AntX.ANT_PRE17) { if (wrapper!=null) { 186 int n=0; 187 for (Iterator itr=CollectionUtils.asIterator(wrapper.getChildren());itr.hasNext();) { 188 itr.next(); 189 n++; 190 } 191 if (n>0) { 192 this.m_configureThreshold = n; 193 } 194 } 195 } 196 super.setRuntimeConfigurableWrapper(wrapper); 197 } 198 199 200 201 209 protected void maybeConfigureSpecialTasks() 210 { 211 } 213 214 215 216 219 protected List getTasksList() 220 { 221 return m_nestedTasks; 222 } 223 224 225 226 230 protected boolean isEmpty() 231 { 232 return getTasksList().isEmpty(); 233 } 234 235 236 237 246 protected boolean includeTask(TaskHandle taskH) 247 { 248 return true; 249 } 250 251 252 253 259 public void addTask(Task task) 260 { 261 require_(task!=null,"addTsk- nonzro task"); 262 263 TaskHandle taskH = new TaskHandle(task); 264 265 if (includeTask(taskH)) { 266 verify_(!taskH.isEmpty(),"addTsk- nonzro task"); 267 getTasksList().add(taskH.getTask()); 268 } else { 269 String ermsg = uistrs().get("taskset.nested.task.disallowed", 270 task.getTaskName(), this.getTaskName()); 271 log(ermsg, Project.MSG_ERR); 272 throw new BuildException(ermsg,getLocation()); 273 } 274 addedTask(); 275 } 276 277 278 279 285 protected final void addedTask() 286 { 287 if (!AntX.ANT_PRE17) { 288 m_configureThreshold--; 289 if (m_configureThreshold==0) { 290 finishedAddingTasks(); 291 } 292 } 293 } 294 295 296 297 304 private void finishedAddingTasks() 305 { 306 m_configureThreshold = INFINITE_TASKS; 307 maybeConfigureSpecialTasks(); 308 } 309 310 311 312 323 protected final Task candidateTask(TaskHandle taskH, Class [] coi) 324 { 325 return TaskExaminer.candidateTask(taskH,coi,this); 326 } 327 328 329 330 334 public Task[] getTasks() 335 { 336 return (Task[])getTasksList().toArray(Empties.EMPTY_TASK_ARRAY); 337 } 338 339 340 341 345 protected final void performTheTasksList() throws BuildException 346 { 347 Iterator itr = getTasksList().listIterator(); 348 while (itr.hasNext()) { 349 Task nestedTask = (Task)itr.next(); 350 nestedTask.perform(); 351 } 352 } 353 354 355 356 367 protected final void performIterationOfTheTasksList(final Project theProject, 368 final Target parent) throws BuildException 369 { 370 require_(theProject!=null,"preformIterOfItems- nonzro project"); 371 require_(parent!=null,"preformIterOfItems- nonzro parent target"); 372 373 Iterator itr = getTasksList().listIterator(); 374 375 while (itr.hasNext()) { 376 UnknownElement template = (UnknownElement)itr.next(); 377 UnknownElement instance = TaskExaminer.copyUEProxy(template,theProject,parent); 378 instance.perform(); 379 instance = null; 380 } 381 } 382 383 384 385 392 protected final void performIterationOfTheTasksList() throws BuildException 393 { 394 performIterationOfTheTasksList(getProject(),getOwningTarget()); 395 } 396 397 398 399 406 protected void performNestedTasks() throws BuildException 407 { 408 performTheTasksList(); 409 } 410 411 412 413 418 public void execute() throws BuildException 419 { 420 verifyCanExecute_("execute"); 421 422 performNestedTasks(); 423 } 424 425 426 427 432 protected final void verifyIsUndefined_(Task currentTask, Task newTask) 433 throws BuildException 434 { 435 if (currentTask!=null) { 436 String ermsg = uistrs().get("taskset.only.one.specialtask", 437 newTask.getTaskName(), 438 currentTask.getTaskName()); 439 log(ermsg, Project.MSG_ERR); 440 throw new BuildException(ermsg,getLocation()); 441 } 442 } 443 444 445 private boolean m_delayConfigure= DEFAULT_DELAY_CONFIGURE; 446 private List m_nestedTasks= AntXFixture.newList(10); private int m_configureThreshold= INFINITE_TASKS; 448 } 449 450 451 | Popular Tags |