CAMEL-11742: File consumer - Delete orphan lock files on startup may not match 
a lock file when using include/antInclude filtering


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/aa79e7b5
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/aa79e7b5
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/aa79e7b5

Branch: refs/heads/camel-2.19.x
Commit: aa79e7b5bee0531fa4216407636a281bc421faf1
Parents: 68bc993
Author: Claus Ibsen <davscl...@apache.org>
Authored: Sun Sep 3 16:24:27 2017 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Sun Sep 3 16:26:41 2017 +0200

----------------------------------------------------------------------
 .../MarkerFileExclusiveReadLockStrategy.java    | 22 +++++-
 ...siveFilterDeleteOldLockFilesIncludeTest.java | 72 ++++++++++++++++++++
 ...leRecursiveFilterDeleteOldLockFilesTest.java |  4 ++
 3 files changed, 97 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/aa79e7b5/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java
 
b/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java
index 9d79094..845e27d 100644
--- 
a/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java
+++ 
b/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java
@@ -167,7 +167,21 @@ public class MarkerFileExclusiveReadLockStrategy 
implements GenericFileExclusive
 
             // filter unwanted files and directories to avoid traveling 
everything
             if (filter != null || antFilter != null || excludePattern != null 
|| includePattern != null) {
-                if (!acceptFile(file, endpointPath, filter, antFilter, 
excludePattern, includePattern)) {
+
+                File targetFile = file;
+
+                // if its a lock file then check if we accept its target file 
to know if we should delete the orphan lock file
+                if 
(file.getName().endsWith(FileComponent.DEFAULT_LOCK_FILE_POSTFIX)) {
+                    String target = file.getName().substring(0, 
file.getName().length() - FileComponent.DEFAULT_LOCK_FILE_POSTFIX.length());
+                    if (file.getParent() != null) {
+                        targetFile = new File(file.getParent(), target);
+                    } else {
+                        targetFile = new File(target);
+                    }
+                }
+
+                boolean accept = acceptFile(targetFile, endpointPath, filter, 
antFilter, excludePattern, includePattern);
+                if (!accept) {
                     continue;
                 }
             }
@@ -219,11 +233,17 @@ public class MarkerFileExclusiveReadLockStrategy 
implements GenericFileExclusive
         gf.setFileName(gf.getRelativeFilePath());
 
         if (filter != null) {
+            // a custom filter can also filter directories
             if (!filter.accept(gf)) {
                 return false;
             }
         }
 
+        // the following filters only works on files so allow any directory 
from this point
+        if (file.isDirectory()) {
+            return true;
+        }
+
         if (antFilter != null) {
             if (!antFilter.accept(gf)) {
                 return false;

http://git-wip-us.apache.org/repos/asf/camel/blob/aa79e7b5/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesIncludeTest.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesIncludeTest.java
 
b/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesIncludeTest.java
new file mode 100644
index 0000000..5dd7277
--- /dev/null
+++ 
b/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesIncludeTest.java
@@ -0,0 +1,72 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.file;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * @version 
+ */
+public class FileMarkerFileRecursiveFilterDeleteOldLockFilesIncludeTest 
extends ContextTestSupport {
+
+    @Override
+    protected void setUp() throws Exception {
+        deleteDirectory("target/oldlock");
+        super.setUp();
+    }
+
+    public void testDeleteOldLockOnStartup() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(2);
+        mock.expectedBodiesReceived("Bye World", "Hi World");
+        mock.message(0).header(Exchange.FILE_NAME_ONLY).isEqualTo("bye.txt");
+        
mock.message(1).header(Exchange.FILE_NAME_ONLY).isEqualTo("gooday.txt");
+        mock.expectedFileExists("target/oldlock/bar/davs.txt");
+        mock.expectedFileExists("target/oldlock/bar/davs.txt" + 
FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
+
+        template.sendBodyAndHeader("file:target/oldlock", "locked", 
Exchange.FILE_NAME, "hello.txt" + FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
+        template.sendBodyAndHeader("file:target/oldlock", "Bye World", 
Exchange.FILE_NAME, "bye.txt");
+        template.sendBodyAndHeader("file:target/oldlock/foo", "locked", 
Exchange.FILE_NAME, "gooday.txt" + FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
+        template.sendBodyAndHeader("file:target/oldlock/foo", "Hi World", 
Exchange.FILE_NAME, "gooday.txt");
+        template.sendBodyAndHeader("file:target/oldlock/bar", "locked", 
Exchange.FILE_NAME, "davs.txt" + FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
+        template.sendBodyAndHeader("file:target/oldlock/bar", "Davs World", 
Exchange.FILE_NAME, "davs.txt");
+
+        // start the route
+        context.startRoute("foo");
+
+        assertMockEndpointsSatisfied();
+
+        // the lock files should be gone
+        assertFileNotExists("target/oldlock/hello.txt." + 
FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
+        assertFileNotExists("target/oldlock/foo/hegooddayllo.txt." + 
FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                
from("file:target/oldlock?initialDelay=0&delay=10&recursive=true&sortBy=file:name&include=.*(hello.txt|bye.txt|gooday.txt)$").routeId("foo").noAutoStartup()
+                        .convertBodyTo(String.class).to("mock:result");
+            }
+        };
+    }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/aa79e7b5/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesTest.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesTest.java
 
b/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesTest.java
index cd20b3e..8060682 100644
--- 
a/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesTest.java
+++ 
b/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesTest.java
@@ -61,6 +61,10 @@ public class 
FileMarkerFileRecursiveFilterDeleteOldLockFilesTest extends Context
         context.startRoute("foo");
 
         assertMockEndpointsSatisfied();
+
+        // the lock files should be gone
+        assertFileNotExists("target/oldlock/hello.txt." + 
FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
+        assertFileNotExists("target/oldlock/foo/hegooddayllo.txt." + 
FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
     }
 
     @Override

Reply via email to