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 2017/02/28 10:00:56 UTC

[1/3] camel git commit: CAMEL-10782: FTP: cannot get files from users home with readlock changed. Thanks to Stefan Roos for finding the bug.

Repository: camel
Updated Branches:
  refs/heads/camel-2.17.x 1ddca5362 -> 932365f12
  refs/heads/camel-2.18.x 94f108832 -> 8d356854a
  refs/heads/master ce490fa52 -> 7769e6cdd


CAMEL-10782: FTP: cannot get files from users home with readlock changed. Thanks to Stefan Roos for finding the bug.


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

Branch: refs/heads/master
Commit: 7769e6cdd100b09405f9187b6773a75c472d7683
Parents: ce490fa
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Feb 28 10:58:04 2017 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Feb 28 10:59:10 2017 +0100

----------------------------------------------------------------------
 .../FtpChangedExclusiveReadLockStrategy.java    | 24 ++++--
 .../SftpChangedExclusiveReadLockStrategy.java   | 25 ++++--
 ...hangedRootDirReadLockFastExistCheckTest.java | 28 +++++++
 .../remote/FtpChangedRootDirReadLockTest.java   | 83 ++++++++++++++++++++
 4 files changed, 149 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/7769e6cd/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/FtpChangedExclusiveReadLockStrategy.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/FtpChangedExclusiveReadLockStrategy.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/FtpChangedExclusiveReadLockStrategy.java
index ba9093d..cca7426 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/FtpChangedExclusiveReadLockStrategy.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/FtpChangedExclusiveReadLockStrategy.java
@@ -53,7 +53,7 @@ public class FtpChangedExclusiveReadLockStrategy implements GenericFileExclusive
         long lastModified = Long.MIN_VALUE;
         long length = Long.MIN_VALUE;
         StopWatch watch = new StopWatch();
