From 6ddfaa33e4f30099b25d76f521239423324d67bc Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Mon, 28 Nov 2011 15:05:57 -0500 Subject: [PATCH] INT-2278: fixed the bug affecting renaming of files --- .../file/FileWritingMessageHandler.java | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/FileWritingMessageHandler.java b/spring-integration-file/src/main/java/org/springframework/integration/file/FileWritingMessageHandler.java index f3c282e057..e413ba9fb9 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/FileWritingMessageHandler.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/FileWritingMessageHandler.java @@ -205,7 +205,7 @@ public class FileWritingMessageHandler extends AbstractReplyProducingMessageHand } } FileCopyUtils.copy(sourceFile, tempFile); - tempFile.renameTo(resultFile); + this.renameTo(tempFile, resultFile); if (this.deleteSourceFiles) { sourceFile.delete(); } @@ -214,7 +214,7 @@ public class FileWritingMessageHandler extends AbstractReplyProducingMessageHand private File handleByteArrayMessage(byte[] bytes, File originalFile, File tempFile, File resultFile) throws IOException { FileCopyUtils.copy(bytes, tempFile); - tempFile.renameTo(resultFile); + this.renameTo(tempFile, resultFile); if (this.deleteSourceFiles && originalFile != null) { originalFile.delete(); } @@ -224,11 +224,33 @@ public class FileWritingMessageHandler extends AbstractReplyProducingMessageHand private File handleStringMessage(String content, File originalFile, File tempFile, File resultFile) throws IOException { OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(tempFile), this.charset); FileCopyUtils.copy(content, writer); - tempFile.renameTo(resultFile); + this.renameTo(tempFile, resultFile); if (this.deleteSourceFiles && originalFile != null) { originalFile.delete(); } return resultFile; } + + private void renameTo(File tempFile, File resultFile) throws IOException{ + Assert.notNull(resultFile, "'resultFile' must not be null"); + Assert.notNull(tempFile, "'tempFile' must not be null"); + + if (resultFile.exists()) { + if (resultFile.setWritable(true, false) && resultFile.delete()){ + if (!tempFile.renameTo(resultFile)) { + throw new IOException("Failed to rename file '" + tempFile.getAbsolutePath() + "' to '" + resultFile.getAbsolutePath() + "'"); + } + } + else { + throw new IOException("Failed to rename file '" + tempFile.getAbsolutePath() + "' to '" + resultFile.getAbsolutePath() + + "' since '" + resultFile.getName() + "' is not writable or can not be deleted"); + } + } + else { + if (!tempFile.renameTo(resultFile)) { + throw new IOException("Failed to rename file '" + tempFile.getAbsolutePath() + "' to '" + resultFile.getAbsolutePath() + "'"); + } + } + } }