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 29 kB

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