Submitted by: Rami Ojares git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@276288 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -148,8 +148,9 @@ Peter Reilly | |||||
| Phillip Wells | Phillip Wells | ||||
| Pierre Delisle | Pierre Delisle | ||||
| Pierre Dittgen | Pierre Dittgen | ||||
| Raphael Pierquin | |||||
| R Handerson | R Handerson | ||||
| Rami Ojares | |||||
| Raphael Pierquin | |||||
| Richard Evans | Richard Evans | ||||
| Rick Beton | Rick Beton | ||||
| Robert Anderson | Robert Anderson | ||||
| @@ -32,6 +32,7 @@ import org.apache.tools.ant.BuildException; | |||||
| public abstract class AbstractSshMessage { | public abstract class AbstractSshMessage { | ||||
| private Session session; | private Session session; | ||||
| private boolean verbose; | |||||
| private LogListener listener = new LogListener() { | private LogListener listener = new LogListener() { | ||||
| public void log(String message) { | public void log(String message) { | ||||
| // do nothing; | // do nothing; | ||||
| @@ -39,6 +40,14 @@ public abstract class AbstractSshMessage { | |||||
| }; | }; | ||||
| public AbstractSshMessage(Session session) { | public AbstractSshMessage(Session session) { | ||||
| this(false, session); | |||||
| } | |||||
| /** | |||||
| * @since Ant 1.6.2 | |||||
| */ | |||||
| public AbstractSshMessage(boolean verbose, Session session) { | |||||
| this.verbose = verbose; | |||||
| this.session = session; | this.session = session; | ||||
| } | } | ||||
| @@ -114,4 +123,33 @@ public abstract class AbstractSshMessage { | |||||
| + " Average Rate: " + format.format(totalLength / duration) | + " Average Rate: " + format.format(totalLength / duration) | ||||
| + " B/s"); | + " B/s"); | ||||
| } | } | ||||
| /** | |||||
| * @since Ant 1.6.2 | |||||
| */ | |||||
| protected final boolean getVerbose() { | |||||
| return verbose; | |||||
| } | |||||
| /* | |||||
| * Track progress every 10% if 100kb < filesize < 1mb. For larger | |||||
| * files track progress for every percent transmitted. | |||||
| */ | |||||
| protected final int trackProgress(int filesize, int totalLength, | |||||
| int percentTransmitted) { | |||||
| int percent = (int) Math.round(Math.floor((totalLength | |||||
| / (double)filesize) | |||||
| * 100)); | |||||
| if (percent > percentTransmitted) { | |||||
| if (filesize < 1048576 && (percent % 10 != 0)) { | |||||
| // do not track between tenths | |||||
| } else { | |||||
| log("" + percent + "%"); | |||||
| } | |||||
| } | |||||
| return percent; | |||||
| } | |||||
| } | } | ||||
| @@ -40,6 +40,7 @@ public abstract class SSHBase extends Task implements LogListener { | |||||
| private String knownHosts; | private String knownHosts; | ||||
| private int port = SSH_PORT; | private int port = SSH_PORT; | ||||
| private boolean failOnError = true; | private boolean failOnError = true; | ||||
| private boolean verbose; | |||||
| private SSHUserInfo userInfo; | private SSHUserInfo userInfo; | ||||
| /** | /** | ||||
| @@ -71,6 +72,20 @@ public abstract class SSHBase extends Task implements LogListener { | |||||
| return failOnError; | return failOnError; | ||||
| } | } | ||||
| /** | |||||
| * @since Ant 1.6.2 | |||||
| */ | |||||
| public void setVerbose(boolean failure) { | |||||
| verbose = failure; | |||||
| } | |||||
| /** | |||||
| * @since Ant 1.6.2 | |||||
| */ | |||||
| public boolean getVerbose() { | |||||
| return verbose; | |||||
| } | |||||
| /** | /** | ||||
| * Username known to remote host. | * Username known to remote host. | ||||
| * | * | ||||
| @@ -140,7 +140,7 @@ public class Scp extends SSHBase { | |||||
| try { | try { | ||||
| session = openSession(); | session = openSession(); | ||||
| ScpFromMessage message = | ScpFromMessage message = | ||||
| new ScpFromMessage(session, file, | |||||
| new ScpFromMessage(getVerbose(), session, file, | |||||
| getProject().resolveFile(toPath), | getProject().resolveFile(toPath), | ||||
| fromSshUri.endsWith("*")); | fromSshUri.endsWith("*")); | ||||
| log("Receiving file: " + file); | log("Receiving file: " + file); | ||||
| @@ -169,7 +169,8 @@ public class Scp extends SSHBase { | |||||
| } | } | ||||
| if (!list.isEmpty()) { | if (!list.isEmpty()) { | ||||
| session = openSession(); | session = openSession(); | ||||
| ScpToMessage message = new ScpToMessage(session, list, file); | |||||
| ScpToMessage message = new ScpToMessage(getVerbose(), session, | |||||
| list, file); | |||||
| message.setLogListener(this); | message.setLogListener(this); | ||||
| message.execute(); | message.execute(); | ||||
| } | } | ||||
| @@ -188,8 +189,8 @@ public class Scp extends SSHBase { | |||||
| try { | try { | ||||
| session = openSession(); | session = openSession(); | ||||
| ScpToMessage message = | ScpToMessage message = | ||||
| new ScpToMessage(session, getProject().resolveFile(fromPath), | |||||
| file); | |||||
| new ScpToMessage(getVerbose(), session, | |||||
| getProject().resolveFile(fromPath), file); | |||||
| message.setLogListener(this); | message.setLogListener(this); | ||||
| message.execute(); | message.execute(); | ||||
| } finally { | } finally { | ||||
| @@ -38,14 +38,25 @@ public class ScpFromMessage extends AbstractSshMessage { | |||||
| private File localFile; | private File localFile; | ||||
| private boolean isRecursive = false; | private boolean isRecursive = false; | ||||
| /** | |||||
| * @since Ant 1.6.2 | |||||
| */ | |||||
| public ScpFromMessage(boolean verbose, | |||||
| Session session, | |||||
| String aRemoteFile, | |||||
| File aLocalFile, | |||||
| boolean recursive) { | |||||
| super(verbose, session); | |||||
| this.remoteFile = aRemoteFile; | |||||
| this.localFile = aLocalFile; | |||||
| this.isRecursive = recursive; | |||||
| } | |||||
| public ScpFromMessage(Session session, | public ScpFromMessage(Session session, | ||||
| String aRemoteFile, | String aRemoteFile, | ||||
| File aLocalFile, | File aLocalFile, | ||||
| boolean recursive) { | boolean recursive) { | ||||
| super(session); | |||||
| this.remoteFile = aRemoteFile; | |||||
| this.localFile = aLocalFile; | |||||
| this.isRecursive = recursive; | |||||
| this(false, session, aRemoteFile, aLocalFile, recursive); | |||||
| } | } | ||||
| public void execute() throws IOException, JSchException { | public void execute() throws IOException, JSchException { | ||||
| @@ -153,6 +164,14 @@ public class ScpFromMessage extends AbstractSshMessage { | |||||
| int length; | int length; | ||||
| int totalLength = 0; | int totalLength = 0; | ||||
| long startTime = System.currentTimeMillis(); | long startTime = System.currentTimeMillis(); | ||||
| // only track progress for files larger than 100kb in verbose mode | |||||
| boolean trackProgress = getVerbose() && filesize > 102400; | |||||
| // since filesize keeps on decreasing we have to store the | |||||
| // initial filesize | |||||
| int initFilesize = filesize; | |||||
| int percentTransmitted = 0; | |||||
| try { | try { | ||||
| while (true) { | while (true) { | ||||
| length = in.read(buf, 0, | length = in.read(buf, 0, | ||||
| @@ -166,6 +185,12 @@ public class ScpFromMessage extends AbstractSshMessage { | |||||
| if (filesize == 0) { | if (filesize == 0) { | ||||
| break; | break; | ||||
| } | } | ||||
| if (trackProgress) { | |||||
| percentTransmitted = trackProgress(initFilesize, | |||||
| totalLength, | |||||
| percentTransmitted); | |||||
| } | |||||
| } | } | ||||
| } finally { | } finally { | ||||
| long endTime = System.currentTimeMillis(); | long endTime = System.currentTimeMillis(); | ||||
| @@ -36,22 +36,50 @@ public class ScpToMessage extends AbstractSshMessage { | |||||
| private String remotePath; | private String remotePath; | ||||
| private List directoryList; | private List directoryList; | ||||
| public ScpToMessage(Session session, | |||||
| /** | |||||
| * @since Ant 1.6.2 | |||||
| */ | |||||
| public ScpToMessage(boolean verbose, | |||||
| Session session, | |||||
| File aLocalFile, | File aLocalFile, | ||||
| String aRemotePath) { | String aRemotePath) { | ||||
| super(session); | |||||
| this(verbose, session, aRemotePath); | |||||
| this.localFile = aLocalFile; | this.localFile = aLocalFile; | ||||
| } | |||||
| /** | |||||
| * @since Ant 1.6.2 | |||||
| */ | |||||
| public ScpToMessage(boolean verbose, | |||||
| Session session, | |||||
| List aDirectoryList, | |||||
| String aRemotePath) { | |||||
| this(verbose, session, aRemotePath); | |||||
| this.directoryList = aDirectoryList; | |||||
| } | |||||
| /** | |||||
| * @since Ant 1.6.2 | |||||
| */ | |||||
| private ScpToMessage(boolean verbose, | |||||
| Session session, | |||||
| String aRemotePath) { | |||||
| super(verbose, session); | |||||
| this.remotePath = aRemotePath; | this.remotePath = aRemotePath; | ||||
| } | } | ||||
| public ScpToMessage(Session session, | |||||
| File aLocalFile, | |||||
| String aRemotePath) { | |||||
| this(false, session, aLocalFile, aRemotePath); | |||||
| } | |||||
| public ScpToMessage(Session session, | public ScpToMessage(Session session, | ||||
| List aDirectoryList, | List aDirectoryList, | ||||
| String aRemotePath) { | String aRemotePath) { | ||||
| super(session); | |||||
| this.directoryList = aDirectoryList; | |||||
| this.remotePath = aRemotePath; | |||||
| this(false, session, aDirectoryList, aRemotePath); | |||||
| } | } | ||||
| public void execute() throws IOException, JSchException { | public void execute() throws IOException, JSchException { | ||||
| @@ -150,6 +178,14 @@ public class ScpToMessage extends AbstractSshMessage { | |||||
| byte[] buf = new byte[BUFFER_SIZE]; | byte[] buf = new byte[BUFFER_SIZE]; | ||||
| long startTime = System.currentTimeMillis(); | long startTime = System.currentTimeMillis(); | ||||
| int totalLength = 0; | int totalLength = 0; | ||||
| // only track progress for files larger than 100kb in verbose mode | |||||
| boolean trackProgress = getVerbose() && filesize > 102400; | |||||
| // since filesize keeps on decreasing we have to store the | |||||
| // initial filesize | |||||
| int initFilesize = filesize; | |||||
| int percentTransmitted = 0; | |||||
| try { | try { | ||||
| log("Sending: " + localFile.getName() + " : " + localFile.length()); | log("Sending: " + localFile.getName() + " : " + localFile.length()); | ||||
| while (true) { | while (true) { | ||||
| @@ -159,6 +195,12 @@ public class ScpToMessage extends AbstractSshMessage { | |||||
| } | } | ||||
| out.write(buf, 0, len); | out.write(buf, 0, len); | ||||
| totalLength += len; | totalLength += len; | ||||
| if (trackProgress) { | |||||
| percentTransmitted = trackProgress(initFilesize, | |||||
| totalLength, | |||||
| percentTransmitted); | |||||
| } | |||||
| } | } | ||||
| out.flush(); | out.flush(); | ||||
| sendAck(out); | sendAck(out); | ||||