camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r1143954 - in /camel/branches/camel-2.7.x: ./ camel-core/src/main/java/org/apache/camel/component/file/ camel-core/src/main/java/org/apache/camel/component/file/strategy/ camel-core/src/test/java/org/apache/camel/component/file/strategy/
Date Thu, 07 Jul 2011 18:28:10 GMT
Author: dkulp
Date: Thu Jul  7 18:28:10 2011
New Revision: 1143954

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

........
  r1085905 | davsclaus | 2011-03-27 06:25:11 -0400 (Sun, 27 Mar 2011) | 1 line
  
  CAMEL-3789: Fixed marker file strategy not being thread safe. Thanks to Maria Iracheta for
the patch.
........

Added:
    camel/branches/camel-2.7.x/camel-core/src/test/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategyTest.java
      - copied unchanged from r1085905, camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategyTest.java
Modified:
    camel/branches/camel-2.7.x/   (props changed)
    camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
    camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileDeleteProcessStrategy.java
    camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileRenameProcessStrategy.java
    camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java

Propchange: camel/branches/camel-2.7.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Thu Jul  7 18:28:10 2011
@@ -1 +1 @@
-/camel/trunk:1083696,1084150,1086231,1087276,1087612,1087856,1088583,1088916-1088917,1089275,1090166,1090960-1090969,1091518,1091771,1091799,1092068,1092577,1092667,1093978,1094156,1095405,1095469,1095471,1095475-1095476,1097912,1099417,1102162,1104076,1124497,1127744,1127988,1131411,1134252,1134501,1135364,1139163,1143925
+/camel/trunk:1083696,1084150,1085905,1086231,1087276,1087612,1087856,1088583,1088916-1088917,1089275,1090166,1090960-1090969,1091518,1091771,1091799,1092068,1092577,1092667,1093978,1094156,1095405,1095469,1095471,1095475-1095476,1097912,1099417,1102162,1104076,1124497,1127744,1127988,1131411,1134252,1134501,1135364,1139163,1143925

Modified: camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java?rev=1143954&r1=1143953&r2=1143954&view=diff
==============================================================================
--- camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
(original)
+++ camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
Thu Jul  7 18:28:10 2011
@@ -54,7 +54,7 @@ public class FileOperations implements G
         this.endpoint = (FileEndpoint) endpoint;
     }
 
-    public boolean deleteFile(String name) throws GenericFileOperationFailedException { 
      
+    public boolean deleteFile(String name) throws GenericFileOperationFailedException {
         File file = new File(name);
         return FileUtil.deleteFile(file);
     }
@@ -71,7 +71,7 @@ public class FileOperations implements G
     }
 
     public boolean buildDirectory(String directory, boolean absolute) throws GenericFileOperationFailedException
{
-        ObjectHelper.notNull(endpoint, "endpoint");       
+        ObjectHelper.notNull(endpoint, "endpoint");
 
         // always create endpoint defined directory
         if (endpoint.isAutoCreate() && !endpoint.getFile().exists()) {
@@ -108,14 +108,17 @@ public class FileOperations implements G
             }
         }
 
-        if (path.isDirectory() && path.exists()) {
-            // the directory already exists
-            return true;
-        } else {
-            if (LOG.isTraceEnabled()) {
-                LOG.trace("Building directory: " + path);
+        // We need to make sure that this is thread-safe and only one thread tries to create
the path directory at the same time.
+        synchronized (this) {
+            if (path.isDirectory() && path.exists()) {
+                // the directory already exists
+                return true;
+            } else {
+                if (LOG.isTraceEnabled()) {
+                    LOG.trace("Building directory: " + path);
+                }
+                return path.mkdirs();
             }
-            return path.mkdirs();
         }
     }
 
@@ -174,7 +177,7 @@ public class FileOperations implements G
             // is the body file based
             File source = null;
             // get the File Object from in message
-            source = exchange.getIn().getBody(File.class);            
+            source = exchange.getIn().getBody(File.class);
 
             if (source != null) {
                 // okay we know the body is a file type
@@ -291,7 +294,7 @@ public class FileOperations implements G
 
     /**
      * Creates and prepares the output file channel. Will position itself in correct position
if the file is writable
-     *  eg. it should append or override any existing content.
+     * eg. it should append or override any existing content.
      */
     private FileChannel prepareOutputFileChannel(File target, FileChannel out) throws IOException
{
         if (endpoint.getFileExist() == GenericFileExist.Append) {

Modified: camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileDeleteProcessStrategy.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileDeleteProcessStrategy.java?rev=1143954&r1=1143953&r2=1143954&view=diff
==============================================================================
--- camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileDeleteProcessStrategy.java
(original)
+++ camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileDeleteProcessStrategy.java
Thu Jul  7 18:28:10 2011
@@ -29,19 +29,22 @@ public class GenericFileDeleteProcessStr
 
     @Override
     public boolean begin(GenericFileOperations<T> operations, GenericFileEndpoint<T>
endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
-        // must invoke super
-        boolean result = super.begin(operations, endpoint, exchange, file);
-        if (!result) {
-            return false;
-        }
+
+        // We need to invoke super, but to the file that we are going to use for processing,
so we do super after renaming.
+        GenericFile<T> to = file;
 
         if (beginRenamer != null) {
             GenericFile<T> newName = beginRenamer.renameFile(exchange, file);
-            GenericFile<T> to = renameFile(operations, file, newName);
+            to = renameFile(operations, file, newName);
             if (to != null) {
                 to.bindToExchange(exchange);
             }
         }
+        // must invoke super
+        boolean result = super.begin(operations, endpoint, exchange, to);
+        if (!result) {
+            return false;
+        }
 
         return true;
     }
@@ -81,7 +84,7 @@ public class GenericFileDeleteProcessStr
             throw new GenericFileOperationFailedException("Cannot delete file: " + file);
         }
     }
-    
+
     @Override
     public void rollback(GenericFileOperations<T> operations, GenericFileEndpoint<T>
endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
         // must invoke super
@@ -100,7 +103,7 @@ public class GenericFileDeleteProcessStr
             renameFile(operations, file, newName);
         }
     }
-    
+
     public GenericFileRenamer<T> getFailureRenamer() {
         return failureRenamer;
     }
@@ -116,4 +119,4 @@ public class GenericFileDeleteProcessStr
     public void setBeginRenamer(GenericFileRenamer<T> beginRenamer) {
         this.beginRenamer = beginRenamer;
     }
-}
\ No newline at end of file
+}

