--- ant/src/main/org/apache/tools/ant/taskdefs/optional/ssh/AbstractSshMessage.java 2005-10-13 04:42:41.000000000 +0000 +++ ant/src/main/org/apache/tools/ant/taskdefs/optional/ssh/AbstractSshMessage.java 2006-04-13 09:21:52.000000000 +0000 @@ -21,6 +21,10 @@ import com.jcraft.jsch.ChannelExec; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; +import com.jcraft.jsch.ChannelSftp; +import com.jcraft.jsch.SftpATTRS; +import com.jcraft.jsch.SftpException; +import com.jcraft.jsch.SftpProgressMonitor; import java.io.IOException; import java.io.OutputStream; @@ -75,6 +79,17 @@ } /** + * Open an ssh sftp channel. + * @return the channel + * @throws JSchException on error + */ + protected ChannelSftp openSftpChannel() throws JSchException { + ChannelSftp channel = (ChannelSftp) session.openChannel("sftp"); + + return channel; + } + + /** * Send an ack. * @param out the output stream to use * @throws IOException on error @@ -213,4 +228,32 @@ return percent; } + private ProgressMonitor monitor=null; + + ProgressMonitor getProgressMonitor(){ + if(monitor==null)monitor=new ProgressMonitor(); + return monitor; + } + class ProgressMonitor implements SftpProgressMonitor{ + long initFileSize=0; + long totalLength=0; + int percentTransmitted = 0; + public void init(int op, String src, String dest, long max){ + initFileSize=max; + totalLength=0; + percentTransmitted = 0; + } + public boolean count(long len){ + totalLength+=len; + percentTransmitted = trackProgress(initFileSize, + totalLength, + percentTransmitted); + return true; + } + public void end(){ + } + public long getTotalLength(){ + return totalLength; + } + }; } --- ant/src/main/org/apache/tools/ant/taskdefs/optional/ssh/Scp.java 2005-10-13 04:42:41.000000000 +0000 +++ ant/src/main/org/apache/tools/ant/taskdefs/optional/ssh/Scp.java 2006-04-17 07:23:20.000000000 +0000 @@ -44,6 +44,7 @@ private String toUri; private List fileSets = null; private boolean isFromRemote, isToRemote; + private boolean isSftp = false; /** * Sets the file to be transferred. This can either be a remote @@ -142,6 +143,15 @@ } /** + * Setting this to true to use sftp protocol. + * + * @param yesOrNo if true sftp protocol will be used. + */ + public void setSftp(boolean yesOrNo) { + isSftp=yesOrNo; + } + + /** * Adds a FileSet tranfer to remote host. NOTE: Either * addFileSet() or setFile() are required. But, not both. * @@ -213,10 +223,19 @@ Session session = null; try { session = openSession(); - ScpFromMessage message = - new ScpFromMessage(getVerbose(), session, file, - getProject().resolveFile(toPath), - fromSshUri.endsWith("*")); + 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(); @@ -243,8 +262,15 @@ } if (!list.isEmpty()) { session = openSession(); - ScpToMessage message = new ScpToMessage(getVerbose(), session, - list, file); + 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(); } @@ -262,9 +288,17 @@ Session session = null; try { session = openSession(); - ScpToMessage message = - new ScpToMessage(getVerbose(), session, - getProject().resolveFile(fromPath), file); + 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 { --- ant/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpFromMessage.java 2005-10-13 04:42:41.000000000 +0000 +++ ant/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpFromMessage.java 2006-04-13 09:22:50.000000000 +0000 @@ -41,6 +41,24 @@ private boolean isRecursive = false; /** + * Constructor for ScpFromMessage + * @param session the ssh session to use + */ + public ScpFromMessage(Session session) { + super(session); + } + + /** + * Constructor for ScpFromMessage + * @param verbose if true do verbose logging + * @param session the ssh session to use + * @since Ant 1.6.2 + */ + public ScpFromMessage(boolean verbose, Session session) { + super(verbose, session); + } + + /** * Constructor for ScpFromMessage. * @param verbose if true log extra information * @param session the Scp session to use --- ant/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpFromMessageBySftp.java 1970-01-01 00:00:00.000000000 +0000 +++ ant/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpFromMessageBySftp.java 2006-04-13 09:21:34.000000000 +0000 @@ -0,0 +1,177 @@ +/* + * Copyright 2003-2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant.taskdefs.optional.ssh; + +import java.io.File; +import java.io.IOException; +import java.io.EOFException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.FileOutputStream; +import java.io.ByteArrayOutputStream; + +import com.jcraft.jsch.JSchException; +import com.jcraft.jsch.Session; +import com.jcraft.jsch.Channel; +import com.jcraft.jsch.ChannelSftp; +import com.jcraft.jsch.SftpException; +import com.jcraft.jsch.SftpATTRS; +import com.jcraft.jsch.SftpProgressMonitor; + +/** + * A helper object representing an scp download. + */ +public class ScpFromMessageBySftp extends ScpFromMessage { + + private String remoteFile; + private File localFile; + private boolean isRecursive = false; + private boolean verbose = false; + + + /** + * Constructor for ScpFromMessageBySftp. + * @param verbose if true log extra information + * @param session the Scp session to use + * @param aRemoteFile the remote file name + * @param aLocalFile the local file + * @param recursive if true use recursion + * @since Ant 1.6.2 + */ + public ScpFromMessageBySftp(boolean verbose, + Session session, + String aRemoteFile, + File aLocalFile, + boolean recursive) { + super(verbose, session); + this.verbose = verbose; + this.remoteFile = aRemoteFile; + this.localFile = aLocalFile; + this.isRecursive = recursive; + } + + /** + * Constructor for ScpFromMessageBySftp. + * @param session the Scp session to use + * @param aRemoteFile the remote file name + * @param aLocalFile the local file + * @param recursive if true use recursion + */ + public ScpFromMessageBySftp(Session session, + String aRemoteFile, + File aLocalFile, + boolean recursive) { + this(false, session, aRemoteFile, aLocalFile, recursive); + } + + /** + * Carry out the transfer. + * @throws IOException on i/o errors + * @throws JSchException on errors detected by scp + */ + public void execute() throws IOException, JSchException { + ChannelSftp channel = openSftpChannel(); + try { + channel.connect(); + try { + SftpATTRS attrs=channel.stat(remoteFile); + if(attrs.isDir() && !remoteFile.endsWith("/")){ + remoteFile=remoteFile+"/"; + } + } catch(SftpException ee) { + } + getDir(channel, remoteFile, localFile); + } catch(SftpException e) { + throw new JSchException(e.toString()); + } finally { + if (channel != null) { + channel.disconnect(); + } + } + log("done\n"); + } + + private void getDir(ChannelSftp channel, + String remoteFile, + File localFile) throws IOException, SftpException { + String pwd=remoteFile; + if(remoteFile.lastIndexOf('/')!=-1){ + if(remoteFile.length()>1) + pwd=remoteFile.substring(0, remoteFile.lastIndexOf('/')); + } + channel.cd(pwd); + if(!localFile.exists()){ + localFile.mkdirs(); + } + java.util.Vector files=channel.ls(remoteFile); + for(int i=0; iFile.pathSeparator.length()) + new File(path.substring(0, i)).mkdirs(); + } + } + + if(localFile.isDirectory()){ + localFile=new File(localFile, remoteFile); + } + + long startTime = System.currentTimeMillis(); + long totalLength=le.getAttrs().getSize(); + + ProgressMonitor monitor=null; + boolean trackProgress = getVerbose() && totalLength > 102400; + if(trackProgress){ + monitor=getProgressMonitor(); + } + try{ + log("Receiving: " + remoteFile + " : " + le.getAttrs().getSize()); + channel.get(remoteFile, localFile.getAbsolutePath(), monitor); + } + finally{ + long endTime = System.currentTimeMillis(); +// if(monitor!=null && monitor.getTotalLength() 102400; + + ProgressMonitor monitor=null; + if(trackProgress){ + monitor=getProgressMonitor(); + } + + try{ + log("Sending: " + localFile.getName() + " : " + filesize); + channel.put(localFile.getAbsolutePath(), remotePath, monitor); + } + finally { + long endTime = System.currentTimeMillis(); +// if(monitor!=null && monitor.getTotalLength()