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/02/11 10:38:12 UTC

svn commit: r1069721 - in /camel/trunk/camel-core/src/main/java/org/apache/camel: BinaryPredicate.java builder/BinaryPredicateSupport.java impl/DefaultCamelContext.java util/PredicateAssertHelper.java

Author: davsclaus
Date: Fri Feb 11 09:38:12 2011
New Revision: 1069721

URL: http://svn.apache.org/viewvc?rev=1069721&view=rev
Log:
CAMEL-3654: BinaryPredicate is now thread safe.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/BinaryPredicate.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BinaryPredicateSupport.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/PredicateAssertHelper.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/BinaryPredicate.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/BinaryPredicate.java?rev=1069721&r1=1069720&r2=1069721&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/BinaryPredicate.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/BinaryPredicate.java Fri Feb 11 09:38:12 2011
@@ -22,9 +22,8 @@ package org.apache.camel;
  * The predicate has a left and right hand side expressions which
  * is matched based on an operator.
  * <p/>
- * This predicate can return information about the evaluated expressions
- * which allows you to get detailed information, so you better understand
- * why the predicate did not match.
+ * This predicate offers the {@link #matchesReturningFailureMessage} method
+ * which evaluates and return a detailed failure message if the predicate did not match.
  *
  * @version $Revision$
  */
@@ -52,21 +51,14 @@ public interface BinaryPredicate extends
     Expression getRight();
 
     /**
-     * Gets the evaluated left hand side value.
-     * <p/>
-     * Beware of thread safety that the result of the {@link #getRightValue()} may in fact be from another evaluation.
+     * Evaluates the predicate on the message exchange and returns <tt>null</tt> if this
+     * exchange matches the predicate. If it did <b>not</b> match, then a failure message
+     * is returned detailing why it did not fail, which can be used for end users to understand
+     * the failure.
      *
-     * @return the left value, may be <tt>null</tt> if predicate has not been matched yet.
+     * @param exchange the message exchange
+     * @return <tt>null</tt> if the predicate matches.
      */
-    Object getLeftValue();
-
-    /**
-     * Gets the evaluated right hand side value.
-     * <p/>
-     * Beware of thread safety that the result of the {@link #getLeftValue()} may in fact be from another evaluation.
-     *
-     * @return the right value, may be <tt>null</tt> if predicate has not been matched yet.
-     */
-    Object getRightValue();
+    String matchesReturningFailureMessage(Exchange exchange);
 
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BinaryPredicateSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BinaryPredicateSupport.java?rev=1069721&r1=1069720&r2=1069721&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BinaryPredicateSupport.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BinaryPredicateSupport.java Fri Feb 11 09:38:12 2011
@@ -32,8 +32,6 @@ public abstract class BinaryPredicateSup
 
     private final Expression left;
     private final Expression right;
-    private Object lastLeftValue;
-    private Object lastRightValue;
 
     protected BinaryPredicateSupport(Expression left, Expression right) {
         notNull(left, "left");
@@ -49,36 +47,39 @@ public abstract class BinaryPredicateSup
     }
 
     public boolean matches(Exchange exchange) {
+        return matchesReturningFailureMessage(exchange) == null;
+    }
+
+    public String matchesReturningFailureMessage(Exchange exchange) {
+        // we must not store any state, so we can be thread safe
+        // and thus we offer this method which returns a failure message if
+        // we did not match
+        String answer = null;
+
         // must be thread safe and store result in local objects
         Object leftValue = left.evaluate(exchange, Object.class);
         Object rightValue = right.evaluate(exchange, Object.class);
-        // remember last result (may not be thread safe)
-        lastRightValue = rightValue;
-        lastLeftValue = leftValue;
-        return matches(exchange, leftValue, rightValue);
+        if (!matches(exchange, leftValue, rightValue)) {
+            answer = leftValue + " " + getOperator() + " " + rightValue;
+        }
+
+        return answer;
     }
 
     protected abstract boolean matches(Exchange exchange, Object leftValue, Object rightValue);
 
     protected abstract String getOperationText();
 
-    public Expression getRight() {
-        return right;
-    }
-
     public Expression getLeft() {
         return left;
     }
 
-    public String getOperator() {
-        return getOperationText();
+    public Expression getRight() {
+        return right;
     }
 
-    public Object getRightValue() {
-        return lastRightValue;
+    public String getOperator() {
+        return getOperationText();
     }
 
-    public Object getLeftValue() {
-        return lastLeftValue;
-    }
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=1069721&r1=1069720&r2=1069721&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java Fri Feb 11 09:38:12 2011
@@ -16,7 +16,6 @@
  */
 package org.apache.camel.impl;
 
-
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/PredicateAssertHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/PredicateAssertHelper.java?rev=1069721&r1=1069720&r2=1069721&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/PredicateAssertHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/PredicateAssertHelper.java Fri Feb 11 09:38:12 2011
@@ -35,13 +35,9 @@ public final class PredicateAssertHelper
         if (predicate instanceof BinaryPredicate) {
             // special for binary evaluable as we can get more detailed information
             BinaryPredicate eval = (BinaryPredicate) predicate;
-            if (!eval.matches(exchange)) {
-                String evalText = eval.getLeftValue() + " " + eval.getOperator() + " " + eval.getRightValue();
-                if (text == null) {
-                    throw new AssertionError(predicate + " evaluated as " + evalText + "  on " + exchange);
-                } else {
-                    throw new AssertionError(text + predicate + " evaluated as: " + evalText + " on " + exchange);
-                }
+            String evalText = eval.matchesReturningFailureMessage(exchange);
+            if (evalText != null) {
+                throw new AssertionError(text + predicate + " evaluated as: " + evalText + " on " + exchange);
             }
         } else {
             doAssertMatches(predicate, text, exchange);