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 2009/02/23 04:52:44 UTC

svn commit: r746872 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/converter/stream/ main/java/org/apache/camel/processor/interceptor/ test/java/org/apache/camel/converter/stream/ test/java/org/apache/camel/processor/interceptor/

Author: ningjiang
Date: Mon Feb 23 03:52:43 2009
New Revision: 746872

URL: http://svn.apache.org/viewvc?rev=746872&view=rev
Log:
CAMEL-1370 caching the StreamSource by caching the inputStream or reader

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCaching.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/StreamCachingInterceptorTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java?rev=746872&r1=746871&r2=746872&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java Mon Feb 23 03:52:43 2009
@@ -45,18 +45,18 @@
     private XmlConverter converter = new XmlConverter();
 
     @Converter
-    public StreamCache convertToStreamCache(StreamSource source) throws TransformerException {
-        return new SourceCache(converter.toString(source));
+    public StreamCache convertToStreamCache(StreamSource source) throws IOException {
+        return new StreamSourceCache(source);
     }
     
     @Converter
-    public StreamCache convertToStreamCache(StringSource source) throws TransformerException {
+    public StreamCache convertToStreamCache(StringSource source) {
         //no need to do stream caching for a StringSource
         return null;
     }
     
     @Converter
-    public StreamCache convertToStreamCache(BytesSource source) throws TransformerException {
+    public StreamCache convertToStreamCache(BytesSource source) {
         //no need to do stream caching for a BytesSource
         return null;
     }
@@ -95,6 +95,35 @@
         }
 
     }
+    
+    /*
+     * {@link StreamCache} implementation for Cache the StreamSource {@link StreamSource}s
+     */
+    private class StreamSourceCache extends StreamSource implements StreamCache {
+        InputStreamCache inputStreamCache;
+        ReaderCache readCache;
+        
+        public StreamSourceCache(StreamSource source) throws IOException {
+            if (source.getInputStream() != null) {
+                inputStreamCache = new InputStreamCache(IOConverter.toBytes(source.getInputStream()));
+                setInputStream(inputStreamCache);
+                setSystemId(source.getSystemId());
+            }
+            if (source.getReader() != null) {
+                readCache = new ReaderCache(IOConverter.toString(source.getReader()));
+                setReader(readCache);
+            }
+        }
+        public void reset() {
+            if (inputStreamCache != null) {
+                inputStreamCache.reset();
+            }
+            if (readCache != null) {
+                readCache.reset();
+            }            
+        }
+        
+    }
 
     private class InputStreamCache extends ByteArrayInputStream implements StreamCache {
 

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCaching.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCaching.java?rev=746872&r1=746871&r2=746872&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCaching.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCaching.java Mon Feb 23 03:52:43 2009
@@ -54,4 +54,18 @@
         }
         context.addInterceptStrategy(new StreamCaching());
     }
+    
+    /**
+     * Enable stream caching for a RouteContext
+     * 
+     * @param context the route context
+     */
+    public static void disable(RouteContext context) {
+        for (InterceptStrategy strategy : context.getInterceptStrategies()) {
+            if (strategy instanceof StreamCaching) {
+                context.getInterceptStrategies().remove(strategy);
+                return;
+            }
+        }        
+    }
 }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java?rev=746872&r1=746871&r2=746872&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java Mon Feb 23 03:52:43 2009
@@ -20,6 +20,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 
+import javax.xml.transform.Source;
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.stream.StreamSource;
 
@@ -42,13 +43,14 @@
         this.converter = new StreamCacheConverter();
     }
 
