diff --git a/bundles/org.eclipse.remote.jsch.ui/META-INF/MANIFEST.MF b/bundles/org.eclipse.remote.jsch.ui/META-INF/MANIFEST.MF index 9cfe410ec89..20ee771b8e8 100644 --- a/bundles/org.eclipse.remote.jsch.ui/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.remote.jsch.ui/META-INF/MANIFEST.MF @@ -11,6 +11,7 @@ Require-Bundle: org.eclipse.core.runtime, org.eclipse.remote.ui, org.eclipse.remote.jsch.core, org.eclipse.jsch.core, + org.eclipse.jsch.ui, com.jcraft.jsch, org.eclipse.ui.forms, org.eclipse.ui.ide, diff --git a/bundles/org.eclipse.remote.jsch.ui/src/org/eclipse/internal/remote/jsch/ui/JSchUIConnectionManager.java b/bundles/org.eclipse.remote.jsch.ui/src/org/eclipse/internal/remote/jsch/ui/JSchUIConnectionManager.java index 2ce2a85f782..8c797e2bc45 100644 --- a/bundles/org.eclipse.remote.jsch.ui/src/org/eclipse/internal/remote/jsch/ui/JSchUIConnectionManager.java +++ b/bundles/org.eclipse.remote.jsch.ui/src/org/eclipse/internal/remote/jsch/ui/JSchUIConnectionManager.java @@ -10,14 +10,100 @@ *******************************************************************************/ package org.eclipse.internal.remote.jsch.ui; +import java.lang.reflect.InvocationTargetException; +import java.net.PasswordAuthentication; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; import org.eclipse.internal.remote.jsch.core.JSchConnectionManager; +import org.eclipse.internal.remote.jsch.ui.messages.Messages; import org.eclipse.internal.remote.jsch.ui.wizards.JSchConnectionWizard; +import org.eclipse.jface.dialogs.ErrorDialog; +import org.eclipse.jface.dialogs.IDialogConstants; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.dialogs.ProgressMonitorDialog; +import org.eclipse.jface.operation.IRunnableContext; +import org.eclipse.jface.operation.IRunnableWithProgress; +import org.eclipse.jsch.ui.UserInfoPrompter; +import org.eclipse.remote.core.IRemoteConnection; import org.eclipse.remote.core.IRemoteServices; +import org.eclipse.remote.core.IUserAuthenticator; +import org.eclipse.remote.core.exception.RemoteConnectionException; import org.eclipse.remote.ui.AbstractRemoteUIConnectionManager; import org.eclipse.remote.ui.IRemoteUIConnectionWizard; +import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; +import com.jcraft.jsch.JSch; +import com.jcraft.jsch.JSchException; + public class JSchUIConnectionManager extends AbstractRemoteUIConnectionManager { + private class RemoteAuthenticator implements IUserAuthenticator { + UserInfoPrompter prompter; + + public RemoteAuthenticator(IRemoteConnection conn) { + try { + prompter = new UserInfoPrompter(new JSch().getSession(conn.getUsername(), conn.getAddress())); + } catch (JSchException e) { + // Not allowed + } + } + + public PasswordAuthentication prompt(String username, String message) { + if (prompter.promptPassword(message)) { + PasswordAuthentication auth = new PasswordAuthentication(username, prompter.getPassword().toCharArray()); + return auth; + } + return null; + } + + public String[] prompt(String destination, String name, String message, String[] prompt, boolean[] echo) { + return prompter.promptKeyboardInteractive(destination, name, message, prompt, echo); + } + + public int prompt(final int promptType, final String title, final String message, final int[] promptResponses, + final int defaultResponseIndex) { + final Display display = getDisplay(); + final int[] retval = new int[1]; + final String[] buttons = new String[promptResponses.length]; + for (int i = 0; i < promptResponses.length; i++) { + int prompt = promptResponses[i]; + switch (prompt) { + case IDialogConstants.OK_ID: + buttons[i] = IDialogConstants.OK_LABEL; + break; + case IDialogConstants.CANCEL_ID: + buttons[i] = IDialogConstants.CANCEL_LABEL; + break; + case IDialogConstants.NO_ID: + buttons[i] = IDialogConstants.NO_LABEL; + break; + case IDialogConstants.YES_ID: + buttons[i] = IDialogConstants.YES_LABEL; + break; + } + } + + display.syncExec(new Runnable() { + public void run() { + final MessageDialog dialog = new MessageDialog(new Shell(display), title, null /* title image */, message, + promptType, buttons, defaultResponseIndex); + retval[0] = dialog.open(); + } + }); + return retval[0]; + } + } + + private Display getDisplay() { + Display display = Display.getCurrent(); + if (display == null) { + display = Display.getDefault(); + } + return display; + } + private final JSchConnectionManager fConnMgr; public JSchUIConnectionManager(IRemoteServices services) { @@ -32,4 +118,35 @@ public class JSchUIConnectionManager extends AbstractRemoteUIConnectionManager { public IRemoteUIConnectionWizard getConnectionWizard(Shell shell) { return new JSchConnectionWizard(shell, fConnMgr); } + + @Override + public void openConnectionWithProgress(Shell shell, IRunnableContext context, final IRemoteConnection connection) { + if (!connection.isOpen()) { + IRunnableWithProgress op = new IRunnableWithProgress() { + public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { + try { + connection.open(new RemoteAuthenticator(connection), monitor); + } catch (RemoteConnectionException e) { + throw new InvocationTargetException(e); + } + if (monitor.isCanceled()) { + throw new InterruptedException(); + } + } + }; + try { + if (context != null) { + context.run(true, true, op); + } else { + new ProgressMonitorDialog(shell).run(true, true, op); + } + } catch (InvocationTargetException e) { + ErrorDialog.openError(shell, Messages.JSchUIConnectionManager_Connection_Error, Messages.JSchUIConnectionManager_Could_not_open_connection, + new Status(IStatus.ERROR, Activator.getUniqueIdentifier(), e.getCause().getMessage())); + } catch (InterruptedException e) { + ErrorDialog.openError(shell, Messages.JSchUIConnectionManager_Connection_Error, Messages.JSchUIConnectionManager_Could_not_open_connection, + new Status(IStatus.ERROR, Activator.getUniqueIdentifier(), e.getMessage())); + } + } + } } diff --git a/bundles/org.eclipse.remote.jsch.ui/src/org/eclipse/internal/remote/jsch/ui/messages/Messages.java b/bundles/org.eclipse.remote.jsch.ui/src/org/eclipse/internal/remote/jsch/ui/messages/Messages.java index 81f632182ed..2f4526ca276 100755 --- a/bundles/org.eclipse.remote.jsch.ui/src/org/eclipse/internal/remote/jsch/ui/messages/Messages.java +++ b/bundles/org.eclipse.remote.jsch.ui/src/org/eclipse/internal/remote/jsch/ui/messages/Messages.java @@ -53,4 +53,6 @@ public class Messages extends NLS { public static String JSchNewConnectionPage_Timeout_is_not_valid; public static String JSchNewConnectionPage_User; public static String JSchNewConnectionPage_User_name_cannot_be_empty; + public static String JSchUIConnectionManager_Connection_Error; + public static String JSchUIConnectionManager_Could_not_open_connection; } diff --git a/bundles/org.eclipse.remote.jsch.ui/src/org/eclipse/internal/remote/jsch/ui/messages/messages.properties b/bundles/org.eclipse.remote.jsch.ui/src/org/eclipse/internal/remote/jsch/ui/messages/messages.properties index 7d56387b5ac..89e22f3cba3 100755 --- a/bundles/org.eclipse.remote.jsch.ui/src/org/eclipse/internal/remote/jsch/ui/messages/messages.properties +++ b/bundles/org.eclipse.remote.jsch.ui/src/org/eclipse/internal/remote/jsch/ui/messages/messages.properties @@ -36,3 +36,5 @@ JSchNewConnectionPage_Timeout=Timeout: JSchNewConnectionPage_Timeout_is_not_valid=Timeout is not valid JSchNewConnectionPage_User=User: JSchNewConnectionPage_User_name_cannot_be_empty=User name cannot be empty +JSchUIConnectionManager_Connection_Error=Connection Error +JSchUIConnectionManager_Could_not_open_connection=Could not open connection