You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by js...@apache.org on 2007/03/20 05:01:41 UTC
svn commit: r520261 - in /activemq/camel/trunk/camel-core/src:
main/java/org/apache/camel/ main/java/org/apache/camel/builder/
main/java/org/apache/camel/util/ test/java/org/apache/camel/
Author: jstrachan
Date: Mon Mar 19 21:01:39 2007
New Revision: 520261
URL: http://svn.apache.org/viewvc?view=rev&rev=520261
Log:
added more predicates and a helper header() method to build easier and more powerful expressions and predicates
Added:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Expression.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Expressions.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Predicates.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DestinationBuilder.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/RouteBuilderTest.java
Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Expression.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Expression.java?view=auto&rev=520261
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Expression.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Expression.java Mon Mar 19 21:01:39 2007
@@ -0,0 +1,33 @@
+/*
+ * 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;
+
+/**
+ * Represents an arbitrary expression evaluated on a message exchange
+ *
+ * @version $Revision: $
+ */
+public interface Expression<E extends Exchange> {
+
+ /**
+ * Returns the value of the expression on the given exchange
+ *
+ * @param exchange the message exchange on which to evaluate the expression
+ * @return the value of the expression
+ */
+ Object evaluate(E exchange);
+}
Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Expressions.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Expressions.java?view=auto&rev=520261
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Expressions.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Expressions.java Mon Mar 19 21:01:39 2007
@@ -0,0 +1,50 @@
+/*
+ * 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;
+
+/**
+ * @version $Revision: $
+ */
+public class Expressions {
+
+ /**
+ * Returns an expression for the header value with the given name
+ *
+ * @param headerName the name of the header the expression will return
+ * @return an expression object which will return the header value
+ */
+ public static <E extends Exchange> Expression<E> headerExpression(final String headerName) {
+ return new Expression<E>() {
+ public Object evaluate(E exchange) {
+ return exchange.getHeader(headerName);
+ }
+ };
+ }
+ /**
+ * Returns an expression for the contant value
+ *
+ * @param value the value the expression will return
+ * @return an expression object which will return the constant value
+ */
+ public static <E extends Exchange> Expression<E> constantExpression(final Object value) {
+ return new Expression<E>() {
+ public Object evaluate(E exchange) {
+ return value;
+ }
+ };
+ }
+}
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Predicates.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Predicates.java?view=diff&rev=520261&r1=520260&r2=520261
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Predicates.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Predicates.java Mon Mar 19 21:01:39 2007
@@ -17,6 +17,7 @@
package org.apache.camel;
import org.apache.camel.util.ObjectHelper;
+import static org.apache.camel.util.ObjectHelper.notNull;
/**
* A helper class for working with predicates
@@ -27,9 +28,9 @@
/**
* A helper method to combine multiple predicates by a logical AND
*/
- public static <E> Predicate<E> and(final Predicate<E> left, final Predicate<E> right) {
- ObjectHelper.notNull(left, "left");
- ObjectHelper.notNull(right, "right");
+ public static <E extends Exchange> Predicate<E> and(final Predicate<E> left, final Predicate<E> right) {
+ notNull(left, "left");
+ notNull(right, "right");
return new Predicate<E>() {
public boolean evaluate(E exchange) {
return left.evaluate(exchange) && right.evaluate(exchange);
@@ -40,9 +41,9 @@
/**
* A helper method to combine multiple predicates by a logical OR
*/
- public static <E> Predicate<E> or(final Predicate<E> left, final Predicate<E> right) {
- ObjectHelper.notNull(left, "left");
- ObjectHelper.notNull(right, "right");
+ public static <E extends Exchange> Predicate<E> or(final Predicate<E> left, final Predicate<E> right) {
+ notNull(left, "left");
+ notNull(right, "right");
return new Predicate<E>() {
public boolean evaluate(E exchange) {
return left.evaluate(exchange) || right.evaluate(exchange);
@@ -51,4 +52,81 @@
}
+ public static <E extends Exchange> Predicate<E> isEqualTo(final Expression<E> left, final Expression<E> right) {
+ notNull(left, "left");
+ notNull(right, "right");
+
+ return new Predicate<E>() {
+ public boolean evaluate(E exchange) {
+ Object value1 = left.evaluate(exchange);
+ Object value2 = right.evaluate(exchange);
+ return ObjectHelper.equals(value1, value2);
+ }
+ };
+ }
+
+ public static <E extends Exchange> Predicate<E> isNotEqualTo(final Expression<E> left, final Expression<E> right) {
+ notNull(left, "left");
+ notNull(right, "right");
+
+ return new Predicate<E>() {
+ public boolean evaluate(E exchange) {
+ Object value1 = left.evaluate(exchange);
+ Object value2 = right.evaluate(exchange);
+ return !ObjectHelper.equals(value1, value2);
+ }
+ };
+ }
+
+ public static <E extends Exchange> Predicate<E> isLessThan(final Expression<E> left, final Expression<E> right) {
+ notNull(left, "left");
+ notNull(right, "right");
+
+ return new Predicate<E>() {
+ public boolean evaluate(E exchange) {
+ Object value1 = left.evaluate(exchange);
+ Object value2 = right.evaluate(exchange);
+ return ObjectHelper.compare(value1, value2) < 0;
+ }
+ };
+ }
+
+ public static <E extends Exchange> Predicate<E> isLessThanOrEqualTo(final Expression<E> left, final Expression<E> right) {
+ notNull(left, "left");
+ notNull(right, "right");
+
+ return new Predicate<E>() {
+ public boolean evaluate(E exchange) {
+ Object value1 = left.evaluate(exchange);
+ Object value2 = right.evaluate(exchange);
+ return ObjectHelper.compare(value1, value2) <= 0;
+ }
+ };
+ }
+
+ public static <E extends Exchange> Predicate<E> isGreaterThan(final Expression<E> left, final Expression<E> right) {
+ notNull(left, "left");
+ notNull(right, "right");
+
+ return new Predicate<E>() {
+ public boolean evaluate(E exchange) {
+ Object value1 = left.evaluate(exchange);
+ Object value2 = right.evaluate(exchange);
+ return ObjectHelper.compare(value1, value2) > 0;
+ }
+ };
+ }
+
+ public static <E extends Exchange> Predicate<E> isGreaterThanOrEqualTo(final Expression<E> left, final Expression<E> right) {
+ notNull(left, "left");
+ notNull(right, "right");
+
+ return new Predicate<E>() {
+ public boolean evaluate(E exchange) {
+ Object value1 = left.evaluate(exchange);
+ Object value2 = right.evaluate(exchange);
+ return ObjectHelper.compare(value1, value2) >= 0;
+ }
+ };
+ }
}
Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java?view=auto&rev=520261
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java Mon Mar 19 21:01:39 2007
@@ -0,0 +1,36 @@
+/*
+ * 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.builder;
+
+import org.apache.camel.Expression;
+import org.apache.camel.Expressions;
+import org.apache.camel.Exchange;
+
+/**
+ * Base class for implementation inheritence
+ *
+ * @version $Revision: $
+ */
+public abstract class BuilderSupport<E extends Exchange> {
+
+ /**
+ * Returns a predicate and value builder for headers on an exchange
+ */
+ public ValueBuilder<E> header(String name) {
+ Expression<E> expression = Expressions.headerExpression(name);
+ return new ValueBuilder<E>(expression);
+ }}
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DestinationBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DestinationBuilder.java?view=diff&rev=520261&r1=520260&r2=520261
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DestinationBuilder.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DestinationBuilder.java Mon Mar 19 21:01:39 2007
@@ -16,20 +16,19 @@
*/
package org.apache.camel.builder;
-import java.util.ArrayList;
-import java.util.List;
-
import org.apache.camel.CompositeProcessor;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.Predicate;
import org.apache.camel.Processor;
+import java.util.ArrayList;
+import java.util.List;
+
/**
* @version $Revision$
*/
-public class DestinationBuilder<E extends Exchange> implements ProcessorBuilder<E> {
- private DestinationBuilder<E> parent;
+public class DestinationBuilder<E extends Exchange> extends BuilderSupport<E> implements ProcessorBuilder<E> {
private RouteBuilder<E> builder;
private Endpoint<E> from;
private List<Processor<E>> processors = new ArrayList<Processor<E>>();
@@ -38,11 +37,9 @@
public DestinationBuilder(RouteBuilder<E> builder, Endpoint<E> from) {
this.builder = builder;
this.from = from;
- this.parent = this;
}
public DestinationBuilder(DestinationBuilder<E> parent) {
- this.parent = parent;
this.builder = parent.getBuilder();
this.from = parent.getFrom();
}
@@ -54,10 +51,6 @@
return getBuilder().endpoint(uri);
}
- public DestinationBuilder<E> getParent() {
- return parent;
- }
-
/**
* Sends the exchange to the given endpoint URI
*/
@@ -120,6 +113,7 @@
addProcessBuilder(answer);
return answer;
}
+
/**
* Creates a choice of one or more predicates with an otherwise clause
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java?view=diff&rev=520261&r1=520260&r2=520261
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java Mon Mar 19 21:01:39 2007
@@ -59,7 +59,4 @@
return new FilterProcessor<E>(predicate, childProcessor);
}
- public void addProcessor(Processor<E> processor) {
- getParent().addProcessor(new FilterProcessor<E>(predicate, processor));
- }
}
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java?view=diff&rev=520261&r1=520260&r2=520261
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java Mon Mar 19 21:01:39 2007
@@ -35,7 +35,7 @@
*
* @version $Revision$
*/
-public abstract class RouteBuilder<E extends Exchange> {
+public abstract class RouteBuilder<E extends Exchange> extends BuilderSupport<E> {
private CamelContainer<E> container;
private List<DestinationBuilder<E>> destinationBuilders = new ArrayList<DestinationBuilder<E>>();
private AtomicBoolean initalized = new AtomicBoolean(false);
Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java?view=auto&rev=520261
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java Mon Mar 19 21:01:39 2007
@@ -0,0 +1,70 @@
+/*
+ * 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.builder;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
+import org.apache.camel.Predicate;
+import org.apache.camel.Predicates;
+import org.apache.camel.Expressions;
+
+/**
+ * A builder of expressions or predicates based on values.
+ *
+ * @version $Revision: $
+ */
+public class ValueBuilder<E extends Exchange> {
+ private Expression<E> expression;
+
+ public ValueBuilder(Expression<E> expression) {
+ this.expression = expression;
+ }
+
+ public Predicate<E> isNotEqualTo(Object value) {
+ Expression<E> right = Expressions.constantExpression(value);
+ return Predicates.isNotEqualTo(expression, right);
+ }
+
+ public Predicate<E> isEqualTo(Object value) {
+ Expression<E> right = Expressions.constantExpression(value);
+ return Predicates.isEqualTo(expression, right);
+ }
+
+ public Predicate<E> isLessThan(Object value) {
+ Expression<E> right = Expressions.constantExpression(value);
+ return Predicates.isLessThan(expression, right);
+ }
+
+ public Predicate<E> isLessThanOrEqualTo(Object value) {
+ Expression<E> right = Expressions.constantExpression(value);
+ return Predicates.isLessThanOrEqualTo(expression, right);
+ }
+
+ public Predicate<E> isGreaterThan(Object value) {
+ Expression<E> right = Expressions.constantExpression(value);
+ return Predicates.isGreaterThan(expression, right);
+ }
+
+ public Predicate<E> isGreaterThanOrEqualTo(Object value) {
+ Expression<E> right = Expressions.constantExpression(value);
+ return Predicates.isGreaterThanOrEqualTo(expression, right);
+ }
+
+ public Expression<E> getExpression() {
+ return expression;
+ }
+}
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java?view=diff&rev=520261&r1=520260&r2=520261
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java Mon Mar 19 21:01:39 2007
@@ -20,12 +20,44 @@
* @version $Revision$
*/
public class ObjectHelper {
+ /**
+ * A helper method for comparing objects for equality while handling nulls
+ */
public static boolean equals(Object a, Object b) {
if (a == b) {
return true;
}
return a != null && b != null && a.equals(b);
}
+
+ /**
+ * A helper method for performing an ordered comparsion on the objects
+ * handling nulls and objects which do not
+ * handle sorting gracefully
+ */
+ public static int compare(Object a, Object b) {
+ if (a == b) {
+ return 0;
+ }
+ if (a == null) {
+ return -1;
+ }
+ if (b == null) {
+ return 1;
+ }
+ if (a instanceof Comparable) {
+ Comparable comparable = (Comparable) a;
+ return comparable.compareTo(b);
+ }
+ else {
+ int answer = a.getClass().getName().compareTo(b.getClass().getName());
+ if (answer == 0) {
+ answer = a.hashCode() - b.hashCode();
+ }
+ return answer;
+ }
+ }
+
public static void notNull(Object value, String name) {
if (value == null) {
Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/RouteBuilderTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/RouteBuilderTest.java?view=diff&rev=520261&r1=520260&r2=520261
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/RouteBuilderTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/RouteBuilderTest.java Mon Mar 19 21:01:39 2007
@@ -16,15 +16,14 @@
*/
package org.apache.camel;
+import junit.framework.TestCase;
+import org.apache.camel.builder.RouteBuilder;
+
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
-import junit.framework.TestCase;
-
-import org.apache.camel.builder.RouteBuilder;
-
/**
* @version $Revision$
*/
@@ -292,6 +291,38 @@
InterceptorProcessor<Exchange> p2 = (InterceptorProcessor<Exchange>) processor;
assertSendTo(p2.getNext(), "queue:d");
+ }
+ }
+
+ public void testComplexExpressions() throws Exception {
+ // START SNIPPET: e7
+ RouteBuilder<Exchange> builder = new RouteBuilder<Exchange>() {
+ public void configure() {
+ from("queue:a").filter(header("foo").isEqualTo(123)).to("queue:b");
+ from("queue:a").filter(header("bar").isGreaterThan(45)).to("queue:b");
+ }
+ };
+ // END SNIPPET: e7
+
+
+ Map<Endpoint<Exchange>, Processor<Exchange>> routeMap = builder.getRouteMap();
+ System.out.println("Created map: " + routeMap);
+
+ Set<Map.Entry<Endpoint<Exchange>, Processor<Exchange>>> routes = routeMap.entrySet();
+ assertEquals("Number routes created", 1, routes.size());
+ for (Map.Entry<Endpoint<Exchange>, Processor<Exchange>> route : routes) {
+ Endpoint<Exchange> key = route.getKey();
+ assertEquals("From endpoint", "queue:a", key.getEndpointUri());
+ Processor processor = route.getValue();
+
+ System.out.println("processor: " + processor);
+ /* TODO
+ assertTrue("Processor should be a FilterProcessor but was: " + processor + " with type: " + processor.getClass().getName(), processor instanceof FilterProcessor);
+ FilterProcessor filterProcessor = (FilterProcessor) processor;
+
+ SendProcessor sendProcessor = (SendProcessor) filterProcessor.getProcessor();
+ assertEquals("Endpoint URI", "queue:b", sendProcessor.getDestination().getEndpointUri());
+ */
}
}