Skip to content

Commit 3491cba

Browse files
artembilangaryrussell
authored andcommitted
GH-2820: Close session in FtpRemFileTempl.exists
Fixes: #2820 The current `FtpRemoteFileTemplate.exists()` implementation obtains a `Session` for the `NLST_AND_DIRS` mode, but doesn't close it. * Fix the `FtpRemoteFileTemplate.exists()` method to delegate to `super` for the proper interaction with a `Session` **Cherry-pick to 5.1.x, 5.0.x & 4.3.x**
1 parent 1cc9ff2 commit 3491cba

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

spring-integration-ftp/src/main/java/org/springframework/integration/ftp/session/FtpRemoteFileTemplate.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -92,7 +92,7 @@ public boolean exists(final String path) {
9292
return !ObjectUtils.isEmpty(names);
9393

9494
case NLST_AND_DIRS:
95-
return getSession().exists(path);
95+
return FtpRemoteFileTemplate.super.exists(path);
9696

9797
default:
9898
throw new IllegalStateException("Unsupported 'existsMode': " +

spring-integration-ftp/src/test/java/org/springframework/integration/ftp/session/FtpRemoteFileTemplateTests.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@
4242
import org.springframework.integration.file.remote.ClientCallbackWithoutResult;
4343
import org.springframework.integration.file.remote.SessionCallbackWithoutResult;
4444
import org.springframework.integration.file.remote.session.SessionFactory;
45+
import org.springframework.integration.file.support.FileExistsMode;
4546
import org.springframework.integration.ftp.FtpTestSupport;
47+
import org.springframework.integration.test.util.TestUtils;
48+
import org.springframework.integration.util.SimplePool;
4649
import org.springframework.messaging.MessagingException;
4750
import org.springframework.messaging.support.GenericMessage;
4851
import org.springframework.test.context.ContextConfiguration;
@@ -63,7 +66,7 @@ public class FtpRemoteFileTemplateTests extends FtpTestSupport {
6366
private SessionFactory<FTPFile> sessionFactory;
6467

6568
@Test
66-
public void testINT3412AppendStatRmdir() throws IOException {
69+
public void testINT3412AppendStatRmdir() {
6770
FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(sessionFactory);
6871
DefaultFileNameGenerator fileNameGenerator = new DefaultFileNameGenerator();
6972
fileNameGenerator.setBeanFactory(mock(BeanFactory.class));
@@ -96,7 +99,7 @@ public void testINT3412AppendStatRmdir() throws IOException {
9699
assertEquals(0, files.length);
97100
assertTrue(session.rmdir("foo/"));
98101
});
99-
assertFalse(template.getSession().exists("foo"));
102+
assertFalse(template.exists("foo"));
100103
}
101104

102105
@Test
@@ -124,6 +127,27 @@ public void testFileCloseOnBadConnect() throws Exception {
124127
newFile.delete();
125128
}
126129

130+
@Test
131+
public void testConnectionClosedAfterExists() throws Exception {
132+
FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(this.sessionFactory);
133+
template.setRemoteDirectoryExpression(new LiteralExpression("/"));
134+
template.setExistsMode(FtpRemoteFileTemplate.ExistsMode.NLST_AND_DIRS);
135+
template.setBeanFactory(mock(BeanFactory.class));
136+
template.afterPropertiesSet();
137+
File file = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
138+
FileOutputStream fileOutputStream = new FileOutputStream(file);
139+
fileOutputStream.write("foo".getBytes());
140+
fileOutputStream.close();
141+
template.send(new GenericMessage<>(file), FileExistsMode.IGNORE);
142+
File newFile = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
143+
assertTrue(file.renameTo(newFile));
144+
file.delete();
145+
newFile.delete();
146+
147+
SimplePool<?> pool = TestUtils.getPropertyValue(this.sessionFactory, "pool", SimplePool.class);
148+
assertEquals(0, pool.getActiveCount());
149+
}
150+
127151
@Configuration
128152
public static class Config {
129153

0 commit comments

Comments
 (0)