1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-29 03:45:35 +02:00

Tweak API.

Fix check for process completion.
Add use login shell option.

Signed-off-by: Greg Watson <g.watson@computer.org>
This commit is contained in:
Greg Watson 2013-09-12 13:41:20 -04:00
parent 0f1d75b0ad
commit 1047fcb037
13 changed files with 142 additions and 122 deletions

View file

@ -1,8 +1,10 @@
package org.eclipse.remote.core.tests;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
@ -23,7 +25,7 @@ public class ProcessTests extends TestCase {
private static final String USERNAME = "test"; //$NON-NLS-1$
private static final String PASSWORD = ""; //$NON-NLS-1$
private static final String HOST = "localhost"; //$NON-NLS-1$
private static int NUM_THREADS = 5;
private static int NUM_THREADS = 2;
private IRemoteServices fRemoteServices;
private IRemoteConnection fRemoteConnection;
@ -33,10 +35,11 @@ public class ProcessTests extends TestCase {
final Set<String> results = Collections.synchronizedSet(new HashSet<String>());
for (int t = 0; t < NUM_THREADS; t++) {
final String threadNum = Integer.toString(t);
Thread thread = new Thread("test thread " + t) {
@Override
public void run() {
IRemoteProcessBuilder builder = fRemoteConnection.getProcessBuilder("perl", "-V:version"); //$NON-NLS-1$
IRemoteProcessBuilder builder = fRemoteConnection.getProcessBuilder("perl", "-v", threadNum); //$NON-NLS-1$
assertNotNull(builder);
builder.redirectErrorStream(true);
for (int i = 0; i < 10; i++) {
@ -46,12 +49,14 @@ public class ProcessTests extends TestCase {
String line;
while ((line = stdout.readLine()) != null) {
results.add(line);
results.add("\n");
}
try {
proc.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
assertTrue(results.toString().contains("Larry Wall"));
} catch (IOException e) {
e.printStackTrace();
fail(e.getLocalizedMessage());
@ -95,6 +100,72 @@ public class ProcessTests extends TestCase {
}
}
public void testEcho() {
IRemoteProcessBuilder builder = fRemoteConnection.getProcessBuilder("cat"); //$NON-NLS-1$
assertNotNull(builder);
builder.redirectErrorStream(true);
final StringBuffer result = new StringBuffer();
try {
final IRemoteProcess proc = builder.start();
Thread readerThread = new Thread("echo reader thread") {
@Override
public void run() {
try {
BufferedReader stdout = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line;
while ((line = stdout.readLine()) != null) {
result.append(line);
}
try {
proc.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
fail(e.getLocalizedMessage());
}
}
};
Thread writerThread = new Thread("echo writer thread") {
@Override
public void run() {
try {
BufferedWriter stdin = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
for (int i = 0; i < 10; i++) {
String line = i + "\n";
stdin.append(line);
stdin.flush();
}
proc.getOutputStream().close();
try {
proc.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
fail(e.getLocalizedMessage());
}
}
};
writerThread.start();
readerThread.start();
writerThread.join();
readerThread.join();
} catch (IOException e) {
e.printStackTrace();
fail(e.getLocalizedMessage());
} catch (InterruptedException e) {
e.printStackTrace();
fail(e.getLocalizedMessage());
}
assertEquals("0123456789", result.toString());
}
/*
* (non-Javadoc)
*

View file

@ -3,16 +3,15 @@ package org.eclipse.remote.core.tests.suite;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.remote.core.tests.ConnectionTests;
import org.eclipse.remote.core.tests.FileStoreTests;
import org.eclipse.remote.core.tests.ProcessTests;
public class RemoteCoreTestSuite {
public static Test suite() {
TestSuite suite = new TestSuite(RemoteCoreTestSuite.class.getName());
suite.addTestSuite(ConnectionTests.class);
suite.addTestSuite(FileStoreTests.class);
// suite.addTestSuite(ProcessTests.class);
// suite.addTestSuite(ConnectionTests.class);
// suite.addTestSuite(FileStoreTests.class);
suite.addTestSuite(ProcessTests.class);
return suite;
}

View file

@ -11,8 +11,8 @@
package org.eclipse.internal.remote.core.services.local;
import java.net.URI;
import java.util.HashSet;
import java.util.Set;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.remote.core.IRemoteConnection;
@ -64,10 +64,10 @@ public class LocalConnectionManager implements IRemoteConnectionManager {
* org.eclipse.remote.core.IRemoteConnectionManager#getConnections()
*/
@Override
public Set<IRemoteConnection> getConnections() {
Set<IRemoteConnection> set = new HashSet<IRemoteConnection>();
set.add(fLocalConnection);
return set;
public List<IRemoteConnection> getConnections() {
List<IRemoteConnection> list = new ArrayList<IRemoteConnection>();
list.add(fLocalConnection);
return list;
}
/*

View file

@ -11,7 +11,7 @@
package org.eclipse.remote.core;
import java.net.URI;
import java.util.Set;
import java.util.List;
import org.eclipse.remote.core.exception.RemoteConnectionException;
@ -51,7 +51,7 @@ public interface IRemoteConnectionManager {
*
* @return connections that we know about
*/
public Set<IRemoteConnection> getConnections();
public List<IRemoteConnection> getConnections();
/**
* Creates a new remote connection named with supplied name. The connection

View file

@ -49,10 +49,11 @@ import com.jcraft.jsch.UserInfo;
* @since 5.0
*/
public class JSchConnection implements IRemoteConnection {
protected static final int DEFAULT_PORT = 22;
protected static final int DEFAULT_TIMEOUT = 5;
protected static final boolean DEFAULT_IS_PASSWORD = true;
protected static final String EMPTY_STRING = ""; //$NON-NLS-1$
public static final int DEFAULT_PORT = 22;
public static final int DEFAULT_TIMEOUT = 5;
public static final boolean DEFAULT_IS_PASSWORD = true;
public static final boolean DEFAULT_USE_LOGIN_SHELL = true;
public static final String EMPTY_STRING = ""; //$NON-NLS-1$
private String fWorkingDir;
private boolean fIsOpen;
@ -133,18 +134,20 @@ public class JSchConnection implements IRemoteConnection {
* @see org.eclipse.remote.core.IRemoteConnection#close()
*/
public synchronized void close() {
if (isOpen()) {
if (fSftpChannel != null && fSftpChannel.isConnected()) {
if (fSftpChannel != null) {
if (fSftpChannel.isConnected()) {
fSftpChannel.disconnect();
}
for (Session session : fSessions) {
if (session.isConnected()) {
session.disconnect();
}
}
fIsOpen = false;
fireConnectionChangeEvent(IRemoteConnectionChangeEvent.CONNECTION_CLOSED);
fSftpChannel = null;
}
for (Session session : fSessions) {
if (session.isConnected()) {
session.disconnect();
}
}
fSessions.clear();
fIsOpen = false;
fireConnectionChangeEvent(IRemoteConnectionChangeEvent.CONNECTION_CLOSED);
}
/*
@ -502,7 +505,16 @@ public class JSchConnection implements IRemoteConnection {
* @see org.eclipse.remote.core.IRemoteConnection#isOpen()
*/
public boolean isOpen() {
return fIsOpen;
boolean isOpen = fIsOpen & fSessions.size() > 0;
if (isOpen) {
for (Session session : fSessions) {
isOpen &= session.isConnected();
}
if (!isOpen) {
close(); // Cleanup if session is closed
}
}
return isOpen;
}
public boolean isPasswordAuth() {
@ -791,4 +803,8 @@ public class JSchConnection implements IRemoteConnection {
}
return str + "]"; //$NON-NLS-1$
}
public boolean useLoginShell() {
return fAttributes.getBoolean(JSchConnectionAttributes.USE_LOGIN_SHELL_ATTR, DEFAULT_USE_LOGIN_SHELL);
}
}

View file

@ -35,6 +35,7 @@ public class JSchConnectionAttributes {
public static final String PASSPHRASE_ATTR = "JSCH_PASSPHRASE_ATTR"; //$NON-NLS-1$
public static final String KEYFILE_ATTR = "JSCH_KEYFILE_ATTR"; //$NON-NLS-1$
public static final String TIMEOUT_ATTR = "JSCH_TIMEOUT_ATTR"; //$NON-NLS-1$
public static final String USE_LOGIN_SHELL_ATTR = "JSCH_USE_LOGIN_SHELL_ATTR"; //$NON-NLS-1$
private String fName;
private String fNewName;

View file

@ -11,11 +11,11 @@
package org.eclipse.internal.remote.jsch.core;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
@ -78,29 +78,13 @@ public class JSchConnectionManager implements IRemoteConnectionManager {
* @see
* org.eclipse.remote.core.IRemoteConnectionManager#getConnections()
*/
public Set<IRemoteConnection> getConnections() {
public List<IRemoteConnection> getConnections() {
loadConnections();
Set<IRemoteConnection> set = new HashSet<IRemoteConnection>();
set.addAll(fConnections.values());
return set;
List<IRemoteConnection> conns = new ArrayList<IRemoteConnection>();
conns.addAll(fConnections.values());
return conns;
}
// private void loadAuth(ISecurePreferences node) throws StorageException {
// JSchConnection connection = fConnections.get(node.name());
// if (connection != null) {
// boolean isPasswordAuth = node.getBoolean(IS_PASSWORD_AUTH_KEY, true);
// connection.setIsPasswordAuth(isPasswordAuth);
// if (isPasswordAuth) {
// connection.setPassword(node.get(PASSWORD_KEY, null));
// } else {
// connection.setPassphrase(node.get(PASSPHRASE_KEY, null));
// connection.setKeyFile(node.get(KEYFILE_KEY, null));
// }
// } else {
// node.removeNode();
// }
// }
private synchronized void loadConnections() {
if (fConnections == null) {
fConnections = Collections.synchronizedMap(new HashMap<String, JSchConnection>());
@ -163,59 +147,4 @@ public class JSchConnectionManager implements IRemoteConnectionManager {
((JSchConnection) conn).getInfo().remove();
fConnections.remove(conn.getName());
}
// private void saveAuth(JSchConnection conn, ISecurePreferences node) throws StorageException {
// boolean isPasswordAuth = conn.isPasswordAuth();
// node.putBoolean(IS_PASSWORD_AUTH_KEY, isPasswordAuth, false);
// if (isPasswordAuth) {
// node.put(PASSWORD_KEY, conn.getPassword(), true);
// } else {
// node.put(PASSPHRASE_KEY, conn.getPassphrase(), true);
// node.put(KEYFILE_KEY, conn.getKeyFile(), false);
// }
// }
//
// private void saveConnection(JSchConnection conn, Preferences node) {
// node.put(HOST_KEY, conn.getAddress());
// node.put(USER_KEY, conn.getUsername());
// node.putInt(PORT_KEY, conn.getPort());
// node.putInt(TIMEOUT_KEY, conn.getTimeout());
// }
//
// public synchronized void saveConnections() {
// if (fConnections != null) {
// IEclipsePreferences root = InstanceScope.INSTANCE.getNode(Activator.getUniqueIdentifier());
// Preferences connections = root.node(CONNECTIONS_KEY);
// try {
// connections.clear();
// } catch (BackingStoreException e) {
// Activator.log(e.getMessage());
// }
// for (JSchConnection conn : fConnections.values()) {
// Preferences node = connections.node(conn.getName());
// saveConnection(conn, node);
// }
// ISecurePreferences secRoot = SecurePreferencesFactory.getDefault();
// ISecurePreferences secConnections = secRoot.node("org.eclipse.remote.jsch.connections");
// secConnections.clear();
// try {
// for (JSchConnection conn : fConnections.values()) {
// ISecurePreferences secNode = secConnections.node(conn.getName());
// saveAuth(conn, secNode);
// }
// } catch (StorageException e) {
// Activator.log(e.getMessage());
// }
// try {
// root.flush();
// } catch (BackingStoreException e) {
// Activator.log(e.getMessage());
// }
// try {
// secRoot.flush();
// } catch (IOException e) {
// Activator.log(e.getMessage());
// }
// }
// }
}

View file

@ -172,8 +172,8 @@ public class JSchConnectionWorkingCopy extends JSchConnection implements IRemote
fWorkingAttributes.setAttribute(key, value);
}
public void setIsPasswordAuth(boolean isPasswordAuth) {
fWorkingAttributes.setAttribute(JSchConnectionAttributes.IS_PASSWORD_ATTR, Boolean.toString(isPasswordAuth));
public void setIsPasswordAuth(boolean flag) {
fWorkingAttributes.setAttribute(JSchConnectionAttributes.IS_PASSWORD_ATTR, Boolean.toString(flag));
}
public void setKeyFile(String keyFile) {
@ -220,6 +220,10 @@ public class JSchConnectionWorkingCopy extends JSchConnection implements IRemote
fWorkingAttributes.setAttribute(JSchConnectionAttributes.TIMEOUT_ATTR, Integer.toString(timeout));
}
public void setUseLoginShell(boolean flag) {
fWorkingAttributes.setAttribute(JSchConnectionAttributes.USE_LOGIN_SHELL_ATTR, Boolean.toString(flag));
}
/*
* (non-Javadoc)
*

View file

@ -174,6 +174,6 @@ public class JSchProcess extends AbstractRemoteProcess {
*/
@Override
public boolean isCompleted() {
return !fChannel.isClosed();
return fChannel.isClosed();
}
}

View file

@ -206,6 +206,10 @@ public class JSchProcessBuilder extends AbstractRemoteProcessBuilder {
}
}
sb.append(cmd);
if (fConnection.useLoginShell()) {
sb.insert(0, "/bin/bash -l -c '"); //$NON-NLS-1$
sb.append("'"); //$NON-NLS-1$
}
return sb.toString();
}

View file

@ -20,10 +20,10 @@ import org.eclipse.remote.ui.AbstractRemoteUIConnectionManager;
import org.eclipse.swt.widgets.Shell;
public class JSchUIConnectionManager extends AbstractRemoteUIConnectionManager {
private final JSchConnectionManager connMgr;
private final JSchConnectionManager fConnMgr;
public JSchUIConnectionManager(IRemoteServices services) {
connMgr = (JSchConnectionManager) services.getConnectionManager();
fConnMgr = (JSchConnectionManager) services.getConnectionManager();
}
/*
@ -46,15 +46,7 @@ public class JSchUIConnectionManager extends AbstractRemoteUIConnectionManager {
* .IRemoteConnectionAttributeHint[], java.lang.String[])
*/
public IRemoteConnectionWorkingCopy newConnection(Shell shell, String[] attrHints, String[] attrHintValues) {
JSchConnectionWorkingCopy conn = createConnection(shell, attrHints, attrHintValues);
if (conn != null) {
return conn;
}
return null;
}
public JSchConnectionWorkingCopy createConnection(Shell shell, String[] attrHints, String[] attrHintValues) {
JSchConnectionWizard wizard = new JSchConnectionWizard(connMgr);
JSchConnectionWizard wizard = new JSchConnectionWizard(fConnMgr);
WizardDialog dialog = new WizardDialog(shell, wizard);
dialog.setBlockOnOpen(true);
if (dialog.open() == WizardDialog.OK) {
@ -72,7 +64,7 @@ public class JSchUIConnectionManager extends AbstractRemoteUIConnectionManager {
public boolean updateConnection(Shell shell, IRemoteConnectionWorkingCopy connection) {
if (connection instanceof JSchConnectionWorkingCopy) {
JSchConnectionWorkingCopy jSchConn = (JSchConnectionWorkingCopy) connection;
JSchConnectionWizard wizard = new JSchConnectionWizard(connMgr, jSchConn);
JSchConnectionWizard wizard = new JSchConnectionWizard(fConnMgr, jSchConn);
WizardDialog dialog = new WizardDialog(shell, wizard);
dialog.setBlockOnOpen(true);
if (dialog.open() == WizardDialog.OK) {

View file

@ -117,6 +117,7 @@ public class JSchConnectionPage extends WizardPage {
portLabel.setText(Messages.JSchNewConnectionPage_Port);
portLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
fPortText = new Text(advancedComp, SWT.BORDER | SWT.SINGLE);
fPortText.setText(Integer.toString(JSchConnection.DEFAULT_PORT));
fPortText.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false));
fPortText.setTextLimit(5);
@ -124,6 +125,7 @@ public class JSchConnectionPage extends WizardPage {
timeoutLabel.setText(Messages.JSchNewConnectionPage_Timeout);
timeoutLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
fTimeoutText = new Text(advancedComp, SWT.BORDER | SWT.SINGLE);
fTimeoutText.setText(Integer.toString(JSchConnection.DEFAULT_TIMEOUT));
fTimeoutText.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false));
fTimeoutText.setTextLimit(5);

View file

@ -22,6 +22,7 @@ import org.eclipse.internal.remote.ui.messages.Messages;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.remote.core.IRemoteConnection;
import org.eclipse.remote.core.IRemoteConnectionManager;
import org.eclipse.remote.core.IRemoteConnectionWorkingCopy;
import org.eclipse.remote.core.IRemotePreferenceConstants;
import org.eclipse.remote.core.IRemoteServices;
import org.eclipse.remote.core.RemoteServices;
@ -393,8 +394,9 @@ public class RemoteConnectionWidget extends Composite {
*/
protected void handleNewRemoteConnectionSelected() {
if (getUIConnectionManager() != null) {
IRemoteConnection conn = getUIConnectionManager().newConnection(getShell(), fAttrHints, fAttrHintValues);
IRemoteConnectionWorkingCopy conn = getUIConnectionManager().newConnection(getShell(), fAttrHints, fAttrHintValues);
if (conn != null) {
conn.save();
handleRemoteServiceSelected(conn);
handleConnectionSelected();
}