Methods Summary |
---|
public void | addFileset(org.apache.tools.ant.types.FileSet set)Adds a FileSet tranfer to remote host. NOTE: Either
addFileSet() or setFile() are required. But, not both.
if (fileSets == null) {
fileSets = new LinkedList();
}
fileSets.add(set);
|
private Directory | createDirectory(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 void | download(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.BuildException | exactlyOne(java.lang.String[] attrs)
return exactlyOne(attrs, null);
|
private org.apache.tools.ant.BuildException | exactlyOne(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 void | execute()Execute this task.
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 void | init()Initialize this task.
super.init();
this.toUri = null;
this.fromUri = null;
this.fileSets = null;
|
private boolean | isRemoteUri(java.lang.String uri)
boolean isRemote = true;
int indexOfAt = uri.indexOf('@");
if (indexOfAt < 0) {
isRemote = false;
}
return isRemote;
|
private java.lang.String | parseUri(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 void | setFile(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/*
setFromUri(aFromUri);
this.isFromRemote = isRemoteUri(this.fromUri);
|
private void | setFromUri(java.lang.String fromUri)
if (this.fromUri != null) {
throw exactlyOne(FROM_ATTRS);
}
this.fromUri = fromUri;
|
public void | setLocalFile(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.
setFromUri(aFromUri);
this.isFromRemote = false;
|
public void | setLocalTodir(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.
setToUri(aToUri);
this.isToRemote = false;
|
public void | setLocalTofile(java.lang.String aToUri)Changes the file name to the given name while receiving it,
only useful if receiving a single file.
setToUri(aToUri);
this.isToRemote = false;
|
public void | setRemoteFile(java.lang.String aFromUri)Similiar to {@link #setFile setFile} but explicitly states that
the file is a remote file.
setFromUri(aFromUri);
this.isFromRemote = true;
|
public void | setRemoteTodir(java.lang.String aToUri)Similiar to {@link #setTodir setTodir} but explicitly states
that the directory is a remote.
setToUri(aToUri);
this.isToRemote = true;
|
public void | setRemoteTofile(java.lang.String aToUri)Changes the file name to the given name while sending it,
only useful if sending a single file.
setToUri(aToUri);
this.isToRemote = true;
|
public void | setSftp(boolean yesOrNo)Setting this to true to use sftp protocol.
isSftp = yesOrNo;
|
private void | setToUri(java.lang.String toUri)
if (this.toUri != null) {
throw exactlyOne(TO_ATTRS);
}
this.toUri = toUri;
|
public void | setTodir(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.
setToUri(aToUri);
this.isToRemote = isRemoteUri(this.toUri);
|
private void | upload(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 void | upload(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();
}
}
|