Skip to content

Commit d83922a

Browse files
committed
Implements getEnvironmentProperty to allow environment variable configuration
This will enable configuration of one OFBiz instance without modifying source code, using environment variables. Available in : * property files * serviceengine.xml * entityengine.xml * build.gradle (native)
1 parent a7d80fd commit d83922a

File tree

7 files changed

+52
-12
lines changed

7 files changed

+52
-12
lines changed

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public static String getPropertyValue(String resource, String name) {
285285
} catch (Exception e) {
286286
Debug.logInfo(e, MODULE);
287287
}
288-
return value == null ? "" : value.trim();
288+
return value == null ? "" : getEnvironmentProperty(value.trim());
289289
}
290290

291291
/**
@@ -988,6 +988,35 @@ public static Properties xmlToProperties(InputStream in, Locale locale, Properti
988988
return properties;
989989
}
990990

991+
/**
992+
* Resolve a property to check if it contains an environment variable
993+
* represented by ${env:ENV_VARIABLE:DEFAULT_VALUE}
994+
* @param value
995+
* @return
996+
*/
997+
public static String getEnvironmentProperty(String value) {
998+
if (value == null) return "";
999+
value = value.trim();
1000+
if (value.startsWith("${env:") && value.endsWith("}")) {
1001+
String envNameWithDefault = value.substring(6, value.length() - 1);
1002+
String environmentName = envNameWithDefault;
1003+
String defaultValue = null;
1004+
if (envNameWithDefault.contains(":")) {
1005+
environmentName = envNameWithDefault.substring(0, envNameWithDefault.indexOf(":"));
1006+
defaultValue = envNameWithDefault.substring(envNameWithDefault.indexOf(":") + 1, envNameWithDefault.length());
1007+
}
1008+
String environmentValue = System.getenv(environmentName);
1009+
if (environmentValue != null) {
1010+
return environmentValue;
1011+
}
1012+
if (defaultValue != null) {
1013+
return defaultValue;
1014+
}
1015+
return "";
1016+
}
1017+
return value;
1018+
}
1019+
9911020
/** Custom ResourceBundle class. This class extends ResourceBundle
9921021
* to add custom bundle caching code and support for the OFBiz custom XML
9931022
* properties file format.

framework/common/config/general.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
###############################################################################
1919

2020
# -- unique instance id (20 char max)
21-
unique.instanceId=ofbiz1
21+
unique.instanceId=${env:OFB_INSTANCE_ID:ofbiz1}
2222

2323
# -- the default currency to use for prices, etc
2424
currency.uom.id.default=USD

framework/entity/config/entityengine.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,9 @@ access. For a detailed description see the core/docs/entityconfig.html file.
489489
<read-data reader-name="ext-demo"/>
490490
<inline-jdbc
491491
jdbc-driver="org.postgresql.Driver"
492-
jdbc-uri="jdbc:postgresql://127.0.0.1/ofbiz"
493-
jdbc-username="ofbiz"
494-
jdbc-password="ofbiz"
492+
jdbc-uri="${env:OFB_POSTGRES_DB:jdbc:postgresql://127.0.0.1/ofbiz}"
493+
jdbc-username="${env:OFB_POSTGRES_USER:ofbiz}"
494+
jdbc-password="${env:OFB_POSTGRES_PASS:ofbiz}"
495495
isolation-level="ReadCommitted"
496496
pool-minsize="2"
497497
pool-maxsize="250"

framework/entity/src/main/java/org/apache/ofbiz/entity/config/model/InlineJdbc.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.ofbiz.entity.config.model;
2020

2121
import org.apache.ofbiz.base.lang.ThreadSafe;
22+
import org.apache.ofbiz.base.util.UtilProperties;
2223
import org.apache.ofbiz.entity.GenericEntityConfException;
2324
import org.w3c.dom.Element;
2425

@@ -60,16 +61,18 @@ public final class InlineJdbc extends JdbcElement {
6061
}
6162
this.jdbcDriver = jdbcDriver;
6263
String jdbcUri = element.getAttribute("jdbc-uri").intern();
64+
jdbcUri = UtilProperties.getEnvironmentProperty(jdbcUri);
6365
if (jdbcUri.isEmpty()) {
6466
throw new GenericEntityConfException("<inline-jdbc> element jdbc-uri attribute is empty" + lineNumberText);
6567
}
6668
this.jdbcUri = jdbcUri;
6769
String jdbcUsername = element.getAttribute("jdbc-username").intern();
70+
jdbcUsername = UtilProperties.getEnvironmentProperty(jdbcUsername);
6871
if (jdbcUsername.isEmpty()) {
6972
throw new GenericEntityConfException("<inline-jdbc> element jdbc-username attribute is empty" + lineNumberText);
7073
}
7174
this.jdbcUsername = jdbcUsername;
72-
this.jdbcPassword = element.getAttribute("jdbc-password").intern();
75+
this.jdbcPassword = UtilProperties.getEnvironmentProperty(element.getAttribute("jdbc-password").intern());
7376
this.jdbcPasswordLookup = element.getAttribute("jdbc-password-lookup").intern();
7477
String poolMaxsize = element.getAttribute("pool-maxsize");
7578
if (poolMaxsize.isEmpty()) {

framework/service/src/main/java/org/apache/ofbiz/service/config/model/Parameter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.ofbiz.service.config.model;
2020

2121
import org.apache.ofbiz.base.lang.ThreadSafe;
22+
import org.apache.ofbiz.base.util.UtilProperties;
2223
import org.apache.ofbiz.service.config.ServiceConfigException;
2324
import org.w3c.dom.Element;
2425

@@ -38,6 +39,7 @@ public final class Parameter {
3839
}
3940
this.name = name;
4041
String value = parameterElement.getAttribute("value").intern();
42+
value = UtilProperties.getEnvironmentProperty(value);
4143
if (value.isEmpty()) {
4244
throw new ServiceConfigException("<parameter> element value attribute is empty");
4345
}

framework/service/src/main/java/org/apache/ofbiz/service/config/model/Server.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.ofbiz.service.config.model;
2020

2121
import org.apache.ofbiz.base.lang.ThreadSafe;
22+
import org.apache.ofbiz.base.util.UtilProperties;
2223
import org.apache.ofbiz.service.config.ServiceConfigException;
2324
import org.w3c.dom.Element;
2425

@@ -40,16 +41,19 @@ public final class Server {
4041

4142
Server(Element serverElement) throws ServiceConfigException {
4243
String jndiServerName = serverElement.getAttribute("jndi-server-name").intern();
44+
jndiServerName = UtilProperties.getEnvironmentProperty(jndiServerName);
4345
if (jndiServerName.isEmpty()) {
4446
throw new ServiceConfigException("<server> element jndi-server-name attribute is empty");
4547
}
4648
this.jndiServerName = jndiServerName;
4749
String jndiName = serverElement.getAttribute("jndi-name").intern();
50+
jndiName = UtilProperties.getEnvironmentProperty(jndiName);
4851
if (jndiName.isEmpty()) {
4952
throw new ServiceConfigException("<server> element jndi-name attribute is empty");
5053
}
5154
this.jndiName = jndiName;
5255
String topicQueue = serverElement.getAttribute("topic-queue").intern();
56+
topicQueue = UtilProperties.getEnvironmentProperty(topicQueue);
5357
if (topicQueue.isEmpty()) {
5458
throw new ServiceConfigException("<server> element topic-queue attribute is empty");
5559
}
@@ -59,11 +63,11 @@ public final class Server {
5963
throw new ServiceConfigException("<server> element type attribute is empty");
6064
}
6165
this.type = type;
62-
this.username = serverElement.getAttribute("username").intern();
63-
this.password = serverElement.getAttribute("password").intern();
64-
this.clientId = serverElement.getAttribute("client-id").intern();
65-
this.listen = "true".equals(serverElement.getAttribute("listen"));
66-
this.listenerClass = serverElement.getAttribute("listener-class").intern();
66+
this.username = UtilProperties.getEnvironmentProperty(serverElement.getAttribute("username").intern());
67+
this.password = UtilProperties.getEnvironmentProperty(serverElement.getAttribute("password").intern());
68+
this.clientId = UtilProperties.getEnvironmentProperty(serverElement.getAttribute("client-id").intern());
69+
this.listen = "true".equals(UtilProperties.getEnvironmentProperty(serverElement.getAttribute("listen")));
70+
this.listenerClass = UtilProperties.getEnvironmentProperty(serverElement.getAttribute("listener-class").intern());
6771
}
6872

6973
public String getClientId() {

framework/service/src/main/java/org/apache/ofbiz/service/mail/JavaMailContainer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.apache.ofbiz.base.start.StartupCommand;
4949
import org.apache.ofbiz.base.util.Debug;
5050
import org.apache.ofbiz.base.util.UtilMisc;
51+
import org.apache.ofbiz.base.util.UtilProperties;
5152
import org.apache.ofbiz.base.util.UtilValidate;
5253
import org.apache.ofbiz.entity.Delegator;
5354
import org.apache.ofbiz.entity.DelegatorFactory;
@@ -147,7 +148,8 @@ protected Session makeSession(Configuration.Property client) {
147148
Map<String, Configuration.Property> clientProps = client.properties();
148149
if (clientProps != null) {
149150
for (Configuration.Property p: clientProps.values()) {
150-
props.setProperty(p.name().toLowerCase(Locale.getDefault()), p.value());
151+
props.setProperty(p.name().toLowerCase(Locale.getDefault()),
152+
UtilProperties.getEnvironmentProperty(p.value()));
151153
}
152154
}
153155
return Session.getInstance(props);

0 commit comments

Comments
 (0)