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 2013/02/12 09:44:57 UTC

svn commit: r1445059 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/component/file/ main/java/org/apache/camel/component/file/strategy/ main/java/org/apache/camel/processor/idempotent/ main/java/org/apache/camel/util/ test/java/org/apache...

Author: davsclaus
Date: Tue Feb 12 08:44:57 2013
New Revision: 1445059

URL: http://svn.apache.org/r1445059
Log:
CAMEL-6069: Creating new file can cause problem if NAS. Use FileUtil to handle this.

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/strategy/MarkerFileExclusiveReadLockStrategy.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/idempotent/FileIdempotentRepository.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.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=1445059&r1=1445058&r2=1445059&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 Tue Feb 12 08:44:57 2013
@@ -404,7 +404,7 @@ public class FileOperations implements G
     private void writeFileEmptyBody(File target) throws IOException {
         if (!target.exists()) {
             LOG.debug("Creating new empty file: {}", target);
-            target.createNewFile();
+            FileUtil.createNewFile(target);
         } else if (endpoint.getFileExist() == GenericFileExist.Override) {
             LOG.debug("Truncating existing file: {}", target);
             FileChannel out = new FileOutputStream(target).getChannel();

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java?rev=1445059&r1=1445058&r2=1445059&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java Tue Feb 12 08:44:57 2013
@@ -57,8 +57,7 @@ public class MarkerFileExclusiveReadLock
         LOG.trace("Locking the file: {} using the lock file name: {}", file, lockFileName);
 
         // create a plain file as marker filer for locking (do not use FileLock)
-        File lock = new File(lockFileName);
-        boolean acquired = lock.createNewFile();
+        boolean acquired = FileUtil.createNewFile(new File(lockFileName));
         exchange.setProperty(Exchange.FILE_LOCK_FILE_ACQUIRED, acquired);
 
         return acquired;

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/idempotent/FileIdempotentRepository.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/idempotent/FileIdempotentRepository.java?rev=1445059&r1=1445058&r2=1445059&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/idempotent/FileIdempotentRepository.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/idempotent/FileIdempotentRepository.java Tue Feb 12 08:44:57 2013
@@ -28,6 +28,7 @@ import org.apache.camel.api.management.M
 import org.apache.camel.api.management.ManagedResource;
 import org.apache.camel.spi.IdempotentRepository;
 import org.apache.camel.support.ServiceSupport;
+import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.LRUCache;
 import org.apache.camel.util.ObjectHelper;
@@ -231,7 +232,7 @@ public class FileIdempotentRepository ex
         try {
             // create store if missing
             if (!fileStore.exists()) {
-                fileStore.createNewFile();
+                FileUtil.createNewFile(fileStore);
             }
             // append to store
             fos = new FileOutputStream(fileStore, true);

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=1445059&r1=1445058&r2=1445059&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 Tue Feb 12 08:44:57 2013
@@ -476,4 +476,23 @@ public final class FileUtil {
         return file.isAbsolute();
     }
 
+    /**
+     * Creates a new file.
+     *
+     * @param file the file
+     * @return <tt>true</tt> if created a new file, <tt>false</tt> otherwise
+     * @throws IOException is thrown if error creating the new file
+     */
+    public static boolean createNewFile(File file) throws IOException {
+        try {
+            return file.createNewFile();
+        } catch (IOException e) {
+            if (file.exists()) {
+                return true;
+            } else {
+                throw e;
+            }
+        }
+    }
+
 }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java?rev=1445059&r1=1445058&r2=1445059&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java Tue Feb 12 08:44:57 2013
@@ -192,4 +192,14 @@ public class FileUtilTest extends TestCa
         assertTrue("Should be a file", tmp.isFile());
     }
 
+    public void testCreateNewFile() throws Exception {
+        File file = new File("target/foo.txt");
+        if (file.exists()) {
+            FileUtil.deleteFile(file);
+        }
+
+        assertFalse("File should not exist " + file, file.exists());
+        assertTrue("A new file should be created " + file, FileUtil.createNewFile(file));
+    }
+
 }