1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-02 22:55:26 +02:00

[173619][api] Make RemoteUtil.createSocket() public available

This commit is contained in:
Martin Oberhuber 2007-02-09 11:00:47 +00:00
parent 7fa0ec0b33
commit 76410247f4
6 changed files with 186 additions and 103 deletions

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2006 Wind River Systems, Inc. and others.
* Copyright (c) 2006, 2007 Wind River Systems, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@ -42,7 +42,4 @@ public class SshConnectorResources extends NLS {
public static String KeyboardInteractiveDialog_message;
public static String KeyboardInteractiveDialog_labelConnection;
//These are from cvs/messages.properties
public static String Socket_timeout;
}

View file

@ -1,5 +1,5 @@
################################################################################
# Copyright (c) 2006 Wind River Systems, Inc. and others.
# Copyright (c) 2006, 2007 Wind River Systems, Inc. and others.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
@ -32,6 +32,3 @@ UserValidationDialog_7=Saved passwords are stored on your computer in a file tha
KeyboardInteractiveDialog_message=Keyboard Interactive authentication for {0}
KeyboardInteractiveDialog_labelConnection=Enter values for the following connection: {0}
#These are from cvs/messages.properties
Socket_timeout=A timeout occurred connecting to host {0}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2006 Wind River Systems, Inc. and others.
* Copyright (c) 2006, 2007 Wind River Systems, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@ -13,7 +13,6 @@ package org.eclipse.rse.connectorservice.ssh;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
@ -34,7 +33,6 @@ import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.window.Window;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;
@ -59,6 +57,7 @@ import org.eclipse.rse.core.subsystems.AbstractConnectorService;
import org.eclipse.rse.core.subsystems.CommunicationsEvent;
import org.eclipse.rse.core.subsystems.IConnectorService;
import org.eclipse.rse.core.subsystems.SubSystemConfiguration;
import org.eclipse.rse.services.RemoteUtil;
import org.eclipse.rse.services.clientserver.messages.SystemMessage;
import org.eclipse.rse.services.ssh.ISshSessionProvider;
import org.eclipse.rse.ui.ISystemMessages;
@ -85,97 +84,6 @@ public class SshConnectorService extends AbstractConnectorService implements ISs
fSessionLostHandler = null;
}
//----------------------------------------------------------------------
// <copied from org.eclipse.team.cvs>
//----------------------------------------------------------------------
/**
* Progress Monitor Helper: from team.cvs.core.Policy
* @param monitor
*/
public static void checkCanceled(IProgressMonitor monitor) {
if (monitor.isCanceled())
throw new OperationCanceledException();
}
/**
* Helper method that will time out when making a socket connection.
* This is required because there is no way to provide a timeout value
* when creating a socket and in some instances, they don't seem to
* timeout at all.
* @param host inetaddress to connect to
* @param port port to connect to
* @param timeout number of seconds for timeout (default=60)
* @param monitor progress monitor
*/
public static Socket createSocket(final String host, final int port, int timeout, IProgressMonitor monitor) throws UnknownHostException, IOException {
// Start a thread to open a socket
final Socket[] socket = new Socket[] { null };
final Exception[] exception = new Exception[] {null };
final Thread thread = new Thread(new Runnable() {
public void run() {
try {
Socket newSocket = new Socket(host, port);
synchronized (socket) {
if (Thread.interrupted()) {
// we we're either cancelled or timed out so just close the socket
newSocket.close();
} else {
socket[0] = newSocket;
}
}
} catch (UnknownHostException e) {
exception[0] = e;
} catch (IOException e) {
exception[0] = e;
}
}
});
thread.start();
// Wait the appropriate number of seconds
if (timeout <= 0) timeout = 60;
for (int i = 0; i < timeout; i++) {
try {
// wait for the thread to complete or 1 second, which ever comes first
thread.join(1000);
} catch (InterruptedException e) {
// I think this means the thread was interupted but not necessarily timed out
// so we don't need to do anything
}
synchronized (socket) {
// if the user cancelled, clean up before preempting the operation
if (monitor.isCanceled()) {
if (thread.isAlive()) {
thread.interrupt();
}
if (socket[0] != null) {
socket[0].close();
}
// this method will throw the proper exception
checkCanceled(monitor);
}
}
}
// If the thread is still running (i.e. we timed out) signal that it is too late
synchronized (socket) {
if (thread.isAlive()) {
thread.interrupt();
}
}
if (exception[0] != null) {
if (exception[0] instanceof UnknownHostException)
throw (UnknownHostException)exception[0];
else
throw (IOException)exception[0];
}
if (socket[0] == null) {
throw new InterruptedIOException(NLS.bind(SshConnectorResources.Socket_timeout, new String[] { host }));
}
return socket[0];
}
//----------------------------------------------------------------------
// <copied from org.eclipse.team.cvs.ssh2>
//----------------------------------------------------------------------
@ -349,7 +257,7 @@ public class SshConnectorService extends AbstractConnectorService implements ISs
Socket socket = null;
//Allows to cancel the socket creation operation if necessary.
//Waits for the timeout specified in CVS Preferences, maximum.
socket = SshConnectorService.createSocket(host, port, CONNECT_DEFAULT_TIMEOUT, monitor);
socket = RemoteUtil.createSocket(host, port, CONNECT_DEFAULT_TIMEOUT, monitor);
// Null out the monitor so we don't hold onto anything
// (i.e. the SSH2 session will keep a handle to the socket factory around
monitor = new NullProgressMonitor();

