You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by dk...@apache.org on 2011/09/20 01:00:17 UTC

svn commit: r1172884 - in /camel/branches/camel-2.8.x: ./ components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/ components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/

Author: dkulp
Date: Mon Sep 19 23:00:16 2011
New Revision: 1172884

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

........
  r1162647 | ningjiang | 2011-08-29 00:49:33 -0400 (Mon, 29 Aug 2011) | 1 line
  
  CAMEL-4356 add an option fastExist to test the file exist in the FTP server
........

Added:
    camel/branches/camel-2.8.x/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileFastExistFailTest.java
      - copied unchanged from r1162647, camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileFastExistFailTest.java
Modified:
    camel/branches/camel-2.8.x/   (props changed)
    camel/branches/camel-2.8.x/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
    camel/branches/camel-2.8.x/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileEndpoint.java
    camel/branches/camel-2.8.x/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java

Propchange: camel/branches/camel-2.8.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: camel/branches/camel-2.8.x/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java?rev=1172884&r1=1172883&r2=1172884&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java (original)
+++ camel/branches/camel-2.8.x/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java Mon Sep 19 23:00:16 2011
@@ -507,6 +507,38 @@ public class FtpOperations implements Re
 
     public boolean existsFile(String name) throws GenericFileOperationFailedException {
         log.trace("existsFile({})", name);
+        if (endpoint.isFastExist()) {
+            return fastExistsFile(name);
+        }
+        // check whether a file already exists
+        String directory = FileUtil.onlyPath(name);
+        String onlyName = FileUtil.stripPath(name);
+        try {
+            String[] names;
+            if (directory != null) {
+                names = client.listNames(directory);
+            } else {
+                names = client.listNames();
+            }
+            // can return either null or an empty list depending on FTP servers
+            if (names == null) {
+                return false;
+            }
+            for (String existing : names) {
+                log.trace("Existing file: {}, target file: {}", existing, name);
+                existing = FileUtil.stripPath(existing);
+                if (existing != null && existing.equals(onlyName)) {
+                    return true;
+                }
+            }
+            return false;
+        } catch (IOException e) {
+            throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
+        }
+    }
+
+    protected boolean fastExistsFile(String name) throws GenericFileOperationFailedException {
+        log.trace("fastexistsFile({})", name);
         try {
             String[] names = client.listNames(name);
             if (names == null) {

Modified: camel/branches/camel-2.8.x/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileEndpoint.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileEndpoint.java?rev=1172884&r1=1172883&r2=1172884&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileEndpoint.java (original)
+++ camel/branches/camel-2.8.x/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileEndpoint.java Mon Sep 19 23:00:16 2011
@@ -33,6 +33,7 @@ public abstract class RemoteFileEndpoint
     private int maximumReconnectAttempts = 3;
     private long reconnectDelay = 1000;
     private boolean disconnect;
+    private boolean fastExist;
 
     public RemoteFileEndpoint() {
         // no args constructor for spring bean endpoint configuration
@@ -170,4 +171,13 @@ public abstract class RemoteFileEndpoint
     public void setDisconnect(boolean disconnect) {
         this.disconnect = disconnect;
     }
+
+    public boolean isFastExist() {
+        return fastExist;
+    }
+
+    public void setFastExist(boolean fastExist) {
+        this.fastExist = fastExist;
+    }
+
 }

Modified: camel/branches/camel-2.8.x/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java?rev=1172884&r1=1172883&r2=1172884&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java (original)
+++ camel/branches/camel-2.8.x/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java Mon Sep 19 23:00:16 2011
@@ -678,6 +678,45 @@ public class SftpOperations implements R
 
     public boolean existsFile(String name) throws GenericFileOperationFailedException {
         LOG.trace("existsFile({})", name);
+        if (endpoint.isFastExist()) {
+            return fastExistsFile(name);
+        }
+        // check whether a file already exists
+        String directory = FileUtil.onlyPath(name);
+        if (directory == null) {
+            // assume current dir if no path could be extracted
+            directory = ".";
+        }
+        String onlyName = FileUtil.stripPath(name);
+
+        try {
+            Vector files = channel.ls(directory);
+            // can return either null or an empty list depending on FTP servers
+            if (files == null) {
+                return false;
+            }
+            for (Object file : files) {
+                ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) file;
+                String existing = entry.getFilename();
+                LOG.trace("Existing file: {}, target file: {}", existing, name);
+                existing = FileUtil.stripPath(existing);
+                if (existing != null && existing.equals(onlyName)) {
+                    return true;
+                }
+            }
+            return false;
+        } catch (SftpException e) {
+            // or an exception can be thrown with id 2 which means file does not exists
+            if (ChannelSftp.SSH_FX_NO_SUCH_FILE == e.id) {
+                return false;
+            }
+            // otherwise its a more serious error so rethrow
+            throw new GenericFileOperationFailedException(e.getMessage(), e);
+        }
+    }
+
+    protected boolean fastExistsFile(String name) throws GenericFileOperationFailedException {
+        LOG.trace("fastExistsFile({})", name);
         try {
             Vector files = channel.ls(name);
             if (files == null) {