Skip to content

Commit

Permalink
Implements getEnvironmentProperty to allow environment variable confi…
Browse files Browse the repository at this point in the history
…guration

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)
  • Loading branch information
gilPts committed Nov 26, 2021
1 parent a58dd36 commit 23b3a27
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -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) {
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);
}
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.
Expand Down
2 changes: 1 addition & 1 deletion framework/common/config/general.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions framework/entity/config/entityengine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,9 @@ access. For a detailed description see the core/docs/entityconfig.html file.
<read-data reader-name="ext-demo"/>
<inline-jdbc
jdbc-driver="org.postgresql.Driver"
jdbc-uri="jdbc:postgresql://127.0.0.1/ofbiz"
jdbc-username="ofbiz"
jdbc-password="ofbiz"
jdbc-uri="${env:OFB_POSTGRES_DB:jdbc:postgresql://127.0.0.1/ofbiz}"
jdbc-username="${env:OFB_POSTGRES_USER:ofbiz}"
jdbc-password="${env:OFB_POSTGRES_PASS:ofbiz}"
isolation-level="ReadCommitted"
pool-minsize="2"
pool-maxsize="250"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.ofbiz.entity.config.model;

import org.apache.ofbiz.base.lang.ThreadSafe;
import org.apache.ofbiz.base.util.UtilProperties;
import org.apache.ofbiz.entity.GenericEntityConfException;
import org.w3c.dom.Element;

Expand Down Expand Up @@ -59,17 +60,17 @@ public final class InlineJdbc extends JdbcElement {
throw new GenericEntityConfException("<inline-jdbc> element jdbc-driver attribute is empty" + lineNumberText);
}
this.jdbcDriver = jdbcDriver;
String jdbcUri = element.getAttribute("jdbc-uri").intern();
String jdbcUri = UtilProperties.getEnvironmentProperty(element.getAttribute("jdbc-uri").intern());
if (jdbcUri.isEmpty()) {
throw new GenericEntityConfException("<inline-jdbc> element jdbc-uri attribute is empty" + lineNumberText);
}
this.jdbcUri = jdbcUri;
String jdbcUsername = element.getAttribute("jdbc-username").intern();
String jdbcUsername = UtilProperties.getEnvironmentProperty(element.getAttribute("jdbc-username").intern());
if (jdbcUsername.isEmpty()) {
throw new GenericEntityConfException("<inline-jdbc> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -37,7 +38,7 @@ public final class Parameter {
throw new ServiceConfigException("<parameter> element name attribute is empty");
}
this.name = name;
String value = parameterElement.getAttribute("value").intern();
String value = UtilProperties.getEnvironmentProperty(parameterElement.getAttribute("value").intern());
if (value.isEmpty()) {
throw new ServiceConfigException("<parameter> element value attribute is empty");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -39,17 +40,17 @@ public final class Server {
private final String username;

Server(Element serverElement) throws ServiceConfigException {
String jndiServerName = serverElement.getAttribute("jndi-server-name").intern();
String jndiServerName = UtilProperties.getEnvironmentProperty(serverElement.getAttribute("jndi-server-name").intern());
if (jndiServerName.isEmpty()) {
throw new ServiceConfigException("<server> element jndi-server-name attribute is empty");
}
this.jndiServerName = jndiServerName;
String jndiName = serverElement.getAttribute("jndi-name").intern();
String jndiName = UtilProperties.getEnvironmentProperty(serverElement.getAttribute("jndi-name").intern());
if (jndiName.isEmpty()) {
throw new ServiceConfigException("<server> element jndi-name attribute is empty");
}
this.jndiName = jndiName;
String topicQueue = serverElement.getAttribute("topic-queue").intern();
String topicQueue = UtilProperties.getEnvironmentProperty(serverElement.getAttribute("topic-queue").intern());
if (topicQueue.isEmpty()) {
throw new ServiceConfigException("<server> element topic-queue attribute is empty");
}
Expand All @@ -59,11 +60,11 @@ public final class Server {
throw new ServiceConfigException("<server> 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -147,7 +148,8 @@ protected Session makeSession(Configuration.Property client) {
Map<String, Configuration.Property> 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);
Expand Down

0 comments on commit 23b3a27

Please sign in to comment.