Hi Willem

Yes I have thought about this as well.

We have a ticket in the JIRA about creating a common kind of virtual file 
system VFS so the File and the FTP parts can be shared (and what else is file 
based).

So I have refrained myself from doing a refactor to the FTP to get the regular 
FTP and SFTP to share even more code (producer and consumer that is). If the 
VFS will replace it in Camel 2.x.

But I do think there can be more shared code in their common super class (= the 
shared options, some of the more util methods etc.)

I will take a 2nd look as I am about to try to implement the missing move 
option CAMEL-764.



Med venlig hilsen
 
Claus Ibsen
......................................
Silverbullet
Skovsgårdsvænget 21
8362 Hørning
Tlf. +45 2962 7576
Web: www.silverbullet.dk

-----Original Message-----
From: Willem Jiang [mailto:[EMAIL PROTECTED] 
Sent: 28. juli 2008 06:33
To: [email protected]
Subject: Re: svn commit: r680250 - in 
/activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote:
 FtpProducer.java SftpProducer.java

Can we share the common logic in a base class of the ftp producers?
I can see lots of similar code between the ftp producer and sftp producer.

Willem

[EMAIL PROTECTED] wrote:
> Author: davsclaus
> Date: Sun Jul 27 21:13:47 2008
> New Revision: 680250
>
> URL: http://svn.apache.org/viewvc?rev=680250&view=rev
> Log:
> CAMEL-758: ftp producer creates remote folders one-by-one if remote server 
> can not build muti folders at once. Applied patch from Bela. Implemented the 
> same logic on both SFTP and FTP.
>
> Modified:
>     
> activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpProducer.java
>     
> activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpProducer.java
>
> Modified: 
> activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpProducer.java
> URL: 
> http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpProducer.java?rev=680250&r1=680249&r2=680250&view=diff
> ==============================================================================
> --- 
> activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpProducer.java
>  (original)
> +++ 
> activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpProducer.java
>  Sun Jul 27 21:13:47 2008
> @@ -24,7 +24,6 @@
>  import org.apache.commons.logging.Log;
>  import org.apache.commons.logging.LogFactory;
>  import org.apache.commons.net.ftp.FTPClient;
> -import org.apache.commons.net.ftp.FTPConnectionClosedException;
>  
>  public class FtpProducer extends RemoteFileProducer<RemoteFileExchange> {
>      private static final transient Log LOG = 
> LogFactory.getLog(FtpProducer.class);
> @@ -139,6 +138,11 @@
>                      LOG.debug("Trying to build remote directory: " + 
> dirName);
>                  }
>                  success = ftpClient.makeDirectory(dirName);
> +                if (! success) {
> +                    // we are here if the server side doesn't create 
> intermediate folders
> +                    // so create the folder one by one
> +                    buildDirectoryChunks(ftpClient, dirName);
> +                }
>              }
>          } finally {
>              // change back to original directory
> @@ -148,6 +152,24 @@
>          return success;
>      }
>  
> +    private static boolean buildDirectoryChunks(FTPClient ftpClient, String 
> dirName) throws IOException {
> +        final StringBuilder sb = new StringBuilder(dirName.length());
> +        final String[] dirs = dirName.split("\\/");
> +
> +        boolean success = false;
> +        for (String dir : dirs) {
> +            sb.append(dir).append('/');
> +            String directory = sb.toString();
> +            if (LOG.isDebugEnabled()) {
> +                LOG.debug("Trying to build remote directory: " + directory);
> +            }
> +
> +            success = ftpClient.makeDirectory(directory);
> +        }
> +
> +        return success;
> +    }
> +
>      private String remoteServer() {
>          return endpoint.getConfiguration().remoteServerInformation();
>      }
>
> Modified: 
> activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpProducer.java
> URL: 
> http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpProducer.java?rev=680250&r1=680249&r2=680250&view=diff
> ==============================================================================
> --- 
> activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpProducer.java
>  (original)
> +++ 
> activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpProducer.java
>  Sun Jul 27 21:13:47 2008
> @@ -156,8 +156,15 @@
>                  if (LOG.isDebugEnabled()) {
>                      LOG.debug("Trying to build remote directory: " + 
> dirName);
>                  }
> -                sftpClient.mkdir(dirName);
> -                success = true;
> +                
> +                try {
> +                     sftpClient.mkdir(dirName);
> +                     success = true;
> +                } catch (SftpException e) {
> +                    // we are here if the server side doesn't create 
> intermediate folders
> +                    // so create the folder one by one
> +                    success = buildDirectoryChunks(sftpClient, dirName);
> +                }
>              }
>          } finally {
>              // change back to original directory
> @@ -167,8 +174,32 @@
>          return success;
>      }
>  
> +    private static boolean buildDirectoryChunks(ChannelSftp sftpClient, 
> String dirName) 
> +     throws IOException, SftpException {
> +        final StringBuilder sb = new StringBuilder(dirName.length());
> +        final String[] dirs = dirName.split("\\/");
> +        
> +        boolean success = false;
> +        for (String dir : dirs) {
> +            sb.append(dir).append('/');
> +            String directory = sb.toString();
> +            if (LOG.isDebugEnabled()) {
> +                LOG.debug("Trying to build remote directory: " + directory);
> +            }
> +
> +            try {
> +             sftpClient.mkdir(directory);
> +             success = true;
> +            } catch (SftpException e) {
> +             // ignore keep trying to create the rest of the path
> +            }
> +        }
> +        
> +        return success;
> +    }
> +    
>      private String remoteServer() {
>          return endpoint.getConfiguration().remoteServerInformation();
>      }
>  
> -}
> \ No newline at end of file
> +}
>
>
>
>   

Reply via email to