Commit c21deeb9 authored by Antoine Ouvrard's avatar Antoine Ouvrard

OFBIZ-9498 - patch variables d'environnements

parent a7179baf
Pipeline #1489 passed with stage
in 2 minutes and 30 seconds
......@@ -283,7 +283,7 @@ public final class UtilProperties implements Serializable {
} catch (Exception e) {
Debug.logInfo(e, module);
}
return value == null ? "" : value.trim();
return value == null ? "" : getEnvironmentProperty(value.trim());
}
/**
......@@ -1067,6 +1067,32 @@ public final class UtilProperties implements Serializable {
return properties;
}
/** resolve a property to check if is contains a environment variable present by ${env:ENV_VARIABLE:DEFAULT_VALUE}
*
*/
static public String getEnvironmentProperty(String value) {
if (value == null) return "";
value = value.trim();
if (value.startsWith("${env:") && value.endsWith("}")) {
String environmentNameAndDefautlValue = value.substring(6, value.length() - 1);
String environmentName = environmentNameAndDefautlValue;
String defaultValue = null;
if (environmentNameAndDefautlValue.contains(":")) {
environmentName = environmentNameAndDefautlValue.substring(0, environmentNameAndDefautlValue.indexOf(":"));
defaultValue = environmentNameAndDefautlValue.substring(environmentNameAndDefautlValue.indexOf(":") + 1, environmentNameAndDefautlValue.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.
......
......@@ -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
......
......@@ -484,9 +484,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"
......
......@@ -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;
......@@ -60,16 +61,18 @@ public final class InlineJdbc extends JdbcElement {
}
this.jdbcDriver = jdbcDriver;
String jdbcUri = element.getAttribute("jdbc-uri").intern();
jdbcUri = UtilProperties.getEnvironmentProperty(jdbcUri);
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();
jdbcUsername = UtilProperties.getEnvironmentProperty(jdbcUsername);
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()) {
......
......@@ -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("<parameter> element value attribute is empty");
}
......
......@@ -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("<server> 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("<server> 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("<server> element topic-queue attribute is empty");
}
......@@ -59,11 +63,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() {
......
......@@ -47,6 +47,7 @@ import org.apache.ofbiz.base.container.ContainerException;
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;
......@@ -143,7 +144,7 @@ public class JavaMailContainer implements Container {
Map<String, ContainerConfig.Configuration.Property> clientProps = client.properties;
if (clientProps != null) {
for (ContainerConfig.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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment