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 2011/08/20 12:33:05 UTC

svn commit: r1159867 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/impl/ camel-core/src/main/java/org/apache/camel/util/ components/camel-jaxb/src/test/java/org/apache/camel/example/

Author: davsclaus
Date: Sat Aug 20 10:33:05 2011
New Revision: 1159867

URL: http://svn.apache.org/viewvc?rev=1159867&view=rev
Log:
CAMEL-4359: MessageHelper extract/dump message should ignore thrown exceptions during type convertion of message body. LoggingExceptionHandler should run in try catch to not propagate any new exceptions.

Added:
    camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/example/JaxbErrorLogTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/LoggingExceptionHandler.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/LoggingExceptionHandler.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/LoggingExceptionHandler.java?rev=1159867&r1=1159866&r2=1159867&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/LoggingExceptionHandler.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/LoggingExceptionHandler.java Sat Aug 20 10:33:05 2011
@@ -52,12 +52,16 @@ public class LoggingExceptionHandler imp
     }
 
     public void handleException(String message, Exchange exchange, Throwable exception) {
-        String msg = ExchangeHelper.createExceptionMessage(message, exchange, exception);
-        if (isCausedByRollbackExchangeException(exception)) {
-            // do not log stacktrace for intended rollbacks
-            logger.log(msg);
-        } else {
-            logger.log(msg, exception);
+        try {
+            String msg = ExchangeHelper.createExceptionMessage(message, exchange, exception);
+            if (isCausedByRollbackExchangeException(exception)) {
+                // do not log stacktrace for intended rollbacks
+                logger.log(msg);
+            } else {
+                logger.log(msg, exception);
+            }
+        } catch (Throwable e) {
+            // the logging exception handler must not cause new exceptions to occur
         }
     }
 

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java?rev=1159867&r1=1159866&r2=1159867&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java Sat Aug 20 10:33:05 2011
@@ -21,7 +21,6 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.Reader;
 import java.io.Writer;
-import java.util.AbstractList;
 import java.util.Map;
 import java.util.TreeMap;
 import javax.xml.transform.stream.StreamSource;
@@ -220,10 +219,15 @@ public final class MessageHelper {
         }
 
         // grab the message body as a string
-        String body;
+        String body = null;
         if (message.getExchange() != null) {
-            body = message.getExchange().getContext().getTypeConverter().convertTo(String.class, obj);
-        } else {
+            try {
+                body = message.getExchange().getContext().getTypeConverter().convertTo(String.class, obj);
+            } catch (Exception e) {
+                // ignore as the body is for logging purpose
+            }
+        }
+        if (body == null) {
             body = obj.toString();
         }
 
@@ -282,10 +286,14 @@ public final class MessageHelper {
 
                 // dump header value as XML, use Camel type converter to convert to String
                 if (value != null) {
-                    String xml = message.getExchange().getContext().getTypeConverter().convertTo(String.class, value);
-                    if (xml != null) {
-                        // must always xml encode
-                        sb.append(StringHelper.xmlEncode(xml));
+                    try {
+                        String xml = message.getExchange().getContext().getTypeConverter().convertTo(String.class, value);
+                        if (xml != null) {
+                            // must always xml encode
+                            sb.append(StringHelper.xmlEncode(xml));
+                        }
+                    } catch (Exception e) {
+                        // ignore as the body is for logging purpose
                     }
                 }
 

Added: camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/example/JaxbErrorLogTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/example/JaxbErrorLogTest.java?rev=1159867&view=auto
==============================================================================
--- camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/example/JaxbErrorLogTest.java (added)
+++ camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/example/JaxbErrorLogTest.java Sat Aug 20 10:33:05 2011
@@ -0,0 +1,100 @@
+/**
+ * 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.example;
+
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.apache.camel.Body;
+import org.apache.camel.Handler;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class JaxbErrorLogTest extends CamelTestSupport {
+
+    @Test
+    public void testErrorHandling() throws Exception {
+        // the 2nd message is set to fail, but the 4 others should be routed
+        getMockEndpoint("mock:end").expectedMessageCount(4);
+
+        // FailingBean will cause message at index 2 to throw exception
+        for (int i = 0; i < 5; i++) {
+            sendBody("seda:test", new CannotMarshal(i));
+        }
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("seda:test")
+                    .bean(new FailingBean())
+                    .to("log:end", "mock:end");
+            }
+        };
+    }
+
+    public static final class FailingBean {
+        @Handler
+        public void handle(@Body CannotMarshal body) {
+            if (body.getMessageNo() == 2) {
+                // fail on second message
+                throw new RuntimeException("Kaboom");
+            }
+        }
+    }
+
+    /**
+     * This class will throw RuntimeException on JAXB marshal
+     */
+    @XmlRootElement
+    public static final class CannotMarshal {
+
+        private int messageNo;
+
+        public CannotMarshal() {
+        }
+
+        public CannotMarshal(int messageNo) {
+            this.messageNo = messageNo;
+        }
+
+        public int getMessageNo() {
+            return messageNo;
+        }
+
+        public void setMessageNo(int messageNo) {
+            this.messageNo = messageNo;
+        }
+
+        public void setUhoh(String name) {
+        }
+
+        public String getUhoh() {
+            throw new RuntimeException("Can't marshal this");
+        }
+
+        @Override
+        public String toString() {
+            return "MessageNo. " + messageNo;
+        }
+    }
+
+}
\ No newline at end of file