Repository: camel Updated Branches: refs/heads/camel-2.13.x 2f7523140 -> fa6db18be refs/heads/camel-2.14.x 0a252ecd7 -> 479329bdb
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/479329bd Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/479329bd Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/479329bd Branch: refs/heads/camel-2.14.x Commit: 479329bdbf8168cddb994b38b912a41be48832a8 Parents: 0a252ec Author: Willem Jiang <[email protected]> Authored: Tue Dec 9 17:40:08 2014 +0800 Committer: Willem Jiang <[email protected]> Committed: Tue Dec 9 20:42:41 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/479329bd/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/479329bd/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); }
