1 28 29 package com.idaremedia.antx.flowcontrol.call; 30 31 import java.util.ArrayList ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 35 import org.apache.tools.ant.BuildException; 36 import org.apache.tools.ant.Project; 37 import org.apache.tools.ant.Target; 38 import org.apache.tools.ant.Task; 39 40 import com.idaremedia.antx.AntX; 41 import com.idaremedia.antx.AssertableTask; 42 import com.idaremedia.antx.flowcontrol.FlowConstants; 43 import com.idaremedia.antx.ownhelpers.TaskExaminer; 44 45 68 69 public class StepLauncher extends AssertableTask 70 { 71 74 public StepLauncher() 75 { 76 super(AntX.flow+"steplauncher"); 77 } 78 79 80 86 public void maybeConfigure() 87 throws BuildException 88 { 89 if (!TaskExaminer.ignoreConfigure(this)) { 90 super.maybeConfigure(); 91 } 92 } 93 94 95 96 99 private BuildException missingInformation(String msgId, String arg1) 100 { 101 String ermsg = uistrs().get(msgId,arg1); 102 log(ermsg,Project.MSG_ERR); 103 return new BuildException(ermsg, getLocation()); 104 } 105 106 107 112 protected void verifyCanExecute_(String calr) 113 throws BuildException 114 { 115 verifyInProject_(calr); 116 117 String targetProperty = getTargetProperty(); 118 require_(targetProperty!=null,"exec- nonzro target property"); 119 120 String nameProperty = getInlinedTargetNameProperty(); 121 require_(nameProperty!=null,"exec- nonzro stepname property"); 122 123 final Project P= getProject(); 124 125 if (P.getProperty(targetProperty)==null) { 126 throw missingInformation("flow.steplaunch.need.target",targetProperty); 127 } 128 if (P.getProperty(nameProperty)==null) { 129 throw missingInformation("flow.steplaunch.need.stepname",nameProperty); 130 } 131 } 132 133 134 135 143 protected Target getLocalTarget() 144 throws BuildException 145 { 146 Target localTarget = (Target)getProject().getTargets().get(getTargetName()); 147 148 if (localTarget==null) { 149 throw missingInformation("flow.steplaunch.missing.caller", 150 getTargetName()); 151 } 152 return localTarget; 153 } 154 155 156 162 protected final List getTasksOfKind(Task[] tasks, Class ofKind) 163 throws BuildException 164 { 165 List l= new ArrayList (tasks.length); 166 Class [] COI= new Class []{ofKind}; 167 for (int i=0;i<tasks.length;i++) { 168 Task task = TaskExaminer.trueTask(tasks[i],COI,this); 169 if (ofKind.isAssignableFrom(task.getClass())) { 170 l.add(task); 171 if (task!=tasks[i]) { tasks[i]= task; } 174 } 175 } 176 return l; 177 } 178 179 180 191 protected InlinedTarget getInlinedTarget(final String stepName, final Class filterClass, 192 Task[] tasks, int depth, final int depthLimit) 193 { 194 InlinedTarget it = null; 195 List candidates = getTasksOfKind(tasks, InlinedTarget.class); 196 197 if (!candidates.isEmpty()) { 198 Iterator itr= candidates.iterator(); 199 while (itr.hasNext()) { 200 it = (InlinedTarget)itr.next(); 201 if (it.getName()==null && (it instanceof Task)) { 203 ((Task)it).maybeConfigure(); 204 } 205 if (stepName.equals(it.getName())) { 206 if (filterClass!=null && !filterClass.isInstance(it)) { 207 continue; 208 } 209 return it; 210 } 211 } 212 it = null; 213 candidates.clear(); 214 } 215 candidates=null; 216 217 if (depth<depthLimit) { 218 candidates = getTasksOfKind(tasks, InlinedTargetContainer.class); 219 if (!candidates.isEmpty()) { 220 Iterator itr = candidates.iterator(); 221 Task[] nestedtasks; 222 while (itr.hasNext()) { 223 InlinedTargetContainer itc = (InlinedTargetContainer)itr.next(); 224 nestedtasks = itc.getTasks(); 225 if (nestedtasks.length>0) { 226 it = getInlinedTarget(stepName, filterClass, nestedtasks, 227 depth+1,depthLimit); 228 if (it!=null) { 229 break; 230 } 231 } 232 } 233 candidates.clear(); 234 } 235 candidates=null; 236 } 237 238 return it; 239 } 240 241 242 250 protected InlinedTarget findInlinedTarget(String stepName, String requiredClass) 251 throws BuildException 252 { 253 require_(stepName!=null,"fndInlinTargt- nonzro name"); 254 255 Class filterClass = null; 256 if (requiredClass!=null) { 257 try { 258 filterClass = Class.forName(requiredClass); 259 } catch(ClassNotFoundException cnfx) { 260 throw new BuildException(cnfx, getLocation()); 261 } 262 } 263 264 return getInlinedTarget(stepName, filterClass, 265 getLocalTarget().getTasks(), 266 1, getMaxSearchDepth()); 267 } 268 269 270 271 280 public void execute() throws BuildException 281 { 282 verifyCanExecute_("execute"); 283 284 InlinedTarget it = findInlinedTarget(getInlinedTargetName(), 285 getKindOfInlinedTargetClass()); 286 if (it==null) { 287 String ermsg = uistrs().get("flow.steplaunch.missing.step", 288 getInlinedTargetName(), getTargetName()); 289 log(ermsg, Project.MSG_ERR); 290 throw new BuildException(ermsg, getLocation()); 291 } 292 293 it.run(); 294 } 295 296 297 298 303 protected String getTargetProperty() 304 { 305 return FlowConstants.DEFAULT_STEP_LAUNCHER_TARGET_PROP; 306 } 307 308 309 314 protected String getInlinedTargetNameProperty() 315 { 316 return FlowConstants.DEFAULT_STEP_LAUNCHER_STEPNAME_PROP; 317 } 318 319 320 321 327 protected String getKindOfInlinedTargetClass() 328 { 329 String kindOfInlinedTargetClass = 330 getProject().getProperty 331 (FlowConstants.DEFAULT_STEP_LAUNCHER_STEPCLASS_PROP); 332 333 if (kindOfInlinedTargetClass==null) { 334 kindOfInlinedTargetClass = InlineStep.class.getName(); 335 } 336 337 return kindOfInlinedTargetClass; 338 } 339 340 341 342 345 private String getTargetName() 346 { 347 return getProject().getProperty(getTargetProperty()); 348 } 349 350 351 354 private String getInlinedTargetName() 355 { 356 return getProject().getProperty(getInlinedTargetNameProperty()); 357 } 358 359 360 364 protected int getMaxSearchDepth() 365 { 366 String s = 367 getProject().getProperty 368 (FlowConstants.DEFAULT_STEP_LAUNCHER_SEARCHDEPTH_PROP); 369 if (s!=null) { 370 try { 371 int i = Integer.parseInt(s); 372 if (i>0) { 373 return i; 374 } 375 } catch(NumberFormatException nfx) {} 376 String ermsg = uistrs().get("flow.steplaunch.bad.depth",s,"1"); 377 log(ermsg,Project.MSG_WARN); 378 } 379 return 1; 380 } 381 382 } 383 384 385 | Popular Tags |