From 82f4d12873f52698f518c19d6515124cf412ee64 Mon Sep 17 00:00:00 2001 From: Greg Watson Date: Mon, 30 Mar 2015 16:37:39 -0400 Subject: [PATCH] Bug 463468 - provide set methods for host services Change-Id: I68d4e7bb1cdcc81d9584d8055b0460d7c4cad5c2 Signed-off-by: Greg Watson --- .../core/IRemoteConnectionHostService.java | 93 ++++++++++++++++++- .../internal/jsch/core/JSchConnection.java | 66 +++++++++++++ .../core/tests/internal/ServiceTests.java | 83 +++++++++++++++++ 3 files changed, 238 insertions(+), 4 deletions(-) create mode 100644 tests/org.eclipse.remote.core.tests/src/org/eclipse/remote/core/tests/internal/ServiceTests.java diff --git a/bundles/org.eclipse.remote.core/src/org/eclipse/remote/core/IRemoteConnectionHostService.java b/bundles/org.eclipse.remote.core/src/org/eclipse/remote/core/IRemoteConnectionHostService.java index 949c8efe070..995054aa3a5 100644 --- a/bundles/org.eclipse.remote.core/src/org/eclipse/remote/core/IRemoteConnectionHostService.java +++ b/bundles/org.eclipse.remote.core/src/org/eclipse/remote/core/IRemoteConnectionHostService.java @@ -20,10 +20,31 @@ public interface IRemoteConnectionHostService extends IRemoteConnection.Service /** * Obtain the hostname associated with this connection. * - * @return + * @return hostname */ String getHostname(); + /** + * Obtain the port associated with this connection + * + * @return port + */ + int getPort(); + + /** + * Obtain the timeout used when establishing the connection. + * + * @return timeout + */ + int getTimeout(); + + /** + * Obtain the flag that indicates a login shell should be started once the connection is established + * + * @return login shell flag + */ + boolean useLoginShell(); + /** * Obtain the username associated with this connection. * @@ -32,9 +53,73 @@ public interface IRemoteConnectionHostService extends IRemoteConnection.Service String getUsername(); /** - * Obtain the port associated with this connection + * Set the hostname associated with this connection. Note, this method can only be used for an + * IRemoteConnectionWorkingCopy and will have no effect otherwise. * - * @return + * @param hostname + * new hostname for connection */ - int getPort(); + void setHostname(String hostname); + + /** + * Set the pass phrase associated with this connection. Note, this method can only be used for an + * IRemoteConnectionWorkingCopy and will have no effect otherwise. + * + * @param passphrase + */ + void setPassphrase(String passphrase); + + /** + * Set the password associated with this connection. Note, this method can only be used for an + * IRemoteConnectionWorkingCopy and will have no effect otherwise. + * + * @param password + * new password for connection + */ + void setPassword(String password); + + /** + * Set the port used for the connection. Note, this method can only be used forh an + * IRemoteConnectionWorkingCopy and will have no effect otherwise. + * + * @param port + * new port for connection + */ + void setPort(int port); + + /** + * Set the timeout used when establishing the connection. A timeout of 0 means infinite. Note, this method can only be used + * for an IRemoteConnectionWorkingCopy and will have no effect otherwise. + * + * @param timeout + * new timeout value + */ + void setTimeout(int timeout); + + /** + * Set the flag indicating a login shell should be stated for this connection. Note, this method can only be used + * for an IRemoteConnectionWorkingCopy and will have no effect otherwise. + * + * @param useLogingShell + * true to start a login shell + */ + void setUseLoginShell(boolean useLogingShell); + + /** + * Set the connection to try password authentication first. Note, this method can only be used for an + * IRemoteConnectionWorkingCopy and will have no effect otherwise. + * + * @param usePassword + * use password authentication + */ + void setUsePassword(boolean usePassword); + + /** + * Set the username associated with this connection. Note, this method can only be used for an + * IRemoteConnectionWorkingCopy and will have no effect otherwise. + * + * @param username + * new username for connection + */ + void setUsername(String username); } 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 c38d967ff08..712fe4f7fc1 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 @@ -652,6 +652,7 @@ public class JSchConnection implements IRemoteConnectionControlService, IRemoteC } } + @Override public int getTimeout() { String str = fRemoteConnection.getAttribute(TIMEOUT_ATTR); return !str.isEmpty() ? Integer.parseInt(str) : DEFAULT_TIMEOUT; @@ -967,9 +968,74 @@ public class JSchConnection implements IRemoteConnectionControlService, IRemoteC } } + @Override public boolean useLoginShell() { String str = fRemoteConnection.getAttribute(USE_LOGIN_SHELL_ATTR); return !str.isEmpty() ? Boolean.parseBoolean(str) : DEFAULT_USE_LOGIN_SHELL; } + @Override + public void setHostname(String hostname) { + if (fRemoteConnection instanceof IRemoteConnectionWorkingCopy) { + IRemoteConnectionWorkingCopy wc = (IRemoteConnectionWorkingCopy) fRemoteConnection; + wc.setAttribute(ADDRESS_ATTR, hostname); + } + } + + @Override + public void setPassphrase(String passphrase) { + if (fRemoteConnection instanceof IRemoteConnectionWorkingCopy) { + IRemoteConnectionWorkingCopy wc = (IRemoteConnectionWorkingCopy) fRemoteConnection; + wc.setSecureAttribute(PASSPHRASE_ATTR, passphrase); + } + } + + @Override + public void setPassword(String password) { + if (fRemoteConnection instanceof IRemoteConnectionWorkingCopy) { + IRemoteConnectionWorkingCopy wc = (IRemoteConnectionWorkingCopy) fRemoteConnection; + wc.setSecureAttribute(PASSWORD_ATTR, password); + } + } + + @Override + public void setPort(int port) { + if (fRemoteConnection instanceof IRemoteConnectionWorkingCopy) { + IRemoteConnectionWorkingCopy wc = (IRemoteConnectionWorkingCopy) fRemoteConnection; + wc.setAttribute(PORT_ATTR, Integer.toString(port)); + } + } + + @Override + public void setTimeout(int timeout) { + if (fRemoteConnection instanceof IRemoteConnectionWorkingCopy) { + IRemoteConnectionWorkingCopy wc = (IRemoteConnectionWorkingCopy) fRemoteConnection; + wc.setAttribute(TIMEOUT_ATTR, Integer.toString(timeout)); + } + } + + @Override + public void setUseLoginShell(boolean useLogingShell) { + if (fRemoteConnection instanceof IRemoteConnectionWorkingCopy) { + IRemoteConnectionWorkingCopy wc = (IRemoteConnectionWorkingCopy) fRemoteConnection; + wc.setAttribute(USE_LOGIN_SHELL_ATTR, Boolean.toString(useLogingShell)); + } + } + + @Override + public void setUsePassword(boolean usePassword) { + if (fRemoteConnection instanceof IRemoteConnectionWorkingCopy) { + IRemoteConnectionWorkingCopy wc = (IRemoteConnectionWorkingCopy) fRemoteConnection; + wc.setAttribute(IS_PASSWORD_ATTR, Boolean.toString(usePassword)); + } + } + + @Override + public void setUsername(String username) { + if (fRemoteConnection instanceof IRemoteConnectionWorkingCopy) { + IRemoteConnectionWorkingCopy wc = (IRemoteConnectionWorkingCopy) fRemoteConnection; + wc.setAttribute(USERNAME_ATTR, username); + } + } + } diff --git a/tests/org.eclipse.remote.core.tests/src/org/eclipse/remote/core/tests/internal/ServiceTests.java b/tests/org.eclipse.remote.core.tests/src/org/eclipse/remote/core/tests/internal/ServiceTests.java new file mode 100644 index 00000000000..3c6a4d7a77b --- /dev/null +++ b/tests/org.eclipse.remote.core.tests/src/org/eclipse/remote/core/tests/internal/ServiceTests.java @@ -0,0 +1,83 @@ +package org.eclipse.remote.core.tests.internal; + +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.remote.core.IRemoteConnection; +import org.eclipse.remote.core.IRemoteConnectionHostService; +import org.eclipse.remote.core.IRemoteConnectionType; +import org.eclipse.remote.core.IRemoteConnectionWorkingCopy; +import org.eclipse.remote.core.IRemoteServicesManager; +import org.eclipse.remote.core.exception.RemoteConnectionException; + +import junit.framework.TestCase; + +public class ServiceTests extends TestCase { + private static final String USERNAME = "greg"; //$NON-NLS-1$ + private static final String PASSWORD = ""; //$NON-NLS-1$ + private static final String HOST = "localhost"; //$NON-NLS-1$ + + private IRemoteConnectionType fConnectionType; + private IRemoteConnection fRemoteConnection; + + public void testHostService() { + IRemoteConnectionWorkingCopy wc = null; + try { + wc = fConnectionType.newConnection("test_connection");//$NON-NLS-1$ + } catch (RemoteConnectionException e) { + fail(e.getLocalizedMessage()); + } + + IRemoteConnectionHostService hostService = wc.getService(IRemoteConnectionHostService.class); + assertNotNull(hostService); + + String host = System.getenv("TEST_HOST"); + if (host == null) { + host = HOST; + } + hostService.setHostname(host); + + String username = System.getenv("TEST_USERNAME"); + if (username == null) { + username = USERNAME; + } + hostService.setUsername(username); + + String password = System.getenv("TEST_PASSWORD"); + if (password == null) { + password = PASSWORD; + } + hostService.setPassword(password); + + try { + fRemoteConnection = wc.save(); + } catch (RemoteConnectionException e) { + fail(e.getLocalizedMessage()); + } + assertNotNull(fRemoteConnection); + + try { + fRemoteConnection.open(new NullProgressMonitor()); + } catch (RemoteConnectionException e) { + fail(e.getLocalizedMessage()); + } + assertTrue(fRemoteConnection.isOpen()); + + hostService = fRemoteConnection.getService(IRemoteConnectionHostService.class); + assertNotNull(hostService); + + assertEquals(hostService.getHostname(), host); + assertEquals(hostService.getUsername(), username); + } + + @Override + protected void setUp() throws Exception { + IRemoteServicesManager manager = Activator.getService(IRemoteServicesManager.class); + fConnectionType = manager.getConnectionType("org.eclipse.remote.JSch"); //$NON-NLS-1$ + assertNotNull(fConnectionType); + } + + @Override + protected void tearDown() throws Exception { + fConnectionType.removeConnection(fRemoteConnection); + } + +}