Return-Path: X-Original-To: apmail-camel-commits-archive@www.apache.org Delivered-To: apmail-camel-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 22741F438 for ; Wed, 8 May 2013 08:35:24 +0000 (UTC) Received: (qmail 75730 invoked by uid 500); 8 May 2013 08:35:24 -0000 Delivered-To: apmail-camel-commits-archive@camel.apache.org Received: (qmail 75619 invoked by uid 500); 8 May 2013 08:35:22 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 75385 invoked by uid 99); 8 May 2013 08:35:21 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 08 May 2013 08:35:21 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id C40AC8896F4; Wed, 8 May 2013 08:35:16 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: davsclaus@apache.org To: commits@camel.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: git commit: CAMEL-6328: SimpleBuilder should create predicate and expressions the same way, and support property placeholders. Date: Wed, 8 May 2013 08:35:16 +0000 (UTC) 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 Authored: Wed May 8 10:35:03 2013 +0200 Committer: Claus Ibsen 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 @@ + + + + + + + bar + + + + + + + + + + {{foo}} + + + + ${header.foo} == 'bar' + + + + + + + + + + + + + '{{foo}}' == 'bar' + + + + + + + + + + + + + + + + + + + +