Repository: camel Updated Branches: refs/heads/camel-2.14.x 7ebecc46f -> 7ed841d1a refs/heads/master 91a052ce5 -> 295e0d62c
CAMEL-8382: Route inputs should also resolve property placeholder during route prepare so its similar to the rest of the route. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/295e0d62 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/295e0d62 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/295e0d62 Branch: refs/heads/master Commit: 295e0d62c48eca3c79e21313967478f46011a004 Parents: 91a052c Author: Claus Ibsen <[email protected]> Authored: Fri Feb 20 11:26:42 2015 +0100 Committer: Claus Ibsen <[email protected]> Committed: Fri Feb 20 11:33:09 2015 +0100 ---------------------------------------------------------------------- .../camel/model/DataFormatDefinition.java | 2 +- .../apache/camel/model/ProcessorDefinition.java | 8 +-- .../camel/model/ProcessorDefinitionHelper.java | 29 +++++++-- .../camel/model/RouteDefinitionHelper.java | 14 +++++ .../properties/PropertiesRouteFromTest.java | 63 ++++++++++++++++++++ .../SpringPropertiesRouteFromTest.java | 34 +++++++++++ .../SpringPropertiesRouteFromTest.xml | 34 +++++++++++ 7 files changed, 174 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/295e0d62/camel-core/src/main/java/org/apache/camel/model/DataFormatDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/DataFormatDefinition.java b/camel-core/src/main/java/org/apache/camel/model/DataFormatDefinition.java index c4e9cf9..d2d9669 100644 --- a/camel-core/src/main/java/org/apache/camel/model/DataFormatDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/DataFormatDefinition.java @@ -88,7 +88,7 @@ public class DataFormatDefinition extends IdentifiedType { // resolve properties before we create the data format try { - ProcessorDefinitionHelper.resolvePropertyPlaceholders(routeContext, this); + ProcessorDefinitionHelper.resolvePropertyPlaceholders(routeContext.getCamelContext(), this); } catch (Exception e) { throw new IllegalArgumentException("Error resolving property placeholders on data format: " + this, e); } http://git-wip-us.apache.org/repos/asf/camel/blob/295e0d62/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java b/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java index a0bf492..3a114a8 100644 --- a/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java @@ -412,7 +412,7 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type> output.preCreateProcessor(); // resolve properties before we create the processor - ProcessorDefinitionHelper.resolvePropertyPlaceholders(routeContext, output); + ProcessorDefinitionHelper.resolvePropertyPlaceholders(routeContext.getCamelContext(), output); // resolve constant fields (eg Exchange.FILE_NAME) ProcessorDefinitionHelper.resolveKnownConstantFields(output); @@ -424,7 +424,7 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type> ExpressionDefinition expressionDefinition = exp.getExpression(); if (expressionDefinition != null) { // resolve properties before we create the processor - ProcessorDefinitionHelper.resolvePropertyPlaceholders(routeContext, expressionDefinition); + ProcessorDefinitionHelper.resolvePropertyPlaceholders(routeContext.getCamelContext(), expressionDefinition); // resolve constant fields (eg Exchange.FILE_NAME) ProcessorDefinitionHelper.resolveKnownConstantFields(expressionDefinition); @@ -477,7 +477,7 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type> preCreateProcessor(); // resolve properties before we create the processor - ProcessorDefinitionHelper.resolvePropertyPlaceholders(routeContext, this); + ProcessorDefinitionHelper.resolvePropertyPlaceholders(routeContext.getCamelContext(), this); // resolve constant fields (eg Exchange.FILE_NAME) ProcessorDefinitionHelper.resolveKnownConstantFields(this); @@ -489,7 +489,7 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type> ExpressionDefinition expressionDefinition = exp.getExpression(); if (expressionDefinition != null) { // resolve properties before we create the processor - ProcessorDefinitionHelper.resolvePropertyPlaceholders(routeContext, expressionDefinition); + ProcessorDefinitionHelper.resolvePropertyPlaceholders(routeContext.getCamelContext(), expressionDefinition); // resolve constant fields (eg Exchange.FILE_NAME) ProcessorDefinitionHelper.resolveKnownConstantFields(expressionDefinition); http://git-wip-us.apache.org/repos/asf/camel/blob/295e0d62/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinitionHelper.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinitionHelper.java b/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinitionHelper.java index 6610723..47cae82 100644 --- a/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinitionHelper.java +++ b/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinitionHelper.java @@ -27,6 +27,7 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.ScheduledExecutorService; import javax.xml.namespace.QName; +import org.apache.camel.CamelContext; import org.apache.camel.Exchange; import org.apache.camel.spi.ExecutorServiceManager; import org.apache.camel.spi.RouteContext; @@ -556,8 +557,26 @@ public final class ProcessorDefinitionHelper { * @throws Exception is thrown if property placeholders was used and there was an error resolving them * @see org.apache.camel.CamelContext#resolvePropertyPlaceholders(String) * @see org.apache.camel.component.properties.PropertiesComponent + * @deprecated use {@link #resolvePropertyPlaceholders(org.apache.camel.CamelContext, Object)} */ + @Deprecated public static void resolvePropertyPlaceholders(RouteContext routeContext, Object definition) throws Exception { + resolvePropertyPlaceholders(routeContext.getCamelContext(), definition); + } + + /** + * Inspects the given definition and resolves any property placeholders from its properties. + * <p/> + * This implementation will check all the getter/setter pairs on this instance and for all the values + * (which is a String type) will be property placeholder resolved. + * + * @param camelContext the Camel context + * @param definition the definition + * @throws Exception is thrown if property placeholders was used and there was an error resolving them + * @see org.apache.camel.CamelContext#resolvePropertyPlaceholders(String) + * @see org.apache.camel.component.properties.PropertiesComponent + */ + public static void resolvePropertyPlaceholders(CamelContext camelContext, Object definition) throws Exception { LOG.trace("Resolving property placeholders for: {}", definition); // find all getter/setter which we can use for property placeholders @@ -577,12 +596,12 @@ public final class ProcessorDefinitionHelper { Object value = processorDefinition.getOtherAttributes().get(key); if (value != null && value instanceof String) { // enforce a properties component to be created if none existed - CamelContextHelper.lookupPropertiesComponent(routeContext.getCamelContext(), true); + CamelContextHelper.lookupPropertiesComponent(camelContext, true); // value must be enclosed with placeholder tokens String s = (String) value; - String prefixToken = routeContext.getCamelContext().getPropertyPrefixToken(); - String suffixToken = routeContext.getCamelContext().getPropertySuffixToken(); + String prefixToken = camelContext.getPropertyPrefixToken(); + String suffixToken = camelContext.getPropertySuffixToken(); if (prefixToken == null) { throw new IllegalArgumentException("Property with name [" + local + "] uses property placeholders; however, no properties component is configured."); } @@ -610,10 +629,10 @@ public final class ProcessorDefinitionHelper { if (value instanceof String) { // value must be a String, as a String is the key for a property placeholder String text = (String) value; - text = routeContext.getCamelContext().resolvePropertyPlaceholders(text); + text = camelContext.resolvePropertyPlaceholders(text); if (text != value) { // invoke setter as the text has changed - boolean changed = IntrospectionSupport.setProperty(routeContext.getCamelContext().getTypeConverter(), definition, name, text); + boolean changed = IntrospectionSupport.setProperty(camelContext.getTypeConverter(), definition, name, text); if (!changed) { throw new IllegalArgumentException("No setter to set property: " + name + " to: " + text + " on: " + definition); } http://git-wip-us.apache.org/repos/asf/camel/blob/295e0d62/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java b/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java index f378222..e869779 100644 --- a/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java +++ b/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java @@ -252,6 +252,9 @@ public final class RouteDefinitionHelper { List<InterceptSendToEndpointDefinition> interceptSendToEndpointDefinitions, List<OnCompletionDefinition> onCompletions) { + // init the route inputs + initRouteInputs(context, route.getInputs()); + // abstracts is the cross cutting concerns List<ProcessorDefinition<?>> abstracts = new ArrayList<ProcessorDefinition<?>>(); @@ -326,6 +329,17 @@ public final class RouteDefinitionHelper { } } + private static void initRouteInputs(CamelContext camelContext, List<FromDefinition> inputs) { + // resolve property placeholders on route inputs which hasn't been done yet + for (FromDefinition input : inputs) { + try { + ProcessorDefinitionHelper.resolvePropertyPlaceholders(camelContext, input); + } catch (Exception e) { + throw ObjectHelper.wrapRuntimeCamelException(e); + } + } + } + private static void initParentAndErrorHandlerBuilder(ModelCamelContext context, RouteDefinition route, List<ProcessorDefinition<?>> abstracts, List<OnExceptionDefinition> onExceptions) { http://git-wip-us.apache.org/repos/asf/camel/blob/295e0d62/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesRouteFromTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesRouteFromTest.java b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesRouteFromTest.java new file mode 100644 index 0000000..f3accc9 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesRouteFromTest.java @@ -0,0 +1,63 @@ +/** + * 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.component.properties; + +import org.apache.camel.CamelContext; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.model.ModelHelper; +import org.apache.camel.model.ProcessorDefinition; +import org.apache.camel.model.RoutesDefinition; +import org.apache.camel.model.SendDefinition; + +/** + * + */ +public class PropertiesRouteFromTest extends ContextTestSupport { + + public void testPropertiesRouteFrom() throws Exception { + ProcessorDefinition out = context.getRouteDefinition("foo").getOutputs().get(0); + assertEquals("mock:result", ((SendDefinition) out).getUri()); + + String uri = context.getRouteDefinition("foo").getInputs().get(0).getUri(); + assertEquals("direct:cool", uri); + + // use a routes definition to dump the routes + String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("foo")); + assertTrue(xml.contains("<from uri=\"direct:cool\"/>")); + assertTrue(xml.contains("<to uri=\"mock:result\"")); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("{{cool.start}}").routeId("foo") + .to("{{cool.end}}"); + } + }; + } + + @Override + protected CamelContext createCamelContext() throws Exception { + CamelContext context = super.createCamelContext(); + context.addComponent("properties", new PropertiesComponent("classpath:org/apache/camel/component/properties/myproperties.properties")); + return context; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/295e0d62/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesRouteFromTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesRouteFromTest.java b/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesRouteFromTest.java new file mode 100644 index 0000000..95e6951 --- /dev/null +++ b/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesRouteFromTest.java @@ -0,0 +1,34 @@ +/** + * 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.component.properties; + +import org.apache.camel.CamelContext; + +import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext; + +/** + * Using Spring property placeholder instead of Camel properties component. + * + * @version + */ +public class SpringPropertiesRouteFromTest extends PropertiesRouteFromTest { + + protected CamelContext createCamelContext() throws Exception { + return createSpringCamelContext(this, "org/apache/camel/component/properties/SpringPropertiesRouteFromTest.xml"); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/295e0d62/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesRouteFromTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesRouteFromTest.xml b/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesRouteFromTest.xml new file mode 100644 index 0000000..0bd4a0b --- /dev/null +++ b/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesRouteFromTest.xml @@ -0,0 +1,34 @@ +<?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" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + "> + + <camelContext id="myCamel" xmlns="http://camel.apache.org/schema/spring"> + <propertyPlaceholder id="properties" location="org/apache/camel/component/properties/myproperties.properties"/> + + <route id="foo"> + <from uri="{{cool.start}}"/> + <to uri="{{cool.end}}"/> + </route> + </camelContext> + +</beans>
