1 18 19 package org.apache.tools.ant.taskdefs.optional.ssh; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 24 import com.jcraft.jsch.JSchException; 25 import com.jcraft.jsch.Session; 26 import com.jcraft.jsch.ChannelSftp; 27 import com.jcraft.jsch.SftpException; 28 import com.jcraft.jsch.SftpATTRS; 29 import com.jcraft.jsch.SftpProgressMonitor; 30 31 34 public class ScpFromMessageBySftp extends ScpFromMessage { 35 36 private String remoteFile; 37 private File localFile; 38 private boolean isRecursive = false; 39 private boolean verbose = false; 40 41 50 public ScpFromMessageBySftp(boolean verbose, 51 Session session, 52 String aRemoteFile, 53 File aLocalFile, 54 boolean recursive) { 55 super(verbose, session); 56 this.verbose = verbose; 57 this.remoteFile = aRemoteFile; 58 this.localFile = aLocalFile; 59 this.isRecursive = recursive; 60 } 61 62 69 public ScpFromMessageBySftp(Session session, 70 String aRemoteFile, 71 File aLocalFile, 72 boolean recursive) { 73 this(false, session, aRemoteFile, aLocalFile, recursive); 74 } 75 76 81 public void execute() throws IOException , JSchException { 82 ChannelSftp channel = openSftpChannel(); 83 try { 84 channel.connect(); 85 try { 86 SftpATTRS attrs = channel.stat(remoteFile); 87 if (attrs.isDir() && !remoteFile.endsWith("/")) { 88 remoteFile = remoteFile + "/"; 89 } 90 } catch (SftpException ee) { 91 } 93 getDir(channel, remoteFile, localFile); 94 } catch (SftpException e) { 95 throw new JSchException(e.toString()); 96 } finally { 97 if (channel != null) { 98 channel.disconnect(); 99 } 100 } 101 log("done\n"); 102 } 103 104 private void getDir(ChannelSftp channel, 105 String remoteFile, 106 File localFile) throws IOException , SftpException { 107 String pwd = remoteFile; 108 if (remoteFile.lastIndexOf('/') != -1) { 109 if (remoteFile.length() > 1) { 110 pwd = remoteFile.substring(0, remoteFile.lastIndexOf('/')); 111 } 112 } 113 channel.cd(pwd); 114 if (!localFile.exists()) { 115 localFile.mkdirs(); 116 } 117 java.util.Vector files = channel.ls(remoteFile); 118 for (int i = 0; i < files.size(); i++) { 119 ChannelSftp.LsEntry le = (ChannelSftp.LsEntry) files.elementAt(i); 120 String name = le.getFilename(); 121 if (le.getAttrs().isDir()) { 122 if (name.equals(".") || name.equals("..")) { 123 continue; 124 } 125 getDir(channel, 126 channel.pwd() + "/" + name + "/", 127 new File (localFile, le.getFilename())); 128 } else { 129 getFile(channel, le, localFile); 130 } 131 } 132 channel.cd(".."); 133 } 134 135 private void getFile(ChannelSftp channel, 136 ChannelSftp.LsEntry le, 137 File localFile) throws IOException , SftpException { 138 String remoteFile = le.getFilename(); 139 if (!localFile.exists()) { 140 String path = localFile.getAbsolutePath(); 141 int i = 0; 142 if ((i = path.lastIndexOf(File.pathSeparator)) != -1) { 143 if (path.length() > File.pathSeparator.length()) { 144 new File (path.substring(0, i)).mkdirs(); 145 } 146 } 147 } 148 149 if (localFile.isDirectory()) { 150 localFile = new File (localFile, remoteFile); 151 } 152 153 long startTime = System.currentTimeMillis(); 154 long totalLength = le.getAttrs().getSize(); 155 156 SftpProgressMonitor monitor = null; 157 boolean trackProgress = getVerbose() && totalLength > 102400; 158 if (trackProgress) { 159 monitor = getProgressMonitor(); 160 } 161 try { 162 log("Receiving: " + remoteFile + " : " + le.getAttrs().getSize()); 163 channel.get(remoteFile, localFile.getAbsolutePath(), monitor); 164 } finally { 165 long endTime = System.currentTimeMillis(); 166 logStats(startTime, endTime, (int) totalLength); 167 } 168 } 169 } 170 | Popular Tags |