/**
* Creates a new connection to the server.
*/
public static Session createConnection(String hostname, int port, char[] username, char[] password, FileSystemOptions fileSystemOptions) throws FileSystemException
{
JSch jsch = new JSch();
File sshDir = null;
// new style - user passed
File knownHostsFile = SftpFileSystemConfigBuilder.getInstance().getKnownHosts(fileSystemOptions);
File[] identities = SftpFileSystemConfigBuilder.getInstance().getIdentities(fileSystemOptions);
if (knownHostsFile != null)
{
try
{
jsch.setKnownHosts(knownHostsFile.getAbsolutePath());
}
catch (JSchException e)
{
throw new FileSystemException("vfs.provider.sftp/known-hosts.error", knownHostsFile.getAbsolutePath(), e);
}
}
else
{
if (sshDir == null)
{
sshDir = findSshDir();
}
// Load the known hosts file
knownHostsFile = new File(sshDir, "known_hosts");
if (knownHostsFile.isFile() && knownHostsFile.canRead())
{
try
{
jsch.setKnownHosts(knownHostsFile.getAbsolutePath());
}
catch (JSchException e)
{
throw new FileSystemException("vfs.provider.sftp/known-hosts.error", knownHostsFile.getAbsolutePath(), e);
}
}
}
if (identities != null)
{
for (int iterIdentities = 0; iterIdentities < identities.length; iterIdentities++)
{
final File privateKeyFile = identities[iterIdentities];
try
{
jsch.addIdentity(privateKeyFile.getAbsolutePath());
}
catch (final JSchException e)
{
throw new FileSystemException("vfs.provider.sftp/load-private-key.error", privateKeyFile, e);
}
}
}
else
{
if (sshDir == null)
{
sshDir = findSshDir();
}
// Load the private key (rsa-key only)
final File privateKeyFile = new File(sshDir, "id_rsa");
if (privateKeyFile.isFile() && privateKeyFile.canRead())
{
try
{
jsch.addIdentity(privateKeyFile.getAbsolutePath());
}
catch (final JSchException e)
{
throw new FileSystemException("vfs.provider.sftp/load-private-key.error", privateKeyFile, e);
}
}
}
Session session;
try
{
session = jsch.getSession(new String(username),
hostname,
port);
if (password != null)
{
session.setPassword(new String(password));