You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by js...@apache.org on 2007/09/11 07:48:57 UTC
svn commit: r574468 - in /activemq/camel/trunk/camel-core/src:
main/java/org/apache/camel/builder/
main/java/org/apache/camel/language/simple/
main/java/org/apache/camel/util/ test/ide-resources/
test/java/org/apache/camel/ test/java/org/apache/camel/l...
Author: jstrachan
Date: Mon Sep 10 22:48:56 2007
New Revision: 574468
URL: http://svn.apache.org/viewvc?rev=574468&view=rev
Log:
added improvements to the Simple language to support CAMEL-141
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
activemq/camel/trunk/camel-core/src/test/ide-resources/log4j.properties
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java?rev=574468&r1=574467&r2=574468&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java Mon Sep 10 22:48:56 2007
@@ -24,6 +24,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
+import java.util.Collection;
import java.util.regex.Pattern;
/**
@@ -35,12 +36,12 @@
/**
* Utility classes should not have a public constructor.
*/
- private ExpressionBuilder() {
+ private ExpressionBuilder() {
}
-
+
/**
* 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
*/
@@ -83,7 +84,7 @@
/**
* Returns an expression for the out 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
*/
@@ -147,7 +148,7 @@
/**
* Returns an expression for a system property value with the given name
- *
+ *
* @param propertyName the name of the system property the expression will
* return
* @return an expression object which will return the system property value
@@ -158,7 +159,7 @@
/**
* Returns an expression for a system property value with the given name
- *
+ *
* @param propertyName the name of the system property the expression will
* return
* @return an expression object which will return the system property value
@@ -179,7 +180,7 @@
/**
* 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
*/
@@ -463,7 +464,7 @@
/**
* Evaluates the expression on the given exchange and returns the String
* representation
- *
+ *
* @param expression the expression to evaluate
* @param exchange the exchange to use to evaluate the expression
* @return the String representation of the expression or null if it could
@@ -489,6 +490,50 @@
return new Expression<E>() {
public Object evaluate(E exchange) {
return System.getProperty(name, defaultValue);
+ }
+ };
+ }
+
+ /**
+ * Returns an expression which returns the string concatenation value of the various
+ * expressions
+ *
+ * @param expressions the expression to be concatenated dynamically
+ * @return an expression which when evaluated will return the concatenated values
+ */
+ public static <E extends Exchange> Expression<E> concatExpression(final Collection<Expression> expressions) {
+ return concatExpression(expressions, null);
+ }
+
+ /**
+ * Returns an expression which returns the string concatenation value of the various
+ * expressions
+ *
+ * @param expressions the expression to be concatenated dynamically
+ * @param expression the text description of the expression
+ * @return an expression which when evaluated will return the concatenated values
+ */
+ public static <E extends Exchange> Expression<E> concatExpression(final Collection<Expression> expressions, final String expression) {
+ return new Expression<E>() {
+ public Object evaluate(E exchange) {
+ StringBuffer buffer = new StringBuffer();
+ for (Expression<E> expression : expressions) {
+ String text = evaluateStringExpression(expression, exchange);
+ if (text != null) {
+ buffer.append(text);
+ }
+ }
+ return buffer.toString();
+ }
+
+ @Override
+ public String toString() {
+ if (expression != null) {
+ return expression;
+ }
+ else {
+ return "concat" + expressions;
+ }
}
};
}
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java?rev=574468&r1=574467&r2=574468&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java Mon Sep 10 22:48:56 2007
@@ -16,6 +16,9 @@
*/
package org.apache.camel.language.simple;
+import java.util.ArrayList;
+import java.util.List;
+
import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.Predicate;
@@ -37,36 +40,100 @@
* <li>property.foo to access the exchange property called 'foo'</li>
* <li>sys.foo to access the system property called 'foo'</li>
* </ul>
- *
+ *
* @version $Revision: $
*/
public class SimpleLanguage implements Language {
+ public static Expression simple(String expression) {
+ SimpleLanguage language = new SimpleLanguage();
+ return language.createExpression(expression);
+ }
+
public Predicate<Exchange> createPredicate(String expression) {
return PredicateBuilder.toPredicate(createExpression(expression));
}
public Expression<Exchange> createExpression(String expression) {
+ if (expression.indexOf("${") >= 0) {
+ return createComplexExpression(expression);
+ }
+ return createSimpleExpression(expression);
+ }
+
+ protected Expression<Exchange> createComplexExpression(String expression) {
+ List<Expression> results = new ArrayList<Expression>();
+
+ int pivot = 0;
+ int size = expression.length();
+ while (pivot < size) {
+ int idx = expression.indexOf("${", pivot);
+ if (idx < 0) {
+ results.add(createConstantExpression(expression, pivot, size));
+ break;
+ }
+ else {
+ if (pivot < idx) {
+ results.add(createConstantExpression(expression, pivot, idx));
+ }
+ pivot = idx + 2;
+ int endIdx = expression.indexOf("}", pivot);
+ if (endIdx < 0) {
+ throw new IllegalArgumentException("Expecting } but found end of string for simple expression: " + expression);
+ }
+ String simpleText = expression.substring(pivot, endIdx);
+
+ Expression simpleExpression = createSimpleExpression(simpleText);
+ results.add(simpleExpression);
+ pivot = endIdx + 1;
+ }
+ }
+ return ExpressionBuilder.concatExpression(results, expression);
+ }
+
+ protected Expression createConstantExpression(String expression, int start, int end) {
+ return ExpressionBuilder.constantExpression(expression.substring(start, end));
+ }
+
+ protected Expression<Exchange> createSimpleExpression(String expression) {
if (ObjectHelper.isEqualToAny(expression, "body", "in.body")) {
return ExpressionBuilder.bodyExpression();
- } else if (ObjectHelper.equals(expression, "out.body")) {
+ }
+ else if (ObjectHelper.equals(expression, "out.body")) {
return ExpressionBuilder.outBodyExpression();
}
+
+ // in header expression
String remainder = ifStartsWithReturnRemainder("in.header.", expression);
if (remainder == null) {
remainder = ifStartsWithReturnRemainder("header.", expression);
}
+ if (remainder == null) {
+ remainder = ifStartsWithReturnRemainder("headers.", expression);
+ }
+ if (remainder == null) {
+ remainder = ifStartsWithReturnRemainder("in.headers.", expression);
+ }
if (remainder != null) {
return ExpressionBuilder.headerExpression(remainder);
}
+
+ // out header expression
remainder = ifStartsWithReturnRemainder("out.header.", expression);
+ if (remainder == null) {
+ remainder = ifStartsWithReturnRemainder("out.headers.", expression);
+ }
if (remainder != null) {
return ExpressionBuilder.outHeaderExpression(remainder);
}
+
+ // property
remainder = ifStartsWithReturnRemainder("property.", expression);
if (remainder != null) {
return ExpressionBuilder.propertyExpression(remainder);
}
+
+ // system property
remainder = ifStartsWithReturnRemainder("sys.", expression);
if (remainder != null) {
return ExpressionBuilder.propertyExpression(remainder);
@@ -78,7 +145,6 @@
if (text.startsWith(prefix)) {
String remainder = text.substring(prefix.length());
if (remainder.length() > 0) {
-
return remainder;
}
}
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java?rev=574468&r1=574467&r2=574468&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java Mon Sep 10 22:48:56 2007
@@ -18,11 +18,12 @@
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePattern;
import org.apache.camel.InvalidPayloadException;
import org.apache.camel.InvalidTypeException;
+import org.apache.camel.Message;
import org.apache.camel.NoSuchEndpointException;
import org.apache.camel.NoSuchPropertyException;
-import org.apache.camel.Message;
/**
* Some helper methods for working with {@link Exchange} objects
@@ -115,6 +116,30 @@
}
/**
+ * Returns the mandatory outbound message body of the correct type or throws
+ * an exception if it is not present
+ */
+ public static Object getMandatoryOutBody(Exchange exchange) throws InvalidPayloadException {
+ Object answer = exchange.getOut().getBody();
+ if (answer == null) {
+ throw new InvalidPayloadException(exchange, Object.class);
+ }
+ return answer;
+ }
+
+ /**
+ * Returns the mandatory outbound message body of the correct type or throws
+ * an exception if it is not present
+ */
+ public static <T> T getMandatoryOutBody(Exchange exchange, Class<T> type) throws InvalidPayloadException {
+ T answer = exchange.getOut().getBody(type);
+ if (answer == null) {
+ throw new InvalidPayloadException(exchange, type);
+ }
+ return answer;
+ }
+
+ /**
* Converts the value to the given expected type or throws an exception
*/
public static <T> T convertToMandatoryType(Exchange exchange, Class<T> type, Object value)
@@ -154,5 +179,29 @@
result.getOut(true).copyFrom(out);
}
}
+ }
+
+ /**
+ * Returns true if the given exchange pattern (if defined) can support IN messagea
+ *
+ * @param exchange the exchange to interrogate
+ * @return true if the exchange is defined as an {@link ExchangePattern} which supports
+ * IN messages
+ */
+ public static boolean isInCapable(Exchange exchange) {
+ ExchangePattern pattern = exchange.getPattern();
+ return pattern != null && pattern.isInCapable();
+ }
+
+ /**
+ * Returns true if the given exchange pattern (if defined) can support OUT messagea
+ *
+ * @param exchange the exchange to interrogate
+ * @return true if the exchange is defined as an {@link ExchangePattern} which supports
+ * OUT messages
+ */
+ public static boolean isOutCapable(Exchange exchange) {
+ ExchangePattern pattern = exchange.getPattern();
+ return pattern != null && pattern.isOutCapable();
}
}
Modified: activemq/camel/trunk/camel-core/src/test/ide-resources/log4j.properties
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/ide-resources/log4j.properties?rev=574468&r1=574467&r2=574468&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/ide-resources/log4j.properties (original)
+++ activemq/camel/trunk/camel-core/src/test/ide-resources/log4j.properties Mon Sep 10 22:48:56 2007
@@ -18,10 +18,10 @@
#
# The logging properties used for eclipse testing, We want to see debug output on the console.
#
-log4j.rootLogger=INFO, out
+log4j.rootLogger=DEBUG, out
# uncomment the next line to debug Camel
-log4j.logger.org.apache.camel=INFO
+log4j.logger.org.apache.camel=DEBUG
log4j.logger.org.apache.camel.impl.converter=INFO
#log4j.logger.org.apache.activemq=DEBUG
Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java?rev=574468&r1=574467&r2=574468&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java Mon Sep 10 22:48:56 2007
@@ -263,4 +263,15 @@
context.stop();
return answer;
}
+
+ /**
+ * Asserts that the text contains the given string
+ *
+ * @param text the text to compare
+ * @param containedText the text which must be contained inside the other text parameter
+ */
+ protected void assertStringContains(String text, String containedText) {
+ assertNotNull("Text should not be null!", text);
+ assertTrue("Text: " + text + " does not contain: " + containedText, text.contains(containedText));
+ }
}
Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java?rev=574468&r1=574467&r2=574468&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java Mon Sep 10 22:48:56 2007
@@ -23,11 +23,32 @@
*/
public class SimpleTest extends LanguageTestSupport {
- public void testExpressions() throws Exception {
+ public void testSimpleExpressions() throws Exception {
assertExpression("body", "<hello id='m123'>world!</hello>");
assertExpression("in.body", "<hello id='m123'>world!</hello>");
assertExpression("in.header.foo", "abc");
assertExpression("header.foo", "abc");
+ }
+
+ public void testComplexExpressions() throws Exception {
+ assertExpression("hey ${in.header.foo}", "hey abc");
+ assertExpression("hey ${in.header.foo}!", "hey abc!");
+ assertExpression("hey ${in.header.foo}-${in.header.foo}!", "hey abc-abc!");
+ assertExpression("hey ${in.header.foo}${in.header.foo}", "hey abcabc");
+ assertExpression("${in.header.foo}${in.header.foo}", "abcabc");
+ assertExpression("${in.header.foo}", "abc");
+ assertExpression("${in.header.foo}!", "abc!");
+ }
+
+
+ public void testInvalidComplexExpression() throws Exception {
+ try {
+ assertExpression("hey ${foo", "bad expression!");
+ fail("Should have thrown an exception!");
+ }
+ catch (IllegalArgumentException e) {
+ log.debug("Caught expected exception: " + e, e);
+ }
}
public void testPredicates() throws Exception {