Author: imario
Date: Fri May 14 09:30:58 2010
New Revision: 944171

URL: http://svn.apache.org/viewvc?rev=944171&view=rev
Log:
VFS-307: avoid unnecessary roundtrips to the server when listing the directory

Modified:
    
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/sftp/SftpFileObject.java

Modified: 
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/sftp/SftpFileObject.java
URL: 
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/sftp/SftpFileObject.java?rev=944171&r1=944170&r2=944171&view=diff
==============================================================================
--- 
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/sftp/SftpFileObject.java
 (original)
+++ 
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/sftp/SftpFileObject.java
 Fri May 14 09:30:58 2010
@@ -288,57 +288,59 @@ public class SftpFileObject extends Abst
     protected FileObject[] doListChildrenResolved() throws Exception
     {
         // List the contents of the folder
-        final Vector vector;
+        Vector vector = null;
         final ChannelSftp channel = fileSystem.getChannel();
 
-        String workingDirectory = null;
-        try
-        {
-            try
-            {
-                if (relPath != null)
-                {
-                    workingDirectory = channel.pwd();
-                    channel.cd(relPath);
-                }
-            }
-            catch (SftpException e)
-            {
-                // VFS-210: seems not to be a directory
-                return null;
-            }
-
-            try
-            {
-                vector = channel.ls(".");
-            }
-            catch (SftpException e)
-            {
-                try
-                {
-                    if (relPath != null)
-                    {
-                        channel.cd(workingDirectory);
-                    }
-                }
-                catch (SftpException e2)
-                {
-                    throw e;
-                }
-                throw e;
-            }
-
-            try
-            {
-                if (relPath != null)
-                {
-                    channel.cd(workingDirectory);
-                }
-            }
-            catch (SftpException e)
-            {
-                throw new 
FileSystemException("vfs.provider.sftp/change-work-directory-back.error", 
workingDirectory);
-            }
+               try
+               {
+                       // try the direct way to list the directory on the 
server to avoid too many roundtrips
+                       vector = channel.ls(relPath);
+               }
+               catch (SftpException e)
+               {
+                       String workingDirectory = null;
+                       try
+                       {
+                               if (relPath != null)
+                               {
+                                       workingDirectory = channel.pwd();
+                                       channel.cd(relPath);
+                               }
+                       }
+                       catch (SftpException ex)
+                       {
+                               // VFS-210: seems not to be a directory
+                               return null;
+                       }
+
+                       SftpException lsEx = null;
+                       try
+                       {
+                               vector = channel.ls(".");
+                       }
+                       catch (SftpException ex)
+                       {
+                               lsEx = ex;
+                       }
+                       finally
+                       {
+                               try
+                               {
+                                       if (relPath != null)
+                                       {
+                                               channel.cd(workingDirectory);
+                                       }
+                               }
+                               catch (SftpException xe)
+                               {
+                                       throw new 
FileSystemException("vfs.provider.sftp/change-work-directory-back.error", 
workingDirectory, lsEx);
+                               }
+                       }
+
+                       if (lsEx != null)
+                       {
+                               throw lsEx;
+                       }
         }
         finally
         {


Reply via email to