From 61b5360d324e3624ca453c037f2e24a398094ab7 Mon Sep 17 00:00:00 2001 From: Martin Oberhuber < martin.oberhuber@windriver.com> Date: Fri, 10 Aug 2007 13:17:27 +0000 Subject: [PATCH] Fix exception handling for cloneFTPClient() --- .../services/files/ftp/FTPService.java | 57 +++++++++++-------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/internal/services/files/ftp/FTPService.java b/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/internal/services/files/ftp/FTPService.java index 0a7d2304d44..6b029443d1a 100644 --- a/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/internal/services/files/ftp/FTPService.java +++ b/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/internal/services/files/ftp/FTPService.java @@ -424,31 +424,38 @@ public class FTPService extends AbstractFileService implements IFileService, IFT */ private FTPClient cloneFTPClient(boolean isBinary) throws IOException { - FTPClient ftpClient = new FTPClient(); - - ftpClient.connect(_ftpClient.getRemoteAddress()); - ftpClient.login(_userId,_password); - - if (_clientConfigProxy != null) { - ftpClient.configure(_clientConfigProxy.getFTPClientConfig()); - } else { - // UNIX parsing by default if no suitable parser found - ftpClient.configure(new FTPClientConfig(FTPClientConfig.SYST_UNIX)); - } + boolean ok=false; + try { + ftpClient.connect(_ftpClient.getRemoteAddress()); + ftpClient.login(_userId,_password); + + if (_clientConfigProxy != null) { + ftpClient.configure(_clientConfigProxy.getFTPClientConfig()); + } else { + // UNIX parsing by default if no suitable parser found + ftpClient.configure(new FTPClientConfig(FTPClientConfig.SYST_UNIX)); + } - if (_isPassiveDataConnectionMode) { - ftpClient.enterLocalPassiveMode(); - } + if (_isPassiveDataConnectionMode) { + ftpClient.enterLocalPassiveMode(); + } - if (isBinary) { - ftpClient.setFileType(FTP.BINARY_FILE_TYPE); - } else { - ftpClient.setFileType(FTP.ASCII_FILE_TYPE); + if (isBinary) { + ftpClient.setFileType(FTP.BINARY_FILE_TYPE); + } else { + ftpClient.setFileType(FTP.ASCII_FILE_TYPE); + } + ftpClient.registerSpyStream(_ftpLoggingOutputStream); + ok=true; + } finally { + //disconnect the erroneous ftpClient, but forward the exception + if (!ok) { + try { + ftpClient.disconnect(); + } catch(Throwable t) { /*ignore*/ } + } } - - ftpClient.registerSpyStream(_ftpLoggingOutputStream); - return ftpClient; } @@ -1350,10 +1357,10 @@ public class FTPService extends AbstractFileService implements IFileService, IFT FTPClient ftpClient = cloneFTPClient(isBinary); ftpClient.changeWorkingDirectory(remoteParent); stream = new FTPBufferedInputStream(ftpClient.retrieveFileStream(remoteFile), ftpClient); - } - catch (Exception e) { - throw new RemoteFileIOException(e); - } + } + catch (Exception e) { + throw new RemoteFileIOException(e); + } return stream; }