Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAMEL-13833: Properties component - Fallback to ENV should replace dots with underscores #3085

Merged
merged 1 commit into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static String newStringFromBytes(byte[] bytes, int start, int length) {
* object and returns that. If the passed <code>in</code> is already an
* instance of {@link BufferedInputStream} returns the same passed
* <code>in</code> reference as is (avoiding double wrapping).
*
*
* @param in the wrapee to be used for the buffering support
* @return the passed <code>in</code> decorated through a
* {@link BufferedInputStream} object as wrapper
Expand All @@ -111,7 +111,7 @@ public static BufferedInputStream buffered(InputStream in) {
* object and returns that. If the passed <code>out</code> is already an
* instance of {@link BufferedOutputStream} returns the same passed
* <code>out</code> reference as is (avoiding double wrapping).
*
*
* @param out the wrapee to be used for the buffering support
* @return the passed <code>out</code> decorated through a
* {@link BufferedOutputStream} object as wrapper
Expand All @@ -126,7 +126,7 @@ public static BufferedOutputStream buffered(OutputStream out) {
* and returns that. If the passed <code>reader</code> is already an
* instance of {@link BufferedReader} returns the same passed
* <code>reader</code> reference as is (avoiding double wrapping).
*
*
* @param reader the wrapee to be used for the buffering support
* @return the passed <code>reader</code> decorated through a
* {@link BufferedReader} object as wrapper
Expand All @@ -141,7 +141,7 @@ public static BufferedReader buffered(Reader reader) {
* and returns that. If the passed <code>writer</code> is already an
* instance of {@link BufferedWriter} returns the same passed
* <code>writer</code> reference as is (avoiding double wrapping).
*
*
* @param writer the wrapee to be used for the buffering support
* @return the passed <code>writer</code> decorated through a
* {@link BufferedWriter} object as wrapper
Expand Down Expand Up @@ -430,7 +430,7 @@ public static void close(Closeable closeable) {

/**
* Closes the given resources if they are available.
*
*
* @param closeables the objects to close
*/
public static void close(Closeable... closeables) {
Expand Down Expand Up @@ -491,7 +491,7 @@ public static String loadText(InputStream in) throws IOException {

/**
* Get the charset name from the content type string
*
*
* @param contentType
* @return the charset name, or <tt>UTF-8</tt> if no found
*/
Expand Down Expand Up @@ -539,10 +539,16 @@ public static String lookupEnvironmentVariable(String key) {
// lookup OS env with upper case key
String upperKey = key.toUpperCase();
String value = System.getenv(upperKey);
// some OS do not support dashes in keys, so replace with underscore

if (value == null) {
String noDashKey = upperKey.replace('-', '_');
value = System.getenv(noDashKey);
// some OS do not support dashes in keys, so replace with underscore
String normalizedKey = upperKey.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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.util;

import org.junit.Assert;
import org.junit.Test;

public class IOHelperTest extends Assert {
@Test
public void testLookupEnvironmentVariable() throws Exception {
assertEquals("8081", IOHelper.lookupEnvironmentVariable("FOO_SERVICE_PORT"));
assertEquals("8081", IOHelper.lookupEnvironmentVariable("foo-service.port"));
assertEquals("8081", IOHelper.lookupEnvironmentVariable("foo-service-port"));
assertEquals("8081", IOHelper.lookupEnvironmentVariable("foo.service.port"));
}
}