View file

@ -0,0 +1,26 @@
/*******************************************************************************
* Copyright (c) 2007 Wind River Systems, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Martin Oberhuber (Wind River) - initial API and implementation
*******************************************************************************/
package org.eclipse.rse.internal.services;
import org.eclipse.osgi.util.NLS;
public class RSEServicesMessages extends NLS {
private static final String BUNDLE_NAME = "org.eclipse.rse.internal.services.messages"; //$NON-NLS-1$
static {
NLS.initializeMessages(BUNDLE_NAME, RSEServicesMessages.class);
}
private RSEServicesMessages() {
}
public static String Socket_timeout;
}

View file

@ -0,0 +1,17 @@
###############################################################################
# Copyright (c) 2000, 2007 IBM Corporation and others.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
#
# Contributors:
# IBM Corporation - initial API and implementation
# Martin Oberhuber (Wind River) - copy Socket_timeout from team.cvs.core
################################################################################
# NLS_MESSAGEFORMAT_VAR
# NLS_ENCODING=UTF-8
#From org.eclipse.team.internal.ccvs.core/messages.properties
Socket_timeout=A timeout occurred connecting to host {0}

View file

@ -0,0 +1,138 @@
/*******************************************************************************
* Copyright (c) 2000, 2007 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
* Martin Oberhuber (Wind River) - added createSocket() from team.cvs.core/Util
*******************************************************************************/
package org.eclipse.rse.services;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.Socket;
import java.net.UnknownHostException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.osgi.util.NLS;
import org.eclipse.rse.internal.services.RSEServicesMessages;
/**
* Static helper-methods for network access.
*/
public class RemoteUtil {
//----------------------------------------------------------------------
// <copied
// plugin: org.eclipse.team.cvs.core
// class: org.eclipse.team.internal.ccvs.core.Policy
// copyright: (c) 2000, 2006 IBM Corporation and others />
//----------------------------------------------------------------------
/**
* Progress Monitor Helper: Checks the passed progress monitor
* and throws an {@link OperationCanceledException} when it is
* canceled.
*
* @param monitor The ProgressMonitor to be checked
*/
public static void checkCanceled(IProgressMonitor monitor) {
if (monitor.isCanceled())
throw new OperationCanceledException();
}
//</copied>
//----------------------------------------------------------------------
// <copied
// plugin: org.eclipse.team.cvs
// class: org.eclipse.team.internal.ccvs.core.util.Util
// copyright: (c) 2000, 2006 IBM Corporation and others />
//----------------------------------------------------------------------
/**
* Helper method that will time out when making a socket connection.
* This is required because there is no way to provide a timeout value
* when creating a socket and in some instances, they don't seem to
* timeout at all.
* @param host inetaddress to connect to
* @param port port to connect to
* @param timeout number of seconds for timeout (default=60)
* @param monitor progress monitor
*/
public static Socket createSocket(final String host, final int port, int timeout, IProgressMonitor monitor) throws UnknownHostException, IOException {
// Start a thread to open a socket
final Socket[] socket = new Socket[] { null };
final Exception[] exception = new Exception[] {null };
final Thread thread = new Thread(new Runnable() {
public void run() {
try {
Socket newSocket = new Socket(host, port);
synchronized (socket) {
if (Thread.interrupted()) {
// we we're either cancelled or timed out so just close the socket
newSocket.close();
} else {
socket[0] = newSocket;
}
}
} catch (UnknownHostException e) {
exception[0] = e;
} catch (IOException e) {
exception[0] = e;
}
}
});
thread.start();
// Wait the appropriate number of seconds
if (timeout <= 0) timeout = 60;
for (int i = 0; i < timeout; i++) {
try {
// wait for the thread to complete or 1 second, which ever comes first
thread.join(1000);
} catch (InterruptedException e) {
// I think this means the thread was interupted but not necessarily timed out
// so we don't need to do anything
}
synchronized (socket) {
// if the user cancelled, clean up before preempting the operation
if (monitor.isCanceled()) {
if (thread.isAlive()) {
thread.interrupt();
}
if (socket[0] != null) {
socket[0].close();
}
// this method will throw the proper exception
checkCanceled(monitor);
}
}
}
// If the thread is still running (i.e. we timed out) signal that it is too late
synchronized (socket) {
if (thread.isAlive()) {
thread.interrupt();
}
}
if (exception[0] != null) {
if (exception[0] instanceof UnknownHostException)
throw (UnknownHostException)exception[0];
else
throw (IOException)exception[0];
}
if (socket[0] == null) {
throw new InterruptedIOException(NLS.bind(RSEServicesMessages.Socket_timeout, new String[] { host }));
}
return socket[0];
}
//</copied>
}