Skip to content

Commit afa98c1

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 407ad3a commit afa98c1

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

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

+2-2
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.
@@ -103,7 +103,7 @@ public Boolean doWithClient(FTPClient client) {
103103
return !ObjectUtils.isEmpty(names);
104104

105105
case NLST_AND_DIRS:
106-
return getSession().exists(path);
106+
return FtpRemoteFileTemplate.super.exists(path);
107107

108108
default:
109109
throw new IllegalStateException("Unsupported 'existsMode': " +

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

+27-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.junit.Test;
3434
import org.junit.runner.RunWith;
3535

36+
import org.springframework.beans.factory.BeanFactory;
3637
import org.springframework.beans.factory.annotation.Autowired;
3738
import org.springframework.context.annotation.Bean;
3839
import org.springframework.context.annotation.Configuration;
@@ -43,7 +44,10 @@
4344
import org.springframework.integration.file.remote.SessionCallbackWithoutResult;
4445
import org.springframework.integration.file.remote.session.Session;
4546
import org.springframework.integration.file.remote.session.SessionFactory;
47+
import org.springframework.integration.file.support.FileExistsMode;
4648
import org.springframework.integration.ftp.FtpTestSupport;
49+
import org.springframework.integration.test.util.TestUtils;
50+
import org.springframework.integration.util.SimplePool;
4751
import org.springframework.messaging.MessagingException;
4852
import org.springframework.messaging.support.GenericMessage;
4953
import org.springframework.test.context.ContextConfiguration;
@@ -62,7 +66,7 @@ public class FtpRemoteFileTemplateTests extends FtpTestSupport {
6266
private SessionFactory<FTPFile> sessionFactory;
6367

6468
@Test
65-
public void testINT3412AppendStatRmdir() throws IOException {
69+
public void testINT3412AppendStatRmdir() {
6670
FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(sessionFactory);
6771
DefaultFileNameGenerator fileNameGenerator = new DefaultFileNameGenerator();
6872
fileNameGenerator.setExpression("'foobar.txt'");
@@ -105,7 +109,7 @@ public void doInSessionWithoutResult(Session<FTPFile> session) throws IOExceptio
105109
assertTrue(session.rmdir("foo/"));
106110
}
107111
});
108-
assertFalse(template.getSession().exists("foo"));
112+
assertFalse(template.exists("foo"));
109113
}
110114

111115
@Test
@@ -133,6 +137,27 @@ public void testFileCloseOnBadConnect() throws Exception {
133137
newFile.delete();
134138
}
135139

140+
@Test
141+
public void testConnectionClosedAfterExists() throws Exception {
142+
FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(this.sessionFactory);
143+
template.setRemoteDirectoryExpression(new LiteralExpression("/"));
144+
template.setExistsMode(FtpRemoteFileTemplate.ExistsMode.NLST_AND_DIRS);
145+
template.setBeanFactory(mock(BeanFactory.class));
146+
template.afterPropertiesSet();
147+
File file = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
148+
FileOutputStream fileOutputStream = new FileOutputStream(file);
149+
fileOutputStream.write("foo".getBytes());
150+
fileOutputStream.close();
151+
template.send(new GenericMessage<>(file), FileExistsMode.IGNORE);
152+
File newFile = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
153+
assertTrue(file.renameTo(newFile));
154+
file.delete();
155+
newFile.delete();
156+
157+
SimplePool<?> pool = TestUtils.getPropertyValue(this.sessionFactory, "pool", SimplePool.class);
158+
assertEquals(0, pool.getActiveCount());
159+
}
160+
136161
@Configuration
137162
public static class Config {
138163

0 commit comments

Comments
 (0)