Updated Branches: refs/heads/master ed17edf40 -> 9215b94ad
CAMEL-6328: SimpleBuilder should create predicate and expressions the same way, and support property placeholders. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/9215b94a Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/9215b94a Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/9215b94a Branch: refs/heads/master Commit: 9215b94ad37846c5681c9325d07aa0f26138c0a2 Parents: ed17edf Author: Claus Ibsen <davscl...@apache.org> Authored: Wed May 8 10:35:03 2013 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed May 8 10:35:03 2013 +0200 ---------------------------------------------------------------------- .../org/apache/camel/builder/SimpleBuilder.java | 25 ++++- .../issues/PropertiesAvailableEverywhereTest.java | 88 +++++++++++++++ .../SpringPropertiesAvailableEverywhereTest.java | 30 +++++ .../SpringPropertiesAvailableEverywhereTest.xml | 76 +++++++++++++ 4 files changed, 217 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/9215b94a/camel-core/src/main/java/org/apache/camel/builder/SimpleBuilder.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/builder/SimpleBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/SimpleBuilder.java index e19bcf2..0201d42 100644 --- a/camel-core/src/main/java/org/apache/camel/builder/SimpleBuilder.java +++ b/camel-core/src/main/java/org/apache/camel/builder/SimpleBuilder.java @@ -20,6 +20,7 @@ import org.apache.camel.Exchange; import org.apache.camel.Expression; import org.apache.camel.Predicate; import org.apache.camel.language.simple.SimpleLanguage; +import org.apache.camel.util.ObjectHelper; /** * Creates an {@link org.apache.camel.language.Simple} language builder. @@ -70,7 +71,7 @@ public class SimpleBuilder implements Predicate, Expression { public boolean matches(Exchange exchange) { if (predicate == null) { - predicate = exchange.getContext().resolveLanguage("simple").createPredicate(text); + predicate = createPredicate(exchange); } return predicate.matches(exchange); } @@ -82,12 +83,32 @@ public class SimpleBuilder implements Predicate, Expression { return expression.evaluate(exchange, type); } + private Predicate createPredicate(Exchange exchange) { + SimpleLanguage simple = (SimpleLanguage) exchange.getContext().resolveLanguage("simple"); + if (resultType != null) { + simple.setResultType(resultType); + } + // resolve property placeholders + try { + String resolve = exchange.getContext().resolvePropertyPlaceholders(text); + return simple.createPredicate(resolve); + } catch (Exception e) { + throw ObjectHelper.wrapCamelExecutionException(exchange, e); + } + } + private Expression createExpression(Exchange exchange) { SimpleLanguage simple = (SimpleLanguage) exchange.getContext().resolveLanguage("simple"); if (resultType != null) { simple.setResultType(resultType); } - return simple.createExpression(text); + // resolve property placeholders + try { + String resolve = exchange.getContext().resolvePropertyPlaceholders(text); + return simple.createExpression(resolve); + } catch (Exception e) { + throw ObjectHelper.wrapCamelExecutionException(exchange, e); + } } public String toString() { http://git-wip-us.apache.org/repos/asf/camel/blob/9215b94a/camel-core/src/test/java/org/apache/camel/issues/PropertiesAvailableEverywhereTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/issues/PropertiesAvailableEverywhereTest.java b/camel-core/src/test/java/org/apache/camel/issues/PropertiesAvailableEverywhereTest.java new file mode 100644 index 0000000..94a9086 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/issues/PropertiesAvailableEverywhereTest.java @@ -0,0 +1,88 @@ +/** + * 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.issues; + +import java.util.Properties; + +import org.apache.camel.CamelContext; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.properties.PropertiesComponent; +import org.apache.camel.component.properties.PropertiesResolver; + +public class PropertiesAvailableEverywhereTest extends ContextTestSupport { + + @Override + protected CamelContext createCamelContext() throws Exception { + CamelContext camelContext = super.createCamelContext(); + + final Properties properties = new Properties(); + properties.put("foo", "bar"); + PropertiesComponent pc = camelContext.getComponent("properties", PropertiesComponent.class); + pc.setLocations(new String[0]); + pc.setPropertiesResolver(new PropertiesResolver() { + @Override + public Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, String... uri) { + return properties; + } + }); + + return camelContext; + } + + public void testPropertiesInPredicates() throws Exception { + getMockEndpoint("mock:header-ok").expectedBodiesReceived("Hello Camel"); + getMockEndpoint("mock:choice-ok").expectedBodiesReceived("Hello Camel"); + getMockEndpoint("mock:direct-ok").expectedBodiesReceived("Hello Camel"); + getMockEndpoint("mock:ko").expectedMessageCount(0); + + template.sendBody("direct:header-start", "Hello Camel"); + template.sendBody("direct:choice-start", "Hello Camel"); + template.sendBody("direct:direct-start", "Hello Camel"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // Properties in headers + from("direct:header-start") + .setHeader("foo", simple("{{foo}}")) + .choice() + .when(simple("${header.foo} == 'bar'")) + .to("mock:header-ok") + .otherwise() + .to("mock:ko"); + + // Properties in choices + from("direct:choice-start") + .choice() + .when(simple("'{{foo}}' == 'bar'")) + .to("mock:choice-ok") + .otherwise() + .to("mock:ko"); + + // Properties in URI + from("direct:direct-start").to("direct:direct-{{foo}}"); + from("direct:direct-bar").to("mock:direct-ok"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/9215b94a/components/camel-spring/src/test/java/org/apache/camel/spring/issues/SpringPropertiesAvailableEverywhereTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/issues/SpringPropertiesAvailableEverywhereTest.java b/components/camel-spring/src/test/java/org/apache/camel/spring/issues/SpringPropertiesAvailableEverywhereTest.java new file mode 100644 index 0000000..2f6f7f5 --- /dev/null +++ b/components/camel-spring/src/test/java/org/apache/camel/spring/issues/SpringPropertiesAvailableEverywhereTest.java @@ -0,0 +1,30 @@ +/** + * 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.spring.issues; + +import org.apache.camel.CamelContext; +import org.apache.camel.issues.PropertiesAvailableEverywhereTest; + +import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext; + +public class SpringPropertiesAvailableEverywhereTest extends PropertiesAvailableEverywhereTest { + + protected CamelContext createCamelContext() throws Exception { + return createSpringCamelContext(this, "org/apache/camel/spring/issues/SpringPropertiesAvailableEverywhereTest.xml"); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/9215b94a/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/SpringPropertiesAvailableEverywhereTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/SpringPropertiesAvailableEverywhereTest.xml b/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/SpringPropertiesAvailableEverywhereTest.xml new file mode 100644 index 0000000..ef579c8 --- /dev/null +++ b/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/SpringPropertiesAvailableEverywhereTest.xml @@ -0,0 +1,76 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:util="http://www.springframework.org/schema/util" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + "> + + <!-- properties --> + <util:properties id="prop"> + <prop key="foo">bar</prop> + </util:properties> + + <camelContext xmlns="http://camel.apache.org/schema/spring"> + + <propertyPlaceholder id="properties" location="ref:prop"/> + + <route> + <from uri="direct:header-start"/> + <setHeader headerName="foo"> + <simple>{{foo}}</simple> + </setHeader> + <choice> + <when> + <simple>${header.foo} == 'bar'</simple> + <to uri="mock:header-ok"/> + </when> + <otherwise> + <to uri="mock:ko"/> + </otherwise> + </choice> + </route> + + <route> + <from uri="direct:choice-start"/> + <choice> + <when> + <simple>'{{foo}}' == 'bar'</simple> + <to uri="mock:choice-ok"/> + </when> + <otherwise> + <to uri="mock:ko"/> + </otherwise> + </choice> + </route> + + <route> + <from uri="direct:direct-start"/> + <to uri="direct:direct-{{foo}}"/> + </route> + <route> + <from uri="direct:direct-bar"/> + <to uri="mock:direct-ok"/> + </route> + + </camelContext> + +</beans>