Skip to content

Commit 8606b77

Browse files
committed
Implements getEnvironmentProperty to allow environment variable configuration
This commit add unit test and documentation regarding the improvement Thanks Eugen for the review
1 parent c73bfb8 commit 8606b77

File tree

5 files changed

+102
-4
lines changed

5 files changed

+102
-4
lines changed

docs/asciidoc/developer-manual.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ For a core configuration guide check
289289
https://cwiki.apache.org/confluence/display/OFBIZ/Framework+Configuration+Guide[the OFBiz configuration Guide]
290290
(some points are not up to date).
291291

292+
include::../../framework/base/src/docs/asciidoc/configuration.adoc[leveloffset=+2]
292293

293294
include::../../framework/base/src/docs/asciidoc/email.adoc[leveloffset=+2]
294295

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
////
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
////
19+
= Configuration by environment variable
20+
21+
https://issues.apache.org/jira/browse/OFBIZ-9498[OFBIZ-9498] introduce a new way to configure an OFBiz instance at
22+
launch using environment variable.
23+
24+
This is a first intermediate step of the task, that allow configuration without modifying
25+
source code of a distributed OFBiz artefact.
26+
27+
Currently, some configurations are designed to be set through environment variable :
28+
29+
* framework/entity/config/entityengine.xml for database access credentials
30+
** jdbc-uri="${env:OFB_POSTGRES_DB:jdbc:postgresql://127.0.0.1/ofbiz}"
31+
** jdbc-username="${env:OFB_POSTGRES_USER:ofbiz}"
32+
** jdbc-password="${env:OFB_POSTGRES_PASS:ofbiz}"
33+
34+
* framework/common/config/general.properties
35+
** unique.instanceId=${env:OFB_INSTANCE_ID:ofbiz1}
36+
37+
Others could be designed using the same notation `${env:ENV_NAME:DEFAULT_VALUE}`, such as :
38+
39+
* any property in the `*.properties` files
40+
* In `serviceengine.xml`, for new specific service engine, to allow api access credential configuration by the Ops.
41+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
////
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
////
19+
= Configuration System
20+
21+
include::_include/env-config.adoc[leveloffset=+2]

framework/base/src/main/java/org/apache/ofbiz/base/util/UtilProperties.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -991,10 +991,11 @@ public static Properties xmlToProperties(InputStream in, Locale locale, Properti
991991
/**
992992
* Resolve a property to check if it contains an environment variable
993993
* represented by ${env:ENV_VARIABLE:DEFAULT_VALUE}
994-
* @param value
995-
* @return
994+
* @param env : map that contains available env variables
995+
* @param value : the property to resolve
996+
* @return resolved value
996997
*/
997-
public static String getEnvironmentProperty(String value) {
998+
public static String getEnvironmentProperty(Map<String, String> env, String value) {
998999
if (value != null) {
9991000
if (value.startsWith("${env:") && value.endsWith("}")) {
10001001
String envNameWithDefault = value.substring(6, value.length() - 1);
@@ -1004,7 +1005,7 @@ public static String getEnvironmentProperty(String value) {
10041005
environmentName = envNameWithDefault.substring(0, envNameWithDefault.indexOf(":"));
10051006
defaultValue = envNameWithDefault.substring(envNameWithDefault.indexOf(":") + 1);
10061007
}
1007-
String environmentValue = System.getenv(environmentName);
1008+
String environmentValue = env.get(environmentName);
10081009
if (environmentValue != null) {
10091010
return environmentValue;
10101011
}
@@ -1017,6 +1018,16 @@ public static String getEnvironmentProperty(String value) {
10171018
return value;
10181019
}
10191020

1021+
/**
1022+
* Resolve a property to check if it contains an environment variable
1023+
* represented by ${env:ENV_VARIABLE:DEFAULT_VALUE}
1024+
* @param value : the property to resolve
1025+
* @return resolved value
1026+
*/
1027+
public static String getEnvironmentProperty(String value) {
1028+
return getEnvironmentProperty(System.getenv(), value);
1029+
}
1030+
10201031
/** Custom ResourceBundle class. This class extends ResourceBundle
10211032
* to add custom bundle caching code and support for the OFBiz custom XML
10221033
* properties file format.

framework/base/src/test/java/org/apache/ofbiz/base/util/UtilPropertiesTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626
import java.io.IOException;
2727
import java.io.InputStream;
2828
import java.nio.charset.Charset;
29+
import java.util.HashMap;
2930
import java.util.Locale;
31+
import java.util.Map;
3032
import java.util.Properties;
3133

34+
import com.google.common.collect.ImmutableMap;
3235
import org.junit.Test;
3336

3437
public class UtilPropertiesTests {
@@ -78,4 +81,25 @@ private Properties xmlToProperties(String separator) throws IOException {
7881
return UtilProperties.xmlToProperties(in, locale, null);
7982
}
8083
}
84+
85+
/**
86+
* Environment Variable property retrieval
87+
* Test that default value is retrieved if no variable set.
88+
*/
89+
@Test
90+
public void testEnvironmentPropertyDefaultValue() {
91+
String value = UtilProperties.getEnvironmentProperty(new HashMap<>(), "${env:ENV_VARIABLE:DEFAULT_VALUE}");
92+
assertEquals("DEFAULT_VALUE", value);
93+
}
94+
95+
/**
96+
* Environment Variable property retrieval
97+
* Test that defined value is retrieved if env variable set.
98+
*/
99+
@Test
100+
public void testEnvironmentPropertyMatch() {
101+
Map<String, String> env = ImmutableMap.of("ENV_VARIABLE", "SET_VALUE");
102+
String value = UtilProperties.getEnvironmentProperty(env, "${env:ENV_VARIABLE:DEFAULT_VALUE}");
103+
assertEquals("SET_VALUE", value);
104+
}
81105
}

0 commit comments

Comments
 (0)