From 76410247f40d976d04f800d65eb0831c97a5208a Mon Sep 17 00:00:00 2001 From: Martin Oberhuber < martin.oberhuber@windriver.com> Date: Fri, 9 Feb 2007 11:00:47 +0000 Subject: [PATCH] [173619][api] Make RemoteUtil.createSocket() public available --- .../ssh/SshConnectorResources.java | 5 +- .../ssh/SshConnectorResources.properties | 5 +- .../ssh/SshConnectorService.java | 98 +------------ .../services/RSEServicesMessages.java | 26 ++++ .../rse/internal/services/messages.properties | 17 +++ .../org/eclipse/rse/services/RemoteUtil.java | 138 ++++++++++++++++++ 6 files changed, 186 insertions(+), 103 deletions(-) create mode 100644 rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/internal/services/RSEServicesMessages.java create mode 100644 rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/internal/services/messages.properties create mode 100644 rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/services/RemoteUtil.java diff --git a/rse/plugins/org.eclipse.rse.connectorservice.ssh/src/org/eclipse/rse/connectorservice/ssh/SshConnectorResources.java b/rse/plugins/org.eclipse.rse.connectorservice.ssh/src/org/eclipse/rse/connectorservice/ssh/SshConnectorResources.java index 047765020fd..5370be1cea3 100644 --- a/rse/plugins/org.eclipse.rse.connectorservice.ssh/src/org/eclipse/rse/connectorservice/ssh/SshConnectorResources.java +++ b/rse/plugins/org.eclipse.rse.connectorservice.ssh/src/org/eclipse/rse/connectorservice/ssh/SshConnectorResources.java @@ -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; - } diff --git a/rse/plugins/org.eclipse.rse.connectorservice.ssh/src/org/eclipse/rse/connectorservice/ssh/SshConnectorResources.properties b/rse/plugins/org.eclipse.rse.connectorservice.ssh/src/org/eclipse/rse/connectorservice/ssh/SshConnectorResources.properties index 1df864cd944..8041534f220 100644 --- a/rse/plugins/org.eclipse.rse.connectorservice.ssh/src/org/eclipse/rse/connectorservice/ssh/SshConnectorResources.properties +++ b/rse/plugins/org.eclipse.rse.connectorservice.ssh/src/org/eclipse/rse/connectorservice/ssh/SshConnectorResources.properties @@ -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} diff --git a/rse/plugins/org.eclipse.rse.connectorservice.ssh/src/org/eclipse/rse/connectorservice/ssh/SshConnectorService.java b/rse/plugins/org.eclipse.rse.connectorservice.ssh/src/org/eclipse/rse/connectorservice/ssh/SshConnectorService.java index 38327728edd..7a48ae3caee 100644 --- a/rse/plugins/org.eclipse.rse.connectorservice.ssh/src/org/eclipse/rse/connectorservice/ssh/SshConnectorService.java +++ b/rse/plugins/org.eclipse.rse.connectorservice.ssh/src/org/eclipse/rse/connectorservice/ssh/SshConnectorService.java @@ -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; } - //---------------------------------------------------------------------- - // - //---------------------------------------------------------------------- - - /** - * 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]; - } - //---------------------------------------------------------------------- // //---------------------------------------------------------------------- @@ -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(); diff --git a/rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/internal/services/RSEServicesMessages.java b/rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/internal/services/RSEServicesMessages.java new file mode 100644 index 00000000000..f33f6523431 --- /dev/null +++ b/rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/internal/services/RSEServicesMessages.java @@ -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; + +} diff --git a/rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/internal/services/messages.properties b/rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/internal/services/messages.properties new file mode 100644 index 00000000000..57b1e72e1b2 --- /dev/null +++ b/rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/internal/services/messages.properties @@ -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} diff --git a/rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/services/RemoteUtil.java b/rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/services/RemoteUtil.java new file mode 100644 index 00000000000..8cd3811b2b6 --- /dev/null +++ b/rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/services/RemoteUtil.java @@ -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 { + + //---------------------------------------------------------------------- + // + //---------------------------------------------------------------------- + + /** + * 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(); + } + + // + + //---------------------------------------------------------------------- + // + //---------------------------------------------------------------------- + + /** + * 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]; + } + + // + +}