From b511b31ad11eca63aa3d253258b9a728e476b178 Mon Sep 17 00:00:00 2001 From: Roland Schulz Date: Tue, 10 Jun 2014 20:02:32 -0400 Subject: [PATCH] Bug 437083 - Improve error message if ssh fails Execute loadEnv once before trying sftp to make sure exec works. This way we don't assume it is an sftp problem if the ssh connection doesn't work at all. To make this work throw an exception if ExecCommand fails (if exit code != 0). ExecCommand doesn't have a way to check exit code. Thus this also fixes that if any command executed with ExecCommand failed, the calling code didn't notice. Change-Id: I30b8baa0d87166e179fad67643e31a9f17c8ead2 Signed-off-by: Roland Schulz --- .../internal/jsch/core/JSchConnection.java | 17 +++++------------ .../core/commands/AbstractRemoteCommand.java | 2 +- .../jsch/core/commands/ExecCommand.java | 7 ++++++- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/JSchConnection.java b/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/JSchConnection.java index 4e83f70adec..325f20c104b 100644 --- a/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/JSchConnection.java +++ b/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/JSchConnection.java @@ -171,7 +171,6 @@ public class JSchConnection implements IRemoteConnection { public static final String EMPTY_STRING = ""; //$NON-NLS-1$ private String fWorkingDir; - private boolean fIsOpen; private final IJSchService fJSchService; @@ -263,7 +262,6 @@ public class JSchConnection implements IRemoteConnection { } } fSessions.clear(); - fIsOpen = false; fireConnectionChangeEvent(IRemoteConnectionChangeEvent.CONNECTION_CLOSED); } @@ -661,7 +659,7 @@ public class JSchConnection implements IRemoteConnection { */ @Override public boolean isOpen() { - boolean isOpen = fIsOpen & fSessions.size() > 0; + boolean isOpen = fSessions.size() > 0; if (isOpen) { for (Session session : fSessions) { isOpen &= session.isConnected(); @@ -814,23 +812,18 @@ public class JSchConnection implements IRemoteConnection { public void open(IProgressMonitor monitor) throws RemoteConnectionException { if (!isOpen()) { checkIsConfigured(); - SubMonitor subMon = SubMonitor.convert(monitor, 70); + SubMonitor subMon = SubMonitor.convert(monitor, 60); Session session = newSession(fManager.getUserAuthenticator(this), subMon.newChild(10)); if (subMon.isCanceled()) { throw new RemoteConnectionException(Messages.JSchConnection_Connection_was_cancelled); } + //getCwd checks the exec channel before checkConfiguration checks the sftp channel + fWorkingDir = getCwd(subMon.newChild(10)); if (!checkConfiguration(session, subMon.newChild(20))) { newSession(fManager.getUserAuthenticator(this), subMon.newChild(10)); loadEnv(subMon.newChild(10)); } - fIsOpen = true; - try { - fWorkingDir = getCwd(subMon.newChild(10)); - loadProperties(subMon.newChild(10)); - } catch (RemoteConnectionException e) { - fIsOpen = false; - throw e; - } + loadProperties(subMon.newChild(10)); fireConnectionChangeEvent(IRemoteConnectionChangeEvent.CONNECTION_OPENED); } } diff --git a/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/AbstractRemoteCommand.java b/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/AbstractRemoteCommand.java index 95ec7aa0fcb..60ea373d49b 100755 --- a/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/AbstractRemoteCommand.java +++ b/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/AbstractRemoteCommand.java @@ -126,7 +126,7 @@ public abstract class AbstractRemoteCommand { * @see java.util.concurrent.Callable#call() */ @Override - public abstract T1 call() throws JSchException, IOException; + public abstract T1 call() throws JSchException, IOException, RemoteConnectionException; private void finalizeCmdInThread() { setChannel(null); diff --git a/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/ExecCommand.java b/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/ExecCommand.java index 0dffebc5217..8e8fb3b2573 100644 --- a/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/ExecCommand.java +++ b/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/ExecCommand.java @@ -29,10 +29,12 @@ public class ExecCommand extends AbstractRemoteCommand { final SubMonitor subMon = SubMonitor.convert(monitor, 10); ExecCallable c = new ExecCallable() { @Override - public String call() throws JSchException { + public String call() throws JSchException, RemoteConnectionException { getChannel().setCommand(fCommand); ByteArrayOutputStream stream = new ByteArrayOutputStream(); + ByteArrayOutputStream err = new ByteArrayOutputStream(); getChannel().setOutputStream(stream); + getChannel().setErrStream(err); getChannel().connect(); while (!getChannel().isClosed() && !getProgressMonitor().isCanceled()) { synchronized (this) { @@ -46,6 +48,9 @@ public class ExecCommand extends AbstractRemoteCommand { if (getProgressMonitor().isCanceled()) { return ""; //$NON-NLS-1$ } + if (getChannel().getExitStatus()!=0) { + throw new RemoteConnectionException(err.toString()); + } return stream.toString(); } };