1 18 19 package org.apache.tools.ant.taskdefs.optional.ssh; 20 21 import com.jcraft.jsch.Session; 22 import com.jcraft.jsch.ChannelSftp; 23 import com.jcraft.jsch.JSchException; 24 import com.jcraft.jsch.SftpException; 25 import com.jcraft.jsch.SftpProgressMonitor; 26 27 import java.io.File ; 28 import java.io.IOException ; 29 import java.util.List ; 30 import java.util.Iterator ; 31 32 35 public class ScpToMessageBySftp extends ScpToMessage { 36 37 private File localFile; 38 private String remotePath; 39 private List directoryList; 40 41 49 public ScpToMessageBySftp(boolean verbose, 50 Session session, 51 File aLocalFile, 52 String aRemotePath) { 53 this(verbose, session, aRemotePath); 54 55 this.localFile = aLocalFile; 56 } 57 58 66 public ScpToMessageBySftp(boolean verbose, 67 Session session, 68 List aDirectoryList, 69 String aRemotePath) { 70 this(verbose, session, aRemotePath); 71 72 this.directoryList = aDirectoryList; 73 } 74 75 82 private ScpToMessageBySftp(boolean verbose, 83 Session session, 84 String aRemotePath) { 85 super(verbose, session); 86 this.remotePath = aRemotePath; 87 } 88 89 95 public ScpToMessageBySftp(Session session, 96 File aLocalFile, 97 String aRemotePath) { 98 this(false, session, aLocalFile, aRemotePath); 99 } 100 101 107 public ScpToMessageBySftp(Session session, 108 List aDirectoryList, 109 String aRemotePath) { 110 this(false, session, aDirectoryList, aRemotePath); 111 } 112 113 118 public void execute() throws IOException , JSchException { 119 if (directoryList != null) { 120 doMultipleTransfer(); 121 } 122 if (localFile != null) { 123 doSingleTransfer(); 124 } 125 log("done.\n"); 126 } 127 128 private void doSingleTransfer() throws IOException , JSchException { 129 ChannelSftp channel = openSftpChannel(); 130 try { 131 channel.connect(); 132 try { 133 sendFileToRemote(channel, localFile, remotePath); 134 } catch (SftpException e) { 135 throw new JSchException(e.toString()); 136 } 137 } finally { 138 if (channel != null) { 139 channel.disconnect(); 140 } 141 } 142 } 143 144 private void doMultipleTransfer() throws IOException , JSchException { 145 ChannelSftp channel = openSftpChannel(); 146 try { 147 channel.connect(); 148 149 try { 150 channel.cd(remotePath); 151 for (Iterator i = directoryList.iterator(); i.hasNext();) { 152 Directory current = (Directory) i.next(); 153 sendDirectory(channel, current); 154 } 155 } catch (SftpException e) { 156 throw new JSchException(e.toString()); 157 } 158 } finally { 159 if (channel != null) { 160 channel.disconnect(); 161 } 162 } 163 } 164 165 private void sendDirectory(ChannelSftp channel, 166 Directory current) 167 throws IOException , SftpException { 168 for (Iterator fileIt = current.filesIterator(); fileIt.hasNext();) { 169 sendFileToRemote(channel, (File ) fileIt.next(), null); 170 } 171 for (Iterator dirIt = current.directoryIterator(); dirIt.hasNext();) { 172 Directory dir = (Directory) dirIt.next(); 173 sendDirectoryToRemote(channel, dir); 174 } 175 } 176 177 private void sendDirectoryToRemote(ChannelSftp channel, 178 Directory directory) 179 throws IOException , SftpException { 180 String dir = directory.getDirectory().getName(); 181 try { 182 channel.stat(dir); 183 } catch (SftpException e) { 184 if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) { 186 channel.mkdir(dir); 187 } 188 } 189 channel.cd(dir); 190 sendDirectory(channel, directory); 191 channel.cd(".."); 192 } 193 194 private void sendFileToRemote(ChannelSftp channel, 195 File localFile, 196 String remotePath) 197 throws IOException , SftpException { 198 long filesize = localFile.length(); 199 200 if (remotePath == null) { 201 remotePath = localFile.getName(); 202 } 203 204 long startTime = System.currentTimeMillis(); 205 long totalLength = filesize; 206 207 boolean trackProgress = getVerbose() && filesize > 102400; 209 210 SftpProgressMonitor monitor = null; 211 if (trackProgress) { 212 monitor = getProgressMonitor(); 213 } 214 215 try { 216 if (this.getVerbose()) { 217 log("Sending: " + localFile.getName() + " : " + filesize); 218 } 219 channel.put(localFile.getAbsolutePath(), remotePath, monitor); 220 } finally { 221 if (this.getVerbose()) { 222 long endTime = System.currentTimeMillis(); 223 logStats(startTime, endTime, (int) totalLength); 224 } 225 } 226 } 227 228 232 public File getLocalFile() { 233 return localFile; 234 } 235 236 240 public String getRemotePath() { 241 return remotePath; 242 } 243 } 244 | Popular Tags |