From 398fdee5585ab3d23aeca5c2217b7584a22516ab Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Fri, 19 Nov 2010 18:19:07 -0500 Subject: [PATCH] INT-1614 removed connect() from Session, renamed disconnect() to close() --- .../remote/session/CachingSessionFactory.java | 14 +++----- .../file/remote/session/Session.java | 6 ++-- .../inbound/FtpInboundFileSynchronizer.java | 2 +- .../outbound/FtpSendingMessageHandler.java | 2 +- .../integration/ftp/session/FtpSession.java | 29 ++++++--------- .../inbound/SftpInboundFileSynchronizer.java | 3 +- .../outbound/SftpSendingMessageHandler.java | 3 +- .../sftp/session/DefaultSftpSession.java | 35 ++++--------------- .../session/SimpleSftpSessionFactory.java | 5 ++- 9 files changed, 33 insertions(+), 66 deletions(-) diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/session/CachingSessionFactory.java b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/session/CachingSessionFactory.java index 01efd3f221..0072f7730d 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/session/CachingSessionFactory.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/session/CachingSessionFactory.java @@ -82,15 +82,15 @@ public class CachingSessionFactory implements SessionFactory, DisposableBean { public void destroy() { if (this.queue != null) { for (Session session : this.queue) { - this.destroySession(session); + this.closeSession(session); } } } - private void destroySession(Session session) { + private void closeSession(Session session) { try { if (session != null) { - session.disconnect(); + session.close(); } } catch (Throwable e) { @@ -108,16 +108,12 @@ public class CachingSessionFactory implements SessionFactory, DisposableBean { this.targetSession = targetSession; } - public void connect() { - targetSession.connect(); - } - - public void disconnect() { + public void close() { if (queue.size() < maxPoolSize) { queue.add(targetSession); } else { - targetSession.disconnect(); + targetSession.close(); } } diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/session/Session.java b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/session/Session.java index 454de9f63b..744f9f6967 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/session/Session.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/session/Session.java @@ -29,10 +29,6 @@ import java.util.Collection; */ public interface Session { - void connect(); - - void disconnect(); - boolean rm(String path); Collection ls(String path); @@ -41,4 +37,6 @@ public interface Session { void put(InputStream inputStream, String destination); + void close(); + } diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/inbound/FtpInboundFileSynchronizer.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/inbound/FtpInboundFileSynchronizer.java index 954afb51a5..4fa24406b5 100644 --- a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/inbound/FtpInboundFileSynchronizer.java +++ b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/inbound/FtpInboundFileSynchronizer.java @@ -86,7 +86,7 @@ public class FtpInboundFileSynchronizer extends AbstractInboundFileSynchronizer< } } finally { - session.disconnect(); + session.close(); } } catch (IOException e) { diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/outbound/FtpSendingMessageHandler.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/outbound/FtpSendingMessageHandler.java index 6f8106a2d3..ebb8b611a3 100644 --- a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/outbound/FtpSendingMessageHandler.java +++ b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/outbound/FtpSendingMessageHandler.java @@ -186,7 +186,7 @@ public class FtpSendingMessageHandler extends AbstractMessageHandler{ } } if (session != null) { - session.disconnect(); + session.close(); } } if (!sentSuccesfully) { diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/session/FtpSession.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/session/FtpSession.java index e191845f2b..ffe209c8cd 100644 --- a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/session/FtpSession.java +++ b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/session/FtpSession.java @@ -49,24 +49,6 @@ public class FtpSession implements Session { } - public void connect() { - } - - public void disconnect() { - try { - this.client.disconnect(); - } - catch (IOException e) { - if (logger.isWarnEnabled()) { - logger.warn("failed to disconnect FTPClient", e); - } - } - } - - public boolean exists(String path) { - return false; - } - public boolean rm(String path) { try { this.client.deleteFile(path); @@ -137,4 +119,15 @@ public class FtpSession implements Session { } } + public void close() { + try { + this.client.disconnect(); + } + catch (IOException e) { + if (logger.isWarnEnabled()) { + logger.warn("failed to disconnect FTPClient", e); + } + } + } + } diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/inbound/SftpInboundFileSynchronizer.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/inbound/SftpInboundFileSynchronizer.java index 12ed43b6ad..20374cf3df 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/inbound/SftpInboundFileSynchronizer.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/inbound/SftpInboundFileSynchronizer.java @@ -77,7 +77,6 @@ public class SftpInboundFileSynchronizer extends AbstractInboundFileSynchronizer if (logger.isTraceEnabled()) { logger.trace("Pooled SftpSession " + session + " from the pool"); } - session.connect(); Collection beforeFilter = session.ls(remotePath); ChannelSftp.LsEntry[] entries = (beforeFilter == null) ? new ChannelSftp.LsEntry[0] : beforeFilter.toArray(new ChannelSftp.LsEntry[beforeFilter.size()]); @@ -92,7 +91,7 @@ public class SftpInboundFileSynchronizer extends AbstractInboundFileSynchronizer throw new MessagingException("couldn't synchronize remote to local directory", e); } finally { - session.disconnect(); + session.close(); if (logger.isTraceEnabled()) { logger.trace("Putting SftpSession " + session + " back into the pool"); } diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/outbound/SftpSendingMessageHandler.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/outbound/SftpSendingMessageHandler.java index 44d1f0cd7b..bb786900e2 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/outbound/SftpSendingMessageHandler.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/outbound/SftpSendingMessageHandler.java @@ -169,7 +169,6 @@ public class SftpSendingMessageHandler extends AbstractMessageHandler { } InputStream fileInputStream = null; try { - session.connect(); fileInputStream = new FileInputStream(file); String baseOfRemotePath = ""; if (this.directoryExpressionProcesor != null) { @@ -186,7 +185,7 @@ public class SftpSendingMessageHandler extends AbstractMessageHandler { } finally { IOUtils.closeQuietly(fileInputStream); - session.disconnect(); + session.close(); } } diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSession.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSession.java index 019ab9dec7..dda1a95e2e 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSession.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSession.java @@ -29,7 +29,6 @@ import org.springframework.integration.file.remote.session.Session; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; -import com.jcraft.jsch.SftpATTRS; import com.jcraft.jsch.SftpException; import com.jcraft.jsch.UserInfo; @@ -109,26 +108,19 @@ public class DefaultSftpSession implements Session { } this.userInfo = new OptimisticUserInfoImpl(userPassword); this.jschSession.setUserInfo(userInfo); - this.jschSession.connect(); this.channel = (ChannelSftp) this.jschSession.openChannel("sftp"); } - public ChannelSftp getChannel() { - return channel; - } - - public void connect() { - if (!channel.isConnected()) { - try { - channel.connect(); - } - catch (JSchException e) { - throw new IllegalStateException("failed to connect", e); - } + void connect() { + try { + this.jschSession.connect(); + } + catch (JSchException e) { + throw new IllegalStateException("failed to connect", e); } } - public void disconnect() { + public void close() { if (jschSession.isConnected()) { jschSession.disconnect(); if (channel.isConnected()) { @@ -137,19 +129,6 @@ public class DefaultSftpSession implements Session { } } - public boolean exists(String path) { - try { - SftpATTRS attrs = channel.stat(path); - return (attrs != null) && attrs.isDir(); - } - catch (SftpException e) { - if (logger.isWarnEnabled()) { - logger.warn("directoryExists failed", e); - } - return false; - } - } - public boolean mkdir(String path) { try { channel.mkdir(path); diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SimpleSftpSessionFactory.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SimpleSftpSessionFactory.java index 0afed290ec..a6a76823eb 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SimpleSftpSessionFactory.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SimpleSftpSessionFactory.java @@ -86,7 +86,10 @@ public class SimpleSftpSessionFactory implements SessionFactory { if (privateKey != null){ privateKeyToPass = privateKey.getFile().getAbsolutePath(); } - return new DefaultSftpSession(this.user, this.host, this.password, this.port, this.knownHosts, null, privateKeyToPass, this.privateKeyPassphrase); + DefaultSftpSession session = new DefaultSftpSession( + this.user, this.host, this.password, this.port, this.knownHosts, null, privateKeyToPass, this.privateKeyPassphrase); + session.connect(); + return session; } catch (Exception e) { throw new IllegalStateException("failed to create SFTP Session", e);