You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2010/04/19 17:21:39 UTC

svn commit: r935619 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/component/file/ test/java/org/apache/camel/component/file/

Author: davsclaus
Date: Mon Apr 19 15:21:38 2010
New Revision: 935619

URL: http://svn.apache.org/viewvc?rev=935619&view=rev
Log:
CAMEL-2658: GenericFile is not longer serializable. File body is prefered when coverting to Serializable.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/GenericFileConverterTest.java   (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileBinding.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileBinding.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConverter.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileDefaultBinding.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileBinding.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileBinding.java?rev=935619&r1=935618&r2=935619&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileBinding.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileBinding.java Mon Apr 19 15:21:38 2010
@@ -27,8 +27,6 @@ import org.apache.camel.util.IOHelper;
  * File binding with the {@link java.io.File} type.
  */
 public class FileBinding implements GenericFileBinding<File> {
-    private static final long serialVersionUID = 1488800911529475617L;
-
     private File body;
     private byte[] content;
 
@@ -54,11 +52,13 @@ public class FileBinding implements Gene
         // noop
     }
 
-    public void loadContent(Exchange exchange, GenericFile<File> file) throws IOException {
-        try {
-            content = exchange.getContext().getTypeConverter().mandatoryConvertTo(byte[].class, file.getFile());
-        } catch (NoTypeConversionAvailableException e) {
-            throw IOHelper.createIOException("Cannot load file content: " + file.getAbsoluteFilePath(), e);
+    public void loadContent(Exchange exchange, GenericFile<?> file) throws IOException {
+        if (content == null) {
+            try {
+                content = exchange.getContext().getTypeConverter().mandatoryConvertTo(byte[].class, file.getFile());
+            } catch (NoTypeConversionAvailableException e) {
+                throw IOHelper.createIOException("Cannot load file content: " + file.getAbsoluteFilePath(), e);
+            }
         }
     }
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java?rev=935619&r1=935618&r2=935619&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java Mon Apr 19 15:21:38 2010
@@ -17,7 +17,6 @@
 package org.apache.camel.component.file;
 
 import java.io.File;
-import java.io.Serializable;
 import java.util.Date;
 
 import org.apache.camel.Exchange;
@@ -30,9 +29,8 @@ import org.apache.commons.logging.LogFac
  * Generic File. Specific implementations of a file based endpoint need to
  * provide a File for transfer.
  */
-public class GenericFile<T> implements Serializable {
-    private static final long serialVersionUID = 565270785443491000L;
-    private static final Log LOG = LogFactory.getLog(GenericFile.class);
+public class GenericFile<T>  {
+    private static final transient Log LOG = LogFactory.getLog(GenericFile.class);
 
     private String endpointPath;
     private String fileName;

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileBinding.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileBinding.java?rev=935619&r1=935618&r2=935619&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileBinding.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileBinding.java Mon Apr 19 15:21:38 2010
@@ -17,14 +17,13 @@
 package org.apache.camel.component.file;
 
 import java.io.IOException;
-import java.io.Serializable;
 
 import org.apache.camel.Exchange;
 
 /**
  * Binding between the generic file and the body content.
  */
-public interface GenericFileBinding<T> extends Serializable {
+public interface GenericFileBinding<T> {
 
     /**
      * Gets the body of the file
@@ -43,11 +42,11 @@ public interface GenericFileBinding<T> e
     void setBody(GenericFile<T> file, Object body);
 
     /**
-     * Loads the content from the file into memory
+     * Ensures the content is loaded from the file into memory
      *
      * @param exchange the current exchange
      * @param file the file
      * @throws java.io.IOException is thrown if the content could not be loaded
      */
-    void loadContent(Exchange exchange, GenericFile<T> file) throws IOException;
+    void loadContent(Exchange exchange, GenericFile<?> file) throws IOException;
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConverter.java?rev=935619&r1=935618&r2=935619&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConverter.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConverter.java Mon Apr 19 15:21:38 2010
@@ -16,6 +16,9 @@
  */
 package org.apache.camel.component.file;
 
+import java.io.IOException;
+import java.io.Serializable;
+
 import org.apache.camel.Converter;
 import org.apache.camel.Exchange;
 import org.apache.camel.FallbackConverter;
@@ -57,12 +60,26 @@ public final class GenericFileConverter 
     }
 
     @Converter
-    public static String convertToString(GenericFile<?> file, Exchange exchange) {
+    public static String convertToString(GenericFile<?> file, Exchange exchange) throws IOException {
         if (exchange != null) {
+            // ensure the body is loaded as we do not want a toString of java.io.File handle returned, but the file content
+            file.getBinding().loadContent(exchange, file);
             return exchange.getContext().getTypeConverter().convertTo(String.class, exchange, file.getBody());
         } else {
             // should revert to fallback converter if we don't have an exchange
             return null;
         }
     }
+
+    @Converter
+    public static Serializable convertToSerializable(GenericFile<?> file, Exchange exchange) throws IOException {
+        if (exchange != null) {
+            // ensure the body is loaded as we do not want a java.io.File handle returned, but the file content
+            file.getBinding().loadContent(exchange, file);
+            return exchange.getContext().getTypeConverter().convertTo(Serializable.class, exchange, file.getBody());
+        } else {
+            // should revert to fallback converter if we don't have an exchange
+            return null;
+        }
+    }
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileDefaultBinding.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileDefaultBinding.java?rev=935619&r1=935618&r2=935619&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileDefaultBinding.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileDefaultBinding.java Mon Apr 19 15:21:38 2010
@@ -24,8 +24,6 @@ import org.apache.camel.Exchange;
  * Default binding for generic file.
  */
 public class GenericFileDefaultBinding<T> implements GenericFileBinding<T> {
-    private static final long serialVersionUID = 1156079468647449995L;
-
     private Object body;
 
     public Object getBody(GenericFile<T> file) {
@@ -36,7 +34,7 @@ public class GenericFileDefaultBinding<T
         this.body = body;
     }
 
-    public void loadContent(Exchange exchange, GenericFile<T> file) throws IOException {
+    public void loadContent(Exchange exchange, GenericFile<?> file) throws IOException {
         // noop as the body is already loaded
     }
 }

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/GenericFileConverterTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/GenericFileConverterTest.java?rev=935619&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/GenericFileConverterTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/GenericFileConverterTest.java Mon Apr 19 15:21:38 2010
@@ -0,0 +1,148 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.file;
+
+import java.io.File;
+import java.io.InputStream;
+import java.io.Serializable;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * @version $Revision$
+ */
+public class GenericFileConverterTest extends ContextTestSupport {
+
+    @Override
+    protected void setUp() throws Exception {
+        deleteDirectory("target/gf");
+        super.setUp();
+    }
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    public void testToFile() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("file://target/gf")
+                    .convertBodyTo(File.class)
+                    .to("mock:result");
+            }
+        });
+        context.start();
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.message(0).body().isInstanceOf(File.class);
+
+        template.sendBodyAndHeader("file://target/gf", "Hello World", Exchange.FILE_NAME, "hello.txt");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testToString() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("file://target/gf")
+                    .convertBodyTo(String.class)
+                    .to("mock:result");
+            }
+        });
+        context.start();
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.message(0).body().isInstanceOf(String.class);
+        mock.message(0).body().isEqualTo("Hello World");
+
+        template.sendBodyAndHeader("file://target/gf", "Hello World", Exchange.FILE_NAME, "hello.txt");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testToBytes() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("file://target/gf")
+                    .convertBodyTo(byte[].class)
+                    .to("mock:result");
+            }
+        });
+        context.start();
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.message(0).body().isInstanceOf(byte[].class);
+        mock.message(0).body(String.class).isEqualTo("Hello World");
+
+        template.sendBodyAndHeader("file://target/gf", "Hello World", Exchange.FILE_NAME, "hello.txt");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testToSerializable() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("file://target/gf")
+                    .convertBodyTo(Serializable.class)
+                    .to("mock:result");
+            }
+        });
+        context.start();
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.message(0).body().isInstanceOf(Serializable.class);
+        mock.message(0).body(String.class).isEqualTo("Hello World");
+
+        template.sendBodyAndHeader("file://target/gf", "Hello World", Exchange.FILE_NAME, "hello.txt");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testToInputStream() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("file://target/gf")
+                    .convertBodyTo(InputStream.class)
+                    .to("mock:result");
+            }
+        });
+        context.start();
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.message(0).body().isInstanceOf(InputStream.class);
+        mock.message(0).body(String.class).isEqualTo("Hello World");
+
+        template.sendBodyAndHeader("file://target/gf", "Hello World", Exchange.FILE_NAME, "hello.txt");
+
+        assertMockEndpointsSatisfied();
+    }
+
+}

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/GenericFileConverterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/GenericFileConverterTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date