commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ima...@apache.org
Subject cvs commit: jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/ftp FtpClient.java FTPClientWrapper.java FtpFileObject.java FtpFileProvider.java FtpFileSystem.java
Date Mon, 18 Oct 2004 18:20:39 GMT
imario      2004/10/18 11:20:39

  Modified:    vfs/src/java/org/apache/commons/vfs/provider/ftp
                        FtpFileObject.java FtpFileProvider.java
                        FtpFileSystem.java
  Added:       vfs/src/java/org/apache/commons/vfs/provider/ftp
                        FtpClient.java FTPClientWrapper.java
  Log:
  automatic ftp reconnect on connection loss
  
  Revision  Changes    Path
  1.29      +12 -13    jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/ftp/FtpFileObject.java
  
  Index: FtpFileObject.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/ftp/FtpFileObject.java,v
  retrieving revision 1.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- FtpFileObject.java	21 Sep 2004 05:32:56 -0000	1.28
  +++ FtpFileObject.java	18 Oct 2004 18:20:39 -0000	1.29
  @@ -15,7 +15,6 @@
    */
   package org.apache.commons.vfs.provider.ftp;
   
  -import org.apache.commons.net.ftp.FTPClient;
   import org.apache.commons.net.ftp.FTPFile;
   import org.apache.commons.vfs.FileName;
   import org.apache.commons.vfs.FileObject;
  @@ -111,7 +110,7 @@
               return;
           }
   
  -        final FTPClient client = ftpFs.getClient();
  +        final FtpClient client = ftpFs.getClient();
           try
           {
               String key = FtpFileSystemConfigBuilder.getInstance().getEntryParser(getFileSystem().getFileSystemOptions());
  @@ -127,8 +126,8 @@
                   for (int i = 0; i < tmpChildren.length; i++)
                   {
                       final FTPFile child = tmpChildren[i];
  -                    if (!child.getName().equals(".")
  -                        && !child.getName().equals(".."))
  +                    if (!".".equals(child.getName())
  +                        && !"..".equals(child.getName()))
                       {
                           childList.add(child);
                       }
  @@ -275,7 +274,7 @@
       protected void doDelete() throws Exception
       {
           final boolean ok;
  -        final FTPClient ftpClient = ftpFs.getClient();
  +        final FtpClient ftpClient = ftpFs.getClient();
           try
           {
               if (fileInfo.isDirectory())
  @@ -306,7 +305,7 @@
       protected void doRename(FileObject newfile) throws Exception
       {
           final boolean ok;
  -        final FTPClient ftpClient = ftpFs.getClient();
  +        final FtpClient ftpClient = ftpFs.getClient();
           try
           {
               String oldName = getName().getPath();
  @@ -333,7 +332,7 @@
           throws Exception
       {
           final boolean ok;
  -        final FTPClient client = ftpFs.getClient();
  +        final FtpClient client = ftpFs.getClient();
           try
           {
               ok = client.makeDirectory(relPath);
  @@ -415,7 +414,7 @@
        */
       protected InputStream doGetInputStream() throws Exception
       {
  -        final FTPClient client = ftpFs.getClient();
  +        final FtpClient client = ftpFs.getClient();
           final InputStream instr = client.retrieveFileStream(relPath);
           return new FtpInputStream(client, instr);
       }
  @@ -426,7 +425,7 @@
       protected OutputStream doGetOutputStream(boolean bAppend)
           throws Exception
       {
  -        final FTPClient client = ftpFs.getClient();
  +        final FtpClient client = ftpFs.getClient();
           if (bAppend)
           {
               return new FtpOutputStream(client, client.appendFileStream(relPath));
  @@ -443,9 +442,9 @@
       private class FtpInputStream
           extends MonitorInputStream
       {
  -        private final FTPClient client;
  +        private final FtpClient client;
   
  -        public FtpInputStream(final FTPClient client, final InputStream in)
  +        public FtpInputStream(final FtpClient client, final InputStream in)
           {
               super(in);
               this.client = client;
  @@ -479,9 +478,9 @@
       private class FtpOutputStream
           extends MonitorOutputStream
       {
  -        private final FTPClient client;
  +        private final FtpClient client;
   
  -        public FtpOutputStream(final FTPClient client, final OutputStream outstr)
  +        public FtpOutputStream(final FtpClient client, final OutputStream outstr)
           {
               super(outstr);
               this.client = client;
  
  
  
  1.11      +4 -2      jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/ftp/FtpFileProvider.java
  
  Index: FtpFileProvider.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/ftp/FtpFileProvider.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- FtpFileProvider.java	26 Aug 2004 16:37:55 -0000	1.10
  +++ FtpFileProvider.java	18 Oct 2004 18:20:39 -0000	1.11
  @@ -15,7 +15,6 @@
    */
   package org.apache.commons.vfs.provider.ftp;
   
  -import org.apache.commons.net.ftp.FTPClient;
   import org.apache.commons.vfs.Capability;
   import org.apache.commons.vfs.FileName;
   import org.apache.commons.vfs.FileSystem;
  @@ -80,12 +79,15 @@
           // Create the file system
           final GenericFileName rootName = (GenericFileName) name;
   
  +        FTPClientWrapper ftpClient = new FTPClientWrapper(rootName, fileSystemOptions);
  +        /*
           FTPClient ftpClient = FtpClientFactory.createConnection(rootName.getHostName(),
               rootName.getPort(),
               rootName.getUserName(),
               rootName.getPassword(),
               rootName.getPath(),
               fileSystemOptions);
  +        */
   
           return new FtpFileSystem(rootName, ftpClient, fileSystemOptions);
       }
  
  
  
  1.32      +12 -9     jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/ftp/FtpFileSystem.java
  
  Index: FtpFileSystem.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/ftp/FtpFileSystem.java,v
  retrieving revision 1.31
  retrieving revision 1.32
  diff -u -r1.31 -r1.32
  --- FtpFileSystem.java	26 Aug 2004 16:37:55 -0000	1.31
  +++ FtpFileSystem.java	18 Oct 2004 18:20:39 -0000	1.32
  @@ -17,7 +17,6 @@
   
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  -import org.apache.commons.net.ftp.FTPClient;
   import org.apache.commons.vfs.FileName;
   import org.apache.commons.vfs.FileObject;
   import org.apache.commons.vfs.FileSystemException;
  @@ -46,9 +45,9 @@
   //    private final String password;
   
       // An idle client
  -    private FTPClient idleClient;
  +    private FtpClient idleClient;
   
  -    public FtpFileSystem(final GenericFileName rootName, final FTPClient ftpClient, final
FileSystemOptions fileSystemOptions)
  +    public FtpFileSystem(final GenericFileName rootName, final FtpClient ftpClient, final
FileSystemOptions fileSystemOptions)
       {
           super(rootName, null, fileSystemOptions);
           // hostname = rootName.getHostName();
  @@ -63,6 +62,7 @@
           if (idleClient != null)
           {
               closeConnection(idleClient);
  +            idleClient = null;
           }
       }
   
  @@ -77,7 +77,7 @@
       /**
        * Cleans up the connection to the server.
        */
  -    private void closeConnection(final FTPClient client)
  +    private void closeConnection(final FtpClient client)
       {
           try
           {
  @@ -97,10 +97,13 @@
       /**
        * Creates an FTP client to use.
        */
  -    public FTPClient getClient() throws FileSystemException
  +    public FtpClient getClient() throws FileSystemException
       {
  -        if (idleClient == null)
  +        if (idleClient == null || !idleClient.isConnected())
           {
  +            idleClient = new FTPClientWrapper((GenericFileName) getRoot().getName(), getFileSystemOptions());
  +            return idleClient;
  +            /*
               final GenericFileName rootName = (GenericFileName) getRoot().getName();
   
               return FtpClientFactory.createConnection(rootName.getHostName(),
  @@ -109,11 +112,11 @@
                   rootName.getPassword(),
                   rootName.getPath(),
                   getFileSystemOptions());
  -            // return createConnection();
  +            */
           }
           else
           {
  -            final FTPClient client = idleClient;
  +            final FtpClient client = idleClient;
               idleClient = null;
               return client;
           }
  @@ -122,7 +125,7 @@
       /**
        * Returns an FTP client after use.
        */
  -    public void putClient(final FTPClient client)
  +    public void putClient(final FtpClient client)
       {
           if (idleClient == null)
           {
  
  
  
  1.1                  jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/ftp/FtpClient.java
  
  Index: FtpClient.java
  ===================================================================
  /*
   * Copyright 2002, 2003,2004 The Apache Software Foundation.
   *
   * Licensed 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.commons.vfs.provider.ftp;
  
  import org.apache.commons.net.ftp.FTPFile;
  import org.apache.commons.vfs.FileSystemException;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  
  /**
   * What VFS expects from an ftp client to provide.
   */
  public interface FtpClient
  {
      boolean isConnected() throws FileSystemException;
  
      void disconnect() throws IOException;
  
      FTPFile[] listFiles(String key, String relPath) throws IOException;
  
      boolean removeDirectory(String relPath) throws IOException;
  
      boolean deleteFile(String relPath) throws IOException;
  
      boolean rename(String oldName, String newName) throws IOException;
  
      boolean makeDirectory(String relPath) throws IOException;
  
      boolean completePendingCommand() throws IOException;
  
      InputStream retrieveFileStream(String relPath) throws IOException;
  
      OutputStream appendFileStream(String relPath) throws IOException;
  
      OutputStream storeFileStream(String relPath) throws IOException;
  }
  
  
  1.1                  jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/ftp/FTPClientWrapper.java
  
  Index: FTPClientWrapper.java
  ===================================================================
  /*
   * Copyright 2002, 2003,2004 The Apache Software Foundation.
   *
   * Licensed 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.commons.vfs.provider.ftp;
  
  import org.apache.commons.net.ftp.FTPClient;
  import org.apache.commons.net.ftp.FTPConnectionClosedException;
  import org.apache.commons.net.ftp.FTPFile;
  import org.apache.commons.vfs.FileSystemException;
  import org.apache.commons.vfs.FileSystemOptions;
  import org.apache.commons.vfs.provider.GenericFileName;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  
  /**
   * A wrapper to the FTPClient to allow automatic reconnect on connection loss.<br />
   * I decided to not to use eg. noop() to determine the state of the connection to avoid
unnecesary server round-trips.
   */
  class FTPClientWrapper implements FtpClient
  {
      private final GenericFileName root;
      private final FileSystemOptions fileSystemOptions;
  
      private FTPClient ftpClient = null;
  
      FTPClientWrapper(final GenericFileName root, final FileSystemOptions fileSystemOptions)
throws FileSystemException
      {
          this.root = root;
          this.fileSystemOptions = fileSystemOptions;
          getFtpClient(); // fail-fast
      }
  
      public GenericFileName getRoot()
      {
          return root;
      }
  
      public FileSystemOptions getFileSystemOptions()
      {
          return fileSystemOptions;
      }
  
      private FTPClient createClient() throws FileSystemException
      {
          final GenericFileName rootName = getRoot();
  
          return FtpClientFactory.createConnection(rootName.getHostName(),
              rootName.getPort(),
              rootName.getUserName(),
              rootName.getPassword(),
              rootName.getPath(),
              getFileSystemOptions());
      }
  
      private FTPClient getFtpClient() throws FileSystemException
      {
          if (ftpClient == null)
          {
              ftpClient = createClient();
          }
  
          return ftpClient;
      }
  
      public boolean isConnected() throws FileSystemException
      {
          return getFtpClient().isConnected();
      }
  
      public void disconnect() throws IOException
      {
          try
          {
              getFtpClient().disconnect();
          }
          finally
          {
              ftpClient = null;
          }
      }
  
      public FTPFile[] listFiles(String key, String relPath) throws IOException
      {
          try
          {
              return getFtpClient().listFiles(key, relPath);
          }
          catch (FTPConnectionClosedException e)
          {
              disconnect();
              return getFtpClient().listFiles(key, relPath);
          }
      }
  
      public boolean removeDirectory(String relPath) throws IOException
      {
          try
          {
              return getFtpClient().removeDirectory(relPath);
          }
          catch (FTPConnectionClosedException e)
          {
              disconnect();
              return getFtpClient().removeDirectory(relPath);
          }
      }
  
      public boolean deleteFile(String relPath) throws IOException
      {
          try
          {
              return getFtpClient().deleteFile(relPath);
          }
          catch (FTPConnectionClosedException e)
          {
              disconnect();
              return getFtpClient().deleteFile(relPath);
          }
      }
  
      public boolean rename(String oldName, String newName) throws IOException
      {
          try
          {
              return getFtpClient().rename(oldName, newName);
          }
          catch (FTPConnectionClosedException e)
          {
              disconnect();
              return getFtpClient().rename(oldName, newName);
          }
      }
  
      public boolean makeDirectory(String relPath) throws IOException
      {
          try
          {
              return getFtpClient().makeDirectory(relPath);
          }
          catch (FTPConnectionClosedException e)
          {
              disconnect();
              return getFtpClient().makeDirectory(relPath);
          }
      }
  
      public boolean completePendingCommand() throws IOException
      {
          return getFtpClient().completePendingCommand();
      }
  
      public InputStream retrieveFileStream(String relPath) throws IOException
      {
          try
          {
              return getFtpClient().retrieveFileStream(relPath);
          }
          catch (FTPConnectionClosedException e)
          {
              disconnect();
              return getFtpClient().retrieveFileStream(relPath);
          }
      }
  
      public OutputStream appendFileStream(String relPath) throws IOException
      {
          try
          {
              return getFtpClient().appendFileStream(relPath);
          }
          catch (FTPConnectionClosedException e)
          {
              disconnect();
              return getFtpClient().appendFileStream(relPath);
          }
      }
  
      public OutputStream storeFileStream(String relPath) throws IOException
      {
          try
          {
              return getFtpClient().storeFileStream(relPath);
          }
          catch (FTPConnectionClosedException e)
          {
              disconnect();
              return getFtpClient().storeFileStream(relPath);
          }
      }
  }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message