This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit a6175cf19ed63b8522ede9131f53e4b324f256ad Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Wed Aug 4 15:50:23 2021 +0200 CAMEL-16788: route templates (aka kamelets) should always use local parameter value if present, instead of picking up ENV variable that may have a naming clash when using host, port, as names. --- .../properties/DefaultPropertiesParser.java | 12 +++++- .../component/properties/PropertiesComponent.java | 4 +- .../camel/builder/RouteTemplateEnvClashTest.java | 50 ++++++++++++++++++++++ 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/core/camel-base/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java b/core/camel-base/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java index e0b9e1d..cf3b46f 100644 --- a/core/camel-base/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java +++ b/core/camel-base/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java @@ -17,6 +17,7 @@ package org.apache.camel.component.properties; import java.util.HashSet; +import java.util.Properties; import java.util.Set; import org.apache.camel.spi.PropertiesFunction; @@ -308,6 +309,15 @@ public class DefaultPropertiesParser implements PropertiesParser { String value = null; + // favour local properties if + Properties local = propertiesComponent.getLocalProperties(); + if (local != null) { + value = local.getProperty(key); + if (value != null) { + log.debug("Found local property: {} with value: {} to be used.", key, value); + } + } + // override is the default mode for ENV int envMode = propertiesComponent != null ? propertiesComponent.getEnvironmentVariableMode() @@ -316,7 +326,7 @@ public class DefaultPropertiesParser implements PropertiesParser { int sysMode = propertiesComponent != null ? propertiesComponent.getSystemPropertiesMode() : PropertiesComponent.SYSTEM_PROPERTIES_MODE_OVERRIDE; - if (envMode == PropertiesComponent.ENVIRONMENT_VARIABLES_MODE_OVERRIDE) { + if (value == null && envMode == PropertiesComponent.ENVIRONMENT_VARIABLES_MODE_OVERRIDE) { value = lookupEnvironmentVariable(key); if (value != null) { log.debug("Found an OS environment property: {} with value: {} to be used.", key, value); diff --git a/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java b/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java index ea712e2..4e32fe2 100644 --- a/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java +++ b/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java @@ -516,7 +516,7 @@ public class PropertiesComponent extends ServiceSupport * * The default mode (override) is to use system properties if present, and override any existing properties. * - * OS environment variable mode is checked before JVM system property mode + * OS environment variable mode is checked before JVM system property mode. * * @see #SYSTEM_PROPERTIES_MODE_NEVER * @see #SYSTEM_PROPERTIES_MODE_FALLBACK @@ -536,7 +536,7 @@ public class PropertiesComponent extends ServiceSupport * * The default mode (override) is to use OS environment variables if present, and override any existing properties. * - * OS environment variable mode is checked before JVM system property mode + * OS environment variable mode is checked before JVM system property mode. * * @see #ENVIRONMENT_VARIABLES_MODE_NEVER * @see #ENVIRONMENT_VARIABLES_MODE_FALLBACK diff --git a/core/camel-core/src/test/java/org/apache/camel/builder/RouteTemplateEnvClashTest.java b/core/camel-core/src/test/java/org/apache/camel/builder/RouteTemplateEnvClashTest.java new file mode 100644 index 0000000..f1abc83 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/builder/RouteTemplateEnvClashTest.java @@ -0,0 +1,50 @@ +/* + * 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.builder; + +import org.apache.camel.ContextTestSupport; +import org.junit.jupiter.api.Test; + +public class RouteTemplateEnvClashTest extends ContextTestSupport { + + @Test + public void testEnvClash() throws Exception { + // there is a ENV variable when testing with name: FOO_SERVICE_HOST (see pom.xml) + TemplatedRouteBuilder.builder(context, "myTemplate") + .parameter("foo-service-host", "mykamelet") + .routeId("myRoute") + .add(); + + getMockEndpoint("mock:mykamelet:4444").expectedMessageCount(1); + + template.sendBody("direct:foo", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + routeTemplate("myTemplate").templateParameter("foo-service-host").templateParameter("foo-service-port", "4444") + .from("direct:foo") + .to("mock:{{foo-service-host}}:{{foo-service-port}}"); + } + }; + } +}