Skip to content

Commit 8380346

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 5b84936 commit 8380346

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
@@ -40,7 +40,10 @@
4040
import org.springframework.integration.file.remote.ClientCallbackWithoutResult;
4141
import org.springframework.integration.file.remote.SessionCallbackWithoutResult;
4242
import org.springframework.integration.file.remote.session.SessionFactory;
43+
import org.springframework.integration.file.support.FileExistsMode;
4344
import org.springframework.integration.ftp.FtpTestSupport;
45+
import org.springframework.integration.test.util.TestUtils;
46+
import org.springframework.integration.util.SimplePool;
4447
import org.springframework.messaging.MessagingException;
4548
import org.springframework.messaging.support.GenericMessage;
4649
import org.springframework.test.context.ContextConfiguration;
@@ -61,7 +64,7 @@ public class FtpRemoteFileTemplateTests extends FtpTestSupport {
6164
private SessionFactory<FTPFile> sessionFactory;
6265

6366
@Test
64-
public void testINT3412AppendStatRmdir() throws IOException {
67+
public void testINT3412AppendStatRmdir() {
6568
FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(sessionFactory);
6669
DefaultFileNameGenerator fileNameGenerator = new DefaultFileNameGenerator();
6770
fileNameGenerator.setBeanFactory(mock(BeanFactory.class));
@@ -94,7 +97,7 @@ public void testINT3412AppendStatRmdir() throws IOException {
9497
assertThat(files.length).isEqualTo(0);
9598
assertThat(session.rmdir("foo/")).isTrue();
9699
});
97-
assertThat(template.getSession().exists("foo")).isFalse();
100+
assertThat(template.exists("foo")).isFalse();
98101
}
99102

100103
@Test
@@ -122,6 +125,27 @@ public void testFileCloseOnBadConnect() throws Exception {
122125
newFile.delete();
123126
}
124127

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

0 commit comments

Comments
 (0)