Modified: camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileRenameProcessStrategy.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileRenameProcessStrategy.java?rev=1143954&r1=1143953&r2=1143954&view=diff
==============================================================================
--- camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileRenameProcessStrategy.java
(original)
+++ camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileRenameProcessStrategy.java
Thu Jul  7 18:28:10 2011
@@ -31,19 +31,22 @@ public class GenericFileRenameProcessStr
 
     @Override
     public boolean begin(GenericFileOperations<T> operations, GenericFileEndpoint<T>
endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
-        // must invoke super
-        boolean result = super.begin(operations, endpoint, exchange, file);
-        if (!result) {
-            return false;
-        }
+
+        // We need to invoke super, but to the file that we are going to use for processing,
so we do super after renaming.
+        GenericFile<T> to = file;
 
         if (beginRenamer != null) {
             GenericFile<T> newName = beginRenamer.renameFile(exchange, file);
-            GenericFile<T> to = renameFile(operations, file, newName);
+            to = renameFile(operations, file, newName);
             if (to != null) {
                 to.bindToExchange(exchange);
             }
         }
+        // must invoke super
+        boolean result = super.begin(operations, endpoint, exchange, to);
+        if (!result) {
+            return false;
+        }
 
         return true;
     }

Modified: camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java?rev=1143954&r1=1143953&r2=1143954&view=diff
==============================================================================
--- camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java
(original)
+++ camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java
Thu Jul  7 18:28:10 2011
@@ -34,8 +34,6 @@ import org.slf4j.LoggerFactory;
  */
 public class MarkerFileExclusiveReadLockStrategy implements GenericFileExclusiveReadLockStrategy<File>
{
     private static final transient Logger LOG = LoggerFactory.getLogger(MarkerFileExclusiveReadLockStrategy.class);
-    private File lock;
-    private String lockFileName;
 
     public void prepareOnStartup(GenericFileOperations<File> operations, GenericFileEndpoint<File>
endpoint) {
         String dir = endpoint.getConfiguration().getDirectory();
@@ -50,33 +48,29 @@ public class MarkerFileExclusiveReadLock
 
     public boolean acquireExclusiveReadLock(GenericFileOperations<File> operations,
                                             GenericFile<File> file, Exchange exchange)
throws Exception {
-        lockFileName = file.getAbsoluteFilePath() + FileComponent.DEFAULT_LOCK_FILE_POSTFIX;
+        String lockFileName = getLockFileName(file);
         if (LOG.isTraceEnabled()) {
             LOG.trace("Locking the file: " + file + " using the lock file name: " + lockFileName);
         }
 
         // create a plain file as marker filer for locking (do not use FileLock)
-        lock = new File(lockFileName);
+        File lock = new File(lockFileName);
         boolean acquired = lock.createNewFile();
-        if (!acquired) {
-            lock = null;
-
-        }
 
         return acquired;
     }
 
     public void releaseExclusiveReadLock(GenericFileOperations<File> operations,
                                          GenericFile<File> file, Exchange exchange)
throws Exception {
-        if (lock != null) {
-            if (LOG.isTraceEnabled()) {
-                LOG.trace("Unlocking file: " + lockFileName);
-            }
+        String lockFileName = getLockFileName(file);
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Unlocking file: " + lockFileName);
+        }
+        File lock = new File(lockFileName);
 
-            boolean deleted = FileUtil.deleteFile(lock);
-            if (LOG.isTraceEnabled()) {
-                LOG.trace("Lock file: " + lockFileName + " was deleted: " + deleted);
-            }
+        boolean deleted = FileUtil.deleteFile(lock);
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Lock file: " + lockFileName + " was deleted: " + deleted);
         }
     }
 
@@ -107,4 +101,8 @@ public class MarkerFileExclusiveReadLock
         }
     }
 
+    private static String getLockFileName(GenericFile<File> file) {
+        return file.getAbsoluteFilePath() + FileComponent.DEFAULT_LOCK_FILE_POSTFIX;
+    }
+
 }



Mime
View raw message