Author: davsclaus Date: Sat May 21 14:43:38 2011 New Revision: 1125717 URL: http://svn.apache.org/viewvc?rev=1125717&view=rev Log: CAMEL-4002: Added ref langauge, to make it easy to refer and use custom expressions in XML DSL. And as well from Java DSL.
Added: camel/trunk/camel-core/src/main/java/org/apache/camel/language/ref/ camel/trunk/camel-core/src/main/java/org/apache/camel/language/ref/RefLanguage.java (contents, props changed) - copied, changed from r1125230, camel/trunk/camel-core/src/main/java/org/apache/camel/language/constant/ConstantLanguage.java camel/trunk/camel-core/src/main/java/org/apache/camel/language/ref/package.html - copied, changed from r1125230, camel/trunk/camel-core/src/main/java/org/apache/camel/language/constant/package.html camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/RefExpression.java - copied, changed from r1125230, camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/ConstantExpression.java camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/language/ref - copied, changed from r1125230, camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/language/constant camel/trunk/camel-core/src/test/java/org/apache/camel/language/RefTest.java - copied, changed from r1125230, camel/trunk/camel-core/src/test/java/org/apache/camel/language/ConstantTest.java camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitCustomExpressionTest.java camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitRefCustomExpressionTest.java camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringSplitCustomExpressionTest.java - copied, changed from r1125230, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringAOPAfterTest.java camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SplitCustomExpressionTest.xml - copied, changed from r1125230, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aopafter.xml Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java camel/trunk/camel-core/src/main/resources/org/apache/camel/model/language/jaxb.index Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java?rev=1125717&r1=1125716&r2=1125717&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java Sat May 21 14:43:38 2011 @@ -324,9 +324,20 @@ public class ExpressionClause<T> extends } /** - * Evaluates a <a href="http://camel.apache.org/ruby.html">Ruby + * Evaluates a <a href="http://camel.apache.org/ref-language.html">Ref * expression</a> * + * @param ref refers to the expression to be evaluated + * @return the builder to continue processing the DSL + */ + public T ref(String ref) { + return delegate.ref(ref); + } + + /** + * Evaluates a <a href="http://camel.apache.org/ruby.html">Ruby + * expression</a> + * * @param text the expression to be evaluated * @return the builder to continue processing the DSL */ Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java?rev=1125717&r1=1125716&r2=1125717&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java Sat May 21 14:43:38 2011 @@ -34,6 +34,7 @@ import org.apache.camel.model.language.O import org.apache.camel.model.language.PhpExpression; import org.apache.camel.model.language.PropertyExpression; import org.apache.camel.model.language.PythonExpression; +import org.apache.camel.model.language.RefExpression; import org.apache.camel.model.language.RubyExpression; import org.apache.camel.model.language.SimpleExpression; import org.apache.camel.model.language.SpELExpression; @@ -361,6 +362,17 @@ public class ExpressionClauseSupport<T> } /** + * Evaluates a {@link Expression} by looking up existing {@link Expression} + * from the {@link org.apache.camel.spi.Registry} + * + * @param ref refers to the expression to be evaluated + * @return the builder to continue processing the DSL + */ + public T ref(String ref) { + return expression(new RefExpression(ref)); + } + + /** * Evaluates a <a href="http://camel.apache.org/ruby.html">Ruby * expression</a> * Copied: camel/trunk/camel-core/src/main/java/org/apache/camel/language/ref/RefLanguage.java (from r1125230, camel/trunk/camel-core/src/main/java/org/apache/camel/language/constant/ConstantLanguage.java) URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/language/ref/RefLanguage.java?p2=camel/trunk/camel-core/src/main/java/org/apache/camel/language/ref/RefLanguage.java&p1=camel/trunk/camel-core/src/main/java/org/apache/camel/language/constant/ConstantLanguage.java&r1=1125230&r2=1125717&rev=1125717&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/language/constant/ConstantLanguage.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/language/ref/RefLanguage.java Sat May 21 14:43:38 2011 @@ -14,30 +14,47 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel.language.constant; +package org.apache.camel.language.ref; +import org.apache.camel.Exchange; import org.apache.camel.Expression; import org.apache.camel.IsSingleton; import org.apache.camel.Predicate; import org.apache.camel.builder.ExpressionBuilder; import org.apache.camel.builder.PredicateBuilder; +import org.apache.camel.impl.ExpressionAdapter; import org.apache.camel.spi.Language; /** - * A language for constant expressions. + * A language for referred expressions. */ -public class ConstantLanguage implements Language, IsSingleton { +public class RefLanguage implements Language, IsSingleton { - public static Expression constant(Object value) { - return ExpressionBuilder.constantExpression(value); + public static Expression ref(Object value) { + String ref = value.toString(); + return ExpressionBuilder.refExpression(ref); } public Predicate createPredicate(String expression) { return PredicateBuilder.toPredicate(createExpression(expression)); } - public Expression createExpression(String expression) { - return ConstantLanguage.constant(expression); + public Expression createExpression(final String expression) { + final Expression exp = RefLanguage.ref(expression); + return new ExpressionAdapter() { + public Object evaluate(Exchange exchange) { + Expression lookup = exp.evaluate(exchange, Expression.class); + if (lookup != null) { + return lookup.evaluate(exchange, Object.class); + } else { + throw new IllegalArgumentException("Cannot find expression in registry with ref: " + expression); + } + } + + public String toString() { + return exp.toString(); + } + }; } public boolean isSingleton() { Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/language/ref/RefLanguage.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/language/ref/RefLanguage.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Copied: camel/trunk/camel-core/src/main/java/org/apache/camel/language/ref/package.html (from r1125230, camel/trunk/camel-core/src/main/java/org/apache/camel/language/constant/package.html) URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/language/ref/package.html?p2=camel/trunk/camel-core/src/main/java/org/apache/camel/language/ref/package.html&p1=camel/trunk/camel-core/src/main/java/org/apache/camel/language/constant/package.html&r1=1125230&r2=1125717&rev=1125717&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/language/constant/package.html (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/language/ref/package.html Sat May 21 14:43:38 2011 @@ -19,7 +19,7 @@ </head> <body> -Camel <a href="http://camel.apache.org/constant.html">Constant</a> language. +Camel <a href="http://camel.apache.org/ref-language.html">Ref</a> language. </body> </html> Copied: camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/RefExpression.java (from r1125230, camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/ConstantExpression.java) URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/RefExpression.java?p2=camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/RefExpression.java&p1=camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/ConstantExpression.java&r1=1125230&r2=1125717&rev=1125717&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/ConstantExpression.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/RefExpression.java Sat May 21 14:43:38 2011 @@ -21,20 +21,23 @@ import javax.xml.bind.annotation.XmlAcce import javax.xml.bind.annotation.XmlRootElement; /** - * For expressions and predicates using a constant + * For expressions and predicates using a reference to an existing + * {@link org.apache.camel.Expression} or {@link org.apache.camel.Predicate} + * to lookup from the {@link org.apache.camel.spi.Registry}. */ -@XmlRootElement(name = "constant") +@XmlRootElement(name = "ref") @XmlAccessorType(XmlAccessType.FIELD) -public class ConstantExpression extends ExpressionDefinition { +public class RefExpression extends ExpressionDefinition { - public ConstantExpression() { + public RefExpression() { } - public ConstantExpression(String expression) { - super(expression); + public RefExpression(String ref) { + super(ref); } public String getLanguage() { - return "constant"; + return "ref"; } + } Copied: camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/language/ref (from r1125230, camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/language/constant) URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/language/ref?p2=camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/language/ref&p1=camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/language/constant&r1=1125230&r2=1125717&rev=1125717&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/language/constant (original) +++ camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/language/ref Sat May 21 14:43:38 2011 @@ -15,4 +15,4 @@ # limitations under the License. # -class=org.apache.camel.language.constant.ConstantLanguage +class=org.apache.camel.language.ref.RefLanguage Modified: camel/trunk/camel-core/src/main/resources/org/apache/camel/model/language/jaxb.index URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/resources/org/apache/camel/model/language/jaxb.index?rev=1125717&r1=1125716&r2=1125717&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/resources/org/apache/camel/model/language/jaxb.index (original) +++ camel/trunk/camel-core/src/main/resources/org/apache/camel/model/language/jaxb.index Sat May 21 14:43:38 2011 @@ -28,6 +28,7 @@ OgnlExpression PhpExpression PropertyExpression PythonExpression +RefExpression RubyExpression SimpleExpression SpELExpression Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/language/RefTest.java (from r1125230, camel/trunk/camel-core/src/test/java/org/apache/camel/language/ConstantTest.java) URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/language/RefTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/language/RefTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/language/ConstantTest.java&r1=1125230&r2=1125717&rev=1125717&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/language/ConstantTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/language/RefTest.java Sat May 21 14:43:38 2011 @@ -16,21 +16,46 @@ */ package org.apache.camel.language; +import org.apache.camel.Exchange; import org.apache.camel.LanguageTestSupport; +import org.apache.camel.impl.ExpressionAdapter; +import org.apache.camel.impl.JndiRegistry; -public class ConstantTest extends LanguageTestSupport { +public class RefTest extends LanguageTestSupport { - public void testConstantExpressions() throws Exception { - // We can put anything in here, the expression will - // always evaluate to itself - assertExpression("a value", "a value"); + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry jndi = super.createRegistry(); + jndi.bind("myExp", new MyExpression()); + return jndi; + } + + public void testRefExpressions() throws Exception { + assertExpression("myExp", "Hello World"); } + public void testRefExpressionsNotFound() throws Exception { + try { + assertExpression("foo", "Hello World"); + fail("Should have thrown exception"); + } catch (IllegalArgumentException e) { + assertEquals("Cannot find expression in registry with ref: foo", e.getMessage()); + } + } + public void testPredicates() throws Exception { - assertPredicate("another value"); + assertPredicate("myExp"); } protected String getLanguageName() { - return "constant"; + return "ref"; + } + + private class MyExpression extends ExpressionAdapter { + + @Override + public Object evaluate(Exchange exchange) { + return "Hello World"; + } } } Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitCustomExpressionTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitCustomExpressionTest.java?rev=1125717&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitCustomExpressionTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitCustomExpressionTest.java Sat May 21 14:43:38 2011 @@ -0,0 +1,69 @@ +/** + * 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.processor; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.Expression; +import org.apache.camel.builder.RouteBuilder; + +/** + * + */ +public class SplitCustomExpressionTest extends ContextTestSupport { + + public void testSplitCustomExpression() throws Exception { + getMockEndpoint("mock:split").expectedBodiesReceived("A", "B", "C"); + + template.sendBody("direct:start", "A,B,C"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .split(new MyCustomExpression()) + .to("mock:split"); + } + }; + } + + public static class MyCustomExpression implements Expression { + + @Override + @SuppressWarnings("unchecked") + public <T> T evaluate(Exchange exchange, Class<T> type) { + final String body = exchange.getIn().getBody(String.class); + + // just split the body by comma + String[] parts = body.split(","); + List<String> list = new ArrayList<String>(); + for (String part : parts) { + list.add(part); + } + + return (T) list.iterator(); + } + } +} Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitRefCustomExpressionTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitRefCustomExpressionTest.java?rev=1125717&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitRefCustomExpressionTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitRefCustomExpressionTest.java Sat May 21 14:43:38 2011 @@ -0,0 +1,77 @@ +/** + * 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.processor; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.Expression; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.JndiRegistry; + +/** + * + */ +public class SplitRefCustomExpressionTest extends ContextTestSupport { + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry jndi = super.createRegistry(); + jndi.bind("myCustomExpression", new MyCustomExpression()); + return jndi; + } + + public void testSplitCustomExpression() throws Exception { + getMockEndpoint("mock:split").expectedBodiesReceived("A", "B", "C"); + + template.sendBody("direct:start", "A,B,C"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .split().ref("myCustomExpression") + .to("mock:split"); + } + }; + } + + public static class MyCustomExpression implements Expression { + + @Override + @SuppressWarnings("unchecked") + public <T> T evaluate(Exchange exchange, Class<T> type) { + final String body = exchange.getIn().getBody(String.class); + + // just split the body by comma + String[] parts = body.split(","); + List<String> list = new ArrayList<String>(); + for (String part : parts) { + list.add(part); + } + + return (T) list.iterator(); + } + } +} Copied: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringSplitCustomExpressionTest.java (from r1125230, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringAOPAfterTest.java) URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringSplitCustomExpressionTest.java?p2=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringSplitCustomExpressionTest.java&p1=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringAOPAfterTest.java&r1=1125230&r2=1125717&rev=1125717&view=diff ============================================================================== --- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringAOPAfterTest.java (original) +++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringSplitCustomExpressionTest.java Sat May 21 14:43:38 2011 @@ -17,15 +17,16 @@ package org.apache.camel.spring.processor; import org.apache.camel.CamelContext; -import org.apache.camel.processor.AOPAfterTest; +import org.apache.camel.processor.SplitCustomExpressionTest; + import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext; /** * @version */ -public class SpringAOPAfterTest extends AOPAfterTest { +public class SpringSplitCustomExpressionTest extends SplitCustomExpressionTest { protected CamelContext createCamelContext() throws Exception { - return createSpringCamelContext(this, "org/apache/camel/spring/processor/aopafter.xml"); + return createSpringCamelContext(this, "org/apache/camel/spring/processor/SplitCustomExpressionTest.xml"); } } \ No newline at end of file Copied: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SplitCustomExpressionTest.xml (from r1125230, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aopafter.xml) URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SplitCustomExpressionTest.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SplitCustomExpressionTest.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aopafter.xml&r1=1125230&r2=1125717&rev=1125717&view=diff ============================================================================== --- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aopafter.xml (original) +++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SplitCustomExpressionTest.xml Sat May 21 14:43:38 2011 @@ -22,16 +22,21 @@ http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd "> - <!-- START SNIPPET: e1 --> - <camelContext xmlns="http://camel.apache.org/schema/spring"> - <route> - <from uri="direct:start"/> - <aop afterUri="mock:after"> - <transform><constant>Bye World</constant></transform> - <to uri="mock:result"/> - </aop> - </route> - </camelContext> - <!-- END SNIPPET: e1 --> + <!-- START SNIPPET: e1 --> + + <!-- use a bean to define a custom expression --> + <bean id="myExpression" class="org.apache.camel.processor.SplitCustomExpressionTest$MyCustomExpression"/> + + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <route> + <from uri="direct:start"/> + <split> + <!-- refer to our custom expression, using ref tag --> + <ref>myExpression</ref> + <to uri="mock:split"/> + </split> + </route> + </camelContext> + <!-- END SNIPPET: e1 --> </beans>