| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package net.sf.antcontrib.logic; |
| 17 |
|
| 18 |
import java.io.File; |
| 19 |
import java.lang.reflect.Method; |
| 20 |
import java.util.ArrayList; |
| 21 |
import java.util.Collection; |
| 22 |
import java.util.Iterator; |
| 23 |
import java.util.List; |
| 24 |
import java.util.Map; |
| 25 |
import java.util.StringTokenizer; |
| 26 |
|
| 27 |
import org.apache.tools.ant.BuildException; |
| 28 |
import org.apache.tools.ant.Project; |
| 29 |
import org.apache.tools.ant.Task; |
| 30 |
import org.apache.tools.ant.taskdefs.MacroDef; |
| 31 |
import org.apache.tools.ant.taskdefs.MacroInstance; |
| 32 |
import org.apache.tools.ant.taskdefs.Parallel; |
| 33 |
import org.apache.tools.ant.types.DirSet; |
| 34 |
import org.apache.tools.ant.types.FileSet; |
| 35 |
import org.apache.tools.ant.types.Path; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class ForTask extends Task { |
| 44 |
|
| 45 |
private String list; |
| 46 |
private String param; |
| 47 |
private String delimiter = ","; |
| 48 |
private Path currPath; |
| 49 |
private boolean trim; |
| 50 |
private boolean keepgoing = false; |
| 51 |
private MacroDef macroDef; |
| 52 |
private List hasIterators = new ArrayList(); |
| 53 |
private boolean parallel = false; |
| 54 |
private Integer threadCount; |
| 55 |
private Parallel parallelTasks; |
| 56 |
private int begin = 0; |
| 57 |
private Integer end = null; |
| 58 |
private int step = 1; |
| 59 |
|
| 60 |
private int taskCount = 0; |
| 61 |
private int errorCount = 0; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public ForTask() { |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public void setParallel(boolean parallel) { |
| 74 |
this.parallel = parallel; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public void setThreadCount(int threadCount) { |
| 83 |
if (threadCount < 1) { |
| 84 |
throw new BuildException("Illegal value for threadCount " + threadCount |
| 85 |
+ " it should be > 0"); |
| 86 |
} |
| 87 |
this.threadCount = new Integer(threadCount); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public void setTrim(boolean trim) { |
| 96 |
this.trim = trim; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public void setKeepgoing(boolean keepgoing) { |
| 107 |
this.keepgoing = keepgoing; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public void setList(String list) { |
| 116 |
this.list = list; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public void setDelimiter(String delimiter) { |
| 126 |
this.delimiter = delimiter; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public void setParam(String param) { |
| 137 |
this.param = param; |
| 138 |
} |
| 139 |
|
| 140 |
private Path getOrCreatePath() { |
| 141 |
if (currPath == null) { |
| 142 |
currPath = new Path(getProject()); |
| 143 |
} |
| 144 |
return currPath; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public void addConfigured(Path path) { |
| 156 |
getOrCreatePath().append(path); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public void addConfiguredPath(Path path) { |
| 168 |
addConfigured(path); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public Object createSequential() { |
| 175 |
macroDef = new MacroDef(); |
| 176 |
macroDef.setProject(getProject()); |
| 177 |
return macroDef.createSequential(); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public void setBegin(int begin) { |
| 185 |
this.begin = begin; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public void setEnd(Integer end) { |
| 193 |
this.end = end; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public void setStep(int step) { |
| 201 |
this.step = step; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public void execute() { |
| 213 |
if (parallel) { |
| 214 |
parallelTasks = (Parallel) getProject().createTask("parallel"); |
| 215 |
if (threadCount != null) { |
| 216 |
parallelTasks.setThreadCount(threadCount.intValue()); |
| 217 |
} |
| 218 |
} |
| 219 |
if (list == null && currPath == null && hasIterators.size() == 0 |
| 220 |
&& end == null) { |
| 221 |
throw new BuildException( |
| 222 |
"You must have a list or path or sequence to iterate through"); |
| 223 |
} |
| 224 |
if (param == null) { |
| 225 |
throw new BuildException( |
| 226 |
"You must supply a property name to set on" |
| 227 |
+ " each iteration in param"); |
| 228 |
} |
| 229 |
if (macroDef == null) { |
| 230 |
throw new BuildException( |
| 231 |
"You must supply an embedded sequential " |
| 232 |
+ "to perform"); |
| 233 |
} |
| 234 |
if (end != null) { |
| 235 |
int iEnd = end.intValue(); |
| 236 |
if (step == 0) { |
| 237 |
throw new BuildException("step cannot be 0"); |
| 238 |
} else if (iEnd > begin && step < 0) { |
| 239 |
throw new BuildException("end > begin, step needs to be > 0"); |
| 240 |
} else if (iEnd <= begin && step > 0) { |
| 241 |
throw new BuildException("end <= begin, step needs to be < 0"); |
| 242 |
} |
| 243 |
} |
| 244 |
doTheTasks(); |
| 245 |
if (parallel) { |
| 246 |
parallelTasks.perform(); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
private void doSequentialIteration(String val) { |
| 252 |
MacroInstance instance = new MacroInstance(); |
| 253 |
instance.setProject(getProject()); |
| 254 |
instance.setOwningTarget(getOwningTarget()); |
| 255 |
instance.setMacroDef(macroDef); |
| 256 |
instance.setDynamicAttribute(param.toLowerCase(), |
| 257 |
val); |
| 258 |
if (!parallel) { |
| 259 |
instance.execute(); |
| 260 |
} else { |
| 261 |
parallelTasks.addTask(instance); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
private void doToken(String tok) { |
| 266 |
try { |
| 267 |
taskCount++; |
| 268 |
doSequentialIteration(tok); |
| 269 |
} catch (BuildException bx) { |
| 270 |
if (keepgoing) { |
| 271 |
log(tok + ": " + bx.getMessage(), Project.MSG_ERR); |
| 272 |
errorCount++; |
| 273 |
} else { |
| 274 |
throw bx; |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
private void doTheTasks() { |
| 280 |
errorCount = 0; |
| 281 |
taskCount = 0; |
| 282 |
|
| 283 |
|
| 284 |
if (macroDef.getAttributes().isEmpty()) { |
| 285 |
MacroDef.Attribute attribute = new MacroDef.Attribute(); |
| 286 |
attribute.setName(param); |
| 287 |
macroDef.addConfiguredAttribute(attribute); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
if (list != null) { |
| 292 |
StringTokenizer st = new StringTokenizer(list, delimiter); |
| 293 |
|
| 294 |
while (st.hasMoreTokens()) { |
| 295 |
String tok = st.nextToken(); |
| 296 |
if (trim) { |
| 297 |
tok = tok.trim(); |
| 298 |
} |
| 299 |
doToken(tok); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
if (end != null) { |
| 305 |
int iEnd = end.intValue(); |
| 306 |
if (step > 0) { |
| 307 |
for (int i = begin; i < (iEnd + 1); i = i + step) { |
| 308 |
doToken("" + i); |
| 309 |
} |
| 310 |
} else { |
| 311 |
for (int i = begin; i > (iEnd - 1); i = i + step) { |
| 312 |
doToken("" + i); |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
String[] pathElements = new String[0]; |
| 319 |
if (currPath != null) { |
| 320 |
pathElements = currPath.list(); |
| 321 |
} |
| 322 |
for (int i = 0; i < pathElements.length; i++) { |
| 323 |
File nextFile = new File(pathElements[i]); |
| 324 |
doToken(nextFile.getAbsolutePath()); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
for (Iterator i = hasIterators.iterator(); i.hasNext();) { |
| 329 |
Iterator it = ((HasIterator) i.next()).iterator(); |
| 330 |
while (it.hasNext()) { |
| 331 |
doToken(it.next().toString()); |
| 332 |
} |
| 333 |
} |
| 334 |
if (keepgoing && (errorCount != 0)) { |
| 335 |
throw new BuildException( |
| 336 |
"Keepgoing execution: " + errorCount |
| 337 |
+ " of " + taskCount + " iterations failed."); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
public void add(Map map) { |
| 347 |
hasIterators.add(new MapIterator(map)); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public void add(FileSet fileset) { |
| 356 |
getOrCreatePath().addFileset(fileset); |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
public void addFileSet(FileSet fileset) { |
| 365 |
add(fileset); |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
public void add(DirSet dirset) { |
| 374 |
getOrCreatePath().addDirset(dirset); |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
public void addDirSet(DirSet dirset) { |
| 383 |
add(dirset); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
public void add(Collection collection) { |
| 392 |
hasIterators.add(new ReflectIterator(collection)); |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
public void add(Iterator iterator) { |
| 401 |
hasIterators.add(new IteratorIterator(iterator)); |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
public void add(Object obj) { |
| 411 |
hasIterators.add(new ReflectIterator(obj)); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
private interface HasIterator { |
| 418 |
Iterator iterator(); |
| 419 |
} |
| 420 |
|
| 421 |
private static class IteratorIterator implements HasIterator { |
| 422 |
private Iterator iterator; |
| 423 |
public IteratorIterator(Iterator iterator) { |
| 424 |
this.iterator = iterator; |
| 425 |
} |
| 426 |
public Iterator iterator() { |
| 427 |
return this.iterator; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
private static class MapIterator implements HasIterator { |
| 432 |
private Map map; |
| 433 |
public MapIterator(Map map) { |
| 434 |
this.map = map; |
| 435 |
} |
| 436 |
public Iterator iterator() { |
| 437 |
return map.values().iterator(); |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
private static class ReflectIterator implements HasIterator { |
| 442 |
private Object obj; |
| 443 |
private Method method; |
| 444 |
public ReflectIterator(Object obj) { |
| 445 |
this.obj = obj; |
| 446 |
try { |
| 447 |
method = obj.getClass().getMethod( |
| 448 |
"iterator", new Class[] {}); |
| 449 |
} catch (Throwable t) { |
| 450 |
throw new BuildException( |
| 451 |
"Invalid type " + obj.getClass() + " used in For task, it does" |
| 452 |
+ " not have a public iterator method"); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
public Iterator iterator() { |
| 457 |
try { |
| 458 |
return (Iterator) method.invoke(obj, new Object[] {}); |
| 459 |
} catch (Throwable t) { |
| 460 |
throw new BuildException(t); |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
} |