Create the import wizard question checkbox properly
[egit/imyousuf.git] / org.spearce.egit.ui / src / org / spearce / egit / ui / EclipseSshSessionFactory.java
blob098d234733fde33825fc1d99444afeee35c993ed
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;
11 import java.io.File;
12 import java.io.IOException;
13 import java.io.OutputStream;
14 import java.util.HashSet;
15 import java.util.Set;
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) {
33 provider = p;
36 @Override
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();
41 if (port <= 0)
42 port = hc.getPort();
43 if (user == null)
44 user = hc.getUser();
46 final Session session = provider.createSession(host, port, user);
47 if (hc.getIdentityFile() != null)
48 addIdentity(hc.getIdentityFile());
49 if (pass != null)
50 session.setPassword(pass);
51 else if (!hc.isBatchMode())
52 new UserInfoPrompter(session);
54 final String pauth = hc.getPreferredAuthentications();
55 if (pauth != null)
56 session.setConfig("PreferredAuthentications", pauth);
57 return session;
60 private synchronized OpenSshConfig getConfig() {
61 if (config == null)
62 config = OpenSshConfig.get();
63 return config;
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);
73 @Override
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);
85 return r;
88 @Override
89 public void write(int b) throws IOException {
90 if (b == '\r')
91 return;
92 sb.append((char) b);
93 if (b == '\n') {
94 String s = sb.toString();
95 all.append(s);
96 sb = new StringBuilder();
97 Activator.logError(s, new Throwable());