/* ====================================================================
 *
 * The Apache Software License, Version 1.1
 *
 * 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 acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements 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 Group.
 *
 * 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/>.
 *
 */
package org.apache.commons.vfs.provider.sftp;

import org.apache.commons.vfs.FileName;
import org.apache.commons.vfs.FileSystem;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.util.Os;
import org.apache.commons.vfs.provider.AbstractOriginatingFileProvider;
import org.apache.commons.vfs.provider.GenericFileName;
import com.jcraft.jsch.JSch;

/**
 * A provider for accessing files over SFTP.
 *
 * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
 * @version $Revision: 1.1 $ $Date: 2003/02/20 07:30:36 $
 */
public class SftpFileProvider
    extends AbstractOriginatingFileProvider
{
    private JSch jSch = new JSch();

    /**
     * Initialises the component.
     */
    public void init() throws FileSystemException
    {
        // Figure out where the known_hosts file is
        final String knownHostsPath;
        if ( Os.isFamily( Os.OS_FAMILY_WINDOWS ) )
        {
            // TODO - this may not be true
            final String userName = System.getProperty( "user.name" );
            knownHostsPath = "C:\\cygwin\\home\\" + userName + "\\.ssh\\known_hosts";
        }
        else
        {
            knownHostsPath = System.getProperty( "user.home") + "/.ssh/known_hosts";
        }

        jSch.setKnownHosts( knownHostsPath );
    }

    /**
     * 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 );
    }
}

@@ -3017,16 +3017,75 @@
 ch.JSch;
+%0Aimport com.jcraft.jsch.JSchException;%0Aimport java.io.File;
 %0A%0A/**%0A *
@@ -3223,17 +3223,17 @@
 sion: 1.
-1
+2
  $ $Date
@@ -3244,21 +3244,21 @@
 03/0
-2/20 07:30:36
+6/24 10:37:11
  $%0A 
@@ -3515,24 +3515,21 @@
 the 
-known_hosts file
+ssh directory
  is%0A
@@ -3546,29 +3546,19 @@
 nal 
-String knownHostsPath
+File sshDir
 ;%0A  
@@ -3742,33 +3742,34 @@
         
-knownHostsPath = 
+sshDir = new File(
 %22C:%5C%5Ccyg
@@ -3805,22 +3805,11 @@
 .ssh
-%5C%5Cknown_hosts%22
+%22 )
 ;%0A  
@@ -3855,24 +3855,26 @@
     
-knownHostsPath =
+sshDir = new File(
  Sys
@@ -3905,19 +3905,124 @@
 ome%22
-) +
+ ),
  %22
-/
 .ssh
-/
+%22 );%0A        %7D%0A%0A        // Load the known hosts file%0A        final File knownHostsFile = new File(sshDir, %22
 know
@@ -4029,16 +4029,17 @@
 n_hosts%22
+)
 ;%0A      
@@ -4032,35 +4032,105 @@
 osts%22);%0A        
-%7D%0A%0A
+if ( knownHostsFile.isFile() && knownHostsFile.canRead() )%0A        %7B%0A    
         jSch.set
@@ -4155,16 +4155,524 @@
 osts
+File.getAbsolute
 Path
+()
  );%0A
+        %7D%0A%0A        // Load the private key%0A        final File privateKeyFile = new File( sshDir, %22id_rsa%22 );%0A        if ( privateKeyFile.isFile() && privateKeyFile.canRead() )%0A        %7B%0A            try%0A            %7B%0A                jSch.addIdentity(privateKeyFile.getAbsolutePath());%0A            %7D%0A            catch ( final JSchException e )%0A            %7B%0A                throw new FileSystemException(%22vfs.provider.sftp/load-private-key.error%22, privateKeyFile, e);%0A            %7D%0A        %7D%0A
     

Add support to SftpFileProvider for loading the user's private key.