-    public void testConvertToStreamCacheStreamSource() throws TransformerException, FileNotFoundException {
+    public void testConvertToStreamCacheStreamSource() throws IOException, FileNotFoundException, TransformerException {
         StreamSource source = new StreamSource(getTestFileStream());
-        StreamSource cache = (StreamSource) converter.convertToStreamCache(source);
+        StreamCache cache = converter.convertToStreamCache(source);
         //assert re-readability of the cached StreamSource
         XmlConverter converter = new XmlConverter();
-        assertNotNull(converter.toString(cache));
-        assertNotNull(converter.toString(cache));
+        assertNotNull(converter.toString((Source)cache));
+        cache.reset();
+        assertNotNull(converter.toString((Source)cache));
     }
 
     public void testConvertToStreamCacheInputStream() throws IOException {

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/StreamCachingInterceptorTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/StreamCachingInterceptorTest.java?rev=746872&r1=746871&r2=746872&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/StreamCachingInterceptorTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/StreamCachingInterceptorTest.java Mon Feb 23 03:52:43 2009
@@ -81,7 +81,7 @@
         template.sendBody("direct:b", message);
 
         assertMockEndpointsSatisfied();
-        assertTrue(b.assertExchangeReceived(0).getIn().getBody() instanceof StreamCache);
+        assertTrue(b.assertExchangeReceived(0).getIn().getBody() instanceof StreamCache);        
         assertEquals(b.assertExchangeReceived(0).getIn().getBody(String.class), MESSAGE);
     }
 



Re: svn commit: r746872 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/converter/stream/ main/java/org/apache/camel/processor/interceptor/ test/java/org/apache/camel/converter/stream/ test/java/org/apache/camel/processor/interceptor/

Posted by Willem Jiang <wi...@gmail.com>.
Hi Claus

Good catcher , I will fix it in my next commit.

Thanks,

Willem

Claus Ibsen wrote:
> On Mon, Feb 23, 2009 at 4:52 AM,  <ni...@apache.org> wrote:
>> Author: ningjiang
>> Date: Mon Feb 23 03:52:43 2009
>> New Revision: 746872
>>
>> URL: http://svn.apache.org/viewvc?rev=746872&view=rev
>> Log:
>> CAMEL-1370 caching the StreamSource by caching the inputStream or reader
>>
>> Modified:
>>    camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java
>>    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCaching.java
>>    camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java
>>    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/StreamCachingInterceptorTest.java
>>
>> Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java
>> URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java?rev=746872&r1=746871&r2=746872&view=diff
>> ==============================================================================
>> --- camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java (original)
>> +++ camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java Mon Feb 23 03:52:43 2009
>> @@ -45,18 +45,18 @@
>>     private XmlConverter converter = new XmlConverter();
>>
>>     @Converter
>> -    public StreamCache convertToStreamCache(StreamSource source) throws TransformerException {
>> -        return new SourceCache(converter.toString(source));
>> +    public StreamCache convertToStreamCache(StreamSource source) throws IOException {
>> +        return new StreamSourceCache(source);
>>     }
>>
>>     @Converter
>> -    public StreamCache convertToStreamCache(StringSource source) throws TransformerException {
>> +    public StreamCache convertToStreamCache(StringSource source) {
>>         //no need to do stream caching for a StringSource
>>         return null;
>>     }
>>
>>     @Converter
>> -    public StreamCache convertToStreamCache(BytesSource source) throws TransformerException {
>> +    public StreamCache convertToStreamCache(BytesSource source) {
>>         //no need to do stream caching for a BytesSource
>>         return null;
>>     }
>> @@ -95,6 +95,35 @@
>>         }
>>
>>     }
>> +
>> +    /*
>> +     * {@link StreamCache} implementation for Cache the StreamSource {@link StreamSource}s
>> +     */
>> +    private class StreamSourceCache extends StreamSource implements StreamCache {
>> +        InputStreamCache inputStreamCache;
>> +        ReaderCache readCache;
>> +
>> +        public StreamSourceCache(StreamSource source) throws IOException {
>> +            if (source.getInputStream() != null) {
>> +                inputStreamCache = new InputStreamCache(IOConverter.toBytes(source.getInputStream()));
>> +                setInputStream(inputStreamCache);
>> +                setSystemId(source.getSystemId());
>> +            }
>> +            if (source.getReader() != null) {
>> +                readCache = new ReaderCache(IOConverter.toString(source.getReader()));
>> +                setReader(readCache);
>> +            }
>> +        }
>> +        public void reset() {
>> +            if (inputStreamCache != null) {
>> +                inputStreamCache.reset();
>> +            }
>> +            if (readCache != null) {
>> +                readCache.reset();
>> +            }
>> +        }
>> +
>> +    }
>>
>>     private class InputStreamCache extends ByteArrayInputStream implements StreamCache {
>>
>>
>> Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCaching.java
>> URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCaching.java?rev=746872&r1=746871&r2=746872&view=diff
>> ==============================================================================
>> --- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCaching.java (original)
>> +++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCaching.java Mon Feb 23 03:52:43 2009
>> @@ -54,4 +54,18 @@
>>         }
>>         context.addInterceptStrategy(new StreamCaching());
>>     }
>> +
>> +    /**
>> +     * Enable stream caching for a RouteContext
>> +     *
>> +     * @param context the route context
>> +     */
>> +    public static void disable(RouteContext context) {
>> +        for (InterceptStrategy strategy : context.getInterceptStrategies()) {
>> +            if (strategy instanceof StreamCaching) {
>> +                context.getInterceptStrategies().remove(strategy);
>> +                return;
>> +            }
>> +        }
>> +    }
>>  }
> Shouldnt the javadoc for this one be disable the stream cache?
> 
> 
>> Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java
>> URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java?rev=746872&r1=746871&r2=746872&view=diff
>> ==============================================================================
>> --- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java (original)
>> +++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java Mon Feb 23 03:52:43 2009
>> @@ -20,6 +20,7 @@
>>  import java.io.IOException;
>>  import java.io.InputStream;
>>
>> +import javax.xml.transform.Source;
>>  import javax.xml.transform.TransformerException;
>>  import javax.xml.transform.stream.StreamSource;
>>
>> @@ -42,13 +43,14 @@
>>         this.converter = new StreamCacheConverter();
>>     }
>>
>> -    public void testConvertToStreamCacheStreamSource() throws TransformerException, FileNotFoundException {
>> +    public void testConvertToStreamCacheStreamSource() throws IOException, FileNotFoundException, TransformerException {
>>         StreamSource source = new StreamSource(getTestFileStream());
>> -        StreamSource cache = (StreamSource) converter.convertToStreamCache(source);
>> +        StreamCache cache = converter.convertToStreamCache(source);
>>         //assert re-readability of the cached StreamSource
>>         XmlConverter converter = new XmlConverter();
>> -        assertNotNull(converter.toString(cache));
>> -        assertNotNull(converter.toString(cache));
>> +        assertNotNull(converter.toString((Source)cache));
>> +        cache.reset();
>> +        assertNotNull(converter.toString((Source)cache));
>>     }
>>
>>     public void testConvertToStreamCacheInputStream() throws IOException {
>>
>> Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/StreamCachingInterceptorTest.java
>> URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/StreamCachingInterceptorTest.java?rev=746872&r1=746871&r2=746872&view=diff
>> ==============================================================================
>> --- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/StreamCachingInterceptorTest.java (original)
>> +++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/StreamCachingInterceptorTest.java Mon Feb 23 03:52:43 2009
>> @@ -81,7 +81,7 @@
>>         template.sendBody("direct:b", message);
>>
>>         assertMockEndpointsSatisfied();
>> -        assertTrue(b.assertExchangeReceived(0).getIn().getBody() instanceof StreamCache);
>> +        assertTrue(b.assertExchangeReceived(0).getIn().getBody() instanceof StreamCache);
>>         assertEquals(b.assertExchangeReceived(0).getIn().getBody(String.class), MESSAGE);
>>     }
>>
>>
>>
>>
> 
> 
> 


Re: svn commit: r746872 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/converter/stream/ main/java/org/apache/camel/processor/interceptor/ test/java/org/apache/camel/converter/stream/ test/java/org/apache/camel/processor/interceptor/

Posted by Claus Ibsen <cl...@gmail.com>.
On Mon, Feb 23, 2009 at 4:52 AM,  <ni...@apache.org> wrote:
> Author: ningjiang
> Date: Mon Feb 23 03:52:43 2009
> New Revision: 746872
>
> URL: http://svn.apache.org/viewvc?rev=746872&view=rev
> Log:
> CAMEL-1370 caching the StreamSource by caching the inputStream or reader
>
> Modified:
>    camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java
>    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCaching.java
>    camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java
>    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/StreamCachingInterceptorTest.java
>
> Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java
> URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java?rev=746872&r1=746871&r2=746872&view=diff
> ==============================================================================
> --- camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java (original)
> +++ camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java Mon Feb 23 03:52:43 2009
> @@ -45,18 +45,18 @@
>     private XmlConverter converter = new XmlConverter();
>
>     @Converter
> -    public StreamCache convertToStreamCache(StreamSource source) throws TransformerException {
> -        return new SourceCache(converter.toString(source));
> +    public StreamCache convertToStreamCache(StreamSource source) throws IOException {
> +        return new StreamSourceCache(source);
>     }
>
>     @Converter
> -    public StreamCache convertToStreamCache(StringSource source) throws TransformerException {
> +    public StreamCache convertToStreamCache(StringSource source) {
>         //no need to do stream caching for a StringSource
>         return null;
>     }
>
>     @Converter
> -    public StreamCache convertToStreamCache(BytesSource source) throws TransformerException {
> +    public StreamCache convertToStreamCache(BytesSource source) {
>         //no need to do stream caching for a BytesSource
>         return null;
>     }
> @@ -95,6 +95,35 @@
>         }
>
>     }
> +
> +    /*
> +     * {@link StreamCache} implementation for Cache the StreamSource {@link StreamSource}s
> +     */
> +    private class StreamSourceCache extends StreamSource implements StreamCache {
> +        InputStreamCache inputStreamCache;
> +        ReaderCache readCache;
> +
> +        public StreamSourceCache(StreamSource source) throws IOException {
> +            if (source.getInputStream() != null) {
> +                inputStreamCache = new InputStreamCache(IOConverter.toBytes(source.getInputStream()));
> +                setInputStream(inputStreamCache);
> +                setSystemId(source.getSystemId());
> +            }
> +            if (source.getReader() != null) {
> +                readCache = new ReaderCache(IOConverter.toString(source.getReader()));
> +                setReader(readCache);
> +            }
> +        }
> +        public void reset() {
> +            if (inputStreamCache != null) {
> +                inputStreamCache.reset();
> +            }
> +            if (readCache != null) {
> +                readCache.reset();
> +            }
> +        }
> +
> +    }
>
>     private class InputStreamCache extends ByteArrayInputStream implements StreamCache {
>
>
> Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCaching.java
> URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCaching.java?rev=746872&r1=746871&r2=746872&view=diff
> ==============================================================================
> --- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCaching.java (original)
> +++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCaching.java Mon Feb 23 03:52:43 2009
> @@ -54,4 +54,18 @@
>         }
>         context.addInterceptStrategy(new StreamCaching());
>     }
> +
> +    /**
> +     * Enable stream caching for a RouteContext
> +     *
> +     * @param context the route context
> +     */
> +    public static void disable(RouteContext context) {
> +        for (InterceptStrategy strategy : context.getInterceptStrategies()) {
> +            if (strategy instanceof StreamCaching) {
> +                context.getInterceptStrategies().remove(strategy);
> +                return;
> +            }
> +        }
> +    }
>  }
Shouldnt the javadoc for this one be disable the stream cache?


