From d83922a19f73d1554415067c9c7f268a9551b27a Mon Sep 17 00:00:00 2001 From: Gil Portenseigne Date: Tue, 23 Nov 2021 10:25:30 +0100 Subject: [PATCH] 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) --- .../ofbiz/base/util/UtilProperties.java | 31 ++++++++++++++++++- framework/common/config/general.properties | 2 +- framework/entity/config/entityengine.xml | 6 ++-- .../ofbiz/entity/config/model/InlineJdbc.java | 5 ++- .../ofbiz/service/config/model/Parameter.java | 2 ++ .../ofbiz/service/config/model/Server.java | 14 ++++++--- .../ofbiz/service/mail/JavaMailContainer.java | 4 ++- 7 files changed, 52 insertions(+), 12 deletions(-) diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilProperties.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilProperties.java index 8b1f313ae7c..07ba4dac45f 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilProperties.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilProperties.java @@ -285,7 +285,7 @@ public static String getPropertyValue(String resource, String name) { } catch (Exception e) { Debug.logInfo(e, MODULE); } - return value == null ? "" : value.trim(); + return value == null ? "" : getEnvironmentProperty(value.trim()); } /** @@ -988,6 +988,35 @@ public static Properties xmlToProperties(InputStream in, Locale locale, Properti return properties; } + /** + * Resolve a property to check if it contains an environment variable + * represented by ${env:ENV_VARIABLE:DEFAULT_VALUE} + * @param value + * @return + */ + public static String getEnvironmentProperty(String value) { + if (value == null) return ""; + value = value.trim(); + if (value.startsWith("${env:") && value.endsWith("}")) { + String envNameWithDefault = value.substring(6, value.length() - 1); + String environmentName = envNameWithDefault; + String defaultValue = null; + if (envNameWithDefault.contains(":")) { + environmentName = envNameWithDefault.substring(0, envNameWithDefault.indexOf(":")); + defaultValue = envNameWithDefault.substring(envNameWithDefault.indexOf(":") + 1, envNameWithDefault.length()); + } + String environmentValue = System.getenv(environmentName); + if (environmentValue != null) { + return environmentValue; + } + if (defaultValue != null) { + return defaultValue; + } + return ""; + } + return value; + } + /** Custom ResourceBundle class. This class extends ResourceBundle * to add custom bundle caching code and support for the OFBiz custom XML * properties file format. diff --git a/framework/common/config/general.properties b/framework/common/config/general.properties index 8293fc4876c..bf486b28b2d 100644 --- a/framework/common/config/general.properties +++ b/framework/common/config/general.properties @@ -18,7 +18,7 @@ ############################################################################### # -- unique instance id (20 char max) -unique.instanceId=ofbiz1 +unique.instanceId=${env:OFB_INSTANCE_ID:ofbiz1} # -- the default currency to use for prices, etc currency.uom.id.default=USD diff --git a/framework/entity/config/entityengine.xml b/framework/entity/config/entityengine.xml index e9abf076320..47ebc21639e 100644 --- a/framework/entity/config/entityengine.xml +++ b/framework/entity/config/entityengine.xml @@ -489,9 +489,9 @@ access. For a detailed description see the core/docs/entityconfig.html file. element jdbc-uri attribute is empty" + lineNumberText); } this.jdbcUri = jdbcUri; String jdbcUsername = element.getAttribute("jdbc-username").intern(); + jdbcUsername = UtilProperties.getEnvironmentProperty(jdbcUsername); if (jdbcUsername.isEmpty()) { throw new GenericEntityConfException(" element jdbc-username attribute is empty" + lineNumberText); } this.jdbcUsername = jdbcUsername; - this.jdbcPassword = element.getAttribute("jdbc-password").intern(); + this.jdbcPassword = UtilProperties.getEnvironmentProperty(element.getAttribute("jdbc-password").intern()); this.jdbcPasswordLookup = element.getAttribute("jdbc-password-lookup").intern(); String poolMaxsize = element.getAttribute("pool-maxsize"); if (poolMaxsize.isEmpty()) { diff --git a/framework/service/src/main/java/org/apache/ofbiz/service/config/model/Parameter.java b/framework/service/src/main/java/org/apache/ofbiz/service/config/model/Parameter.java index b3fe7eff2c9..a22a68a1380 100644 --- a/framework/service/src/main/java/org/apache/ofbiz/service/config/model/Parameter.java +++ b/framework/service/src/main/java/org/apache/ofbiz/service/config/model/Parameter.java @@ -19,6 +19,7 @@ package org.apache.ofbiz.service.config.model; import org.apache.ofbiz.base.lang.ThreadSafe; +import org.apache.ofbiz.base.util.UtilProperties; import org.apache.ofbiz.service.config.ServiceConfigException; import org.w3c.dom.Element; @@ -38,6 +39,7 @@ public final class Parameter { } this.name = name; String value = parameterElement.getAttribute("value").intern(); + value = UtilProperties.getEnvironmentProperty(value); if (value.isEmpty()) { throw new ServiceConfigException(" element value attribute is empty"); } diff --git a/framework/service/src/main/java/org/apache/ofbiz/service/config/model/Server.java b/framework/service/src/main/java/org/apache/ofbiz/service/config/model/Server.java index 94bb716d162..6b5299bdcaa 100644 --- a/framework/service/src/main/java/org/apache/ofbiz/service/config/model/Server.java +++ b/framework/service/src/main/java/org/apache/ofbiz/service/config/model/Server.java @@ -19,6 +19,7 @@ package org.apache.ofbiz.service.config.model; import org.apache.ofbiz.base.lang.ThreadSafe; +import org.apache.ofbiz.base.util.UtilProperties; import org.apache.ofbiz.service.config.ServiceConfigException; import org.w3c.dom.Element; @@ -40,16 +41,19 @@ public final class Server { Server(Element serverElement) throws ServiceConfigException { String jndiServerName = serverElement.getAttribute("jndi-server-name").intern(); + jndiServerName = UtilProperties.getEnvironmentProperty(jndiServerName); if (jndiServerName.isEmpty()) { throw new ServiceConfigException(" element jndi-server-name attribute is empty"); } this.jndiServerName = jndiServerName; String jndiName = serverElement.getAttribute("jndi-name").intern(); + jndiName = UtilProperties.getEnvironmentProperty(jndiName); if (jndiName.isEmpty()) { throw new ServiceConfigException(" element jndi-name attribute is empty"); } this.jndiName = jndiName; String topicQueue = serverElement.getAttribute("topic-queue").intern(); + topicQueue = UtilProperties.getEnvironmentProperty(topicQueue); if (topicQueue.isEmpty()) { throw new ServiceConfigException(" element topic-queue attribute is empty"); } @@ -59,11 +63,11 @@ public final class Server { throw new ServiceConfigException(" element type attribute is empty"); } this.type = type; - this.username = serverElement.getAttribute("username").intern(); - this.password = serverElement.getAttribute("password").intern(); - this.clientId = serverElement.getAttribute("client-id").intern(); - this.listen = "true".equals(serverElement.getAttribute("listen")); - this.listenerClass = serverElement.getAttribute("listener-class").intern(); + this.username = UtilProperties.getEnvironmentProperty(serverElement.getAttribute("username").intern()); + this.password = UtilProperties.getEnvironmentProperty(serverElement.getAttribute("password").intern()); + this.clientId = UtilProperties.getEnvironmentProperty(serverElement.getAttribute("client-id").intern()); + this.listen = "true".equals(UtilProperties.getEnvironmentProperty(serverElement.getAttribute("listen"))); + this.listenerClass = UtilProperties.getEnvironmentProperty(serverElement.getAttribute("listener-class").intern()); } public String getClientId() { diff --git a/framework/service/src/main/java/org/apache/ofbiz/service/mail/JavaMailContainer.java b/framework/service/src/main/java/org/apache/ofbiz/service/mail/JavaMailContainer.java index 08f8be371a7..68ee7a1b3bc 100644 --- a/framework/service/src/main/java/org/apache/ofbiz/service/mail/JavaMailContainer.java +++ b/framework/service/src/main/java/org/apache/ofbiz/service/mail/JavaMailContainer.java @@ -48,6 +48,7 @@ import org.apache.ofbiz.base.start.StartupCommand; import org.apache.ofbiz.base.util.Debug; import org.apache.ofbiz.base.util.UtilMisc; +import org.apache.ofbiz.base.util.UtilProperties; import org.apache.ofbiz.base.util.UtilValidate; import org.apache.ofbiz.entity.Delegator; import org.apache.ofbiz.entity.DelegatorFactory; @@ -147,7 +148,8 @@ protected Session makeSession(Configuration.Property client) { Map clientProps = client.properties(); if (clientProps != null) { for (Configuration.Property p: clientProps.values()) { - props.setProperty(p.name().toLowerCase(Locale.getDefault()), p.value()); + props.setProperty(p.name().toLowerCase(Locale.getDefault()), + UtilProperties.getEnvironmentProperty(p.value())); } } return Session.getInstance(props);