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 2013/02/03 09:54:01 UTC

svn commit: r1441876 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/component/file/ camel-core/src/main/java/org/apache/camel/impl/ camel-core/src/test/java/org/apache/camel/impl/ components/camel-jms/src/test/java/org/apache/camel/compon...

Author: davsclaus
Date: Sun Feb  3 08:54:01 2013
New Revision: 1441876

URL: http://svn.apache.org/viewvc?rev=1441876&view=rev
Log:
CAMEL-5774: Fixed issuse with file binding when using preMove to load content. Do not support file bodies for transferExchange as this is not a good idea and causes problem on the receiver side.

Added:
    camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/FileRouteJmsPreMoveTest.java
      - copied, changed from r1441783, camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/FileRouteJmsKeepLastModifiedTest.java
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/impl/DefaultExchangeHolder.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeHolderTest.java
    camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/FileRouteJmsKeepLastModifiedTest.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=1441876&r1=1441875&r2=1441876&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 Sun Feb  3 08:54:01 2013
@@ -35,7 +35,7 @@ public class FileBinding implements Gene
             return content;
         }
         
-        // as we use java.io.File itself as the body (not loading its content into a OutputStream etc.)
+        // as we use java.io.File itself as the body (not loading its content into an OutputStream etc.)
         // we just store a java.io.File handle to the actual file denoted by the
         // file.getAbsoluteFilePath. We must do this as the original file consumed can be renamed before
         // being processed (preMove) and thus it points to an invalid file location.