-        long startTime = (new Date()).getTime();
+        long startTime = new Date().getTime();
 
         while (!exclusive) {
             // timeout check
@@ -74,12 +74,26 @@ public class FtpChangedExclusiveReadLockStrategy implements GenericFileExclusive
             if (fastExistsCheck) {
                 // use the absolute file path to only pickup the file we want to check, this avoids expensive
                 // list operations if we have a lot of files in the directory
-                LOG.trace("Using fast exists to update file information for {}", file);
-                files = operations.listFiles(file.getAbsoluteFilePath());
+                String path = file.getAbsoluteFilePath();
+                if (path.equals("/") || path.equals("\\")) {
+                    // special for root (= home) directory
+                    LOG.trace("Using fast exists to update file information in home directory");
+                    files = operations.listFiles();
+                } else {
+                    LOG.trace("Using fast exists to update file information for {}", path);
+                    files = operations.listFiles(path);
+                }
             } else {
-                LOG.trace("Using full directory listing to update file information for {}. Consider enabling fastExistsCheck option.", file);
                 // fast option not enabled, so list the directory and filter the file name
-                files = operations.listFiles(file.getParent());
+                String path = file.getParent();
+                if (path.equals("/") || path.equals("\\")) {
+                    // special for root (= home) directory
+                    LOG.trace("Using full directory listing in home directory to update file information. Consider enabling fastExistsCheck option.");
+                    files = operations.listFiles();
+                } else {
+                    LOG.trace("Using full directory listing to update file information for {}. Consider enabling fastExistsCheck option.", path);
+                    files = operations.listFiles(path);
+                }
             }
             LOG.trace("List files {} found {} files", file.getAbsoluteFilePath(), files.size());
             for (FTPFile f : files) {

http://git-wip-us.apache.org/repos/asf/camel/blob/7769e6cd/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/SftpChangedExclusiveReadLockStrategy.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/SftpChangedExclusiveReadLockStrategy.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/SftpChangedExclusiveReadLockStrategy.java
index 1efe642..49b8363 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/SftpChangedExclusiveReadLockStrategy.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/SftpChangedExclusiveReadLockStrategy.java
@@ -53,7 +53,7 @@ public class SftpChangedExclusiveReadLockStrategy implements GenericFileExclusiv
         long lastModified = Long.MIN_VALUE;
         long length = Long.MIN_VALUE;
         StopWatch watch = new StopWatch();
-        long startTime = (new Date()).getTime();
+        long startTime = new Date().getTime();
 
         while (!exclusive) {
             // timeout check
@@ -73,12 +73,25 @@ public class SftpChangedExclusiveReadLockStrategy implements GenericFileExclusiv
             if (fastExistsCheck) {
                 // use the absolute file path to only pickup the file we want to check, this avoids expensive
                 // list operations if we have a lot of files in the directory
-                LOG.trace("Using fast exists to update file information for {}", file);
-                files = operations.listFiles(file.getAbsoluteFilePath());
+                String path = file.getAbsoluteFilePath();
+                if (path.equals("/") || path.equals("\\")) {
+                    // special for root (= home) directory
+                    LOG.trace("Using fast exists to update file information in home directory");
+                    files = operations.listFiles();
+                } else {
+                    LOG.trace("Using fast exists to update file information for {}", path);
+                    files = operations.listFiles(path);
+                }
             } else {
-                LOG.trace("Using full directory listing to update file information for {}. Consider enabling fastExistsCheck option.", file);
-                // fast option not enabled, so list the directory and filter the file name
-                files = operations.listFiles(file.getParent());
+                String path = file.getParent();
+                if (path.equals("/") || path.equals("\\")) {
+                    // special for root (= home) directory
+                    LOG.trace("Using full directory listing in home directory to update file information. Consider enabling fastExistsCheck option.");
+                    files = operations.listFiles();
+                } else {
+                    LOG.trace("Using full directory listing to update file information for {}. Consider enabling fastExistsCheck option.", path);
+                    files = operations.listFiles(path);
+                }
             }
             LOG.trace("List files {} found {} files", file.getAbsoluteFilePath(), files.size());
             for (ChannelSftp.LsEntry f : files) {

http://git-wip-us.apache.org/repos/asf/camel/blob/7769e6cd/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpChangedRootDirReadLockFastExistCheckTest.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpChangedRootDirReadLockFastExistCheckTest.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpChangedRootDirReadLockFastExistCheckTest.java
new file mode 100644
index 0000000..f9876f8
--- /dev/null
+++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpChangedRootDirReadLockFastExistCheckTest.java
@@ -0,0 +1,28 @@
+/**
+ * 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.remote;
+
+/**
+ *
+ */
+public class FtpChangedRootDirReadLockFastExistCheckTest extends FtpChangedRootDirReadLockTest {
+
+    protected String getFtpUrl() {
+        return "ftp://admin@localhost:" + getPort() + "/?password=admin&readLock=changed&readLockCheckInterval=1000&delete=true&fastExistsCheck=true";
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/7769e6cd/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpChangedRootDirReadLockTest.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpChangedRootDirReadLockTest.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpChangedRootDirReadLockTest.java
new file mode 100644
index 0000000..e7b5569
--- /dev/null
+++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpChangedRootDirReadLockTest.java
@@ -0,0 +1,83 @@
+/**
+ * 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.remote;
+
+import java.io.File;
+import java.io.FileOutputStream;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ *
+ */
+public class FtpChangedRootDirReadLockTest extends FtpServerTestSupport {
+
+    private static final Logger LOG = LoggerFactory.getLogger(FtpChangedRootDirReadLockTest.class);
+
+    protected String getFtpUrl() {
+        return "ftp://admin@localhost:" + getPort() + "/?password=admin&readLock=changed&readLockCheckInterval=1000&delete=true";
+    }
+
+    @Test
+    public void testChangedReadLock() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.expectedFileExists("target/out/slowfile.dat");
+
+        writeSlowFile();
+
+        assertMockEndpointsSatisfied();
+
+        String content = context.getTypeConverter().convertTo(String.class, new File("target/out/slowfile.dat"));
+        String[] lines = content.split(LS);
+        assertEquals("There should be 20 lines in the file", 20, lines.length);
+        for (int i = 0; i < 20; i++) {
+            assertEquals("Line " + i, lines[i]);
+        }
+    }
+
+    private void writeSlowFile() throws Exception {
+        LOG.debug("Writing slow file...");
+
+        createDirectory(FTP_ROOT_DIR + "/");
+        FileOutputStream fos = new FileOutputStream(FTP_ROOT_DIR + "/slowfile.dat", true);
+        for (int i = 0; i < 20; i++) {
+            fos.write(("Line " + i + LS).getBytes());
+            LOG.debug("Writing line " + i);
+            Thread.sleep(200);
+        }
+
+        fos.flush();
+        fos.close();
+        LOG.debug("Writing slow file DONE...");
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from(getFtpUrl()).to("file:target/out", "mock:result");
+            }
+        };
+    }
+
+}


[2/3] camel git commit: CAMEL-10782: FTP: cannot get files from users home with readlock changed. Thanks to Stefan Roos for finding the bug.

Posted by da...@apache.org.
CAMEL-10782: FTP: cannot get files from users home with readlock changed. Thanks to Stefan Roos for finding the bug.


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

Branch: refs/heads/camel-2.18.x
Commit: 8d356854aac539a5ae0307511f14a30f9b911542
Parents: 94f1088
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Feb 28 10:58:04 2017 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Feb 28 11:00:20 2017 +0100

----------------------------------------------------------------------
 .../FtpChangedExclusiveReadLockStrategy.java    | 24 ++++--
 .../SftpChangedExclusiveReadLockStrategy.java   | 25 ++++--
 ...hangedRootDirReadLockFastExistCheckTest.java | 28 +++++++
 .../remote/FtpChangedRootDirReadLockTest.java   | 83 ++++++++++++++++++++
 4 files changed, 149 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/8d356854/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/FtpChangedExclusiveReadLockStrategy.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/FtpChangedExclusiveReadLockStrategy.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/FtpChangedExclusiveReadLockStrategy.java
index ba9093d..cca7426 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/FtpChangedExclusiveReadLockStrategy.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/FtpChangedExclusiveReadLockStrategy.java
@@ -53,7 +53,7 @@ public class FtpChangedExclusiveReadLockStrategy implements GenericFileExclusive
         long lastModified = Long.MIN_VALUE;
         long length = Long.MIN_VALUE;
         StopWatch watch = new StopWatch();
-        long startTime = (new Date()).getTime();
+        long startTime = new Date().getTime();
 
         while (!exclusive) {
             // timeout check
@@ -74,12 +74,26 @@ public class FtpChangedExclusiveReadLockStrategy implements GenericFileExclusive
             if (fastExistsCheck) {
                 // use the absolute file path to only pickup the file we want to check, this avoids expensive
                 // list operations if we have a lot of files in the directory
-                LOG.trace("Using fast exists to update file information for {}", file);
-                files = operations.listFiles(file.getAbsoluteFilePath());
+                String path = file.getAbsoluteFilePath();
+                if (path.equals("/") || path.equals("\\")) {
+                    // special for root (= home) directory
+                    LOG.trace("Using fast exists to update file information in home directory");
+                    files = operations.listFiles();
+                } else {
+                    LOG.trace("Using fast exists to update file information for {}", path);
+                    files = operations.listFiles(path);
+                }
             } else {
-                LOG.trace("Using full directory listing to update file information for {}. Consider enabling fastExistsCheck option.", file);
                 // fast option not enabled, so list the directory and filter the file name
-                files = operations.listFiles(file.getParent());
+                String path = file.getParent();
+                if (path.equals("/") || path.equals("\\")) {
+                    // special for root (= home) directory
+                    LOG.trace("Using full directory listing in home directory to update file information. Consider enabling fastExistsCheck option.");
+                    files = operations.listFiles();
+                } else {
+                    LOG.trace("Using full directory listing to update file information for {}. Consider enabling fastExistsCheck option.", path);
+                    files = operations.listFiles(path);
+                }
             }
             LOG.trace("List files {} found {} files", file.getAbsoluteFilePath(), files.size());
             for (FTPFile f : files) {

http://git-wip-us.apache.org/repos/asf/camel/blob/8d356854/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/SftpChangedExclusiveReadLockStrategy.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/SftpChangedExclusiveReadLockStrategy.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/SftpChangedExclusiveReadLockStrategy.java
index 1efe642..49b8363 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/SftpChangedExclusiveReadLockStrategy.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/SftpChangedExclusiveReadLockStrategy.java
@@ -53,7 +53,7 @@ public class SftpChangedExclusiveReadLockStrategy implements GenericFileExclusiv
         long lastModified = Long.MIN_VALUE;
         long length = Long.MIN_VALUE;
         StopWatch watch = new StopWatch();
-        long startTime = (new Date()).getTime();
+        long startTime = new Date().getTime();
 
         while (!exclusive) {
             // timeout check
@@ -73,12 +73,25 @@ public class SftpChangedExclusiveReadLockStrategy implements GenericFileExclusiv
             if (fastExistsCheck) {
                 // use the absolute file path to only pickup the file we want to check, this avoids expensive
                 // list operations if we have a lot of files in the directory
-                LOG.trace("Using fast exists to update file information for {}", file);
-                files = operations.listFiles(file.getAbsoluteFilePath());
+                String path = file.getAbsoluteFilePath();
+                if (path.equals("/") || path.equals("\\")) {
+                    // special for root (= home) directory
+                    LOG.trace("Using fast exists to update file information in home directory");
+                    files = operations.listFiles();
+                } else {
+                    LOG.trace("Using fast exists to update file information for {}", path);
+                    files = operations.listFiles(path);
+                }
             } else {
-                LOG.trace("Using full directory listing to update file information for {}. Consider enabling fastExistsCheck option.", file);
-                // fast option not enabled, so list the directory and filter the file name
-                files = operations.listFiles(file.getParent());
+                String path = file.getParent();
+                if (path.equals("/") || path.equals("\\")) {
+                    // special for root (= home) directory
+                    LOG.trace("Using full directory listing in home directory to update file information. Consider enabling fastExistsCheck option.");
+                    files = operations.listFiles();
+                } else {
+                    LOG.trace("Using full directory listing to update file information for {}. Consider enabling fastExistsCheck option.", path);
+                    files = operations.listFiles(path);
+                }
             }
             LOG.trace("List files {} found {} files", file.getAbsoluteFilePath(), files.size());
             for (ChannelSftp.LsEntry f : files) {

http://git-wip-us.apache.org/repos/asf/camel/blob/8d356854/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpChangedRootDirReadLockFastExistCheckTest.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpChangedRootDirReadLockFastExistCheckTest.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpChangedRootDirReadLockFastExistCheckTest.java
new file mode 100644
index 0000000..f9876f8
--- /dev/null
+++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpChangedRootDirReadLockFastExistCheckTest.java
@@ -0,0 +1,28 @@
+/**
+ * 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.remote;
+
+/**
+ *
+ */
+public class FtpChangedRootDirReadLockFastExistCheckTest extends FtpChangedRootDirReadLockTest {
+
+    protected String getFtpUrl() {
+        return "ftp://admin@localhost:" + getPort() + "/?password=admin&readLock=changed&readLockCheckInterval=1000&delete=true&fastExistsCheck=true";
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/8d356854/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpChangedRootDirReadLockTest.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpChangedRootDirReadLockTest.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpChangedRootDirReadLockTest.java
new file mode 100644
index 0000000..e7b5569
--- /dev/null
+++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpChangedRootDirReadLockTest.java
@@ -0,0 +1,83 @@
+/**
+ * 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.remote;
+
+import java.io.File;
+import java.io.FileOutputStream;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ *
+ */
+public class FtpChangedRootDirReadLockTest extends FtpServerTestSupport {
+
+    private static final Logger LOG = LoggerFactory.getLogger(FtpChangedRootDirReadLockTest.class);
+
+    protected String getFtpUrl() {
+        return "ftp://admin@localhost:" + getPort() + "/?password=admin&readLock=changed&readLockCheckInterval=1000&delete=true";
+    }
+
+    @Test
+    public void testChangedReadLock() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.expectedFileExists("target/out/slowfile.dat");
+
+        writeSlowFile();
+
+        assertMockEndpointsSatisfied();
+
+        String content = context.getTypeConverter().convertTo(String.class, new File("target/out/slowfile.dat"));
+        String[] lines = content.split(LS);
+        assertEquals("There should be 20 lines in the file", 20, lines.length);
+        for (int i = 0; i < 20; i++) {
+            assertEquals("Line " + i, lines[i]);
+        }
+    }
+
+    private void writeSlowFile() throws Exception {
+        LOG.debug("Writing slow file...");
+
+        createDirectory(FTP_ROOT_DIR + "/");
+        FileOutputStream fos = new FileOutputStream(FTP_ROOT_DIR + "/slowfile.dat", true);
+        for (int i = 0; i < 20; i++) {
+            fos.write(("Line " + i + LS).getBytes());
+            LOG.debug("Writing line " + i);
+            Thread.sleep(200);
+        }
+
+        fos.flush();
+        fos.close();
+        LOG.debug("Writing slow file DONE...");
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from(getFtpUrl()).to("file:target/out", "mock:result");
+            }
+        };
+    }
+
+}


[3/3] camel git commit: CAMEL-10782: FTP: cannot get files from users home with readlock changed. Thanks to Stefan Roos for finding the bug.

Posted by da...@apache.org.
CAMEL-10782: FTP: cannot get files from users home with readlock changed. Thanks to Stefan Roos for finding the bug.


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

Branch: refs/heads/camel-2.17.x
Commit: 932365f12eb419fabb1bc7c8b4ca16e18012ab82
Parents: 1ddca53
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Feb 28 10:58:04 2017 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Feb 28 11:00:46 2017 +0100

----------------------------------------------------------------------
 .../FtpChangedExclusiveReadLockStrategy.java    | 24 ++++--
 .../SftpChangedExclusiveReadLockStrategy.java   | 25 ++++--
 ...hangedRootDirReadLockFastExistCheckTest.java | 28 +++++++
 .../remote/FtpChangedRootDirReadLockTest.java   | 83 ++++++++++++++++++++
 4 files changed, 149 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/932365f1/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/FtpChangedExclusiveReadLockStrategy.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/FtpChangedExclusiveReadLockStrategy.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/FtpChangedExclusiveReadLockStrategy.java
index ba9093d..cca7426 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/FtpChangedExclusiveReadLockStrategy.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/FtpChangedExclusiveReadLockStrategy.java
@@ -53,7 +53,7 @@ public class FtpChangedExclusiveReadLockStrategy implements GenericFileExclusive
         long lastModified = Long.MIN_VALUE;
         long length = Long.MIN_VALUE;
         StopWatch watch = new StopWatch();
-        long startTime = (new Date()).getTime();
+        long startTime = new Date().getTime();
 
         while (!exclusive) {
             // timeout check
@@ -74,12 +74,26 @@ public class FtpChangedExclusiveReadLockStrategy implements GenericFileExclusive
             if (fastExistsCheck) {
                 // use the absolute file path to only pickup the file we want to check, this avoids expensive
                 // list operations if we have a lot of files in the directory
-                LOG.trace("Using fast exists to update file information for {}", file);
-                files = operations.listFiles(file.getAbsoluteFilePath());
+                String path = file.getAbsoluteFilePath();
+                if (path.equals("/") || path.equals("\\")) {
+                    // special for root (= home) directory
+                    LOG.trace("Using fast exists to update file information in home directory");
+                    files = operations.listFiles();
+                } else {
+                    LOG.trace("Using fast exists to update file information for {}", path);
+                    files = operations.listFiles(path);
+                }
             } else {
-                LOG.trace("Using full directory listing to update file information for {}. Consider enabling fastExistsCheck option.", file);
                 // fast option not enabled, so list the directory and filter the file name
-                files = operations.listFiles(file.getParent());
+                String path = file.getParent();
+                if (path.equals("/") || path.equals("\\")) {
+                    // special for root (= home) directory
+                    LOG.trace("Using full directory listing in home directory to update file information. Consider enabling fastExistsCheck option.");
+                    files = operations.listFiles();
+                } else {
+                    LOG.trace("Using full directory listing to update file information for {}. Consider enabling fastExistsCheck option.", path);
+                    files = operations.listFiles(path);
+                }
             }
             LOG.trace("List files {} found {} files", file.getAbsoluteFilePath(), files.size());
             for (FTPFile f : files) {

http://git-wip-us.apache.org/repos/asf/camel/blob/932365f1/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/SftpChangedExclusiveReadLockStrategy.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/SftpChangedExclusiveReadLockStrategy.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/SftpChangedExclusiveReadLockStrategy.java
index 1efe642..49b8363 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/SftpChangedExclusiveReadLockStrategy.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/strategy/SftpChangedExclusiveReadLockStrategy.java
@@ -53,7 +53,7 @@ public class SftpChangedExclusiveReadLockStrategy implements GenericFileExclusiv
         long lastModified = Long.MIN_VALUE;
         long length = Long.MIN_VALUE;
         StopWatch watch = new StopWatch();
-        long startTime = (new Date()).getTime();
+        long startTime = new Date().getTime();
 
         while (!exclusive) {
             // timeout check
@@ -73,12 +73,25 @@ public class SftpChangedExclusiveReadLockStrategy implements GenericFileExclusiv
             if (fastExistsCheck) {
                 // use the absolute file path to only pickup the file we want to check, this avoids expensive
                 // list operations if we have a lot of files in the directory
-                LOG.trace("Using fast exists to update file information for {}", file);
-                files = operations.listFiles(file.getAbsoluteFilePath());
+                String path = file.getAbsoluteFilePath();
+                if (path.equals("/") || path.equals("\\")) {
+                    // special for root (= home) directory
+                    LOG.trace("Using fast exists to update file information in home directory");
+                    files = operations.listFiles();
+                } else {
+                    LOG.trace("Using fast exists to update file information for {}", path);
+                    files = operations.listFiles(path);
+                }
             } else {
-                LOG.trace("Using full directory listing to update file information for {}. Consider enabling fastExistsCheck option.", file);
-                // fast option not enabled, so list the directory and filter the file name
-                files = operations.listFiles(file.getParent());
+                String path = file.getParent();
+                if (path.equals("/") || path.equals("\\")) {
+                    // special for root (= home) directory
+                    LOG.trace("Using full directory listing in home directory to update file information. Consider enabling fastExistsCheck option.");
+                    files = operations.listFiles();
+                } else {
+                    LOG.trace("Using full directory listing to update file information for {}. Consider enabling fastExistsCheck option.", path);
+                    files = operations.listFiles(path);
+                }
             }
             LOG.trace("List files {} found {} files", file.getAbsoluteFilePath(), files.size());
             for (ChannelSftp.LsEntry f : files) {

http://git-wip-us.apache.org/repos/asf/camel/blob/932365f1/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpChangedRootDirReadLockFastExistCheckTest.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpChangedRootDirReadLockFastExistCheckTest.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpChangedRootDirReadLockFastExistCheckTest.java
new file mode 100644
index 0000000..f9876f8
--- /dev/null
+++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpChangedRootDirReadLockFastExistCheckTest.java
@@ -0,0 +1,28 @@
+/**
+ * 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.remote;
+
+/**
+ *
+ */
+public class FtpChangedRootDirReadLockFastExistCheckTest extends FtpChangedRootDirReadLockTest {
+
+    protected String getFtpUrl() {
+        return "ftp://admin@localhost:" + getPort() + "/?password=admin&readLock=changed&readLockCheckInterval=1000&delete=true&fastExistsCheck=true";
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/932365f1/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpChangedRootDirReadLockTest.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpChangedRootDirReadLockTest.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpChangedRootDirReadLockTest.java
new file mode 100644
index 0000000..e7b5569
--- /dev/null
+++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpChangedRootDirReadLockTest.java
@@ -0,0 +1,83 @@
+/**
+ * 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.remote;
+
+import java.io.File;
+import java.io.FileOutputStream;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ *
+ */
+public class FtpChangedRootDirReadLockTest extends FtpServerTestSupport {
+
+    private static final Logger LOG = LoggerFactory.getLogger(FtpChangedRootDirReadLockTest.class);
+
+    protected String getFtpUrl() {
+        return "ftp://admin@localhost:" + getPort() + "/?password=admin&readLock=changed&readLockCheckInterval=1000&delete=true";
+    }
+
+    @Test
+    public void testChangedReadLock() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.expectedFileExists("target/out/slowfile.dat");
+
+        writeSlowFile();
+
+        assertMockEndpointsSatisfied();
+
+        String content = context.getTypeConverter().convertTo(String.class, new File("target/out/slowfile.dat"));
+        String[] lines = content.split(LS);
+        assertEquals("There should be 20 lines in the file", 20, lines.length);
+        for (int i = 0; i < 20; i++) {
+            assertEquals("Line " + i, lines[i]);
+        }
+    }
+
+    private void writeSlowFile() throws Exception {
+        LOG.debug("Writing slow file...");
+
+        createDirectory(FTP_ROOT_DIR + "/");
+        FileOutputStream fos = new FileOutputStream(FTP_ROOT_DIR + "/slowfile.dat", true);
+        for (int i = 0; i < 20; i++) {
+            fos.write(("Line " + i + LS).getBytes());
+            LOG.debug("Writing line " + i);
+            Thread.sleep(200);
+        }
+
+        fos.flush();
+        fos.close();
+        LOG.debug("Writing slow file DONE...");
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from(getFtpUrl()).to("file:target/out", "mock:result");
+            }
+        };
+    }
+
+}