You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2014/12/09 10:40:40 UTC
camel git commit: CAMEL-8134 should not add synchronisation if the
CachedOutputStream closedOnCompletion option is false
Repository: camel
Updated Branches:
refs/heads/master 43d026282 -> 11423ecbe
CAMEL-8134 should not add synchronisation if the CachedOutputStream closedOnCompletion option is false
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/11423ecb
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/11423ecb
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/11423ecb
Branch: refs/heads/master
Commit: 11423ecbe6da802a5a38b6e8da8bde34ed4f68a2
Parents: 43d0262
Author: Willem Jiang <wi...@gmail.com>
Authored: Tue Dec 9 17:40:08 2014 +0800
Committer: Willem Jiang <wi...@gmail.com>
Committed: Tue Dec 9 17:40:08 2014 +0800
----------------------------------------------------------------------
.../converter/stream/CachedOutputStream.java | 40 +++++++-------
.../stream/CachedOutputStreamTest.java | 57 ++++++++++++++------
2 files changed, 62 insertions(+), 35 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/11423ecb/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java b/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java
index f08d52d..63cedc3 100644
--- a/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java
+++ b/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java
@@ -76,33 +76,32 @@ public class CachedOutputStream extends OutputStream {
this.closedOnCompletion = closedOnCompletion;
this.strategy = exchange.getContext().getStreamCachingStrategy();
currentStream = new CachedByteArrayOutputStream(strategy.getBufferSize());
-
- // add on completion so we can cleanup after the exchange is done such as deleting temporary files
- exchange.addOnCompletion(new SynchronizationAdapter() {
- @Override
- public void onDone(Exchange exchange) {
- try {
- if (fileInputStreamCache != null) {
- fileInputStreamCache.close();
- }
- if (closedOnCompletion) {
+ if (closedOnCompletion) {
+ // add on completion so we can cleanup after the exchange is done such as deleting temporary files
+ exchange.addOnCompletion(new SynchronizationAdapter() {
+ @Override
+ public void onDone(Exchange exchange) {
+ try {
+ if (fileInputStreamCache != null) {
+ fileInputStreamCache.close();
+ }
close();
try {
cleanUpTempFile();
} catch (Exception e) {
LOG.warn("Error deleting temporary cache file: " + tempFile + ". This exception will be ignored.", e);
}
+ } catch (Exception e) {
+ LOG.warn("Error closing streams. This exception will be ignored.", e);
}
- } catch (Exception e) {
- LOG.warn("Error closing streams. This exception will be ignored.", e);
}
- }
-
- @Override
- public String toString() {
- return "OnCompletion[CachedOutputStream]";
- }
- });
+
+ @Override
+ public String toString() {
+ return "OnCompletion[CachedOutputStream]";
+ }
+ });
+ }
}
public void flush() throws IOException {
@@ -113,6 +112,9 @@ public class CachedOutputStream extends OutputStream {
currentStream.close();
// need to clean up the temp file this time
if (!closedOnCompletion) {
+ if (fileInputStreamCache != null) {
+ fileInputStreamCache.close();
+ }
try {
cleanUpTempFile();
} catch (Exception e) {
http://git-wip-us.apache.org/repos/asf/camel/blob/11423ecb/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java b/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java
index 39fac58..77f9dc9 100644
--- a/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java
+++ b/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java
@@ -75,6 +75,27 @@ public class CachedOutputStreamTest extends ContextTestSupport {
builder.append(line);
}
}
+
+ public void testCachedStreamAccessStreamWhenExchangeOnCompletion() throws Exception {
+ context.start();
+ CachedOutputStream cos = new CachedOutputStream(exchange, false);
+ cos.write(TEST_STRING.getBytes("UTF-8"));
+
+ File file = new File("target/cachedir");
+ String[] files = file.list();
+ assertEquals("we should have a temp file", 1, files.length);
+ assertTrue("The file name should start with cos" , files[0].startsWith("cos"));
+
+ InputStream is = cos.getWrappedInputStream();
+ exchange.getUnitOfWork().done(exchange);
+ String temp = toString(is);
+ assertEquals("Get a wrong stream content", temp, TEST_STRING);
+ IOHelper.close(is);
+
+ files = file.list();
+ assertEquals("we should have a temp file", 0, files.length);
+ IOHelper.close(cos);
+ }
public void testCacheStreamToFileAndCloseStream() throws Exception {
context.start();
@@ -84,7 +105,7 @@ public class CachedOutputStreamTest extends ContextTestSupport {
File file = new File("target/cachedir");
String[] files = file.list();
- assertEquals("we should have a temp file", files.length, 1);
+ assertEquals("we should have a temp file", 1, files.length);
assertTrue("The file name should start with cos" , files[0].startsWith("cos"));
StreamCache cache = cos.newStreamCache();
@@ -92,7 +113,8 @@ public class CachedOutputStreamTest extends ContextTestSupport {
String temp = toString((InputStream)cache);
((InputStream)cache).close();
- assertEquals("we should have a temp file", files.length, 1);
+ files = file.list();
+ assertEquals("we should have a temp file", 1, files.length);
assertEquals("Cached a wrong file", temp, TEST_STRING);
exchange.getUnitOfWork().done(exchange);
@@ -106,7 +128,7 @@ public class CachedOutputStreamTest extends ContextTestSupport {
files = file.list();
- assertEquals("we should have no temp file", files.length, 0);
+ assertEquals("we should have no temp file", 0, files.length);
IOHelper.close(cos);
}
@@ -123,7 +145,7 @@ public class CachedOutputStreamTest extends ContextTestSupport {
File file = new File("target/cachedir");
String[] files = file.list();
- assertEquals("we should have a temp file", files.length, 1);
+ assertEquals("we should have a temp file", 1, files.length);
assertTrue("The content is written" , new File(file, files[0]).length() > 10);
java.io.FileInputStream tmpin = new java.io.FileInputStream(new File(file, files[0]));
@@ -136,7 +158,7 @@ public class CachedOutputStreamTest extends ContextTestSupport {
temp = toString((InputStream)cache);
((InputStream)cache).close();
- assertEquals("we should have a temp file", files.length, 1);
+ assertEquals("we should have a temp file", 1, files.length);
assertEquals("Cached a wrong file", temp, TEST_STRING);
exchange.getUnitOfWork().done(exchange);
@@ -150,7 +172,7 @@ public class CachedOutputStreamTest extends ContextTestSupport {
files = file.list();
- assertEquals("we should have no temp file", files.length, 0);
+ assertEquals("we should have no temp file", 0, files.length);
IOHelper.close(cos);
}
@@ -163,7 +185,7 @@ public class CachedOutputStreamTest extends ContextTestSupport {
File file = new File("target/cachedir");
String[] files = file.list();
- assertEquals("we should have a temp file", files.length, 1);
+ assertEquals("we should have a temp file", 1, files.length);
assertTrue("The file name should start with cos" , files[0].startsWith("cos"));
StreamCache cache = cos.newStreamCache();
@@ -173,12 +195,13 @@ public class CachedOutputStreamTest extends ContextTestSupport {
cache.reset();
temp = toString((InputStream)cache);
assertEquals("Cached a wrong file", temp, TEST_STRING);
- exchange.getUnitOfWork().done(exchange);
- assertEquals("we should have a temp file", files.length, 1);
((InputStream)cache).close();
+ files = file.list();
+ assertEquals("we should have a temp file", 1, files.length);
+ exchange.getUnitOfWork().done(exchange);
files = file.list();
- assertEquals("we should have no temp file", files.length, 0);
+ assertEquals("we should have no temp file", 0, files.length);
IOHelper.close(cos);
}
@@ -194,7 +217,7 @@ public class CachedOutputStreamTest extends ContextTestSupport {
File file = new File("target/cachedir");
String[] files = file.list();
- assertEquals("we should have no temp file", files.length, 0);
+ assertEquals("we should have no temp file", 0, files.length);
StreamCache cache = cos.newStreamCache();
assertTrue("Should get the InputStreamCache", cache instanceof InputStreamCache);
String temp = IOConverter.toString((InputStream)cache, null);
@@ -215,7 +238,7 @@ public class CachedOutputStreamTest extends ContextTestSupport {
File file = new File("target/cachedir");
String[] files = file.list();
- assertEquals("we should have no temp file", files.length, 0);
+ assertEquals("we should have no temp file", 0, files.length);
StreamCache cache = cos.newStreamCache();
assertTrue("Should get the InputStreamCache", cache instanceof InputStreamCache);
String temp = IOConverter.toString((InputStream)cache, null);
@@ -240,7 +263,7 @@ public class CachedOutputStreamTest extends ContextTestSupport {
// make sure things still work after custom buffer size set
File file = new File("target/cachedir");
String[] files = file.list();
- assertEquals("we should have a temp file", files.length, 1);
+ assertEquals("we should have a temp file", 1, files.length);
assertTrue("The file name should start with cos" , files[0].startsWith("cos"));
StreamCache cache = cos.newStreamCache();
@@ -250,12 +273,14 @@ public class CachedOutputStreamTest extends ContextTestSupport {
cache.reset();
temp = toString((InputStream)cache);
assertEquals("Cached a wrong file", temp, TEST_STRING);
- exchange.getUnitOfWork().done(exchange);
- assertEquals("we should have a temp file", files.length, 1);
+
((InputStream)cache).close();
+ files = file.list();
+ assertEquals("we should have a temp file", 1, files.length);
+ exchange.getUnitOfWork().done(exchange);
files = file.list();
- assertEquals("we should have no temp file", files.length, 0);
+ assertEquals("we should have no temp file", 0, files.length);
IOHelper.close(cos);
}