INT-1614 removed SftpSessionPool and the pool impl now implements SessionFactory

This commit is contained in:
Mark Fisher
2010-11-19 10:22:14 -05:00
parent 8076b68bf2
commit 66fea3257e
8 changed files with 48 additions and 89 deletions

View File

@@ -28,7 +28,7 @@ import org.springframework.integration.MessagingException;
import org.springframework.integration.file.synchronization.AbstractInboundRemoteFileSystemSychronizer;
import org.springframework.integration.file.synchronization.AbstractInboundRemoteFileSystemSynchronizingMessageSource;
import org.springframework.integration.sftp.session.SftpSession;
import org.springframework.integration.sftp.session.SftpSessionPool;
import org.springframework.integration.sftp.session.SftpSessionFactory;
import org.springframework.util.Assert;
import com.jcraft.jsch.ChannelSftp;
@@ -53,12 +53,12 @@ public class SftpInboundSynchronizer extends AbstractInboundRemoteFileSystemSych
/**
* the pool of {@link org.springframework.integration.sftp.session.SftpSessionPool} SFTP sessions
*/
private final SftpSessionPool sessionPool;
private final SftpSessionFactory sessionFactory;
public SftpInboundSynchronizer(SftpSessionPool sessionPool) {
Assert.notNull(sessionPool, "'sessionPool' must not be null");
this.sessionPool = sessionPool;
public SftpInboundSynchronizer(SftpSessionFactory sessionFactory) {
Assert.notNull(sessionFactory, "sessionFactory must not be null");
this.sessionFactory = sessionFactory;
}
@@ -94,7 +94,7 @@ public class SftpInboundSynchronizer extends AbstractInboundRemoteFileSystemSych
return true;
}
catch (Throwable th) {
if (this.autoCreateDirectories && (this.sessionPool != null) && (session != null)) {
if (this.autoCreateDirectories && (this.sessionFactory != null) && (session != null)) {
try {
if (channelSftp != null) {
channelSftp.mkdir(remotePath);
@@ -110,7 +110,6 @@ public class SftpInboundSynchronizer extends AbstractInboundRemoteFileSystemSych
catch (Exception e){
throw new MessagingException("Failed to auto-create remote directory", e);
}
}
}
return false;
@@ -121,8 +120,10 @@ public class SftpInboundSynchronizer extends AbstractInboundRemoteFileSystemSych
protected void syncRemoteToLocalFileSystem(Resource localDirectory) {
SftpSession session = null;
try {
session = sessionPool.getSession();
logger.trace("Pooled SftpSession " + this.sessionPool + " from the pool");
session = this.sessionFactory.getSession();
if (logger.isTraceEnabled()) {
logger.trace("Pooled SftpSession " + session + " from the pool");
}
session.connect();
this.checkThatRemotePathExists(remotePath, session);
ChannelSftp channelSftp = session.getChannel();
@@ -140,8 +141,10 @@ public class SftpInboundSynchronizer extends AbstractInboundRemoteFileSystemSych
throw new MessagingException("couldn't synchronize remote to local directory", e);
}
finally {
this.sessionPool.release(session);
logger.trace("Putting SftpSession " + this.sessionPool + " back into the pool");
session.disconnect();
if (logger.isTraceEnabled()) {
logger.trace("Putting SftpSession " + session + " back into the pool");
}
}
}

View File

@@ -38,7 +38,7 @@ import org.springframework.integration.file.FileNameGenerator;
import org.springframework.integration.handler.AbstractMessageHandler;
import org.springframework.integration.handler.ExpressionEvaluatingMessageProcessor;
import org.springframework.integration.sftp.session.SftpSession;
import org.springframework.integration.sftp.session.SftpSessionPool;
import org.springframework.integration.sftp.session.SftpSessionFactory;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
@@ -57,7 +57,7 @@ public class SftpSendingMessageHandler extends AbstractMessageHandler {
private static final String TEMPORARY_FILE_SUFFIX = ".writing";
private final SftpSessionPool sessionPool;
private final SftpSessionFactory sessionFactory;
private volatile ExpressionEvaluatingMessageProcessor<String> directoryExpressionProcesor;
@@ -72,9 +72,9 @@ public class SftpSendingMessageHandler extends AbstractMessageHandler {
private volatile String charset = Charset.defaultCharset().name();
public SftpSendingMessageHandler(SftpSessionPool sessionPool) {
Assert.notNull(sessionPool, "'sessionPool' must not be null");
this.sessionPool = sessionPool;
public SftpSendingMessageHandler(SftpSessionFactory sessionFactory) {
Assert.notNull(sessionFactory, "sessionFactory must not be null");
this.sessionFactory = sessionFactory;
}
@@ -165,7 +165,7 @@ public class SftpSendingMessageHandler extends AbstractMessageHandler {
}
private boolean sendFileToRemoteEndpoint(Message<?> message, File file) throws Exception {
SftpSession session = this.sessionPool.getSession();
SftpSession session = this.sessionFactory.getSession();
if (session == null) {
throw new MessagingException("The session returned from the pool is null, cannot proceed.");
}
@@ -189,7 +189,7 @@ public class SftpSendingMessageHandler extends AbstractMessageHandler {
}
finally {
IOUtils.closeQuietly(fileInputStream);
this.sessionPool.release(session);
session.disconnect();
}
}

View File

@@ -35,7 +35,7 @@ import com.jcraft.jsch.Channel;
* @author Oleg Zhurakousky
* @since 2.0
*/
public class QueuedSftpSessionPool implements SftpSessionPool, SmartLifecycle {
public class QueuedSftpSessionPool implements SftpSessionFactory, SmartLifecycle {
private static Logger logger = Logger.getLogger(QueuedSftpSessionPool.class.getName());

View File

@@ -1,46 +0,0 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.sftp.session;
import org.springframework.context.Lifecycle;
/**
* Holds instances of {@link SftpSession} since they are stateful
* and might be in use while another operation runs.
*
* @author Josh Long
* @author Oleg Zhurakousky
* @since 2.0
*/
public interface SftpSessionPool extends SftpSessionFactory, Lifecycle {
/**
* Returns a session that can be used to connect to an sftp instance and perform operations
*
* @return the session from the pool ready to be connected to.
* @throws Exception if any fault occurs when trying to connect to the remote server
*/
SftpSession getSession();
/**
* Releases the session.
*
* @param session the session to relinquish / renew
*/
void release(SftpSession session);
}

View File

@@ -56,8 +56,8 @@ public class OutboundChannelAdapaterParserTests {
assertEquals("UTF-8", TestUtils.getPropertyValue(handler, "charset"));
assertNotNull(TestUtils.getPropertyValue(handler, "temporaryBufferFolder"));
assertNotNull(TestUtils.getPropertyValue(handler, "temporaryBufferFolderFile"));
QueuedSftpSessionPool clientPoll = (QueuedSftpSessionPool) TestUtils.getPropertyValue(handler, "sessionPool");
SimpleSftpSessionFactory clientFactory = (SimpleSftpSessionFactory) TestUtils.getPropertyValue(clientPoll, "sftpSessionFactory");
QueuedSftpSessionPool sessionFactory = (QueuedSftpSessionPool) TestUtils.getPropertyValue(handler, "sessionFactory");
SimpleSftpSessionFactory clientFactory = (SimpleSftpSessionFactory) TestUtils.getPropertyValue(sessionFactory, "sftpSessionFactory");
assertEquals("localhost", TestUtils.getPropertyValue(clientFactory, "host"));
assertEquals(2222, TestUtils.getPropertyValue(clientFactory, "port"));
}

View File

@@ -10,6 +10,7 @@
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.integration.sftp.impl;
import static junit.framework.Assert.assertTrue;
@@ -23,12 +24,13 @@ import java.lang.reflect.Method;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.integration.file.synchronization.AbstractInboundRemoteFileSystemSychronizer.EntryAcknowledgmentStrategy;
import org.springframework.integration.sftp.inbound.SftpInboundSynchronizer;
import org.springframework.integration.sftp.session.SftpSession;
import org.springframework.integration.sftp.session.SftpSessionPool;
import org.springframework.integration.sftp.session.SftpSessionFactory;
import org.springframework.util.ReflectionUtils;
import com.jcraft.jsch.ChannelSftp;
@@ -36,7 +38,7 @@ import com.jcraft.jsch.ChannelSftp.LsEntry;
/**
* @author Oleg Zhurakousky
*
* @since 2.0
*/
public class SftpInboundRemoteFileSystemSynchronizerTests {
@Before
@@ -63,7 +65,7 @@ public class SftpInboundRemoteFileSystemSynchronizerTests {
*/
@Test
public void testCopyAndRenameWhenLocalFileExists() throws Exception {
SftpInboundSynchronizer synchronizer = new SftpInboundSynchronizer(mock(SftpSessionPool.class));
SftpInboundSynchronizer synchronizer = new SftpInboundSynchronizer(mock(SftpSessionFactory.class));
Method method =
ReflectionUtils.findMethod(synchronizer.getClass(), "copyFromRemoteToLocalDirectory", SftpSession.class, LsEntry.class, Resource.class);
method.setAccessible(true);
@@ -85,7 +87,7 @@ public class SftpInboundRemoteFileSystemSynchronizerTests {
@org.junit.Ignore
@Test
public void testCopyAndRenameWhenLocalFileDoesntExist() throws Exception {
SftpInboundSynchronizer synchronizer = new SftpInboundSynchronizer(mock(SftpSessionPool.class));
SftpInboundSynchronizer synchronizer = new SftpInboundSynchronizer(mock(SftpSessionFactory.class));
synchronizer.setEntryAcknowledgmentStrategy(mock(EntryAcknowledgmentStrategy.class));
Method method =
ReflectionUtils.findMethod(synchronizer.getClass(), "copyFromRemoteToLocalDirectory", SftpSession.class, LsEntry.class, Resource.class);

View File

@@ -32,7 +32,7 @@ import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.integration.file.filters.FileListFilter;
import org.springframework.integration.sftp.session.SftpSession;
import org.springframework.integration.sftp.session.SftpSessionPool;
import org.springframework.integration.sftp.session.SftpSessionFactory;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
@@ -50,8 +50,8 @@ public class SftpInboundRemoteFileSystemSynchronizerTests {
if (file.exists()){
file.delete();
}
SftpSessionPool sessionPool = mock(SftpSessionPool.class);
SftpInboundSynchronizer syncronizer = new SftpInboundSynchronizer(sessionPool);
SftpSessionFactory sessionFactory = mock(SftpSessionFactory.class);
SftpInboundSynchronizer syncronizer = new SftpInboundSynchronizer(sessionFactory);
syncronizer.setRemotePath("foo/bar");
FileListFilter filter = mock(FileListFilter.class);
@@ -61,7 +61,7 @@ public class SftpInboundRemoteFileSystemSynchronizerTests {
SftpSession sftpSession = mock(SftpSession.class);
when(sessionPool.getSession()).thenReturn(sftpSession);
when(sessionFactory.getSession()).thenReturn(sftpSession);
ChannelSftp channel = mock(ChannelSftp.class);
when(channel.get((String) Mockito.any())).thenReturn(new FileInputStream(new File("template.mf")));
when(sftpSession.getChannel()).thenReturn(channel);
@@ -82,7 +82,7 @@ public class SftpInboundRemoteFileSystemSynchronizerTests {
Resource localDirectory = new FileSystemResource(System.getProperty("java.io.tmpdir"));
syncronizer.syncRemoteToLocalFileSystem(localDirectory);
verify(sessionPool, times(1)).getSession();
verify(sessionFactory, times(1)).getSession();
verify(sftpSession, atLeast(1)).getChannel();
// will add more validation, but for now this test is mainly to get the test coverage up
}

View File

@@ -28,7 +28,7 @@ import org.junit.Test;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.sftp.session.SftpSession;
import org.springframework.integration.sftp.session.SftpSessionPool;
import org.springframework.integration.sftp.session.SftpSessionFactory;
import com.jcraft.jsch.ChannelSftp;
@@ -42,43 +42,43 @@ public class SftpSendingMessageHandlerTest {
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testHandleFileNameMessage() throws Exception {
SftpSessionPool sessionPoll = mock(SftpSessionPool.class);
SftpSessionFactory sessionFactory = mock(SftpSessionFactory.class);
SftpSession session = mock(SftpSession.class);
ChannelSftp channel = mock(ChannelSftp.class);
when(session.getChannel()).thenReturn(channel);
when(sessionPoll.getSession()).thenReturn(session);
SftpSendingMessageHandler handler = new SftpSendingMessageHandler(sessionPoll);
when(sessionFactory.getSession()).thenReturn(session);
SftpSendingMessageHandler handler = new SftpSendingMessageHandler(sessionFactory);
handler.setRemoteDirectoryExpression(new SpelExpressionParser().parseExpression("'foo.txt'"));
handler.handleMessage(new GenericMessage("hello"));
verify(session, atLeast(1)).getChannel();
verify(sessionPoll, times(1)).getSession();
verify(sessionFactory, times(1)).getSession();
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testHandleFileAsByte() throws Exception {
SftpSessionPool sessionPoll = mock(SftpSessionPool.class);
SftpSessionFactory sessionFactory = mock(SftpSessionFactory.class);
SftpSession session = mock(SftpSession.class);
ChannelSftp channel = mock(ChannelSftp.class);
when(session.getChannel()).thenReturn(channel);
when(sessionPoll.getSession()).thenReturn(session);
SftpSendingMessageHandler handler = new SftpSendingMessageHandler(sessionPoll);
when(sessionFactory.getSession()).thenReturn(session);
SftpSendingMessageHandler handler = new SftpSendingMessageHandler(sessionFactory);
handler.setRemoteDirectoryExpression(new SpelExpressionParser().parseExpression("'foo.txt'"));
handler.handleMessage(new GenericMessage("hello".getBytes()));
verify(session, atLeast(1)).getChannel();
verify(sessionPoll, times(1)).getSession();
verify(sessionFactory, times(1)).getSession();
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testHandleFileMessage() throws Exception {
SftpSessionPool sessionPoll = mock(SftpSessionPool.class);
SftpSessionFactory sessionFactory = mock(SftpSessionFactory.class);
SftpSession session = mock(SftpSession.class);
ChannelSftp channel = mock(ChannelSftp.class);
when(session.getChannel()).thenReturn(channel);
when(sessionPoll.getSession()).thenReturn(session);
SftpSendingMessageHandler handler = new SftpSendingMessageHandler(sessionPoll);
when(sessionFactory.getSession()).thenReturn(session);
SftpSendingMessageHandler handler = new SftpSendingMessageHandler(sessionFactory);
handler.setRemoteDirectoryExpression(new SpelExpressionParser().parseExpression("'foo.txt'"));
handler.handleMessage(new GenericMessage("hello".getBytes()));
@@ -87,6 +87,6 @@ public class SftpSendingMessageHandlerTest {
File file = File.createTempFile("foo", ".txt");
handler.handleMessage(new GenericMessage(file));
verify(session, atLeast(1)).getChannel();
verify(sessionPoll, atLeast(1)).getSession();
verify(sessionFactory, atLeast(1)).getSession();
}
}