Author: dkulp
Date: Mon Oct  3 18:24:44 2011
New Revision: 1178493

URL: http://svn.apache.org/viewvc?rev=1178493&view=rev
Log:
Merged revisions 1178035 via svnmerge from 
https://svn.apache.org/repos/asf/camel/trunk

........
  r1178035 | davsclaus | 2011-10-01 11:58:30 -0400 (Sat, 01 Oct 2011) | 1 line
  
  CAMEL-4505: Added option copyAndDeleteOnRenameFail to file component.
........

Added:
    
camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerPreMoveCopyAndDeleteOnRenameFailFalseTest.java
      - copied unchanged from r1178035, 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerPreMoveCopyAndDeleteOnRenameFailFalseTest.java
Modified:
    camel/branches/camel-2.8.x/   (props changed)
    
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
    
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
    
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/impl/DefaultHeaderFilterStrategy.java
    
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/util/FileUtil.java

Propchange: camel/branches/camel-2.8.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: 
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java?rev=1178493&r1=1178492&r2=1178493&view=diff
==============================================================================
--- 
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
 (original)
+++ 
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
 Mon Oct  3 18:24:44 2011
@@ -35,6 +35,7 @@ public class FileEndpoint extends Generi
 
     private FileOperations operations = new FileOperations(this);
     private File file;
+    private boolean copyAndDeleteOnRenameFail = true;
 
     public FileEndpoint() {
         // use marker file as default exclusive read locks
@@ -137,4 +138,11 @@ public class FileEndpoint extends Generi
         return FileUtil.isAbsolute(new File(name));
     }
 
+    public boolean isCopyAndDeleteOnRenameFail() {
+        return copyAndDeleteOnRenameFail;
+    }
+
+    public void setCopyAndDeleteOnRenameFail(boolean 
copyAndDeleteOnRenameFail) {
+        this.copyAndDeleteOnRenameFail = copyAndDeleteOnRenameFail;
+    }
 }

Modified: 
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java?rev=1178493&r1=1178492&r2=1178493&view=diff
==============================================================================
--- 
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
 (original)
+++ 
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
 Mon Oct  3 18:24:44 2011
@@ -62,7 +62,11 @@ public class FileOperations implements G
     public boolean renameFile(String from, String to) throws 
GenericFileOperationFailedException {
         File file = new File(from);
         File target = new File(to);
-        return FileUtil.renameFile(file, target);
+        try {
+            return FileUtil.renameFile(file, target, 
endpoint.isCopyAndDeleteOnRenameFail());
+        } catch (IOException e) {
+            throw new GenericFileOperationFailedException("Error renaming file 
from " + from + " to " + to, e);
+        }
     }
 
     public boolean existsFile(String name) throws 
GenericFileOperationFailedException {
@@ -234,10 +238,10 @@ public class FileOperations implements G
         }
     }
 
-    private boolean writeFileByLocalWorkPath(File source, File file) {
+    private boolean writeFileByLocalWorkPath(File source, File file) throws 
IOException {
         LOG.trace("Using local work file being renamed from: {} to: {}", 
source, file);
 
-        return FileUtil.renameFile(source, file);
+        return FileUtil.renameFile(source, file, 
endpoint.isCopyAndDeleteOnRenameFail());
     }
 
     private void writeFileByFile(File source, File target) throws IOException {

Modified: 
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/impl/DefaultHeaderFilterStrategy.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/impl/DefaultHeaderFilterStrategy.java?rev=1178493&r1=1178492&r2=1178493&view=diff
==============================================================================
--- 
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/impl/DefaultHeaderFilterStrategy.java
 (original)
+++ 
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/impl/DefaultHeaderFilterStrategy.java
 Mon Oct  3 18:24:44 2011
@@ -92,7 +92,6 @@ public class DefaultHeaderFilterStrategy
     public String getOutFilterPattern() {
         return outFilterPattern == null ? null : outFilterPattern.pattern();
     }
-    
 
     /**
      * Sets the "out" direction filter regular expression {@link Pattern}.  The
@@ -162,7 +161,7 @@ public class DefaultHeaderFilterStrategy
     }
 
     /**
-     * Gets the isLowercase property which is a boolean to determinte
+     * Gets the isLowercase property which is a boolean to determine
      * whether header names should be converted to lowercase before
      * checking it the filter Set.  It does not affect filtering using
      * regular expression pattern.
@@ -172,7 +171,7 @@ public class DefaultHeaderFilterStrategy
     }
     
     /**
-     * Sets the isLowercase property which is a boolean to determinte
+     * Sets the isLowercase property which is a boolean to determine
      * whether header names should be converted to lowercase before
      * checking it the filter Set.  It does not affect filtering using
      * regular expression pattern.
@@ -235,4 +234,5 @@ public class DefaultHeaderFilterStrategy
             
         return false;
     }
+
 }

Modified: 
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/util/FileUtil.java?rev=1178493&r1=1178492&r2=1178493&view=diff
==============================================================================
--- 
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
 (original)
+++ 
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
 Mon Oct  3 18:24:44 2011
@@ -305,7 +305,33 @@ public final class FileUtil {
         }
     }
 
+    /**
+     * Renames a file.
+     *
+     * @param from the from file
+     * @param to   the to file
+     * @return <tt>true</tt> if the file was renamed, otherwise <tt>false</tt>
+     * @throws java.io.IOException is thrown if error renaming file
+     */
     public static boolean renameFile(File from, File to) {
+        try {
+            return renameFile(from, to, true);
+        } catch (IOException ioex) {
+            LOG.debug("Error renaming file from: " + from + " to: " + to + " 
using copy/delete", ioex);
+        }
+        return false;
+    }
+
+    /**
+     * Renames a file.
+     *
+     * @param from the from file
+     * @param to   the to file
+     * @param copyAndDeleteOnRenameFail whether to fallback and do copy and 
delete, if renameTo fails
+     * @return <tt>true</tt> if the file was renamed, otherwise <tt>false</tt>
+     * @throws java.io.IOException is thrown if error renaming file
+     */
+    public static boolean renameFile(File from, File to, boolean 
copyAndDeleteOnRenameFail) throws IOException {
         // do not try to rename non existing files
         if (!from.exists()) {
             return false;
@@ -333,18 +359,14 @@ public final class FileUtil {
 
         // 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) {
+        if (!renamed && copyAndDeleteOnRenameFail) {
             // 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;
-                }
+            LOG.debug("Cannot rename file from: {} to: {}, will now use a 
copy/delete approach instead", from, to);
+            copyFile(from, to);
+            if (!deleteFile(from)) {
+                throw new IOException("Renaming file from: " + from + " to: " 
+ to + " failed due cannot delete from file: " + from + " after copy 
succeeded");
+            } else {
                 renamed = true;
-            } catch (IOException e) {
-                LOG.debug("Error renaming file from: " + from + " to: " + to + 
" using copy/delete", e);
             }
         }
 


Reply via email to