From 937e681df786379fd0b98c79662e4c95243e558e Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Thu, 5 May 2011 10:05:40 -0400 Subject: [PATCH] INT-1892 added support in SFTP for renaming files to file names that represent and existing file --- .../integration/sftp/session/SftpSession.java | 31 ++++++++++++++++--- .../SftpInboundOutboundSanitySample.java | 1 - 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SftpSession.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SftpSession.java index 27faf75261..6067b5338e 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SftpSession.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SftpSession.java @@ -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 { diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/SftpInboundOutboundSanitySample.java b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/SftpInboundOutboundSanitySample.java index c9b97bcf96..c01be69628 100644 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/SftpInboundOutboundSanitySample.java +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/SftpInboundOutboundSanitySample.java @@ -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");