camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1028661 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/builder/ main/java/org/apache/camel/language/simple/ test/java/org/apache/camel/language/
Date Fri, 29 Oct 2010 09:47:24 GMT
Author: davsclaus
Date: Fri Oct 29 09:47:23 2010
New Revision: 1028661

URL: http://svn.apache.org/viewvc?rev=1028661&view=rev
Log:
CAMEL-3293: Added exception.stacktrace to Simple language.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java?rev=1028661&r1=1028660&r2=1028661&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java Fri Oct 29
09:47:23 2010
@@ -188,6 +188,14 @@ public final class Builder {
     }
     
     /**
+     * Returns a predicate and value builder for the exception stacktrace on an exchange
+     */
+    public static ValueBuilder exceptionStackTrace() {
+        Expression expression = ExpressionBuilder.exchangeExceptionStackTraceExpression();
+        return new ValueBuilder(expression);
+    }
+
+    /**
      * Returns an expression that replaces all occurrences of the regular 
      * expression with the given replacement
      */

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java?rev=1028661&r1=1028660&r2=1028661&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java Fri
Oct 29 09:47:23 2010
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.builder;
 
+import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.text.SimpleDateFormat;
 import java.util.Collection;
 import java.util.Collections;
@@ -41,6 +43,7 @@ import org.apache.camel.model.language.M
 import org.apache.camel.spi.Language;
 import org.apache.camel.util.ExchangeHelper;
 import org.apache.camel.util.FileUtil;
+import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.OgnlHelper;
 
@@ -421,6 +424,37 @@ public final class ExpressionBuilder {
     }
 
     /**
+     * Returns an expression for an exception stacktrace set on the exchange
+     *
+     * @return an expression object which will return the exception stacktrace set on the
exchange
+     */
+    public static Expression exchangeExceptionStackTraceExpression() {
+        return new ExpressionAdapter() {
+            public Object evaluate(Exchange exchange) {
+                Exception exception = exchange.getException();
+                if (exception == null) {
+                    exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
+                }
+                if (exception != null) {
+                    StringWriter sw = new StringWriter();
+                    PrintWriter pw = new PrintWriter(sw);
+                    exception.printStackTrace(pw);
+                    IOHelper.close(pw);
+                    IOHelper.close(sw);
+                    return sw.toString();
+                } else {
+                    return null;
+                }
+            }
+
+            @Override
+            public String toString() {
+                return "exchangeExceptionStackTrace";
+            }
+        };
+    }
+
+    /**
      * Returns an expression for the property value of exchange with the given name
      *
      * @param propertyName the name of the property the expression will return

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java?rev=1028661&r1=1028660&r2=1028661&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
Fri Oct 29 09:47:23 2010
@@ -124,6 +124,8 @@ public class SimpleLanguage extends Simp
             return ExpressionBuilder.exchangeExceptionExpression();
         } else if (ObjectHelper.equal(expression, "exception.message")) {
             return ExpressionBuilder.exchangeExceptionMessageExpression();
+        } else if (ObjectHelper.equal(expression, "exception.stacktrace")) {
+            return ExpressionBuilder.exchangeExceptionStackTraceExpression();
         } else if (ObjectHelper.equal(expression, "threadName")) {
             return ExpressionBuilder.threadNameExpression();
         }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java?rev=1028661&r1=1028660&r2=1028661&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java Fri Oct
29 09:47:23 2010
@@ -177,6 +177,24 @@ public class SimpleTest extends Language
         assertExpression("Hello ${exception.message} World", "Hello Just testing World");
     }
 
+    public void testExceptionStacktrace() throws Exception {
+        exchange.setException(new IllegalArgumentException("Just testing"));
+
+        String out = SimpleLanguage.simple("exception.stacktrace").evaluate(exchange, String.class);
+        assertNotNull(out);
+        assertTrue(out.startsWith("java.lang.IllegalArgumentException: Just testing"));
+        assertTrue(out.contains("at org.apache.camel.language."));
+    }
+
+    public void testException() throws Exception {
+        exchange.setException(new IllegalArgumentException("Just testing"));
+
+        Exception out = SimpleLanguage.simple("exception").evaluate(exchange, Exception.class);
+        assertNotNull(out);
+        assertIsInstanceOf(IllegalArgumentException.class, out);
+        assertEquals("Just testing", out.getMessage());
+    }
+
     public void testBodyAs() throws Exception {
         assertExpression("${bodyAs(String)}", "<hello id='m123'>world!</hello>");
         assertExpression("${bodyAs('String')}", "<hello id='m123'>world!</hello>");



Mime
View raw message