Author: davsclaus
Date: Sun Apr 24 09:32:34 2011
New Revision: 1096289
URL: http://svn.apache.org/viewvc?rev=1096289&view=rev
Log:
CAMEL-3893: Simple language now support OGNL on exchange property. Thanks to
David Valeri for the patch.
Modified:
camel/trunk/buildingtools/camel-checkstyle.xml
camel/trunk/buildingtools/src/main/resources/camel-checkstyle.xml
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/ExchangeTestSupport.java
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java
camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java
Modified: camel/trunk/buildingtools/camel-checkstyle.xml
URL:
http://svn.apache.org/viewvc/camel/trunk/buildingtools/camel-checkstyle.xml?rev=1096289&r1=1096288&r2=1096289&view=diff
==============================================================================
--- camel/trunk/buildingtools/camel-checkstyle.xml (original)
+++ camel/trunk/buildingtools/camel-checkstyle.xml Sun Apr 24 09:32:34 2011
@@ -131,7 +131,7 @@ lengths, if/try depths, etc...
<property name="max" value="200"/>
</module>
<module name="MethodLength">
- <property name="max" value="150"/>
+ <property name="max" value="200"/>
<property name="countEmpty" value="false"/>
</module>
<!-- We should try to get this enabled soon..
Modified: camel/trunk/buildingtools/src/main/resources/camel-checkstyle.xml
URL:
http://svn.apache.org/viewvc/camel/trunk/buildingtools/src/main/resources/camel-checkstyle.xml?rev=1096289&r1=1096288&r2=1096289&view=diff
==============================================================================
--- camel/trunk/buildingtools/src/main/resources/camel-checkstyle.xml (original)
+++ camel/trunk/buildingtools/src/main/resources/camel-checkstyle.xml Sun Apr
24 09:32:34 2011
@@ -131,7 +131,7 @@ lengths, if/try depths, etc...
<property name="max" value="200"/>
</module>
<module name="MethodLength">
- <property name="max" value="150"/>
+ <property name="max" value="200"/>
<property name="countEmpty" value="false"/>
</module>
<!-- We should try to get this enabled soon..
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=1096289&r1=1096288&r2=1096289&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
Sun Apr 24 09:32:34 2011
@@ -174,41 +174,12 @@ public final class ExpressionBuilder {
* @param ognl methods to invoke on the header in a simple OGNL syntax
*/
public static Expression headersOgnlExpression(final String ognl) {
- return new ExpressionAdapter() {
- public Object evaluate(Exchange exchange) {
- // try with full name first
- Object header = exchange.getIn().getHeader(ognl);
- if (header != null) {
- return header;
+ return new KeyedOgnlExpressionAdapter(ognl, "headerOgnl(" + ognl + ")",
+ new KeyedOgnlExpressionAdapter.KeyedEntityRetrievalStrategy() {
+ public Object getKeyedEntity(Exchange exchange, String key) {
+ return exchange.getIn().getHeader(key);
}
-
- // Split ognl except when this is not a Map, Array
- // and we would like to keep the dots within the key name
- List<String> methods;
- if (ognl.startsWith("[") && ognl.endsWith("]")) {
- methods = new ArrayList<String>();
- methods.add(ognl);
- } else {
- methods = OgnlHelper.splitOgnl(ognl);
- }
-
- // remove any OGNL operators so we got the pure key name
- String key = OgnlHelper.removeOperators(methods.get(0));
-
- header = exchange.getIn().getHeader(key);
- if (header == null) {
- return null;
- }
- // the remainder is the rest of the ognl without the key
- String remainder = ObjectHelper.after(ognl, key);
- return new MethodCallExpression(header,
remainder).evaluate(exchange);
- }
-
- @Override
- public String toString() {
- return "headerOgnl(" + ognl + ")";
- }
- };
+ });
}
/**
@@ -505,6 +476,21 @@ public final class ExpressionBuilder {
}
};
}
+
+ /**
+ * Returns an expression for the property value of exchange with the given
name invoking methods defined
+ * in a simple OGNL notation
+ *
+ * @param ognl methods to invoke on the property in a simple OGNL syntax
+ */
+ public static Expression propertyOgnlExpression(final String ognl) {
+ return new KeyedOgnlExpressionAdapter(ognl, "propertyOgnl(" + ognl +
")",
+ new KeyedOgnlExpressionAdapter.KeyedEntityRetrievalStrategy() {
+ public Object getKeyedEntity(Exchange exchange, String key) {
+ return exchange.getProperty(key);
+ }
+ });
+ }
/**
* Returns an expression for the properties of exchange
@@ -1533,4 +1519,61 @@ public final class ExpressionBuilder {
};
}
+ /**
+ * Expression adapter for OGNL expression from Message Header or Exchange
property
+ */
+ private static class KeyedOgnlExpressionAdapter extends ExpressionAdapter {
+ private final String ognl;
+ private final String toStringValue;
+ private final KeyedEntityRetrievalStrategy
keyedEntityRetrievalStrategy;
+
+ public KeyedOgnlExpressionAdapter(String ognl, String toStringValue,
+ KeyedEntityRetrievalStrategy
keyedEntityRetrievalStrategy) {
+ this.ognl = ognl;
+ this.toStringValue = toStringValue;
+ this.keyedEntityRetrievalStrategy = keyedEntityRetrievalStrategy;
+ }
+
+ public Object evaluate(Exchange exchange) {
+ // try with full name first
+ Object property =
keyedEntityRetrievalStrategy.getKeyedEntity(exchange, ognl);
+ if (property != null) {
+ return property;
+ }
+
+ // Split ognl except when this is not a Map, Array
+ // and we would like to keep the dots within the key name
+ List<String> methods;
+ if (ognl.startsWith("[") && ognl.endsWith("]")) {
+ methods = new ArrayList<String>();
+ methods.add(ognl);
+ } else {
+ methods = OgnlHelper.splitOgnl(ognl);
+ }
+
+ // remove any OGNL operators so we got the pure key name
+ String key = OgnlHelper.removeOperators(methods.get(0));
+
+ property = keyedEntityRetrievalStrategy.getKeyedEntity(exchange,
key);
+ if (property == null) {
+ return null;
+ }
+ // the remainder is the rest of the ognl without the key
+ String remainder = ObjectHelper.after(ognl, key);
+ return new MethodCallExpression(property,
remainder).evaluate(exchange);
+ }
+
+ @Override
+ public String toString() {
+ return toStringValue;
+ }
+
+ /**
+ * Strategy to retrieve the value based on the key
+ */
+ public interface KeyedEntityRetrievalStrategy {
+ Object getKeyedEntity(Exchange exchange, String key);
+ }
+ };
+
}
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=1096289&r1=1096288&r2=1096289&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
Sun Apr 24 09:32:34 2011
@@ -41,6 +41,7 @@ import org.apache.camel.util.StringHelpe
* <li>in.header.foo.OGNL or header.OGNL to access an inbound header called
'foo' using an OGNL expression</li>
* <li>out.header.foo to access an outbound header called 'foo'</li>
* <li>property.foo to access the exchange property called 'foo'</li>
+ * <li>property.foo.OGNL to access the exchange property called 'foo' using an
OGNL expression</li>
* <li>sys.foo to access the system property called 'foo'</li>
* <li>sysenv.foo to access the system environment called 'foo'</li>
* <li>exception.messsage to access the exception message</li>
@@ -86,7 +87,7 @@ import org.apache.camel.util.StringHelpe
* <br/>
* The <b>only</b> file is the filename only with all paths clipped.
*
- * @version
+ * @version
*/
public class SimpleLanguage extends SimpleLanguageSupport {
@@ -109,7 +110,7 @@ public class SimpleLanguage extends Simp
public static Expression simple(String expression) {
return SIMPLE.createExpression(expression);
}
-
+
protected Expression createSimpleExpressionDirectly(String expression) {
if (ObjectHelper.isEqualToAny(expression, "body", "in.body")) {
@@ -134,13 +135,13 @@ public class SimpleLanguage extends Simp
}
protected Expression createSimpleExpression(String expression, boolean
strict) {
-
+
// return the expression directly if we can create expression without
analyzing the prefix
Expression answer = createSimpleExpressionDirectly(expression);
if (answer != null) {
return answer;
}
-
+
// bodyAs
String remainder = ifStartsWithReturnRemainder("bodyAs", expression);
if (remainder != null) {
@@ -174,7 +175,7 @@ public class SimpleLanguage extends Simp
}
return ExpressionBuilder.bodyOgnlExpression(remainder);
}
-
+
// Exception OGNL
remainder = ifStartsWithReturnRemainder("exception", expression);
if (remainder != null) {
@@ -182,7 +183,7 @@ public class SimpleLanguage extends Simp
if (invalid) {
throw new ExpressionIllegalSyntaxException("Valid syntax:
${exception.OGNL} was: " + expression);
}
- return
ExpressionBuilder.exchangeExceptionOgnlExpression(remainder);
+ return
ExpressionBuilder.exchangeExceptionOgnlExpression(remainder);
}
// headerAs
@@ -215,7 +216,7 @@ public class SimpleLanguage extends Simp
remainder = ifStartsWithReturnRemainder("header", expression);
}
if (remainder != null) {
- // remove leading dot
+ // remove leading character (dot or ?)
remainder = remainder.substring(1);
// validate syntax
@@ -243,9 +244,24 @@ public class SimpleLanguage extends Simp
}
// property
- remainder = ifStartsWithReturnRemainder("property.", expression);
+ remainder = ifStartsWithReturnRemainder("property", expression);
if (remainder != null) {
- return ExpressionBuilder.propertyExpression(remainder);
+ // remove leading character (dot or ?)
+ remainder = remainder.substring(1);
+
+ // validate syntax
+ boolean invalid =
OgnlHelper.isInvalidValidOgnlExpression(remainder);
+ if (invalid) {
+ throw new ExpressionIllegalSyntaxException("Valid syntax:
${property.OGNL} was: " + expression);
+ }
+
+ if (OgnlHelper.isValidOgnlExpression(remainder)) {
+ // ognl based property
+ return ExpressionBuilder.propertyOgnlExpression(remainder);
+ } else {
+ // regular property
+ return ExpressionBuilder.propertyExpression(remainder);
+ }
}
// system property
@@ -316,7 +332,7 @@ public class SimpleLanguage extends Simp
return ExpressionBuilder.constantExpression(expression);
}
}
-
+
protected Expression createSimpleFileExpression(String remainder) {
if (ObjectHelper.equal(remainder, "name")) {
return ExpressionBuilder.fileNameExpression();
Modified:
camel/trunk/camel-core/src/test/java/org/apache/camel/ExchangeTestSupport.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/ExchangeTestSupport.java?rev=1096289&r1=1096288&r2=1096289&view=diff
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/ExchangeTestSupport.java
(original)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/ExchangeTestSupport.java
Sun Apr 24 09:32:34 2011
@@ -43,6 +43,8 @@ public abstract class ExchangeTestSuppor
in.setHeader("foo", "abc");
in.setHeader("bar", 123);
in.setBody("<hello id='m123'>world!</hello>");
+
+ exchange.setProperty("foobar", "cba");
}
@Override
Modified:
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java?rev=1096289&r1=1096288&r2=1096289&view=diff
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java
(original)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java
Sun Apr 24 09:32:34 2011
@@ -92,6 +92,7 @@ public class DefaultExchangeTest extends
}
public void testProperty() throws Exception {
+ exchange.removeProperty("foobar");
assertFalse(exchange.hasProperties());
exchange.setProperty("fruit", "apple");
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=1096289&r1=1096288&r2=1096289&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
Sun Apr 24 09:32:34 2011
@@ -124,6 +124,85 @@ public class SimpleTest extends Language
assertExpression("sysenv.PATH", path);
}
}
+
+ public void testOGNLPropertyList() throws Exception {
+ List<String> lines = new ArrayList<String>();
+ lines.add("Camel in Action");
+ lines.add("ActiveMQ in Action");
+ exchange.setProperty("wicket", lines);
+
+ assertExpression("${property.wicket[0]}", "Camel in Action");
+ assertExpression("${property.wicket[1]}", "ActiveMQ in Action");
+ try {
+ assertExpression("${property.wicket[2]}", "");
+ fail("Should have thrown an exception");
+ } catch (Exception e) {
+ IndexOutOfBoundsException cause =
assertIsInstanceOf(IndexOutOfBoundsException.class, e.getCause());
+ assertEquals("Index: 2, Size: 2", cause.getMessage());
+ }
+ assertExpression("${property.unknown[cool]}", null);
+ }
+
+ public void testOGNLPropertyLinesList() throws Exception {
+ List<OrderLine> lines = new ArrayList<OrderLine>();
+ lines.add(new OrderLine(123, "Camel in Action"));
+ lines.add(new OrderLine(456, "ActiveMQ in Action"));
+ exchange.setProperty("wicket", lines);
+
+ assertExpression("${property.wicket[0].getId}", 123);
+ assertExpression("${property.wicket[1].getName}", "ActiveMQ in
Action");
+ try {
+ assertExpression("${property.wicket[2]}", "");
+ fail("Should have thrown an exception");
+ } catch (Exception e) {
+ IndexOutOfBoundsException cause =
assertIsInstanceOf(IndexOutOfBoundsException.class, e.getCause());
+ assertEquals("Index: 2, Size: 2", cause.getMessage());
+ }
+ assertExpression("${property.unknown[cool]}", null);
+ }
+
+ public void testOGNLPropertyMap() throws Exception {
+ Map<String, Object> map = new HashMap<String, Object>();
+ map.put("cool", "Camel rocks");
+ map.put("dude", "Hey dude");
+ map.put("code", 4321);
+ exchange.setProperty("wicket", map);
+
+ assertExpression("${property.wicket[cool]}", "Camel rocks");
+ assertExpression("${property.wicket[dude]}", "Hey dude");
+ assertExpression("${property.wicket[unknown]}", null);
+ assertExpression("${property.wicket[code]}", 4321);
+ // no header named unknown
+ assertExpression("${property?.unknown[cool]}", null);
+ assertExpression("${property.unknown[cool]}", null);
+ }
+
+ public void testOGNLPropertyMapWithDot() throws Exception {
+ Map<String, Object> map = new HashMap<String, Object>();
+ map.put("this.code", "This code");
+ exchange.setProperty("wicket", map);
+
+ assertExpression("${property.wicket[this.code]}", "This code");
+ }
+
+ public void testOGNLPropertyMapNotMap() throws Exception {
+ try {
+ assertExpression("${property.foobar[bar]}", null);
+ fail("Should have thrown an exception");
+ } catch (RuntimeBeanExpressionException e) {
+ IndexOutOfBoundsException cause =
assertIsInstanceOf(IndexOutOfBoundsException.class, e.getCause());
+ assertEquals("Key: bar not found in bean: cba of type:
java.lang.String using OGNL path [[bar]]", cause.getMessage());
+ }
+ }
+
+ public void testOGNLPropertyMapIllegalSyntax() throws Exception {
+ try {
+ assertExpression("${property.foobar[bar}", null);
+ fail("Should have thrown an exception");
+ } catch (ExpressionIllegalSyntaxException e) {
+ assertEquals("Illegal syntax: Valid syntax: ${property.OGNL} was:
property.foobar[bar", e.getMessage());
+ }
+ }
public void testDateExpressions() throws Exception {
Calendar cal = GregorianCalendar.getInstance();