camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject camel git commit: CAMEL-9645: Added messageHistory as function to simple language.
Date Sun, 06 Mar 2016 12:32:21 GMT
Repository: camel
Updated Branches:
  refs/heads/master 02434f00b -> 50037bb3c


CAMEL-9645: Added messageHistory as function to simple language.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/50037bb3
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/50037bb3
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/50037bb3

Branch: refs/heads/master
Commit: 50037bb3c52d125ee9e3483fb92041c9d17748b2
Parents: 02434f0
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Sun Mar 6 13:05:59 2016 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sun Mar 6 13:05:59 2016 +0100

----------------------------------------------------------------------
 .../apache/camel/builder/ExpressionBuilder.java | 55 ++++++++++++
 .../simple/ast/SimpleFunctionExpression.java    | 88 +++++++++++++-------
 .../SimpleMessageHistoryNotDetailedTest.java    | 58 +++++++++++++
 .../simple/SimpleMessageHistoryTest.java        | 57 +++++++++++++
 4 files changed, 227 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/50037bb3/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
index 2290200..7b11ca6 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
@@ -27,6 +27,7 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Random;
 import java.util.Scanner;
+import java.util.Set;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.regex.Pattern;
 
@@ -44,6 +45,8 @@ import org.apache.camel.component.bean.BeanInvocation;
 import org.apache.camel.component.properties.PropertiesComponent;
 import org.apache.camel.language.bean.BeanLanguage;
 import org.apache.camel.model.language.MethodCallExpression;
+import org.apache.camel.processor.DefaultExchangeFormatter;
+import org.apache.camel.spi.ExchangeFormatter;
 import org.apache.camel.spi.Language;
 import org.apache.camel.spi.RouteContext;
 import org.apache.camel.spi.UnitOfWork;
@@ -51,11 +54,13 @@ import org.apache.camel.support.ExpressionAdapter;
 import org.apache.camel.support.TokenPairExpressionIterator;
 import org.apache.camel.support.TokenXMLExpressionIterator;
 import org.apache.camel.support.XMLTokenExpressionIterator;
+import org.apache.camel.util.CamelContextHelper;
 import org.apache.camel.util.ExchangeHelper;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.GroupIterator;
 import org.apache.camel.util.GroupTokenIterator;
 import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.MessageHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.OgnlHelper;
 import org.apache.camel.util.StringHelper;
