You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExecuteOn.java 27 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /*
  2. * Copyright 2000-2005 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.util.Hashtable;
  21. import java.util.Iterator;
  22. import java.util.Vector;
  23. import org.apache.tools.ant.BuildException;
  24. import org.apache.tools.ant.DirectoryScanner;
  25. import org.apache.tools.ant.Project;
  26. import org.apache.tools.ant.types.Commandline;
  27. import org.apache.tools.ant.types.AbstractFileSet;
  28. import org.apache.tools.ant.types.DirSet;
  29. import org.apache.tools.ant.types.EnumeratedAttribute;
  30. import org.apache.tools.ant.types.FileList;
  31. import org.apache.tools.ant.types.FileSet;
  32. import org.apache.tools.ant.types.Mapper;
  33. import org.apache.tools.ant.types.Resource;
  34. import org.apache.tools.ant.types.ResourceCollection;
  35. import org.apache.tools.ant.types.resources.FileResource;
  36. import org.apache.tools.ant.types.resources.Union;
  37. import org.apache.tools.ant.util.FileNameMapper;
  38. import org.apache.tools.ant.util.SourceFileScanner;
  39. /**
  40. * Executes a given command, supplying a set of files as arguments.
  41. *
  42. * @since Ant 1.2
  43. *
  44. * @ant.task category="control" name="apply"
  45. */
  46. public class ExecuteOn extends ExecTask {
  47. // filesets has been protected so we need to keep that even after
  48. // switching to resource collections. In fact, they will still
  49. // get a different treatment form the other resource collections
  50. // even in execute since we have some subtle special features like
  51. // switching type to "dir" when we encounter a DirSet that would
  52. // be more difficult to achieve otherwise.
  53. protected Vector filesets = new Vector(); // contains AbstractFileSet
  54. // (both DirSet and FileSet)
  55. private Union resources = null;
  56. private boolean relative = false;
  57. private boolean parallel = false;
  58. private boolean forwardSlash = false;
  59. protected String type = FileDirBoth.FILE;
  60. protected Commandline.Marker srcFilePos = null;
  61. private boolean skipEmpty = false;
  62. protected Commandline.Marker targetFilePos = null;
  63. protected Mapper mapperElement = null;
  64. protected FileNameMapper mapper = null;
  65. protected File destDir = null;
  66. private int maxParallel = -1;
  67. private boolean addSourceFile = true;
  68. private boolean verbose = false;
  69. private boolean ignoreMissing = true;
  70. private boolean force = false;
  71. /**
  72. * Has <srcfile> been specified before <targetfile>
  73. */
  74. protected boolean srcIsFirst = true;
  75. /**
  76. * Add a set of files upon which to operate.
  77. * @param set the FileSet to add.
  78. */
  79. public void addFileset(FileSet set) {
  80. filesets.addElement(set);
  81. }
  82. /**
  83. * Add a set of directories upon which to operate.
  84. *
  85. * @param set the DirSet to add.
  86. *
  87. * @since Ant 1.6
  88. */
  89. public void addDirset(DirSet set) {
  90. filesets.addElement(set);
  91. }
  92. /**
  93. * Add a list of source files upon which to operate.
  94. * @param list the FileList to add.
  95. */
  96. public void addFilelist(FileList list) {
  97. add(list);
  98. }
  99. /**
  100. * Add a collection of resources upon which to operate.
  101. * @param rc resource collection to add.
  102. * @since Ant 1.7
  103. */
  104. public void add(ResourceCollection rc) {
  105. if (resources == null) {
  106. resources = new Union();
  107. }
  108. resources.add(rc);
  109. }
  110. /**
  111. * Set whether the filenames should be passed on the command line as
  112. * absolute or relative pathnames. Paths are relative to the base
  113. * directory of the corresponding fileset for source files or the
  114. * dest attribute for target files.
  115. * @param relative whether to pass relative pathnames.
  116. */
  117. public void setRelative(boolean relative) {
  118. this.relative = relative;
  119. }
  120. /**
  121. * Set whether to execute in parallel mode.
  122. * If true, run the command only once, appending all files as arguments.
  123. * If false, command will be executed once for every file. Defaults to false.
  124. * @param parallel whether to run in parallel.
  125. */
  126. public void setParallel(boolean parallel) {
  127. this.parallel = parallel;
  128. }
  129. /**
  130. * Set whether the command works only on files, directories or both.
  131. * @param type a FileDirBoth EnumeratedAttribute.
  132. */
  133. public void setType(FileDirBoth type) {
  134. this.type = type.getValue();
  135. }
  136. /**
  137. * Set whether empty filesets will be skipped. If true and
  138. * no source files have been found or are newer than their
  139. * corresponding target files, the command will not be run.
  140. * @param skip whether to skip empty filesets.
  141. */
  142. public void setSkipEmptyFilesets(boolean skip) {
  143. skipEmpty = skip;
  144. }
  145. /**
  146. * Specify the directory where target files are to be placed.
  147. * @param destDir the File object representing the destination directory.
  148. */
  149. public void setDest(File destDir) {
  150. this.destDir = destDir;
  151. }
  152. /**
  153. * Set whether the source and target file names on Windows and OS/2
  154. * must use the forward slash as file separator.
  155. * @param forwardSlash whether the forward slash will be forced.
  156. */
  157. public void setForwardslash(boolean forwardSlash) {
  158. this.forwardSlash = forwardSlash;
  159. }
  160. /**
  161. * Limit the command line length by passing at maximum this many
  162. * sourcefiles at once to the command.
  163. *
  164. * <p>Set to &lt;= 0 for unlimited - this is the default.</p>
  165. *
  166. * @param max <code>int</code> maximum number of sourcefiles
  167. * passed to the executable.
  168. *
  169. * @since Ant 1.6
  170. */
  171. public void setMaxParallel(int max) {
  172. maxParallel = max;
  173. }
  174. /**
  175. * Set whether to send the source file name on the command line.
  176. *
  177. * <p>Defaults to <code>true</code>.
  178. *
  179. * @param b whether to add the source file to the command line.
  180. *
  181. * @since Ant 1.6
  182. */
  183. public void setAddsourcefile(boolean b) {
  184. addSourceFile = b;
  185. }
  186. /**
  187. * Set whether to operate in verbose mode.
  188. * If true, a verbose summary will be printed after execution.
  189. * @param b whether to operate in verbose mode.
  190. *
  191. * @since Ant 1.6
  192. */
  193. public void setVerbose(boolean b) {
  194. verbose = b;
  195. }
  196. /**
  197. * Set whether to ignore nonexistent files from filelists.
  198. * @param b whether to ignore missing files.
  199. *
  200. * @since Ant 1.6.2
  201. */
  202. public void setIgnoremissing(boolean b) {
  203. ignoreMissing = b;
  204. }
  205. /**
  206. * Set whether to bypass timestamp comparisons for target files.
  207. * @param b whether to bypass timestamp comparisons.
  208. *
  209. * @since Ant 1.6.3
  210. */
  211. public void setForce(boolean b) {
  212. force = b;
  213. }
  214. /**
  215. * Create a placeholder indicating where on the command line
  216. * the name of the source file should be inserted.
  217. * @return <code>Commandline.Marker</code>.
  218. */
  219. public Commandline.Marker createSrcfile() {
  220. if (srcFilePos != null) {
  221. throw new BuildException(getTaskType() + " doesn\'t support multiple "
  222. + "srcfile elements.", getLocation());
  223. }
  224. srcFilePos = cmdl.createMarker();
  225. return srcFilePos;
  226. }
  227. /**
  228. * Create a placeholder indicating where on the command line
  229. * the name of the target file should be inserted.
  230. * @return <code>Commandline.Marker</code>.
  231. */
  232. public Commandline.Marker createTargetfile() {
  233. if (targetFilePos != null) {
  234. throw new BuildException(getTaskType() + " doesn\'t support multiple "
  235. + "targetfile elements.", getLocation());
  236. }
  237. targetFilePos = cmdl.createMarker();
  238. srcIsFirst = (srcFilePos != null);
  239. return targetFilePos;
  240. }
  241. /**
  242. * Create a nested Mapper element to use for mapping
  243. * source files to target files.
  244. * @return <code>Mapper</code>.
  245. * @throws BuildException if more than one mapper is defined.
  246. */
  247. public Mapper createMapper() throws BuildException {
  248. if (mapperElement != null) {
  249. throw new BuildException("Cannot define more than one mapper",
  250. getLocation());
  251. }
  252. mapperElement = new Mapper(getProject());
  253. return mapperElement;
  254. }
  255. /**
  256. * Add a nested FileNameMapper.
  257. * @param fileNameMapper the mapper to add.
  258. * @since Ant 1.6.3
  259. */
  260. public void add(FileNameMapper fileNameMapper) {
  261. createMapper().add(fileNameMapper);
  262. }
  263. /**
  264. * Check the configuration of this ExecuteOn instance.
  265. */
  266. protected void checkConfiguration() {
  267. // * @TODO using taskName here is brittle, as a user could override it.
  268. // * this should probably be modified to use the classname instead.
  269. if ("execon".equals(getTaskName())) {
  270. log("!! execon is deprecated. Use apply instead. !!");
  271. }
  272. super.checkConfiguration();
  273. if (filesets.size() == 0 && resources == null) {
  274. throw new BuildException("no resources specified",
  275. getLocation());
  276. }
  277. if (targetFilePos != null && mapperElement == null) {
  278. throw new BuildException("targetfile specified without mapper",
  279. getLocation());
  280. }
  281. if (destDir != null && mapperElement == null) {
  282. throw new BuildException("dest specified without mapper",
  283. getLocation());
  284. }
  285. if (mapperElement != null) {
  286. mapper = mapperElement.getImplementation();
  287. }
  288. }
  289. /**
  290. * Create the ExecuteStreamHandler instance that will be used
  291. * during execution.
  292. * @return <code>ExecuteStreamHandler</code>.
  293. * @throws BuildException on error.
  294. */
  295. protected ExecuteStreamHandler createHandler() throws BuildException {
  296. //if we have a RedirectorElement, return a decoy
  297. return (redirectorElement == null)
  298. ? super.createHandler() : new PumpStreamHandler();
  299. }
  300. /**
  301. * Set up the I/O Redirector.
  302. */
  303. protected void setupRedirector() {
  304. super.setupRedirector();
  305. redirector.setAppendProperties(true);
  306. }
  307. /**
  308. * Run the specified Execute object.
  309. * @param exe the Execute instance representing the external process.
  310. * @throws BuildException on error
  311. */
  312. protected void runExec(Execute exe) throws BuildException {
  313. int totalFiles = 0;
  314. int totalDirs = 0;
  315. boolean haveExecuted = false;
  316. try {
  317. Vector fileNames = new Vector();
  318. Vector baseDirs = new Vector();
  319. for (int i = 0; i < filesets.size(); i++) {
  320. String currentType = type;
  321. AbstractFileSet fs = (AbstractFileSet) filesets.elementAt(i);
  322. if (fs instanceof DirSet) {
  323. if (!FileDirBoth.DIR.equals(type)) {
  324. log("Found a nested dirset but type is " + type + ". "
  325. + "Temporarily switching to type=\"dir\" on the"
  326. + " assumption that you really did mean"
  327. + " <dirset> not <fileset>.", Project.MSG_DEBUG);
  328. currentType = FileDirBoth.DIR;
  329. }
  330. }
  331. File base = fs.getDir(getProject());
  332. DirectoryScanner ds = fs.getDirectoryScanner(getProject());
  333. if (!FileDirBoth.DIR.equals(currentType)) {
  334. String[] s = getFiles(base, ds);
  335. for (int j = 0; j < s.length; j++) {
  336. totalFiles++;
  337. fileNames.addElement(s[j]);
  338. baseDirs.addElement(base);
  339. }
  340. }
  341. if (!FileDirBoth.FILE.equals(currentType)) {
  342. String[] s = getDirs(base, ds);
  343. for (int j = 0; j < s.length; j++) {
  344. totalDirs++;
  345. fileNames.addElement(s[j]);
  346. baseDirs.addElement(base);
  347. }
  348. }
  349. if (fileNames.size() == 0 && skipEmpty) {
  350. int includedCount
  351. = ((!FileDirBoth.DIR.equals(currentType))
  352. ? ds.getIncludedFilesCount() : 0)
  353. + ((!FileDirBoth.FILE.equals(currentType))
  354. ? ds.getIncludedDirsCount() : 0);
  355. log("Skipping fileset for directory " + base + ". It is "
  356. + ((includedCount > 0) ? "up to date." : "empty."),
  357. Project.MSG_INFO);
  358. continue;
  359. }
  360. if (!parallel) {
  361. String[] s = new String[fileNames.size()];
  362. fileNames.copyInto(s);
  363. for (int j = 0; j < s.length; j++) {
  364. String[] command = getCommandline(s[j], base);
  365. log(Commandline.describeCommand(command),
  366. Project.MSG_VERBOSE);
  367. exe.setCommandline(command);
  368. if (redirectorElement != null) {
  369. setupRedirector();
  370. redirectorElement.configure(redirector, s[j]);
  371. }
  372. if (redirectorElement != null || haveExecuted) {
  373. // need to reset the stream handler to restart
  374. // reading of pipes;
  375. // go ahead and do it always w/ nested redirectors
  376. exe.setStreamHandler(redirector.createHandler());
  377. }
  378. runExecute(exe);
  379. haveExecuted = true;
  380. }
  381. fileNames.removeAllElements();
  382. baseDirs.removeAllElements();
  383. }
  384. }
  385. if (resources != null) {
  386. Iterator iter = resources.iterator();
  387. while (iter.hasNext()) {
  388. Resource res = (Resource) iter.next();
  389. if (!res.isExists() && ignoreMissing) {
  390. continue;
  391. }
  392. File base = null;
  393. String name = res.getName();
  394. if (res instanceof FileResource) {
  395. FileResource fr = (FileResource) res;
  396. base = fr.getBaseDir();
  397. if (base == null) {
  398. name = fr.getFile().getAbsolutePath();
  399. }
  400. }
  401. if (restrict(new String[] {name}, base).length == 0) {
  402. continue;
  403. }
  404. if ((!res.isDirectory() || !res.isExists())
  405. && !FileDirBoth.DIR.equals(type)) {
  406. totalFiles++;
  407. } else if (res.isDirectory()
  408. && !FileDirBoth.FILE.equals(type)) {
  409. totalDirs++;
  410. } else {
  411. continue;
  412. }
  413. baseDirs.add(base);
  414. fileNames.add(name);
  415. if (!parallel) {
  416. String[] command = getCommandline(name, base);
  417. log(Commandline.describeCommand(command),
  418. Project.MSG_VERBOSE);
  419. exe.setCommandline(command);
  420. if (redirectorElement != null) {
  421. setupRedirector();
  422. redirectorElement.configure(redirector, name);
  423. }
  424. if (redirectorElement != null || haveExecuted) {
  425. // need to reset the stream handler to restart
  426. // reading of pipes;
  427. // go ahead and do it always w/ nested redirectors
  428. exe.setStreamHandler(redirector.createHandler());
  429. }
  430. runExecute(exe);
  431. haveExecuted = true;
  432. fileNames.removeAllElements();
  433. baseDirs.removeAllElements();
  434. }
  435. }
  436. }
  437. if (parallel && (fileNames.size() > 0 || !skipEmpty)) {
  438. runParallel(exe, fileNames, baseDirs);
  439. haveExecuted = true;
  440. }
  441. if (haveExecuted) {
  442. log("Applied " + cmdl.getExecutable() + " to "
  443. + totalFiles + " file"
  444. + (totalFiles != 1 ? "s" : "") + " and "
  445. + totalDirs + " director"
  446. + (totalDirs != 1 ? "ies" : "y") + ".",
  447. verbose ? Project.MSG_INFO : Project.MSG_VERBOSE);
  448. }
  449. } catch (IOException e) {
  450. throw new BuildException("Execute failed: " + e, e, getLocation());
  451. } finally {
  452. // close the output file if required
  453. logFlush();
  454. redirector.setAppendProperties(false);
  455. redirector.setProperties();
  456. }
  457. }
  458. /**
  459. * Construct the command line for parallel execution.
  460. *
  461. * @param srcFiles The filenames to add to the commandline.
  462. * @param baseDirs filenames are relative to this dir.
  463. * @return the command line in the form of a String[].
  464. */
  465. protected String[] getCommandline(String[] srcFiles, File[] baseDirs) {
  466. final char fileSeparator = File.separatorChar;
  467. Vector targets = new Vector();
  468. if (targetFilePos != null) {
  469. Hashtable addedFiles = new Hashtable();
  470. for (int i = 0; i < srcFiles.length; i++) {
  471. String[] subTargets = mapper.mapFileName(srcFiles[i]);
  472. if (subTargets != null) {
  473. for (int j = 0; j < subTargets.length; j++) {
  474. String name = null;
  475. if (!relative) {
  476. name = (new File(destDir, subTargets[j])).getAbsolutePath();
  477. } else {
  478. name = subTargets[j];
  479. }
  480. if (forwardSlash && fileSeparator != '/') {
  481. name = name.replace(fileSeparator, '/');
  482. }
  483. if (!addedFiles.contains(name)) {
  484. targets.addElement(name);
  485. addedFiles.put(name, name);
  486. }
  487. }
  488. }
  489. }
  490. }
  491. String[] targetFiles = new String[targets.size()];
  492. targets.copyInto(targetFiles);
  493. if (!addSourceFile) {
  494. srcFiles = new String[0];
  495. }
  496. String[] orig = cmdl.getCommandline();
  497. String[] result
  498. = new String[orig.length + srcFiles.length + targetFiles.length];
  499. int srcIndex = orig.length;
  500. if (srcFilePos != null) {
  501. srcIndex = srcFilePos.getPosition();
  502. }
  503. if (targetFilePos != null) {
  504. int targetIndex = targetFilePos.getPosition();
  505. if (srcIndex < targetIndex
  506. || (srcIndex == targetIndex && srcIsFirst)) {
  507. // 0 --> srcIndex
  508. System.arraycopy(orig, 0, result, 0, srcIndex);
  509. // srcIndex --> targetIndex
  510. System.arraycopy(orig, srcIndex, result,
  511. srcIndex + srcFiles.length,
  512. targetIndex - srcIndex);
  513. // targets are already absolute file names
  514. System.arraycopy(targetFiles, 0, result,
  515. targetIndex + srcFiles.length,
  516. targetFiles.length);
  517. // targetIndex --> end
  518. System.arraycopy(orig, targetIndex, result,
  519. targetIndex + srcFiles.length + targetFiles.length,
  520. orig.length - targetIndex);
  521. } else {
  522. // 0 --> targetIndex
  523. System.arraycopy(orig, 0, result, 0, targetIndex);
  524. // targets are already absolute file names
  525. System.arraycopy(targetFiles, 0, result,
  526. targetIndex,
  527. targetFiles.length);
  528. // targetIndex --> srcIndex
  529. System.arraycopy(orig, targetIndex, result,
  530. targetIndex + targetFiles.length,
  531. srcIndex - targetIndex);
  532. // srcIndex --> end
  533. System.arraycopy(orig, srcIndex, result,
  534. srcIndex + srcFiles.length + targetFiles.length,
  535. orig.length - srcIndex);
  536. srcIndex += targetFiles.length;
  537. }
  538. } else { // no targetFilePos
  539. // 0 --> srcIndex
  540. System.arraycopy(orig, 0, result, 0, srcIndex);
  541. // srcIndex --> end
  542. System.arraycopy(orig, srcIndex, result,
  543. srcIndex + srcFiles.length,
  544. orig.length - srcIndex);
  545. }
  546. // fill in source file names
  547. for (int i = 0; i < srcFiles.length; i++) {
  548. if (!relative) {
  549. result[srcIndex + i] =
  550. (new File(baseDirs[i], srcFiles[i])).getAbsolutePath();
  551. } else {
  552. result[srcIndex + i] = srcFiles[i];
  553. }
  554. if (forwardSlash && fileSeparator != '/') {
  555. result[srcIndex + i] =
  556. result[srcIndex + i].replace(fileSeparator, '/');
  557. }
  558. }
  559. return result;
  560. }
  561. /**
  562. * Construct the command line for serial execution.
  563. *
  564. * @param srcFile The filename to add to the commandline.
  565. * @param baseDir filename is relative to this dir.
  566. * @return the command line in the form of a String[].
  567. */
  568. protected String[] getCommandline(String srcFile, File baseDir) {
  569. return getCommandline(new String[] {srcFile}, new File[] {baseDir});
  570. }
  571. /**
  572. * Return the list of files from this DirectoryScanner that should
  573. * be included on the command line.
  574. * @param baseDir the File base directory.
  575. * @param ds the DirectoryScanner to use for file scanning.
  576. * @return a String[] containing the filenames.
  577. */
  578. protected String[] getFiles(File baseDir, DirectoryScanner ds) {
  579. return restrict(ds.getIncludedFiles(), baseDir);
  580. }
  581. /**
  582. * Return the list of Directories from this DirectoryScanner that
  583. * should be included on the command line.
  584. * @param baseDir the File base directory.
  585. * @param ds the DirectoryScanner to use for file scanning.
  586. * @return a String[] containing the directory names.
  587. */
  588. protected String[] getDirs(File baseDir, DirectoryScanner ds) {
  589. return restrict(ds.getIncludedDirectories(), baseDir);
  590. }
  591. /**
  592. * Return the list of files or directories from this FileList that
  593. * should be included on the command line.
  594. * @param list the FileList to check.
  595. * @return a String[] containing the directory names.
  596. *
  597. * @since Ant 1.6.2
  598. */
  599. protected String[] getFilesAndDirs(FileList list) {
  600. return restrict(list.getFiles(getProject()), list.getDir(getProject()));
  601. }
  602. private String[] restrict(String[] s, File baseDir) {
  603. return (mapper == null || force) ? s
  604. : new SourceFileScanner(this).restrict(s, baseDir, destDir, mapper);
  605. }
  606. /**
  607. * Run the command in "parallel" mode, making sure that at most
  608. * maxParallel sourcefiles get passed on the command line.
  609. * @param exe the Executable to use.
  610. * @param fileNames the Vector of filenames.
  611. * @param baseDirs the Vector of base directories corresponding to fileNames.
  612. * @throws IOException on I/O errors.
  613. * @throws BuildException on other errors.
  614. * @since Ant 1.6
  615. */
  616. protected void runParallel(Execute exe, Vector fileNames,
  617. Vector baseDirs)
  618. throws IOException, BuildException {
  619. String[] s = new String[fileNames.size()];
  620. fileNames.copyInto(s);
  621. File[] b = new File[baseDirs.size()];
  622. baseDirs.copyInto(b);
  623. if (maxParallel <= 0
  624. || s.length == 0 /* this is skipEmpty == false */) {
  625. String[] command = getCommandline(s, b);
  626. log(Commandline.describeCommand(command), Project.MSG_VERBOSE);
  627. exe.setCommandline(command);
  628. runExecute(exe);
  629. } else {
  630. int stillToDo = fileNames.size();
  631. int currentOffset = 0;
  632. while (stillToDo > 0) {
  633. int currentAmount = Math.min(stillToDo, maxParallel);
  634. String[] cs = new String[currentAmount];
  635. System.arraycopy(s, currentOffset, cs, 0, currentAmount);
  636. File[] cb = new File[currentAmount];
  637. System.arraycopy(b, currentOffset, cb, 0, currentAmount);
  638. String[] command = getCommandline(cs, cb);
  639. log(Commandline.describeCommand(command), Project.MSG_VERBOSE);
  640. exe.setCommandline(command);
  641. if (redirectorElement != null) {
  642. setupRedirector();
  643. redirectorElement.configure(redirector, null);
  644. }
  645. if (redirectorElement != null || currentOffset > 0) {
  646. // need to reset the stream handler to restart
  647. // reading of pipes;
  648. // go ahead and do it always w/ nested redirectors
  649. exe.setStreamHandler(redirector.createHandler());
  650. }
  651. runExecute(exe);
  652. stillToDo -= currentAmount;
  653. currentOffset += currentAmount;
  654. }
  655. }
  656. }
  657. /**
  658. * Enumerated attribute with the values "file", "dir" and "both"
  659. * for the type attribute.
  660. */
  661. public static class FileDirBoth extends EnumeratedAttribute {
  662. public static final String FILE = "file";
  663. public static final String DIR = "dir";
  664. /**
  665. * @see EnumeratedAttribute#getValues
  666. */
  667. public String[] getValues() {
  668. return new String[] {FILE, DIR, "both"};
  669. }
  670. }
  671. }