INT-1892 added support in SFTP for renaming files to file names that represent and existing file

This commit is contained in:
Oleg Zhurakousky
2011-05-05 10:05:40 -04:00
parent e338233bae
commit 937e681df7
2 changed files with 26 additions and 6 deletions

View File

@@ -21,6 +21,9 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.NestedIOException;
import org.springframework.integration.file.remote.session.Session;
import org.springframework.util.Assert;
@@ -41,9 +44,10 @@ import com.jcraft.jsch.SftpException;
* @since 2.0
*/
class SftpSession implements Session {
private final Log logger = LogFactory.getLog(this.getClass());
private volatile ChannelSftp channel;
private final com.jcraft.jsch.Session jschSession;
@@ -133,11 +137,28 @@ class SftpSession implements Session {
}
public void rename(String pathFrom, String pathTo) throws IOException {
try {
try {
this.channel.rename(pathFrom, pathTo);
} catch (SftpException e) {
throw new NestedIOException("failed to rename from " + pathFrom + " to " + pathTo, e);
}
catch (SftpException e) {
try {
logger.debug("Initial File rename failed, possibly becouse file already exists. Will attempt to delete file: "
+ pathTo + " and execute rename again.");
this.remove(pathTo);
logger.debug("Delete file: " + pathTo + " succeeded");
}
catch (Exception ex) {
// ignore since the exception might be thrown
}
try {
// attempt to rename again
this.channel.rename(pathFrom, pathTo);
}
catch (Exception e2) {
throw new NestedIOException("failed to rename from " + pathFrom + " to " + pathTo, e);
}
}
logger.debug("File: " + pathFrom + " was successfully renamed to " + pathTo);
}
public void mkdir(String directory) throws IOException {

View File

@@ -55,7 +55,6 @@ public class SftpInboundOutboundSanitySample {
}
new ClassPathXmlApplicationContext("SftpInboundReceiveSample-ignored.xml", this.getClass());
System.in.read();
Thread.sleep(5000);
fileA = new File("local-test-dir/a.test");
fileB = new File("local-test-dir/b.test");