commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject cvs commit: jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/sftp SftpFileProvider.java
Date Thu, 04 Dec 2003 05:32:24 GMT
ggregory    2003/12/03 21:32:24

  Modified:    vfs/src/java/org/apache/commons/vfs/provider/sftp
                        SftpFileProvider.java
  Log:
  Make the ".ssh" directory more flexible. Before this change, the lookup never worked on
my non-default Cygwin set up.
  Tested with Cygwin's sshd server.
  
  Revision  Changes    Path
  1.5       +121 -98   jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/sftp/SftpFileProvider.java
  
  Index: SftpFileProvider.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/sftp/SftpFileProvider.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- SftpFileProvider.java	13 Oct 2003 08:44:27 -0000	1.4
  +++ SftpFileProvider.java	4 Dec 2003 05:32:24 -0000	1.5
  @@ -1,57 +1,45 @@
  -/* ====================================================================
  - *
  +/*
  + * ====================================================================
  + * 
    * The Apache Software License, Version 1.1
  - *
  - * Copyright (c) 2003 The Apache Software Foundation.  All rights
  - * reserved.
  - *
  + * 
  + * Copyright (c) 2003 The Apache Software Foundation. All rights reserved.
  + * 
    * Redistribution and use in source and binary forms, with or without
  - * modification, are permitted provided that the following conditions
  - * are met:
  - *
  - * 1. Redistributions of source code must retain the above copyright
  - *    notice, this list of conditions and the following disclaimer.
  - *
  - * 2. Redistributions in binary form must reproduce the above copyright
  - *    notice, this list of conditions and the following disclaimer in
  - *    the documentation and/or other materials provided with the
  - *    distribution.
  - *
  - * 3. The end-user documentation included with the redistribution, if
  - *    any, must include the following acknowledgement:
  - *       "This product includes software developed by the
  - *        Apache Software Foundation (http://www.apache.org/)."
  - *    Alternately, this acknowledgement may appear in the software itself,
  - *    if and wherever such third-party acknowledgements normally appear.
  - *
  - * 4. The names "The Jakarta Project", "Commons", and "Apache Software
  - *    Foundation" must not be used to endorse or promote products derived
  - *    from this software without prior written permission. For written
  - *    permission, please contact apache@apache.org.
  - *
  - * 5. Products derived from this software may not be called "Apache"
  - *    nor may "Apache" appear in their names without prior written
  - *    permission of the Apache Software Foundation.
  - *
  - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  - * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  - * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  - * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  - * SUCH DAMAGE.
  + * modification, are permitted provided that the following conditions are met: 1.
  + * Redistributions of source code must retain the above copyright notice, this
  + * list of conditions and the following disclaimer. 2. Redistributions in
  + * binary form must reproduce the above copyright notice, this list of
  + * conditions and the following disclaimer in the documentation and/or other
  + * materials provided with the distribution. 3. The end-user documentation
  + * included with the redistribution, if any, must include the following
  + * acknowledgement: "This product includes software developed by the Apache
  + * Software Foundation (http://www.apache.org/)." Alternately, this
  + * acknowledgement may appear in the software itself, if and wherever such
  + * third-party acknowledgements normally appear. 4. The names "The Jakarta
  + * Project", "Commons", and "Apache Software Foundation" must not be used to
  + * endorse or promote products derived from this software without prior written
  + * permission. For written permission, please contact apache@apache.org. 5.
  + * Products derived from this software may not be called "Apache" nor may
  + * "Apache" appear in their names without prior written permission of the
  + * Apache Software Foundation.
  + * 
  + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  + * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    * ====================================================================
  - *
  - * This software consists of voluntary contributions made by many
  - * individuals on behalf of the Apache Software Foundation.  For more
  - * information on the Apache Software Foundation, please see
  - * <http://www.apache.org/>.
  - *
  + * 
  + * This software consists of voluntary contributions made by many individuals
  + * on behalf of the Apache Software Foundation. For more information on the
  + * Apache Software Foundation, please see <http://www.apache.org/> .
  + *  
    */
   package org.apache.commons.vfs.provider.sftp;
   
  @@ -67,70 +55,105 @@
   
   /**
    * A provider for accessing files over SFTP.
  - *
  + * 
    * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
  - * @version $Revision$ $Date$
  + * @author Gary D. Gregory
  + * @version $Id$
    */
  -public class SftpFileProvider
  -    extends AbstractOriginatingFileProvider
  -{
  +public class SftpFileProvider extends AbstractOriginatingFileProvider {
  +
  +    private static final String SSH_DIR_NAME = ".ssh";
  +
       private JSch jSch = new JSch();
   
       /**
  -     * Initialises the component.
  -     */
  -    public void init() throws FileSystemException
  -    {
  -        // Figure out where the ssh directory is
  -        final File sshDir;
  -        if ( Os.isFamily( Os.OS_FAMILY_WINDOWS ) )
  -        {
  -            // TODO - this may not be true
  -            final String userName = System.getProperty( "user.name" );
  -            sshDir = new File("C:\\cygwin\\home\\" + userName + "\\.ssh" );
  +	 * Creates a {@link FileSystem}.
  +	 */
  +    protected FileSystem doCreateFileSystem(final FileName rootName) {
  +        return new SftpFileSystem((GenericFileName) rootName, this.getJSch());
  +    }
  +
  +    /**
  +	 * Finds the .ssh directory. 
  +     * <p>The lookup order is:</p>
  +	 * <ol>
  +	 * <li>The system property <code>vfs.sftp.sshdir</code> (the override
  +	 * mechanism)</li>
  +	 * <li><code>{user.home}/.ssh</code></li>
  +	 * <li>On Windows only: C:\cygwin\home\{user.name}\.ssh</li>
  +	 * <li>The current directory, as a last resort.</li>
  +	 * <ol>
  +	 * <p>
  +     * Windows Notes: 
  +     * The default installation directory for Cygwin is <code>C:\cygwin</code>.
  +     * On my set up (Gary here), I have Cygwin in C:\bin\cygwin, not the default.
  +     * Also, my .ssh directory was created in the {user.home} directory.
  +     * </p> 
  +	 * @return The .ssh directory
  +	 */
  +    private File findSshDir() {
  +        String sshDirPath;
  +        sshDirPath = System.getProperty("vfs.sftp.sshdir");
  +        if (sshDirPath != null) {
  +            File sshDir = new File(sshDirPath);
  +            if (sshDir.exists()) {
  +                return sshDir;
  +            }
           }
  -        else
  -        {
  -            sshDir = new File( System.getProperty( "user.home" ), ".ssh" );
  +
  +        File sshDir = new File(System.getProperty("user.home"), SSH_DIR_NAME);
  +        if (sshDir.exists()) {
  +            return sshDir;
           }
   
  +        if (Os.isFamily(Os.OS_FAMILY_WINDOWS)) {
  +            // TODO - this may not be true
  +            final String userName = System.getProperty("user.name");
  +            sshDir = new File("C:\\cygwin\\home\\" + userName + "\\" + SSH_DIR_NAME);
  +            if (sshDir.exists()) {
  +                return sshDir;
  +            }
  +        }
  +        return new File("");
  +    }
  +
  +    /**
  +	 * Returns the JSch.
  +	 * 
  +	 * @return Returns the jSch.
  +	 */
  +    private JSch getJSch() {
  +        return this.jSch;
  +    }
  +
  +    /**
  +	 * Initialises the component.
  +	 */
  +    public void init() throws FileSystemException {
  +        // Figure out where the ssh directory is
  +        File sshDir = this.findSshDir();
  +
           // Load the known hosts file
           final File knownHostsFile = new File(sshDir, "known_hosts");
  -        if ( knownHostsFile.isFile() && knownHostsFile.canRead() )
  -        {
  -            jSch.setKnownHosts( knownHostsFile.getAbsolutePath() );
  +        if (knownHostsFile.isFile() && knownHostsFile.canRead()) {
  +            this.getJSch().setKnownHosts(knownHostsFile.getAbsolutePath());
           }
   
           // Load the private key
  -        final File privateKeyFile = new File( sshDir, "id_rsa" );
  -        if ( privateKeyFile.isFile() && privateKeyFile.canRead() )
  -        {
  -            try
  -            {
  -                jSch.addIdentity(privateKeyFile.getAbsolutePath());
  -            }
  -            catch ( final JSchException e )
  -            {
  +        final File privateKeyFile = new File(sshDir, "id_rsa");
  +        if (privateKeyFile.isFile() && privateKeyFile.canRead()) {
  +            try {
  +                this.getJSch().addIdentity(privateKeyFile.getAbsolutePath());
  +            } catch (final JSchException e) {
                   throw new FileSystemException("vfs.provider.sftp/load-private-key.error",
privateKeyFile, e);
               }
           }
       }
   
       /**
  -     * Parses an absolute URI.
  -     */
  -    protected FileName parseUri( final String uri )
  -        throws FileSystemException
  -    {
  -        return GenericFileName.parseUri( uri, 22 );
  -    }
  -
  -    /**
  -     * Creates a {@link FileSystem}.
  -     */
  -    protected FileSystem doCreateFileSystem( final FileName rootName )
  -        throws FileSystemException
  -    {
  -        return new SftpFileSystem( (GenericFileName)rootName, jSch );
  +	 * Parses an absolute URI.
  +	 */
  +    protected FileName parseUri(final String uri) throws FileSystemException {
  +        return GenericFileName.parseUri(uri, 22);
       }
  -}
  +}
  \ No newline at end of file
  
  
  

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