@@ -54,7 +54,7 @@ public class FileBinding implements Gene
     public void loadContent(Exchange exchange, GenericFile<?> file) throws IOException {
         if (content == null) {
             try {
-                content = exchange.getContext().getTypeConverter().mandatoryConvertTo(byte[].class, exchange, file.getFile());
+                content = exchange.getContext().getTypeConverter().mandatoryConvertTo(byte[].class, exchange, file);
             } catch (NoTypeConversionAvailableException e) {
                 throw new IOException("Cannot load file content: " + file.getAbsoluteFilePath(), e);
             }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchangeHolder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchangeHolder.java?rev=1441876&r1=1441875&r2=1441876&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchangeHolder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchangeHolder.java Sun Feb  3 08:54:01 2013
@@ -16,12 +16,15 @@
  */
 package org.apache.camel.impl;
 
+import java.io.File;
 import java.io.Serializable;
 import java.util.Collection;
 import java.util.LinkedHashMap;
 import java.util.Map;
 
 import org.apache.camel.Exchange;
+import org.apache.camel.RuntimeExchangeException;
+import org.apache.camel.WrappedFile;
 import org.apache.camel.util.ObjectHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -30,6 +33,9 @@ import org.slf4j.LoggerFactory;
  * Holder object for sending an exchange over a remote wire as a serialized object.
  * This is usually configured using the <tt>transferExchange=true</tt> option on the endpoint.
  * <p/>
+ * Note: Message body of type {@link File} or {@link WrappedFile} is <b>not</b> supported and
+ * a {@link RuntimeExchangeException} is thrown.
+ * <p/>
  * As opposed to normal usage where only the body part of the exchange is transferred over the wire,
  * this holder object serializes the following fields over the wire:
  * <ul>
@@ -79,6 +85,12 @@ public class DefaultExchangeHolder imple
      * @return the holder object with information copied form the exchange
      */
     public static DefaultExchangeHolder marshal(Exchange exchange, boolean includeProperties) {
+        // we do not support files
+        Object body = exchange.getIn().getBody();
+        if (body instanceof WrappedFile || body instanceof File) {
+            throw new RuntimeExchangeException("Message body of type " + body.getClass().getCanonicalName() + " is not supported by this marshaller.", exchange);
+        }
+
         DefaultExchangeHolder payload = new DefaultExchangeHolder();
 
         payload.exchangeId = exchange.getExchangeId();

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeHolderTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeHolderTest.java?rev=1441876&r1=1441875&r2=1441876&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeHolderTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeHolderTest.java Sun Feb  3 08:54:01 2013
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.impl;
 
+import java.io.File;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -23,6 +24,7 @@ import java.util.Map;
 
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
+import org.apache.camel.RuntimeExchangeException;
 
 /**
  * @version 
@@ -125,6 +127,18 @@ public class DefaultExchangeHolderTest e
         assertNull(exchange.getIn().getHeader("Foo"));
     }
 
+    public void testFileNotSupported() throws Exception {
+        Exchange exchange = new DefaultExchange(context);
+        exchange.getIn().setBody(new File("src/test/resources/log4j.properties"));
+
+        try {
+            DefaultExchangeHolder.marshal(exchange);
+            fail("Should have thrown exception");
+        } catch (RuntimeExchangeException e) {
+            // expected
+        }
+    }
+
     private DefaultExchangeHolder createHolder(boolean includeProperties) {
         Exchange exchange = new DefaultExchange(context);
         id = exchange.getExchangeId();

Modified: camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/FileRouteJmsKeepLastModifiedTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/FileRouteJmsKeepLastModifiedTest.java?rev=1441876&r1=1441875&r2=1441876&view=diff
==============================================================================
--- camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/FileRouteJmsKeepLastModifiedTest.java (original)
+++ camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/FileRouteJmsKeepLastModifiedTest.java Sun Feb  3 08:54:01 2013
@@ -28,12 +28,19 @@ import org.junit.Test;
 import static org.apache.camel.component.jms.JmsComponent.jmsComponentAutoAcknowledge;
 
 /**
- * Unit test that we can produce JMS message from files
+ *
  */
 public class FileRouteJmsKeepLastModifiedTest extends CamelTestSupport {
 
     protected String componentName = "activemq";
 
+    @Override
+    public void setUp() throws Exception {
+        deleteDirectory("target/inbox");
+        deleteDirectory("target/outbox");
+        super.setUp();
+    }
+
     @Test
     public void testKeepLastModified() throws Exception {
         getMockEndpoint("mock:result").expectedMessageCount(1);

Copied: camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/FileRouteJmsPreMoveTest.java (from r1441783, camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/FileRouteJmsKeepLastModifiedTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/FileRouteJmsPreMoveTest.java?p2=camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/FileRouteJmsPreMoveTest.java&p1=camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/FileRouteJmsKeepLastModifiedTest.java&r1=1441783&r2=1441876&rev=1441876&view=diff
==============================================================================
--- camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/FileRouteJmsKeepLastModifiedTest.java (original)
+++ camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/FileRouteJmsPreMoveTest.java Sun Feb  3 08:54:01 2013
@@ -16,7 +16,6 @@
  */
 package org.apache.camel.component.jms;
 
-import java.io.File;
 import javax.jms.ConnectionFactory;
 
 import org.apache.camel.CamelContext;
@@ -28,24 +27,27 @@ import org.junit.Test;
 import static org.apache.camel.component.jms.JmsComponent.jmsComponentAutoAcknowledge;
 
 /**
- * Unit test that we can produce JMS message from files
+ *
  */
-public class FileRouteJmsKeepLastModifiedTest extends CamelTestSupport {
+public class FileRouteJmsPreMoveTest extends CamelTestSupport {
 
     protected String componentName = "activemq";
 
+    @Override
+    public void setUp() throws Exception {
+        deleteDirectory("target/inbox");
+        deleteDirectory("target/outbox");
+        super.setUp();
+    }
+
     @Test
-    public void testKeepLastModified() throws Exception {
+    public void testPreMove() throws Exception {
         getMockEndpoint("mock:result").expectedMessageCount(1);
+        getMockEndpoint("mock:result").expectedFileExists("target/outbox/hello.txt", "Hello World");
 
         template.sendBodyAndHeader("file://target/inbox", "Hello World", Exchange.FILE_NAME, "hello.txt");
 
         assertMockEndpointsSatisfied();
-
-        File inbox = new File("trarget/inbox/hello.txt");
-        File outbox = new File("trarget/outbox/hello.txt");
-
-        assertEquals("Should keep last modified", inbox.lastModified(), outbox.lastModified());
     }
 
     protected CamelContext createCamelContext() throws Exception {
@@ -60,12 +62,11 @@ public class FileRouteJmsKeepLastModifie
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() throws Exception {
-                from("file://target/inbox?noop=true").to("activemq:queue:hello");
+                from("file://target/inbox?preMove=transfer").to("activemq:queue:hello");
 
                 from("activemq:queue:hello")
-                    // just a little delay so the write of the file happens later
-                    .delayer(100)
-                    .to("file://target/outbox?keepLastModified=true")
+                    .to("log:outbox")
+                    .to("file://target/outbox")
                     .to("mock:result");
             }
         };