@@ -2026,6 +2031,56 @@ public final class ExpressionBuilder {
     }
 
     /**
+     * Returns the message history (including exchange details or not)
+     */
+    public static Expression messageHistoryExpression(final boolean detailed) {
+        return new ExpressionAdapter() {
+
+            private ExchangeFormatter formatter;
+
+            public Object evaluate(Exchange exchange) {
+                ExchangeFormatter ef = null;
+                if (detailed) {
+                    // use the exchange formatter to log exchange details
+                    ef = getOrCreateExchangeFormatter(exchange.getContext());
+                }
+                return MessageHelper.dumpMessageHistoryStacktrace(exchange, ef, false);
+            }
+
+            private ExchangeFormatter getOrCreateExchangeFormatter(CamelContext camelContext)
{
+                if (formatter == null) {
+                    Set<ExchangeFormatter> formatters = camelContext.getRegistry().findByType(ExchangeFormatter.class);
+                    if (formatters != null && formatters.size() == 1) {
+                        formatter = formatters.iterator().next();
+                    } else {
+                        // setup exchange formatter to be used for message history dump
+                        DefaultExchangeFormatter def = new DefaultExchangeFormatter();
+                        def.setShowExchangeId(true);
+                        def.setMultiline(true);
+                        def.setShowHeaders(true);
+                        def.setStyle(DefaultExchangeFormatter.OutputStyle.Fixed);
+                        try {
+                            Integer maxChars = CamelContextHelper.parseInteger(camelContext,
camelContext.getProperty(Exchange.LOG_DEBUG_BODY_MAX_CHARS));
+                            if (maxChars != null) {
+                                def.setMaxChars(maxChars);
+                            }
+                        } catch (Exception e) {
+                            throw ObjectHelper.wrapRuntimeCamelException(e);
+                        }
+                        formatter = def;
+                    }
+                }
+                return formatter;
+            }
+
+            @Override
+            public String toString() {
+                return "messageHistory(" + detailed + ")";
+            }
+        };
+    }
+
+    /**
      * Expression adapter for OGNL expression from Message Header or Exchange property
      */
     private static class KeyedOgnlExpressionAdapter extends ExpressionAdapter {

http://git-wip-us.apache.org/repos/asf/camel/blob/50037bb3/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
b/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
index 5110ae9..8948931 100644
--- a/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
+++ b/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
@@ -206,37 +206,10 @@ public class SimpleFunctionExpression extends LiteralExpression {
             return ExpressionBuilder.cacheExpression(exp);
         }
 
-        // random
-        remainder = ifStartsWithReturnRemainder("random", function);
-        if (remainder != null) {
-            String values = ObjectHelper.between(remainder, "(", ")");
-            if (values == null || ObjectHelper.isEmpty(values)) {
-                throw new SimpleParserException("Valid syntax: ${random(min,max)} or ${random(max)}
was: " + function, token.getIndex());
-            }
-            if (values.contains(",")) {
-                String[] tokens = values.split(",", -1);
-                if (tokens.length > 2) {
-                    throw new SimpleParserException("Valid syntax: ${random(min,max)} or
${random(max)} was: " + function, token.getIndex());
-                }
-                int min = Integer.parseInt(tokens[0].trim());
-                int max = Integer.parseInt(tokens[1].trim());
-                return ExpressionBuilder.randomExpression(min, max);
-            } else {
-                int max = Integer.parseInt(values.trim());
-                return ExpressionBuilder.randomExpression(max);
-            }
-        }
-
-        // collate function
-        remainder = ifStartsWithReturnRemainder("collate", function);
-        if (remainder != null) {
-            String values = ObjectHelper.between(remainder, "(", ")");
-            if (values == null || ObjectHelper.isEmpty(values)) {
-                throw new SimpleParserException("Valid syntax: ${collate(group)} was: " +
function, token.getIndex());
-            }
-            String exp = "${body}";
-            int num = Integer.parseInt(values.trim());
-            return ExpressionBuilder.collateExpression(exp, num);
+        // miscellaneous functions
+        Expression misc = createSimpleExpressionMisc(function);
+        if (misc != null) {
+            return misc;
         }
 
         if (strict) {
@@ -424,6 +397,59 @@ public class SimpleFunctionExpression extends LiteralExpression {
         return null;
     }
 
+    private Expression createSimpleExpressionMisc(String function) {
+        String remainder;
+
+        // random function
+        remainder = ifStartsWithReturnRemainder("random", function);
+        if (remainder != null) {
+            String values = ObjectHelper.between(remainder, "(", ")");
+            if (values == null || ObjectHelper.isEmpty(values)) {
+                throw new SimpleParserException("Valid syntax: ${random(min,max)} or ${random(max)}
was: " + function, token.getIndex());
+            }
+            if (values.contains(",")) {
+                String[] tokens = values.split(",", -1);
+                if (tokens.length > 2) {
+                    throw new SimpleParserException("Valid syntax: ${random(min,max)} or
${random(max)} was: " + function, token.getIndex());
+                }
+                int min = Integer.parseInt(tokens[0].trim());
+                int max = Integer.parseInt(tokens[1].trim());
+                return ExpressionBuilder.randomExpression(min, max);
+            } else {
+                int max = Integer.parseInt(values.trim());
+                return ExpressionBuilder.randomExpression(max);
+            }
+        }
+
+        // collate function
+        remainder = ifStartsWithReturnRemainder("collate", function);
+        if (remainder != null) {
+            String values = ObjectHelper.between(remainder, "(", ")");
+            if (values == null || ObjectHelper.isEmpty(values)) {
+                throw new SimpleParserException("Valid syntax: ${collate(group)} was: " +
function, token.getIndex());
+            }
+            String exp = "${body}";
+            int num = Integer.parseInt(values.trim());
+            return ExpressionBuilder.collateExpression(exp, num);
+        }
+
+        // messageHistory function
+        remainder = ifStartsWithReturnRemainder("messageHistory", function);
+        if (remainder != null) {
+            boolean detailed;
+            String values = ObjectHelper.between(remainder, "(", ")");
+            if (values == null || ObjectHelper.isEmpty(values)) {
+                detailed = true;
+            } else {
+                detailed = Boolean.valueOf(values);
+            }
+            return ExpressionBuilder.messageHistoryExpression(detailed);
+        } else if (ObjectHelper.equal(function, "messageHistory")) {
+            return ExpressionBuilder.messageHistoryExpression(true);
+        }
+        return null;
+    }
+
     private String ifStartsWithReturnRemainder(String prefix, String text) {
         if (text.startsWith(prefix)) {
             String remainder = text.substring(prefix.length());

http://git-wip-us.apache.org/repos/asf/camel/blob/50037bb3/camel-core/src/test/java/org/apache/camel/language/simple/SimpleMessageHistoryNotDetailedTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/language/simple/SimpleMessageHistoryNotDetailedTest.java
b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleMessageHistoryNotDetailedTest.java
new file mode 100644
index 0000000..aaf64b5
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleMessageHistoryNotDetailedTest.java
@@ -0,0 +1,58 @@
+/**
+ * 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.language.simple;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+
+public class SimpleMessageHistoryNotDetailedTest extends ContextTestSupport {
+
+    public void testMessageHistory() throws Exception {
+        getMockEndpoint("mock:a").expectedMessageCount(1);
+        getMockEndpoint("mock:b").expectedMessageCount(1);
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+
+        String result = getMockEndpoint("mock:result").getExchanges().get(0).getIn().getBody(String.class);
+        assertNotNull(result);
+
+        assertTrue(result.contains("mock:a"));
+        assertTrue(result.contains("mock:b"));
+        assertTrue(result.contains("transform[simple{${messageHistory(false)}}"));
+        // should not include exchange details
+        assertFalse(result.contains("Hello World"));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .to("mock:a")
+                    .log("${messageHistory(false)}")
+                    .to("mock:b")
+                    .log("${messageHistory(false)}")
+                    .transform().simple("${messageHistory(false)}")
+                    .to("mock:result");
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/50037bb3/camel-core/src/test/java/org/apache/camel/language/simple/SimpleMessageHistoryTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/language/simple/SimpleMessageHistoryTest.java
b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleMessageHistoryTest.java
new file mode 100644
index 0000000..49a7dbb
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleMessageHistoryTest.java
@@ -0,0 +1,57 @@
+/**
+ * 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.language.simple;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+
+public class SimpleMessageHistoryTest extends ContextTestSupport {
+
+    public void testMessageHistory() throws Exception {
+        getMockEndpoint("mock:a").expectedMessageCount(1);
+        getMockEndpoint("mock:b").expectedMessageCount(1);
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+
+        String result = getMockEndpoint("mock:result").getExchanges().get(0).getIn().getBody(String.class);
+        assertNotNull(result);
+
+        assertTrue(result.contains("mock:a"));
+        assertTrue(result.contains("mock:b"));
+        assertTrue(result.contains("transform[simple{${messageHistory}}"));
+        assertTrue(result.contains("Hello World"));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .to("mock:a")
+                    .log("${messageHistory}")
+                    .to("mock:b")
+                    .log("${messageHistory}")
+                    .transform().simple("${messageHistory}")
+                    .to("mock:result");
+            }
+        };
+    }
+}


Mime
View raw message