You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2011/05/01 12:44:32 UTC
svn commit: r1098249 - in
/camel/trunk/camel-core/src/main/java/org/apache/camel:
component/file/FileOperations.java component/file/GenericFileEndpoint.java
util/FileUtil.java
Author: davsclaus
Date: Sun May 1 10:44:32 2011
New Revision: 1098249
URL: http://svn.apache.org/viewvc?rev=1098249&view=rev
Log:
CAMEL-3793: File component will fallback and use a copy/delete approach if renmaing a file using native io fails.
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java?rev=1098249&r1=1098248&r2=1098249&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java Sun May 1 10:44:32 2011
@@ -261,7 +261,7 @@ public class FileOperations implements G
}
} finally {
IOHelper.close(in, source.getName(), LOG);
- IOHelper.close(out, source.getName(), LOG);
+ IOHelper.close(out, target.getName(), LOG);
}
}
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java?rev=1098249&r1=1098248&r2=1098249&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java Sun May 1 10:44:32 2011
@@ -61,7 +61,7 @@ public abstract class GenericFileEndpoin
protected boolean autoCreate = true;
protected boolean startingDirectoryMustExist;
protected boolean directoryMustExist;
- protected int bufferSize = 128 * 1024;
+ protected int bufferSize = FileUtil.BUFFER_SIZE;
protected GenericFileExist fileExist = GenericFileExist.Override;
protected boolean noop;
protected boolean recursive;
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java?rev=1098249&r1=1098248&r2=1098249&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java Sun May 1 10:44:32 2011
@@ -17,7 +17,10 @@
package org.apache.camel.util;
import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
import java.io.IOException;
+import java.nio.channels.FileChannel;
import java.util.Iterator;
import java.util.Locale;
import java.util.Random;
@@ -31,6 +34,8 @@ import org.slf4j.LoggerFactory;
*/
public final class FileUtil {
+ public static final int BUFFER_SIZE = 128 * 1024;
+
private static final transient Logger LOG = LoggerFactory.getLogger(FileUtil.class);
private static final int RETRY_SLEEP_MILLIS = 10;
private static File defaultTempDir;
@@ -326,12 +331,48 @@ public final class FileUtil {
count++;
}
+ // we could not rename using renameTo, so lets fallback and do a copy/delete approach.
+ // for example if you move files between different file systems (linux -> windows etc.)
+ if (!renamed) {
+ // now do a copy and delete as all rename attempts failed
+ try {
+ LOG.debug("Cannot rename file from: {} to: {}, will now use a copy/delete approach instead", from, to);
+ copyFile(from, to);
+ if (!deleteFile(from)) {
+ LOG.warn("Renaming file from: {} to: {} failed due cannot delete from file: {} after copy succeeded", new Object[]{from, to, from});
+ renamed = false;
+ }
+ renamed = true;
+ } catch (IOException e) {
+ LOG.debug("Error renaming file from: " + from + " to: " + to + " using copy/delete", e);
+ }
+ }
+
if (LOG.isDebugEnabled() && count > 0) {
LOG.debug("Tried {} to rename file: {} to: {} with result: {}", new Object[]{count, from, to, renamed});
}
return renamed;
}
+ public static void copyFile(File from, File to) throws IOException {
+ FileChannel in = new FileInputStream(from).getChannel();
+ FileChannel out = new FileOutputStream(to).getChannel();
+ try {
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Using FileChannel to copy from: " + in + " to: " + out);
+ }
+
+ long size = in.size();
+ long position = 0;
+ while (position < size) {
+ position += in.transferTo(position, BUFFER_SIZE, out);
+ }
+ } finally {
+ IOHelper.close(in, from.getName(), LOG);
+ IOHelper.close(out, to.getName(), LOG);
+ }
+ }
+
public static boolean deleteFile(File file) {
// do not try to delete non existing files
if (!file.exists()) {