svn commit: r563924 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/component/file/ main/java/org/apache/camel/converter/ test/java/org/apache/camel/converter/ test/resources/org/apache/camel/converter/

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r563924 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/component/file/ main/java/org/apache/camel/converter/ test/java/org/apache/camel/converter/ test/resources/org/apache/camel/converter/

jstrachan-2
Author: jstrachan
Date: Wed Aug  8 08:59:13 2007
New Revision: 563924

URL: http://svn.apache.org/viewvc?view=rev&rev=563924
Log:
file message exchanges now use File as the body, rather than FileInputStream so that they are repeatable (such as in deadlock scenarios). Fixes CAMEL-90

Added:
    activemq/camel/trunk/camel-core/src/test/resources/org/apache/camel/converter/
    activemq/camel/trunk/camel-core/src/test/resources/org/apache/camel/converter/dummy.txt   (with props)
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileMessage.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileMessage.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileMessage.java?view=diff&rev=563924&r1=563923&r2=563924
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileMessage.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileMessage.java Wed Aug  8 08:59:13 2007
@@ -16,13 +16,11 @@
  */
 package org.apache.camel.component.file;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-
-import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.Exchange;
 import org.apache.camel.impl.DefaultMessage;
 
+import java.io.File;
+
 /**
  * A {@link Exchange} for File
  *
@@ -64,14 +62,6 @@
 
     @Override
     protected Object createBody() {
-        if (file != null) {
-            // lets extract the body
-            try {
-                return new FileInputStream(file);
-            } catch (FileNotFoundException e) {
-                throw new RuntimeCamelException("File has been deleted: " + file + ". Reason: " + e, e);
-            }
-        }
-        return super.createBody();
+        return file;
     }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java?view=diff&rev=563924&r1=563923&r2=563924
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java Wed Aug  8 08:59:13 2007
@@ -16,30 +16,13 @@
  */
 package org.apache.camel.converter;
 
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
-import java.io.BufferedReader;
-import java.io.BufferedWriter;
-import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.FileReader;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.io.Reader;
-import java.io.StringReader;
-import java.io.Writer;
-
 import org.apache.camel.Converter;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+import java.io.*;
+import java.net.URL;
+
 /**
  * Some core java.io based <a
  * href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a>
@@ -57,6 +40,11 @@
     }
 
     @Converter
+    public static InputStream toInputStream(URL url) throws IOException {
+        return url.openStream();
+    }
+
+    @Converter
     public static InputStream toInputStream(File file) throws FileNotFoundException {
         return new BufferedInputStream(new FileInputStream(file));
     }
@@ -111,6 +99,16 @@
     @Converter
     public static String toString(byte[] data) {
         return new String(data);
+    }
+
+    @Converter
+    public static String toString(File file) throws IOException {
+        return toString(toReader(file));
+    }
+
+    @Converter
+    public static String toString(URL url) throws IOException {
+        return toString(toInputStream(url));
     }
 
     @Converter

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java?view=diff&rev=563924&r1=563923&r2=563924
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java Wed Aug  8 08:59:13 2007
@@ -24,6 +24,7 @@
 import java.util.Collection;
 import java.util.List;
 import java.util.Set;
+import java.net.URL;
 
 import junit.framework.TestCase;
 
@@ -138,6 +139,14 @@
         File file = converter.convertTo(File.class, "foo.txt");
         assertNotNull("Should have converted to a file!");
         assertEquals("file name", "foo.txt", file.getName());
+    }
+
+    public void testFileToString() throws Exception {
+        URL resource = getClass().getResource("dummy.txt");
+        assertNotNull("Cannot find resource!");
+        File file = new File(resource.getFile());
+        String text = converter.convertTo(String.class, file);
+        assertEquals("file content", "Hello World!", text);
     }
 
     public void testPrimitiveBooleanConversion() throws Exception {

Added: activemq/camel/trunk/camel-core/src/test/resources/org/apache/camel/converter/dummy.txt
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/resources/org/apache/camel/converter/dummy.txt?view=auto&rev=563924
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/resources/org/apache/camel/converter/dummy.txt (added)
+++ activemq/camel/trunk/camel-core/src/test/resources/org/apache/camel/converter/dummy.txt Wed Aug  8 08:59:13 2007
@@ -0,0 +1 @@
+Hello World!
\ No newline at end of file

Propchange: activemq/camel/trunk/camel-core/src/test/resources/org/apache/camel/converter/dummy.txt
------------------------------------------------------------------------------
    svn:eol-style = native