commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adammurd...@apache.org
Subject cvs commit: jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/sftp SftpFileObject.java SftpFileSystem.java
Date Mon, 17 Mar 2003 09:04:15 GMT
adammurdoch    2003/03/17 01:04:14

  Modified:    vfs      project.xml
               vfs/src/java/org/apache/commons/vfs/provider/sftp
                        SftpFileObject.java SftpFileSystem.java
  Added:       vfs/lib/jsch/jars jsch-0.1.2.jar
  Removed:     vfs/lib/jsch/jars jsch-0.1.0.jar
  Log:
  Upgraded to jsch 0.1.2.  Should fix gump errors.
  
  Revision  Changes    Path
  1.22      +1 -1      jakarta-commons-sandbox/vfs/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/project.xml,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- project.xml	24 Feb 2003 08:29:02 -0000	1.21
  +++ project.xml	17 Mar 2003 09:04:11 -0000	1.22
  @@ -103,7 +103,7 @@
   
       <dependency>
         <id>jsch</id>
  -      <version>0.1.0</version>
  +      <version>0.1.2</version>
       </dependency>
   
       <dependency>
  
  
  
  1.1                  jakarta-commons-sandbox/vfs/lib/jsch/jars/jsch-0.1.2.jar
  
  	<<Binary file>>
  
  
  1.3       +58 -73    jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/sftp/SftpFileObject.java
  
  Index: SftpFileObject.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/sftp/SftpFileObject.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- SftpFileObject.java	23 Feb 2003 00:40:38 -0000	1.2
  +++ SftpFileObject.java	17 Mar 2003 09:04:13 -0000	1.3
  @@ -55,25 +55,24 @@
    */
   package org.apache.commons.vfs.provider.sftp;
   
  -import org.apache.commons.vfs.FileObject;
  -import org.apache.commons.vfs.FileName;
  -import org.apache.commons.vfs.FileType;
  -import org.apache.commons.vfs.FileSystemException;
  -import org.apache.commons.vfs.util.MonitorOutputStream;
  -import org.apache.commons.vfs.provider.AbstractFileObject;
  -import org.apache.commons.vfs.provider.TemporaryFileStore;
  +import com.jcraft.jsch.ChannelSftp;
  +import com.jcraft.jsch.SftpATTRS;
  +import com.jcraft.jsch.SftpException;
  +import java.io.ByteArrayInputStream;
  +import java.io.ByteArrayOutputStream;
  +import java.io.IOException;
   import java.io.InputStream;
  -import java.io.File;
  -import java.io.FileInputStream;
   import java.io.OutputStream;
  -import java.io.FileOutputStream;
  -import java.io.IOException;
  -import java.util.Vector;
   import java.util.ArrayList;
   import java.util.Iterator;
   import java.util.StringTokenizer;
  -import com.jcraft.jsch.ChannelSftp;
  -import com.jcraft.jsch.SftpATTRS;
  +import java.util.Vector;
  +import org.apache.commons.vfs.FileName;
  +import org.apache.commons.vfs.FileObject;
  +import org.apache.commons.vfs.FileSystemException;
  +import org.apache.commons.vfs.FileType;
  +import org.apache.commons.vfs.provider.AbstractFileObject;
  +import org.apache.commons.vfs.util.MonitorOutputStream;
   
   /**
    * An SFTP file.
  @@ -90,15 +89,12 @@
   
       private final SftpFileSystem fileSystem;
       private SftpATTRS attrs;
  -    private TemporaryFileStore tempFileStore;
   
       public SftpFileObject( final FileName name,
  -                           final SftpFileSystem fileSystem,
  -                           final TemporaryFileStore tempFileStore )
  +                           final SftpFileSystem fileSystem )
       {
           super( name, fileSystem );
           this.fileSystem = fileSystem;
  -        this.tempFileStore = tempFileStore;
       }
   
       /**
  @@ -111,7 +107,6 @@
   
           if ( attrs == null )
           {
  -            // TODO - not quite true, but ChannelSftp.stat() swallows exceptions
               return FileType.IMAGINARY;
           }
           
  @@ -145,6 +140,17 @@
           {
               attrs = channel.stat( getName().getPath() );
           }
  +        catch ( final SftpException e )
  +        {
  +            // TODO - not strictly true, but jsch 0.1.2 does not give us
  +            // enough info in the exception.  Should be using:
  +            // if ( e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE )
  +            // However, sometimes the exception has the correct id, and sometimes
  +            // it does not.  Need to look into why.
  +
  +            // Does not exist
  +            attrs = null;
  +        }
           finally
           {
               fileSystem.putChannel( channel );
  @@ -159,11 +165,7 @@
           final ChannelSftp channel = fileSystem.getChannel();
           try
           {
  -            final boolean ok = channel.mkdir( getName().getPath() );
  -            if ( !ok )
  -            {
  -                throw new FileSystemException( "vfs.provider.sftp/create-folder.error"
);
  -            }
  +            channel.mkdir( getName().getPath() );
           }
           finally
           {
  @@ -179,18 +181,13 @@
           final ChannelSftp channel = fileSystem.getChannel();
           try
           {
  -            final boolean ok;
               if ( getType() == FileType.FILE )
               {
  -                ok = channel.rm( getName().getPath() );
  +                channel.rm( getName().getPath() );
               }
               else
               {
  -                ok = channel.rmdir( getName().getPath() );
  -            }
  -            if ( !ok )
  -            {
  -                throw new FileSystemException( "vfs.provider.sftp/delete.error" );
  +                channel.rmdir( getName().getPath() );
               }
           }
           finally
  @@ -262,16 +259,12 @@
           final ChannelSftp channel = fileSystem.getChannel();
           try
           {
  -            // TODO - this is a dud, need to add stream based methods to Jsch
  -            // TODO - reuse the cached file
  -            // TODO - delete the file on end-of-stream
  -            final File file = tempFileStore.allocateFile( getName().getBaseName() );
  -            final boolean ok = channel.get( getName().getPath(), '/' + file.getAbsolutePath()
);
  -            if ( !ok )
  -            {
  -                throw new FileSystemException( "vfs.provider.sftp/get-file.error" );
  -            }
  -            return new FileInputStream( file );
  +            // TODO - Don't read the entire file into memory.  Use the
  +            // stream-based methods on ChannelSftp once they work properly
  +            final ByteArrayOutputStream outstr = new ByteArrayOutputStream();
  +            channel.get( getName().getPath(), outstr );
  +            outstr.close();
  +            return new ByteArrayInputStream( outstr.toByteArray() );
           }
           finally
           {
  @@ -284,45 +277,25 @@
        */
       protected OutputStream doGetOutputStream() throws Exception
       {
  -        // TODO - this is a dud, need to add stream based methods to Jsch
  -        // TODO - reuse the same file for all content operations
  -        final File file = tempFileStore.allocateFile( getName().getBaseName() );
  -        return new SftpOutputStream( file );
  -    }
  -
  -    /**
  -     * Writes the content from a file.
  -     */
  -    private void putContent( final File file ) throws IOException
  -    {
  +        // TODO - Don't write the entire file into memory.  Use the stream-based
  +        // methods on ChannelSftp once the work properly
           final ChannelSftp channel = fileSystem.getChannel();
  -        try
  -        {
  -            final boolean ok = channel.put( '/' + file.getAbsolutePath(), getName().getPath()
);
  -            if ( !ok )
  -            {
  -                throw new FileSystemException( "vfs.provider.sftp/put-file.error" );
  -            }
  -        }
  -        finally
  -        {
  -            fileSystem.putChannel( channel );
  -        }
  +        return new SftpOutputStream( channel );
       }
   
       /**
  -     * An output stream that pushes content to the server when this stream
  -     * is closed.
  +     * An OutputStream that wraps an sftp OutputStream, and closes the channel
  +     * when the stream is closed.
        */
       private class SftpOutputStream
           extends MonitorOutputStream
       {
  -        private File file;
  +        private final ChannelSftp channel;
   
  -        public SftpOutputStream( final File file ) throws IOException
  +        public SftpOutputStream( final ChannelSftp channel )
           {
  -            super( new FileOutputStream( file ) );
  -            this.file = file;
  +            super( new ByteArrayOutputStream() );
  +            this.channel = channel;
           }
   
           /**
  @@ -330,8 +303,20 @@
            */
           protected void onClose() throws IOException
           {
  -            // TODO - need to delete the file
  -            putContent( file );
  +            try
  +            {
  +                final ByteArrayOutputStream outstr = (ByteArrayOutputStream)out;
  +                channel.put( new ByteArrayInputStream( outstr.toByteArray() ),
  +                             getName().getPath() );
  +            }
  +            catch ( final SftpException e )
  +            {
  +                throw new FileSystemException( e );
  +            }
  +            finally
  +            {
  +                fileSystem.putChannel( channel );
  +            }
           }
       }
   }
  
  
  
  1.2       +21 -19    jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/sftp/SftpFileSystem.java
  
  Index: SftpFileSystem.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/sftp/SftpFileSystem.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SftpFileSystem.java	20 Feb 2003 07:30:36 -0000	1.1
  +++ SftpFileSystem.java	17 Mar 2003 09:04:14 -0000	1.2
  @@ -95,10 +95,10 @@
        */
       protected ChannelSftp getChannel() throws IOException
       {
  -        // Create the session
  -        if ( session == null )
  +        try
           {
  -            try
  +            // Create the session
  +            if ( session == null )
               {
                   final GenericFileName rootName = (GenericFileName)getRootName();
                   session = jSch.getSession( rootName.getUserName(),
  @@ -107,26 +107,28 @@
                   session.setPassword( rootName.getPassword() );
                   session.connect();
               }
  -            catch ( final JSchException e )
  +
  +            // Use the pooled channel, or create a new one
  +            final ChannelSftp channel;
  +            if ( idleChannel != null )
               {
  -                throw new FileSystemException( "vfs.provider.sftp/connect.error", getRootName(),
e );
  +                channel = idleChannel;
  +                idleChannel = null;
  +            }
  +            else
  +            {
  +                channel = (ChannelSftp)session.openChannel( "sftp" );
  +                channel.connect();
               }
  -        }
   
  -        // Use the pooled channel, or create a new one
  -        final ChannelSftp channel;
  -        if ( idleChannel != null )
  -        {
  -            channel = idleChannel;
  -            idleChannel = null;
  +            return channel;
           }
  -        else
  +        catch ( final JSchException e )
           {
  -            channel = (ChannelSftp)session.openChannel( "sftp" );
  -            channel.connect();
  +            throw new FileSystemException( "vfs.provider.sftp/connect.error",
  +                                           getRootName(),
  +                                           e );
           }
  -
  -        return channel;
       }
   
       /**
  @@ -165,6 +167,6 @@
       protected FileObject createFile( final FileName name )
           throws FileSystemException
       {
  -        return new SftpFileObject( name, this, getContext().getTemporaryFileStore() );
  +        return new SftpFileObject( name, this );
       }
   }
  
  
  

---------------------------------------------------------------------
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