Author: davsclaus
Date: Fri Nov 6 05:49:41 2009
New Revision: 833304
URL: http://svn.apache.org/viewvc?rev=833304&view=rev
Log:
CAMEL-2139: Tighted up closing streams after use. Thanks to Chris Gummer for
spotting this.
Modified:
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
Modified:
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java?rev=833304&r1=833303&r2=833304&view=diff
==============================================================================
---
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
(original)
+++
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
Fri Nov 6 05:49:41 2009
@@ -27,6 +27,7 @@
import java.util.List;
import org.apache.camel.Exchange;
+import org.apache.camel.InvalidPayloadException;
import org.apache.camel.component.file.FileComponent;
import org.apache.camel.component.file.GenericFile;
import org.apache.camel.component.file.GenericFileEndpoint;
@@ -108,7 +109,7 @@
}
if (LOG.isTraceEnabled()) {
- LOG.trace("Could not connect due: " + failed.getMessage());
+ LOG.trace("Cannot connect due: " + failed.getMessage());
}
attempt++;
if (attempt > endpoint.getMaximumReconnectAttempts()) {
@@ -176,7 +177,7 @@
public boolean deleteFile(String name) throws
GenericFileOperationFailedException {
if (LOG.isDebugEnabled()) {
- LOG.debug("Deleteing file: " + name);
+ LOG.debug("Deleting file: " + name);
}
try {
return this.client.deleteFile(name);
@@ -345,8 +346,9 @@
}
}
- InputStream is = exchange.getIn().getBody(InputStream.class);
+ InputStream is = null;
try {
+ is = exchange.getIn().getMandatoryBody(InputStream.class);
if (endpoint.getFileExist() == GenericFileExist.Append) {
return client.appendFile(name, is);
} else {
@@ -354,6 +356,8 @@
}
} catch (IOException e) {
throw new
GenericFileOperationFailedException(client.getReplyCode(),
client.getReplyString(), e.getMessage(), e);
+ } catch (InvalidPayloadException e) {
+ throw new GenericFileOperationFailedException("Cannot store file:
" + name, e);
} finally {
ObjectHelper.close(is, "store: " + name, LOG);
}
Modified:
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java?rev=833304&r1=833303&r2=833304&view=diff
==============================================================================
---
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
(original)
+++
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
Fri Nov 6 05:49:41 2009
@@ -47,6 +47,7 @@
import org.apache.commons.logging.LogFactory;
import static org.apache.camel.util.ObjectHelper.isNotEmpty;
+
/**
* SFTP remote file operations
*/
@@ -92,7 +93,7 @@
} catch (Exception e) {
GenericFileOperationFailedException failed = new
GenericFileOperationFailedException("Cannot connect to " +
configuration.remoteServerInformation(), e);
if (LOG.isTraceEnabled()) {
- LOG.trace("Could not connect due: " + failed.getMessage());
+ LOG.trace("Cannot connect due: " + failed.getMessage());
}
attempt++;
if (attempt > endpoint.getMaximumReconnectAttempts()) {
@@ -175,7 +176,7 @@
public boolean deleteFile(String name) throws
GenericFileOperationFailedException {
if (LOG.isDebugEnabled()) {
- LOG.debug("Deleteing file: " + name);
+ LOG.debug("Deleting file: " + name);
}
try {
channel.rm(name);
@@ -203,7 +204,7 @@
String originalDirectory = getCurrentDirectory();
try {
- // maybe the full directory already exsits
+ // maybe the full directory already exists
try {
channel.cd(directory);
success = true;
@@ -287,7 +288,7 @@
public List<ChannelSftp.LsEntry> listFiles(String path) throws
GenericFileOperationFailedException {
if (ObjectHelper.isEmpty(path)) {
- // list current dirctory if file path is not given
+ // list current directory if file path is not given
path = ".";
}
@@ -314,16 +315,19 @@
}
private boolean retrieveFileToStreamInBody(String name, Exchange exchange)
throws GenericFileOperationFailedException {
+ OutputStream os = null;
try {
- GenericFile<ChannelSftp.LsEntry> target =
+ os = new ByteArrayOutputStream();
+ GenericFile<ChannelSftp.LsEntry> target =
(GenericFile<ChannelSftp.LsEntry>)
exchange.getProperty(FileComponent.FILE_EXCHANGE_FILE);
ObjectHelper.notNull(target, "Exchange should have the " +
FileComponent.FILE_EXCHANGE_FILE + " set");
- OutputStream os = new ByteArrayOutputStream();
target.setBody(os);
channel.get(name, os);
return true;
} catch (SftpException e) {
throw new GenericFileOperationFailedException("Cannot retrieve
file: " + name, e);
+ } finally {
+ ObjectHelper.close(os, "retrieve: " + name, LOG);
}
}
@@ -411,19 +415,22 @@
}
}
+ InputStream is = null;
try {
- InputStream in = ExchangeHelper.getMandatoryInBody(exchange,
InputStream.class);
+ is = ExchangeHelper.getMandatoryInBody(exchange,
InputStream.class);
if (endpoint.getFileExist() == GenericFileExist.Append) {
- channel.put(in, name, ChannelSftp.APPEND);
+ channel.put(is, name, ChannelSftp.APPEND);
} else {
// override is default
- channel.put(in, name);
+ channel.put(is, name);
}
return true;
} catch (SftpException e) {
throw new GenericFileOperationFailedException("Cannot store file:
" + name, e);
} catch (InvalidPayloadException e) {
throw new GenericFileOperationFailedException("Cannot store file:
" + name, e);
+ } finally {
+ ObjectHelper.close(is, "store: " + name, LOG);
}
}