Correctly honor IdentityFile from ~/.ssh/config
[jgit.git] / org.spearce.egit.ui / src / org / spearce / egit / ui / EclipseSshSessionFactory.java
blob04600eaaf6b3f7f93421c46c090105e49dbcb2cc
1 /*******************************************************************************
2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * Copyright (C) 2009, Google, Inc.
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of the Eclipse Public License v1.0
8 * See LICENSE for the full license text, also available.
9 *******************************************************************************/
10 package org.spearce.egit.ui;
12 import org.eclipse.jsch.core.IJSchService;
13 import org.eclipse.jsch.ui.UserInfoPrompter;
14 import org.spearce.jgit.transport.OpenSshConfig;
15 import org.spearce.jgit.transport.SshConfigSessionFactory;
17 import com.jcraft.jsch.JSch;
18 import com.jcraft.jsch.JSchException;
19 import com.jcraft.jsch.Session;
21 class EclipseSshSessionFactory extends SshConfigSessionFactory {
22 private final IJSchService provider;
24 EclipseSshSessionFactory(final IJSchService p) {
25 provider = p;
28 @Override
29 protected JSch createDefaultJSch() throws JSchException {
30 // Forcing a dummy session to be created will cause the known hosts
31 // and configured private keys to be initialized. This is needed by
32 // our parent class in case non-default JSch instances need to be made.
34 provider.createSession("127.0.0.1", 0, "eclipse");
35 return provider.getJSch();
38 @Override
39 protected Session createSession(final OpenSshConfig.Host hc,
40 final String user, final String host, final int port)
41 throws JSchException {
42 final JSch jsch = getJSch(hc);
43 if (jsch == provider.getJSch()) {
44 // If its the default JSch desired, let the provider
45 // manage the session creation for us.
47 return provider.createSession(host, port, user);
48 } else {
49 // This host configuration is using a different IdentityFile,
50 // one that is not available through the default JSch.
52 return jsch.getSession(user, host, port);
56 @Override
57 protected void configure(final OpenSshConfig.Host hc, final Session session) {
58 if (!hc.isBatchMode())
59 new UserInfoPrompter(session);