FileDocCategorySizeDatePackage
Scp.javaAPI DocApache Ant 1.7013773Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant.taskdefs.optional.ssh

Scp

public class Scp extends SSHBase
Ant task for sending files to remote machine over ssh/scp.
since
Ant 1.6

Fields Summary
private static final String[]
FROM_ATTRS
private static final String[]
TO_ATTRS
private String
fromUri
private String
toUri
private List
fileSets
private boolean
isFromRemote
private boolean
isToRemote
private boolean
isSftp
Constructors Summary
Methods Summary
public voidaddFileset(org.apache.tools.ant.types.FileSet set)
Adds a FileSet tranfer to remote host. NOTE: Either addFileSet() or setFile() are required. But, not both.

param
set FileSet to send to remote host.

        if (fileSets == null) {
            fileSets = new LinkedList();
        }
        fileSets.add(set);
    
private DirectorycreateDirectory(org.apache.tools.ant.types.FileSet set)

        DirectoryScanner scanner = set.getDirectoryScanner(getProject());
        Directory root = new Directory(scanner.getBasedir());
        String[] files = scanner.getIncludedFiles();
        if (files.length != 0) {
            for (int j = 0; j < files.length; j++) {
                String[] path = Directory.getPath(files[j]);
                Directory current = root;
                File currentParent = scanner.getBasedir();
                for (int i = 0; i < path.length; i++) {
                    File file = new File(currentParent, path[i]);
                    if (file.isDirectory()) {
                        current.addDirectory(new Directory(file));
                        current = current.getChild(file);
                        currentParent = current.getDirectory();
                    } else if (file.isFile()) {
                        current.addFile(file);
                    }
                }
            }
        } else {
            // skip
            root = null;
        }
        return root;
    
private voiddownload(java.lang.String fromSshUri, java.lang.String toPath)

        String file = parseUri(fromSshUri);

        Session session = null;
        try {
            session = openSession();
            ScpFromMessage message = null;
            if (!isSftp) {
                message =
                    new ScpFromMessage(getVerbose(), session, file,
                                       getProject().resolveFile(toPath),
                                       fromSshUri.endsWith("*"));
            } else {
                message =
                    new ScpFromMessageBySftp(getVerbose(), session, file,
                                             getProject().resolveFile(toPath),
                                             fromSshUri.endsWith("*"));
            }
            log("Receiving file: " + file);
            message.setLogListener(this);
            message.execute();
        } finally {
            if (session != null) {
                session.disconnect();
            }
        }
    
private org.apache.tools.ant.BuildExceptionexactlyOne(java.lang.String[] attrs)

        return exactlyOne(attrs, null);
    
