fix for https://issues.apache.org/activemq/browse/AMQ-2548 don't close the ftp connection until the stream is closed.

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@901911 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Timothy A. Bish 2010-01-21 23:02:28 +00:00
parent e166ae3937
commit a438496645
2 changed files with 114 additions and 98 deletions

View File

@ -18,6 +18,7 @@ package org.apache.activemq.blob;
import java.io.IOException;
import java.io.InputStream;
import java.io.FilterInputStream;
import java.net.ConnectException;
import java.net.URL;
@ -40,7 +41,7 @@ public class FTPBlobDownloadStrategy implements BlobDownloadStrategy {
String connectUrl = url.getHost();
int port = url.getPort() < 1 ? 21 : url.getPort();
FTPClient ftp = new FTPClient();
final FTPClient ftp = new FTPClient();
try {
ftp.connect(connectUrl, port);
} catch(ConnectException e) {
@ -58,9 +59,15 @@ public class FTPBlobDownloadStrategy implements BlobDownloadStrategy {
ftp.changeWorkingDirectory(workingDir);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
InputStream input = ftp.retrieveFileStream(file);
InputStream input = new FilterInputStream(ftp.retrieveFileStream(file)) {
public void close() throws IOException {
in.close();
ftp.quit();
ftp.disconnect();
}
};
return input;
}

View File

@ -27,7 +27,6 @@ import junit.framework.Assert;
import junit.framework.TestCase;
import org.apache.activemq.command.ActiveMQBlobMessage;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.UserManager;
@ -46,6 +45,8 @@ public class FTPBlobDownloadStrategyTest extends TestCase {
int ftpPort;
String ftpUrl;
final int FILE_SIZE = Short.MAX_VALUE * 10;
protected void setUp() throws Exception {
final File ftpHomeDirFile = new File("target/FTPBlobTest/ftptest");
ftpHomeDirFile.mkdirs();
@ -78,7 +79,13 @@ public class FTPBlobDownloadStrategyTest extends TestCase {
File uploadFile = new File(ftpHomeDirFile, "test.txt");
FileWriter wrt = new FileWriter(uploadFile);
wrt.write("hello world");
for(int ix = 0; ix < FILE_SIZE; ++ix ) {
wrt.write("a");
}
wrt.close();
}
@ -91,12 +98,14 @@ public class FTPBlobDownloadStrategyTest extends TestCase {
message.setURL(new URL(ftpUrl + "test.txt"));
stream = strategy.getInputStream(message);
int i = stream.read();
StringBuilder sb = new StringBuilder(10);
StringBuilder sb = new StringBuilder(2048);
while(i != -1) {
sb.append((char)i);
i = stream.read();
}
Assert.assertEquals("hello world", sb.toString());
Assert.assertEquals("hello world", sb.toString().substring(0, "hello world".length()));
Assert.assertEquals(FILE_SIZE, sb.toString().substring("hello world".length()).length());
} catch (Exception e) {
e.printStackTrace();
Assert.assertTrue(false);