From 152d5a116c28bcc7c2493048a21b40a84288dee0 Mon Sep 17 00:00:00 2001 From: Richard Ogin Date: Fri, 24 May 2024 06:54:47 -0500 Subject: [PATCH] Implements #6208 --- .../src/com/mirth/connect/server/Mirth.java | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/server/src/com/mirth/connect/server/Mirth.java b/server/src/com/mirth/connect/server/Mirth.java index 23f3f8606..52f1bd6c6 100644 --- a/server/src/com/mirth/connect/server/Mirth.java +++ b/server/src/com/mirth/connect/server/Mirth.java @@ -21,6 +21,7 @@ import java.util.ArrayList; import java.util.Calendar; import java.util.List; +import java.util.Optional; import java.util.Timer; import java.util.TimerTask; @@ -43,6 +44,7 @@ import com.mirth.connect.model.ResourceProperties; import com.mirth.connect.model.ResourcePropertiesList; import com.mirth.connect.model.ServerEvent; +import com.mirth.connect.model.ServerSettings; import com.mirth.connect.model.UpdateSettings; import com.mirth.connect.model.converters.ObjectXMLSerializer; import com.mirth.connect.model.util.MigrationException; @@ -338,12 +340,13 @@ public void startup() { migrationController.clearStartupLockTable(); } - // disable the velocity logging Logger velocityLogger = LogManager.getLogger(RuntimeConstants.DEFAULT_RUNTIME_LOG_NAME); if (velocityLogger != null && velocityLogger.getLevel() == null && velocityLogger instanceof org.apache.logging.log4j.core.Logger) { ((org.apache.logging.log4j.core.Logger) velocityLogger).setLevel(Level.OFF); } + updateServerSettingsFromEnvironment(); + eventController.dispatchEvent(new ServerEvent(configurationController.getServerId(), "Server startup")); // Start web server before starting the engine in case there is a @@ -404,7 +407,35 @@ public void startup() { timer.schedule(new UsageSenderTask(), 0, ConnectServiceUtil.MILLIS_PER_DAY); } + private void updateServerSettingsFromEnvironment() { + Optional newServerName = getEnvironmentVariable("MC_SERVER_NAME"); + Optional newEnvName = getEnvironmentVariable("MC_ENV_NAME"); + + if(newServerName.isPresent() || newEnvName.isPresent()) { + try { + ServerSettings serverSettings = configurationController.getServerSettings(); + + if(newServerName.isPresent()) { serverSettings.setServerName(newServerName.get()); } + if(newEnvName.isPresent()) { serverSettings.setEnvironmentName(newEnvName.get()); } + + configurationController.setServerSettings(serverSettings); + } catch (ControllerException e) { + logger.error("Failed to update server settings via environment variables", e); + } + } + } + /** + * Pull an environment variable. Values are trimmed to a non-empty string. + * @param name + * @return + */ + private Optional getEnvironmentVariable(String name) { + String propValue = StringUtils.trimToEmpty(System.getenv(name)); + return StringUtils.isNotBlank(propValue) ? Optional.of(propValue) : Optional.empty(); + } + + /** * Shuts down the server. * */