private org.apache.tools.ant.BuildExceptionexactlyOne(java.lang.String[] attrs, java.lang.String alt)

        StringBuffer buf = new StringBuffer("Exactly one of ").append(
                '[").append(attrs[0]);
        for (int i = 1; i < attrs.length; i++) {
            buf.append('|").append(attrs[i]);
        }
        buf.append(']");
        if (alt != null) {
            buf.append(" or ").append(alt);
        }
        return new BuildException(buf.append(" is required.").toString());
    
public voidexecute()
Execute this task.

throws
BuildException on error

        if (toUri == null) {
            throw exactlyOne(TO_ATTRS);
        }
        if (fromUri == null && fileSets == null) {
            throw exactlyOne(FROM_ATTRS, "one or more nested filesets");
        }
        try {
            if (isFromRemote && !isToRemote) {
                download(fromUri, toUri);
            } else if (!isFromRemote && isToRemote) {
                if (fileSets != null) {
                    upload(fileSets, toUri);
                } else {
                    upload(fromUri, toUri);
                }
            } else if (isFromRemote && isToRemote) {
                throw new BuildException(
                    "Copying from a remote server to a remote server is not supported.");
            } else {
                throw new BuildException("'todir' and 'file' attributes "
                    + "must have syntax like the following: "
                    + "user:password@host:/path");
            }
        } catch (Exception e) {
            if (getFailonerror()) {
                throw new BuildException(e);
            } else {
                log("Caught exception: " + e.getMessage(), Project.MSG_ERR);
            }
        }
    
public voidinit()
Initialize this task.

throws
BuildException on error

        super.init();
        this.toUri = null;
        this.fromUri = null;
        this.fileSets = null;
    
private booleanisRemoteUri(java.lang.String uri)

        boolean isRemote = true;
        int indexOfAt = uri.indexOf('@");
        if (indexOfAt < 0) {
            isRemote = false;
        }
        return isRemote;
    
private java.lang.StringparseUri(java.lang.String uri)

        int indexOfAt = uri.lastIndexOf('@");
        int indexOfColon = uri.indexOf(':");
        if (indexOfColon > -1 && indexOfColon < indexOfAt) {
            // user:password@host:/path notation
            setUsername(uri.substring(0, indexOfColon));
            setPassword(uri.substring(indexOfColon + 1, indexOfAt));
        } else {
            // no password, will require passphrase
            setUsername(uri.substring(0, indexOfAt));
        }

        if (getUserInfo().getPassword() == null
            && getUserInfo().getPassphrase() == null) {
            throw new BuildException("neither password nor passphrase for user "
                                     + getUserInfo().getName() + " has been "
                                     + "given.  Can't authenticate.");
        }

        int indexOfPath = uri.indexOf(':", indexOfAt + 1);
        if (indexOfPath == -1) {
            throw new BuildException("no remote path in " + uri);
        }

        setHost(uri.substring(indexOfAt + 1, indexOfPath));
        String remotePath = uri.substring(indexOfPath + 1);
        if (remotePath.equals("")) {
            remotePath = ".";
        }
        return remotePath;
    
public voidsetFile(java.lang.String aFromUri)
Sets the file to be transferred. This can either be a remote file or a local file. Remote files take the form:
user:password@host:/directory/path/file.example
Files to transfer can also include a wildcard to include all files in a remote directory. For example:
user:password@host:/directory/path/*

param
aFromUri a string representing the file to transfer.


                                                               
        
        setFromUri(aFromUri);
        this.isFromRemote = isRemoteUri(this.fromUri);
    
private voidsetFromUri(java.lang.String fromUri)

        if (this.fromUri != null) {
            throw exactlyOne(FROM_ATTRS);
        }
        this.fromUri = fromUri;
    
public voidsetLocalFile(java.lang.String aFromUri)
Similiar to {@link #setFile setFile} but explicitly states that the file is a local file. This is the only way to specify a local file with a @ character.

param
aFromUri a string representing the source of the copy.
since
Ant 1.6.2

        setFromUri(aFromUri);
        this.isFromRemote = false;
    
public voidsetLocalTodir(java.lang.String aToUri)
Similiar to {@link #setTodir setTodir} but explicitly states that the directory is a local. This is the only way to specify a local directory with a @ character.

param
aToUri a string representing the target of the copy.
since
Ant 1.6.2

        setToUri(aToUri);
        this.isToRemote = false;
    
public voidsetLocalTofile(java.lang.String aToUri)
Changes the file name to the given name while receiving it, only useful if receiving a single file.

param
aToUri a string representing the target of the copy.
since
Ant 1.6.2

        setToUri(aToUri);
        this.isToRemote = false;
    
public voidsetRemoteFile(java.lang.String aFromUri)
Similiar to {@link #setFile setFile} but explicitly states that the file is a remote file.

param
aFromUri a string representing the source of the copy.
since
Ant 1.6.2

        setFromUri(aFromUri);
        this.isFromRemote = true;
     
public voidsetRemoteTodir(java.lang.String aToUri)
Similiar to {@link #setTodir setTodir} but explicitly states that the directory is a remote.

param
aToUri a string representing the target of the copy.
since
Ant 1.6.2

        setToUri(aToUri);
        this.isToRemote = true;
    
public voidsetRemoteTofile(java.lang.String aToUri)
Changes the file name to the given name while sending it, only useful if sending a single file.

param
aToUri a string representing the target of the copy.
since
Ant 1.6.2

        setToUri(aToUri);
        this.isToRemote = true;
    
public voidsetSftp(boolean yesOrNo)
Setting this to true to use sftp protocol.

param
yesOrNo if true sftp protocol will be used.

        isSftp = yesOrNo;
    
private voidsetToUri(java.lang.String toUri)

        if (this.toUri != null) {
            throw exactlyOne(TO_ATTRS);
        }
        this.toUri = toUri;
    
public voidsetTodir(java.lang.String aToUri)
Sets the location where files will be transferred to. This can either be a remote directory or a local directory. Remote directories take the form of:
user:password@host:/directory/path/
This parameter is required.

param
aToUri a string representing the target of the copy.

        setToUri(aToUri);
        this.isToRemote = isRemoteUri(this.toUri);
    
private voidupload(java.util.List fileSet, java.lang.String toSshUri)

        String file = parseUri(toSshUri);

        Session session = null;
        try {
            List list = new ArrayList(fileSet.size());
            for (Iterator i = fileSet.iterator(); i.hasNext();) {
                FileSet set = (FileSet) i.next();
                Directory d = createDirectory(set);
                if (d != null) {
                    list.add(d);
                }
            }
            if (!list.isEmpty()) {
                session = openSession();
                ScpToMessage message = null;
                if (!isSftp) {
                    message = new ScpToMessage(getVerbose(), session,
                                               list, file);
                } else {
                    message = new ScpToMessageBySftp(getVerbose(), session,
                                                     list, file);
                }
                message.setLogListener(this);
                message.execute();
            }
        } finally {
            if (session != null) {
                session.disconnect();
            }
        }
    
private voidupload(java.lang.String fromPath, java.lang.String toSshUri)

        String file = parseUri(toSshUri);

        Session session = null;
        try {
            session = openSession();
            ScpToMessage message = null;
            if (!isSftp) {
                message =
                    new ScpToMessage(getVerbose(), session,
                                     getProject().resolveFile(fromPath), file);
            } else {
                message =
                    new ScpToMessageBySftp(getVerbose(), session,
                                           getProject().resolveFile(fromPath),
                                           file);
            }
            message.setLogListener(this);
            message.execute();
        } finally {
            if (session != null) {
                session.disconnect();
            }
        }