diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/debugger/DefaultBacklogDebugger.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/debugger/DefaultBacklogDebugger.java index be0c939544e7c..57bad2b4e6b54 100644 --- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/debugger/DefaultBacklogDebugger.java +++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/debugger/DefaultBacklogDebugger.java @@ -52,6 +52,7 @@ import org.apache.camel.support.MessageHelper; import org.apache.camel.support.service.ServiceHelper; import org.apache.camel.support.service.ServiceSupport; +import org.apache.camel.util.IOHelper; import org.apache.camel.util.StopWatch; import org.apache.camel.util.json.JsonObject; import org.slf4j.Logger; @@ -258,7 +259,7 @@ public void detach() { * the value of the system property {@link #SUSPEND_MODE_SYSTEM_PROP_NAME}, {@code false} by default. */ private static boolean resolveSuspendMode() { - final String value = System.getenv(SUSPEND_MODE_ENV_VAR_NAME); + final String value = IOHelper.lookupEnvironmentVariable(SUSPEND_MODE_ENV_VAR_NAME); return value == null ? Boolean.getBoolean(SUSPEND_MODE_SYSTEM_PROP_NAME) : Boolean.parseBoolean(value); } diff --git a/core/camel-base/src/main/java/org/apache/camel/component/properties/EnvPropertiesFunction.java b/core/camel-base/src/main/java/org/apache/camel/component/properties/EnvPropertiesFunction.java index 1d94cbd26b4e0..8bf0ff9f122c5 100644 --- a/core/camel-base/src/main/java/org/apache/camel/component/properties/EnvPropertiesFunction.java +++ b/core/camel-base/src/main/java/org/apache/camel/component/properties/EnvPropertiesFunction.java @@ -17,6 +17,7 @@ package org.apache.camel.component.properties; import org.apache.camel.spi.PropertiesFunction; +import org.apache.camel.util.IOHelper; import org.apache.camel.util.StringHelper; /** @@ -39,15 +40,7 @@ public String apply(String remainder) { defaultValue = StringHelper.after(remainder, ":"); } - // lookup OS environment variable using upper case key - key = key.toUpperCase(); - - String value = System.getenv(key); - // some OS do not support dashes in keys, so replace with underscore - if (value == null) { - String noDashKey = key.replace('-', '_'); - value = System.getenv(noDashKey); - } + String value = IOHelper.lookupEnvironmentVariable(key); return value != null ? value : defaultValue; } diff --git a/core/camel-base/src/main/java/org/apache/camel/component/properties/ServiceHostPropertiesFunction.java b/core/camel-base/src/main/java/org/apache/camel/component/properties/ServiceHostPropertiesFunction.java index 45336fd60d392..4c10798a6711d 100644 --- a/core/camel-base/src/main/java/org/apache/camel/component/properties/ServiceHostPropertiesFunction.java +++ b/core/camel-base/src/main/java/org/apache/camel/component/properties/ServiceHostPropertiesFunction.java @@ -16,9 +16,8 @@ */ package org.apache.camel.component.properties; -import java.util.Locale; - import org.apache.camel.spi.PropertiesFunction; +import org.apache.camel.util.IOHelper; import org.apache.camel.util.StringHelper; /** @@ -57,12 +56,8 @@ public String apply(String remainder) { // make sure to use upper case if (key != null) { - // make sure to use underscore as dash is not supported as ENV variables - key = key.toUpperCase(Locale.ENGLISH).replace('-', '_'); - // a service should have both the host and port defined - String host = System.getenv(key + HOST_PREFIX); - + String host = IOHelper.lookupEnvironmentVariable(key + HOST_PREFIX); if (host != null) { return host; } else { diff --git a/core/camel-base/src/main/java/org/apache/camel/component/properties/ServicePortPropertiesFunction.java b/core/camel-base/src/main/java/org/apache/camel/component/properties/ServicePortPropertiesFunction.java index 70d300c37766c..6a9a12bed9b20 100644 --- a/core/camel-base/src/main/java/org/apache/camel/component/properties/ServicePortPropertiesFunction.java +++ b/core/camel-base/src/main/java/org/apache/camel/component/properties/ServicePortPropertiesFunction.java @@ -16,9 +16,8 @@ */ package org.apache.camel.component.properties; -import java.util.Locale; - import org.apache.camel.spi.PropertiesFunction; +import org.apache.camel.util.IOHelper; import org.apache.camel.util.StringHelper; /** @@ -57,12 +56,8 @@ public String apply(String remainder) { // make sure to use upper case if (key != null) { - // make sure to use underscore as dash is not supported as ENV variables - key = key.toUpperCase(Locale.ENGLISH).replace('-', '_'); - // a service should have both the host and port defined - String port = System.getenv(key + PORT_PREFIX); - + String port = IOHelper.lookupEnvironmentVariable(key + PORT_PREFIX); if (port != null) { return port; } else { diff --git a/core/camel-base/src/main/java/org/apache/camel/component/properties/ServicePropertiesFunction.java b/core/camel-base/src/main/java/org/apache/camel/component/properties/ServicePropertiesFunction.java index fb2f3d137c9fa..4c59acbdc11ca 100644 --- a/core/camel-base/src/main/java/org/apache/camel/component/properties/ServicePropertiesFunction.java +++ b/core/camel-base/src/main/java/org/apache/camel/component/properties/ServicePropertiesFunction.java @@ -19,6 +19,7 @@ import java.util.Locale; import org.apache.camel.spi.PropertiesFunction; +import org.apache.camel.util.IOHelper; import org.apache.camel.util.StringHelper; /** @@ -57,8 +58,8 @@ public String apply(String remainder) { key = key.toUpperCase(Locale.ENGLISH).replace('-', '_'); // a service should have both the host and port defined - String host = System.getenv(key + HOST_PREFIX); - String port = System.getenv(key + PORT_PREFIX); + String host = IOHelper.lookupEnvironmentVariable(key + HOST_PREFIX); + String port = IOHelper.lookupEnvironmentVariable(key + PORT_PREFIX); if (host != null && port != null) { return host + ":" + port; diff --git a/core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java b/core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java index c4647e982c281..d3fbddf00c4a9 100644 --- a/core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java +++ b/core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java @@ -111,7 +111,7 @@ public static String toEnvVar(String name) { public static Optional lookupPropertyFromSysOrEnv(String name) { String answer = System.getProperty(name); if (answer == null) { - answer = System.getenv(toEnvVar(name)); + answer = IOHelper.lookupEnvironmentVariable(name); } return Optional.ofNullable(answer); diff --git a/core/camel-support/src/main/java/org/apache/camel/support/LanguageHelper.java b/core/camel-support/src/main/java/org/apache/camel/support/LanguageHelper.java index 03fd5350720af..f9cc5123e5798 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/LanguageHelper.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/LanguageHelper.java @@ -32,6 +32,7 @@ import org.apache.camel.RuntimeCamelException; import org.apache.camel.spi.ExchangeFormatter; import org.apache.camel.support.processor.DefaultExchangeFormatter; +import org.apache.camel.util.IOHelper; import org.apache.camel.util.TimeUtils; public final class LanguageHelper { @@ -169,18 +170,7 @@ public static ExchangeFormatter getOrCreateExchangeFormatter( * @return the environment variable value */ public static String sysenv(String name) { - String answer = null; - if (name != null) { - // lookup OS env with upper case key - name = name.toUpperCase(); - answer = System.getenv(name); - // some OS do not support dashes in keys, so replace with underscore - if (answer == null) { - String noDashKey = name.replace('-', '_'); - answer = System.getenv(noDashKey); - } - } - return answer; + return IOHelper.lookupEnvironmentVariable(name); } public static String escapeQuotes(String text) { diff --git a/core/camel-util/pom.xml b/core/camel-util/pom.xml index cde6fcb8ac092..43bcb1dd82fbd 100644 --- a/core/camel-util/pom.xml +++ b/core/camel-util/pom.xml @@ -266,6 +266,7 @@ myserver 8081 + mys3arn diff --git a/core/camel-util/src/main/java/org/apache/camel/util/FilePathResolver.java b/core/camel-util/src/main/java/org/apache/camel/util/FilePathResolver.java index 04672fdc9ec6c..bfc8d156f70b4 100644 --- a/core/camel-util/src/main/java/org/apache/camel/util/FilePathResolver.java +++ b/core/camel-util/src/main/java/org/apache/camel/util/FilePathResolver.java @@ -49,7 +49,7 @@ public static String resolvePath(String path) throws IllegalArgumentException { int pos = fun.indexOf("${env:"); if (pos != -1) { String key = fun.substring(pos + 6); - String value = System.getenv(key); + String value = IOHelper.lookupEnvironmentVariable(key); if (value != null) { path = path.replace("${env:" + key + "}", value); } @@ -57,7 +57,7 @@ public static String resolvePath(String path) throws IllegalArgumentException { pos = fun.indexOf("${env."); if (pos != -1) { String key = fun.substring(pos + 6); - String value = System.getenv(key); + String value = IOHelper.lookupEnvironmentVariable(key); if (value != null) { path = path.replace("${env." + key + "}", value); } diff --git a/core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java b/core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java index d57ae4d50b275..a1bbd9f4a3b1f 100644 --- a/core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java +++ b/core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java @@ -668,6 +668,15 @@ public static String normalizeCharset(String charset) { /** * Lookup the OS environment variable in a safe manner by using upper case keys and underscore instead of dash. + * + * At first lookup attempt is made without considering camelCase keys. The second lookup is converting camelCase to + * underscores. + * + * For example given an ENV variable in either format: - CAMEL_KAMELET_AWS_S3_SOURCE_BUCKETNAMEORARN=myArn - + * CAMEL_KAMELET_AWS_S3_SOURCE_BUCKET_NAME_OR_ARN=myArn + * + * Then the following keys can lookup both ENV formats above: - camel.kamelet.awsS3Source.bucketNameOrArn - + * camel.kamelet.aws-s3-source.bucketNameOrArn - camel.kamelet.aws-s3-source.bucket-name-or-arn */ public static String lookupEnvironmentVariable(String key) { // lookup OS env with upper case key @@ -684,6 +693,19 @@ public static String lookupEnvironmentVariable(String key) { value = System.getenv(normalizedKey); } + if (value == null) { + // camelCase keys should use underscore as separator + String caseKey = StringHelper.camelCaseToDash(key); + caseKey = caseKey.toUpperCase(); + // some OS do not support dashes in keys, so replace with underscore + String normalizedKey = caseKey.replace('-', '_'); + + // and replace dots with underscores so keys like my.key are + // translated to MY_KEY + normalizedKey = normalizedKey.replace('.', '_'); + + value = System.getenv(normalizedKey); + } return value; } diff --git a/core/camel-util/src/test/java/org/apache/camel/util/IOHelperTest.java b/core/camel-util/src/test/java/org/apache/camel/util/IOHelperTest.java index 997907a978cd0..c7c820ef04126 100644 --- a/core/camel-util/src/test/java/org/apache/camel/util/IOHelperTest.java +++ b/core/camel-util/src/test/java/org/apache/camel/util/IOHelperTest.java @@ -28,6 +28,10 @@ public void testLookupEnvironmentVariable() { assertEquals("8081", IOHelper.lookupEnvironmentVariable("foo-service.port")); assertEquals("8081", IOHelper.lookupEnvironmentVariable("foo-service-port")); assertEquals("8081", IOHelper.lookupEnvironmentVariable("foo.service.port")); + + assertEquals("mys3arn", IOHelper.lookupEnvironmentVariable("camel.kamelet.aws-s3-source.bucketNameOrArn")); + assertEquals("mys3arn", IOHelper.lookupEnvironmentVariable("camel.kamelet.aws-s3-source.bucket-name-or-arn")); + assertEquals("mys3arn", IOHelper.lookupEnvironmentVariable("camel.kamelet.awsS3Source.bucketNameOrArn")); } @Test