Use the Eclipse UserInfoPrompter to support SSH prompts
[egit/zawir.git] / org.spearce.egit.ui / src / org / spearce / egit / ui / EclipseSshSessionFactory.java
blob0648459ccc7e105db75c9a6b7dc8038d1db5a916
1 /*
2 * Copyright (C) 2008 Shawn Pearce <spearce@spearce.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License, version 2.1, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org.spearce.egit.ui;
19 import java.security.AccessController;
20 import java.security.PrivilegedAction;
22 import org.eclipse.jsch.core.IJSchService;
23 import org.eclipse.jsch.ui.UserInfoPrompter;
24 import org.spearce.jgit.transport.SshSessionFactory;
26 import com.jcraft.jsch.JSchException;
27 import com.jcraft.jsch.Session;
29 class EclipseSshSessionFactory extends SshSessionFactory {
30 private final IJSchService provider;
32 EclipseSshSessionFactory(final IJSchService p) {
33 provider = p;
36 @Override
37 public Session getSession(final String user, final String pass,
38 final String host, final int port) throws JSchException {
39 final Session session = provider.createSession(host, port > 0 ? port
40 : -1, user != null ? user : userName());
41 if (pass != null)
42 session.setPassword(pass);
43 else
44 new UserInfoPrompter(session);
45 return session;
48 private static String userName() {
49 return AccessController.doPrivileged(new PrivilegedAction<String>() {
50 public String run() {
51 return System.getProperty("user.name");
53 });