/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Ant", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* .
*/
package org.apache.tools.ant.taskdefs;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.Mapper;
import org.apache.tools.ant.types.EnumeratedAttribute;
import org.apache.tools.ant.util.FileNameMapper;
import org.apache.tools.ant.util.SourceFileScanner;
import java.util.Hashtable;
import java.util.Vector;
import java.io.File;
import java.io.IOException;
/**
* Executes a given command, supplying a set of files as arguments.
*
* @author Stefan Bodewig
* @author Mariusz Nowostawski
*/
public class ExecuteOn extends ExecTask {
protected Vector filesets = new Vector();
private boolean relative = false;
private boolean parallel = false;
protected String type = "file";
protected Commandline.Marker srcFilePos = null;
private boolean skipEmpty = false;
protected Commandline.Marker targetFilePos = null;
protected Mapper mapperElement = null;
protected FileNameMapper mapper = null;
protected File destDir = null;
/**
* Has <srcfile> been specified before <targetfile>
*/
protected boolean srcIsFirst = true;
/**
* Adds a set of files (nested fileset attribute).
*/
public void addFileset(FileSet set) {
filesets.addElement(set);
}
/**
* Should filenames be returned as relative path names?
*/
public void setRelative(boolean relative) {
this.relative = relative;
}
/**
* Shall the command work on all specified files in parallel?
*/
public void setParallel(boolean parallel) {
this.parallel = parallel;
}
/**
* Shall the command work only on files, directories or both?
*/
public void setType(FileDirBoth type) {
this.type = type.getValue();
}
/**
* Should empty filesets be ignored?
*/
public void setSkipEmptyFilesets(boolean skip) {
skipEmpty = skip;
}
/**
* Set the destination directory.
*/
public void setDest(File destDir) {
this.destDir = destDir;
}
/**
* Marker that indicates where the name of the source file should
* be put on the command line.
*/
public Commandline.Marker createSrcfile() {
if (srcFilePos != null) {
throw new BuildException(taskType + " doesn\'t support multiple srcfile elements.",
location);
}
srcFilePos = cmdl.createMarker();
return srcFilePos;
}
/**
* Marker that indicates where the name of the target file should
* be put on the command line.
*/
public Commandline.Marker createTargetfile() {
if (targetFilePos != null) {
throw new BuildException(taskType + " doesn\'t support multiple targetfile elements.",
location);
}
targetFilePos = cmdl.createMarker();
srcIsFirst = (srcFilePos != null);
return targetFilePos;
}
/**
* Defines the FileNameMapper to use (nested mapper element).
*/
public Mapper createMapper() throws BuildException {
if (mapperElement != null) {
throw new BuildException("Cannot define more than one mapper",
location);
}
mapperElement = new Mapper(project);
return mapperElement;
}
protected void checkConfiguration() {
if ("execon".equals(taskName)) {
log("!! execon is deprecated. Use apply instead. !!");
}
super.checkConfiguration();
if (filesets.size() == 0) {
throw new BuildException("no filesets specified", location);
}
if (targetFilePos != null || mapperElement != null
|| destDir != null) {
if (mapperElement == null) {
throw new BuildException("no mapper specified", location);
}
if (mapperElement == null) {
throw new BuildException("no dest attribute specified",
location);
}
mapper = mapperElement.getImplementation();
}
}
protected void runExec(Execute exe) throws BuildException {
try {
Vector fileNames = new Vector();
Vector baseDirs = new Vector();
for (int i=0; i 0 || !skipEmpty)) {
String[] s = new String[fileNames.size()];
fileNames.copyInto(s);
File[] b = new File[baseDirs.size()];
baseDirs.copyInto(b);
String[] command = getCommandline(s, b);
log("Executing " + Commandline.toString(command),
Project.MSG_VERBOSE);
exe.setCommandline(command);
runExecute(exe);
}
} catch (IOException e) {
throw new BuildException("Execute failed: " + e, e, location);
} finally {
// close the output file if required
logFlush();
}
}
/**
* Construct the command line for parallel execution.
*
* @param srcFiles The filenames to add to the commandline
* @param baseDir filenames are relative to this dir
*/
protected String[] getCommandline(String[] srcFiles, File[] baseDirs) {
Vector targets = new Vector();
if (targetFilePos != null) {
Hashtable addedFiles = new Hashtable();
for (int i=0; i srcIndex
System.arraycopy(orig, 0, result, 0, srcIndex);
// srcIndex --> targetIndex
System.arraycopy(orig, srcIndex, result,
srcIndex + srcFiles.length,
targetIndex - srcIndex);
// targets are already absolute file names
System.arraycopy(targetFiles, 0, result,
targetIndex + srcFiles.length,
targetFiles.length);
// targetIndex --> end
System.arraycopy(orig, targetIndex, result,
targetIndex + srcFiles.length + targetFiles.length,
orig.length - targetIndex);
} else {
// 0 --> targetIndex
System.arraycopy(orig, 0, result, 0, targetIndex);
// targets are already absolute file names
System.arraycopy(targetFiles, 0, result,
targetIndex,
targetFiles.length);
// targetIndex --> srcIndex
System.arraycopy(orig, targetIndex, result,
targetIndex + targetFiles.length,
srcIndex - targetIndex);
// srcIndex --> end
System.arraycopy(orig, srcIndex, result,
srcIndex + srcFiles.length + targetFiles.length,
orig.length - srcIndex);
srcIndex += targetFiles.length;
}
} else { // no targetFilePos
// 0 --> srcIndex
System.arraycopy(orig, 0, result, 0, srcIndex);
// srcIndex --> end
System.arraycopy(orig, srcIndex, result,
srcIndex + srcFiles.length,
orig.length - srcIndex);
}
// fill in source file names
for (int i=0; i < srcFiles.length; i++) {
if (!relative) {
result[srcIndex+i] =
(new File(baseDirs[i], srcFiles[i])).getAbsolutePath();
} else {
result[srcIndex+i] = srcFiles[i];
}
}
return result;
}
/**
* Construct the command line for serial execution.
*
* @param srcFile The filename to add to the commandline
* @param baseDir filename is relative to this dir
*/
protected String[] getCommandline(String srcFile, File baseDir) {
return getCommandline(new String[] {srcFile}, new File[] {baseDir});
}
/**
* Return the list of files from this DirectoryScanner that should
* be included on the command line.
*/
protected String[] getFiles(File baseDir, DirectoryScanner ds) {
if (mapper != null) {
SourceFileScanner sfs = new SourceFileScanner(this);
return sfs.restrict(ds.getIncludedFiles(), baseDir, destDir,
mapper);
} else {
return ds.getIncludedFiles();
}
}
/**
* Return the list of Directories from this DirectoryScanner that
* should be included on the command line.
*/
protected String[] getDirs(File baseDir, DirectoryScanner ds) {
if (mapper != null) {
SourceFileScanner sfs = new SourceFileScanner(this);
return sfs.restrict(ds.getIncludedDirectories(), baseDir, destDir,
mapper);
} else {
return ds.getIncludedDirectories();
}
}
/**
* Enumerated attribute with the values "file", "dir" and "both"
* for the type attribute.
*/
public static class FileDirBoth extends EnumeratedAttribute {
public String[] getValues() {
return new String[] {"file", "dir", "both"};
}
}
}