>
> Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java
> URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java?rev=746872&r1=746871&r2=746872&view=diff
> ==============================================================================
> --- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java (original)
> +++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java Mon Feb 23 03:52:43 2009
> @@ -20,6 +20,7 @@
>  import java.io.IOException;
>  import java.io.InputStream;
>
> +import javax.xml.transform.Source;
>  import javax.xml.transform.TransformerException;
>  import javax.xml.transform.stream.StreamSource;
>
> @@ -42,13 +43,14 @@
>         this.converter = new StreamCacheConverter();
>     }
>
> -    public void testConvertToStreamCacheStreamSource() throws TransformerException, FileNotFoundException {
> +    public void testConvertToStreamCacheStreamSource() throws IOException, FileNotFoundException, TransformerException {
>         StreamSource source = new StreamSource(getTestFileStream());
> -        StreamSource cache = (StreamSource) converter.convertToStreamCache(source);
> +        StreamCache cache = converter.convertToStreamCache(source);
>         //assert re-readability of the cached StreamSource
>         XmlConverter converter = new XmlConverter();
> -        assertNotNull(converter.toString(cache));
> -        assertNotNull(converter.toString(cache));
> +        assertNotNull(converter.toString((Source)cache));
> +        cache.reset();
> +        assertNotNull(converter.toString((Source)cache));
>     }
>
>     public void testConvertToStreamCacheInputStream() throws IOException {
>
> Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/StreamCachingInterceptorTest.java
> URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/StreamCachingInterceptorTest.java?rev=746872&r1=746871&r2=746872&view=diff
> ==============================================================================
> --- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/StreamCachingInterceptorTest.java (original)
> +++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/StreamCachingInterceptorTest.java Mon Feb 23 03:52:43 2009
> @@ -81,7 +81,7 @@
>         template.sendBody("direct:b", message);
>
>         assertMockEndpointsSatisfied();
> -        assertTrue(b.assertExchangeReceived(0).getIn().getBody() instanceof StreamCache);
> +        assertTrue(b.assertExchangeReceived(0).getIn().getBody() instanceof StreamCache);
>         assertEquals(b.assertExchangeReceived(0).getIn().getBody(String.class), MESSAGE);
>     }
>
>
>
>



-- 
Claus Ibsen
Apache Camel Committer

Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/