1 /*******************************************************************************
2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License v1.0
7 * See LICENSE for the full license text, also available.
8 *******************************************************************************/
9 package org
.spearce
.egit
.ui
;
12 import java
.io
.IOException
;
13 import java
.io
.OutputStream
;
14 import java
.util
.HashSet
;
17 import org
.eclipse
.jsch
.core
.IJSchService
;
18 import org
.eclipse
.jsch
.ui
.UserInfoPrompter
;
19 import org
.spearce
.jgit
.transport
.OpenSshConfig
;
20 import org
.spearce
.jgit
.transport
.SshSessionFactory
;
22 import com
.jcraft
.jsch
.JSchException
;
23 import com
.jcraft
.jsch
.Session
;
25 class EclipseSshSessionFactory
extends SshSessionFactory
{
26 private final IJSchService provider
;
28 private final Set
<String
> loadedIdentities
= new HashSet
<String
>();
30 private OpenSshConfig config
;
32 EclipseSshSessionFactory(final IJSchService p
) {
37 public Session
getSession(String user
, String pass
, String host
, int port
)
38 throws JSchException
{
39 final OpenSshConfig
.Host hc
= getConfig().lookup(host
);
40 host
= hc
.getHostName();
46 final Session session
= provider
.createSession(host
, port
, user
);
47 if (hc
.getIdentityFile() != null)
48 addIdentity(hc
.getIdentityFile());
50 session
.setPassword(pass
);
51 else if (!hc
.isBatchMode())
52 new UserInfoPrompter(session
);
54 final String pauth
= hc
.getPreferredAuthentications();
56 session
.setConfig("PreferredAuthentications", pauth
);
60 private synchronized OpenSshConfig
getConfig() {
62 config
= OpenSshConfig
.get();
66 private void addIdentity(final File identityFile
)
67 throws JSchException
{
68 final String path
= identityFile
.getAbsolutePath();
69 if (loadedIdentities
.add(path
))
70 provider
.getJSch().addIdentity(path
);
74 public OutputStream
getErrorStream() {
75 return new OutputStream() {
77 StringBuilder all
= new StringBuilder();
79 StringBuilder sb
= new StringBuilder();
81 public String
toString() {
82 String r
= all
.toString();
83 while (r
.endsWith("\n"))
84 r
= r
.substring(0, r
.length() - 1);
89 public void write(int b
) throws IOException
{
94 String s
= sb
.toString();
96 sb
= new StringBuilder();
97 Activator
.logError(s
, new Throwable());