camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r965043 - in /camel/trunk/components/camel-ftp/src: main/java/org/apache/camel/component/file/remote/ test/java/org/apache/camel/component/file/remote/
Date Sat, 17 Jul 2010 09:45:53 GMT
Author: davsclaus
Date: Sat Jul 17 09:45:53 2010
New Revision: 965043

URL: http://svn.apache.org/viewvc?rev=965043&view=rev
Log:
CAMEL-2952: camel-ftp supports polling from MVS file system. Using new strategy to cd when
traversing directories.

Modified:
    camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
    camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
    camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
    camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpRecursiveNoopTest.java
    camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerMultipleDirectoriesTest.java

Modified: camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java?rev=965043&r1=965042&r2=965043&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
(original)
+++ camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
Sat Jul 17 09:45:53 2010
@@ -37,30 +37,47 @@ public class FtpConsumer extends RemoteF
     }
 
     protected boolean pollDirectory(String fileName, List<GenericFile<FTPFile>>
fileList) {
+        // strip trailing slash
+        fileName = FileUtil.stripTrailingSeparator(fileName);
+        return pollDirectory(fileName, null, fileList);
+    }
+
+    protected boolean pollDirectory(String absolutePath, String dirName, List<GenericFile<FTPFile>>
fileList) {
+        // must remember current dir so we stay in that directory after the poll
+        String currentDir = operations.getCurrentDirectory();
+
+        boolean answer = doPollDirectory(absolutePath, dirName, fileList);
+
+        operations.changeCurrentDirectory(currentDir);
+        return answer;
+    }
+
+    protected boolean doPollDirectory(String absolutePath, String dirName, List<GenericFile<FTPFile>>
fileList) {
         if (log.isTraceEnabled()) {
-            log.trace("pollDirectory from fileName: " + fileName);
-        }
-        if (fileName == null) {
-            return true;
+            log.trace("doPollDirectory from absolutePath: " + absolutePath + ", dirName:
" + dirName);
         }
 
         // remove trailing /
-        fileName = FileUtil.stripTrailingSeparator(fileName);
+        dirName = FileUtil.stripTrailingSeparator(dirName);
+        String dir = ObjectHelper.isNotEmpty(dirName) ? dirName : absolutePath;
+
+        // change into directory (to ensure most FTP servers can list files)
+        operations.changeCurrentDirectory(dir);
 
         if (log.isTraceEnabled()) {
-            log.trace("Polling directory: " + fileName);
+            log.trace("Polling directory: " + dir);
         }
-        List<FTPFile> files = operations.listFiles(fileName);
+        List<FTPFile> files = operations.listFiles();
         if (files == null || files.isEmpty()) {
             // no files in this directory to poll
             if (log.isTraceEnabled()) {
-                log.trace("No files found in directory: " + fileName);
+                log.trace("No files found in directory: " + dir);
             }
             return true;
         } else {
             // we found some files
             if (log.isTraceEnabled()) {
-                log.trace("Found " + files.size() + " in directory: " + fileName);
+                log.trace("Found " + files.size() + " in directory: " + dir);
             }
         }
 
@@ -72,17 +89,18 @@ public class FtpConsumer extends RemoteF
             }
 
             if (file.isDirectory()) {
-                RemoteFile<FTPFile> remote = asRemoteFile(fileName, file);
+                RemoteFile<FTPFile> remote = asRemoteFile(absolutePath, file);
                 if (endpoint.isRecursive() && isValidFile(remote, true)) {
                     // recursive scan and add the sub files and folders
-                    String subDirectory = fileName + "/" + file.getName();
-                    boolean canPollMore = pollDirectory(subDirectory, fileList);
+                    String subDirectory = file.getName();
+                    String path = absolutePath + "/" + subDirectory;
+                    boolean canPollMore = pollDirectory(path, subDirectory, fileList);
                     if (!canPollMore) {
                         return false;
                     }
                 }
             } else if (file.isFile()) {
-                RemoteFile<FTPFile> remote = asRemoteFile(fileName, file);
+                RemoteFile<FTPFile> remote = asRemoteFile(absolutePath, file);
                 if (isValidFile(remote, false)) {
                     if (isInProgress(remote)) {
                         if (log.isTraceEnabled()) {
@@ -101,7 +119,7 @@ public class FtpConsumer extends RemoteF
         return true;
     }
 
-    private RemoteFile<FTPFile> asRemoteFile(String directory, FTPFile file) {
+    private RemoteFile<FTPFile> asRemoteFile(String absolutePath, FTPFile file) {
         RemoteFile<FTPFile> answer = new RemoteFile<FTPFile>();
 
         answer.setEndpointPath(endpointPath);
@@ -118,7 +136,8 @@ public class FtpConsumer extends RemoteF
         answer.setAbsolute(false);
 
         // create a pseudo absolute name
-        String absoluteFileName = (ObjectHelper.isNotEmpty(directory) ? directory + "/" :
"") + file.getName();
+        String dir = FileUtil.stripTrailingSeparator(absolutePath);
+        String absoluteFileName = dir + "/" + file.getName();
         answer.setAbsoluteFilePath(absoluteFileName);
 
         // the relative filename, skip the leading endpoint configured path

Modified: camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java?rev=965043&r1=965042&r2=965043&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
(original)
+++ camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
Sat Jul 17 09:45:53 2010
@@ -410,9 +410,12 @@ public class FtpOperations implements Re
         }
     }
 
-    public void changeCurrentDirectory(String newDirectory) throws GenericFileOperationFailedException
{
+    public void changeCurrentDirectory(String path) throws GenericFileOperationFailedException
{
+        if (log.isTraceEnabled()) {
+            log.trace("Changing current directory to: " + path);
+        }
         try {
-            client.changeWorkingDirectory(newDirectory);
+            client.changeWorkingDirectory(path);
         } catch (IOException e) {
             throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(),
e.getMessage(), e);
         }

Modified: camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java?rev=965043&r1=965042&r2=965043&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
(original)
+++ camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
Sat Jul 17 09:45:53 2010
@@ -336,6 +336,9 @@ public class SftpOperations implements R
     }
 
     public void changeCurrentDirectory(String path) throws GenericFileOperationFailedException
{
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Changing current directory to: " + path);
+        }
         try {
             channel.cd(path);
         } catch (SftpException e) {

Modified: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpRecursiveNoopTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpRecursiveNoopTest.java?rev=965043&r1=965042&r2=965043&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpRecursiveNoopTest.java
(original)
+++ camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpRecursiveNoopTest.java
Sat Jul 17 09:45:53 2010
@@ -27,20 +27,25 @@ import org.junit.Test;
 public class FromFtpRecursiveNoopTest extends FtpServerTestSupport {
 
     private String getFtpUrl() {
-        return "ftp://admin@localhost:" + getPort() + "/noop?password=admin&binary=false"
+        return "ftp://admin@localhost:" + getPort() + "/noop?password=admin&binary=false&initialDelay=3000"
                 + "&recursive=true&noop=true";
     }
 
-    @Test
-    public void testRecursiveNoop() throws Exception {
-        MockEndpoint mock = getMockEndpoint("mock:result");
-        mock.expectedBodiesReceivedInAnyOrder("a", "b", "a2", "c", "b2");
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
 
         template.sendBodyAndHeader(getFtpUrl(), "a", Exchange.FILE_NAME, "a.txt");
         template.sendBodyAndHeader(getFtpUrl(), "b", Exchange.FILE_NAME, "b.txt");
         template.sendBodyAndHeader(getFtpUrl(), "a2", Exchange.FILE_NAME, "foo/a.txt");
         template.sendBodyAndHeader(getFtpUrl(), "c", Exchange.FILE_NAME, "bar/c.txt");
         template.sendBodyAndHeader(getFtpUrl(), "b2", Exchange.FILE_NAME, "bar/b.txt");
+    }
+
+    @Test
+    public void testRecursiveNoop() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceivedInAnyOrder("a", "b", "a2", "c", "b2");
 
         assertMockEndpointsSatisfied();
 
@@ -60,6 +65,7 @@ public class FromFtpRecursiveNoopTest ex
             public void configure() throws Exception {
                 from(getFtpUrl())
                     .convertBodyTo(String.class)
+                    .to("log:ftp")
                     .to("mock:result");
             }
         };

Modified: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerMultipleDirectoriesTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerMultipleDirectoriesTest.java?rev=965043&r1=965042&r2=965043&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerMultipleDirectoriesTest.java
(original)
+++ camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerMultipleDirectoriesTest.java
Sat Jul 17 09:45:53 2010
@@ -42,7 +42,7 @@ public class FtpConsumerMultipleDirector
     @Test
     public void testMultiDir() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
-        mock.expectedBodiesReceived("Bye World", "Hello World", "Godday World");
+        mock.expectedBodiesReceived("Bye World", "Hello World", "Goodday World");
 
         assertMockEndpointsSatisfied();
 
@@ -71,7 +71,7 @@ public class FtpConsumerMultipleDirector
     private void prepareFtpServer() throws Exception {
         sendFile(getFtpUrl(), "Bye World", "bye.txt");
         sendFile(getFtpUrl(), "Hello World", "sub/hello.txt");
-        sendFile(getFtpUrl(), "Godday World", "sub/sub2/godday.txt");
+        sendFile(getFtpUrl(), "Goodday World", "sub/sub2/godday.txt");
     }
 
     protected RouteBuilder createRouteBuilder() throws Exception {



Mime
View raw message