Skip to content

Commit 4ca5c6e

Browse files
committed
Read env vars for server name and env name
1 parent 8175b47 commit 4ca5c6e

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

server/src/com/mirth/connect/server/Mirth.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@
1919
import java.sql.DriverManager;
2020
import java.sql.SQLException;
2121
import java.util.ArrayList;
22-
import java.util.Calendar;
2322
import java.util.List;
23+
import java.util.Optional;
2424
import java.util.Timer;
2525
import java.util.TimerTask;
2626

2727
import org.apache.commons.configuration2.PropertiesConfiguration;
2828
import org.apache.commons.io.IOUtils;
2929
import org.apache.commons.lang3.StringUtils;
3030
import org.apache.commons.lang3.math.NumberUtils;
31-
import org.apache.logging.log4j.ThreadContext;
3231
import org.apache.logging.log4j.Level;
3332
import org.apache.logging.log4j.LogManager;
3433
import org.apache.logging.log4j.Logger;
34+
import org.apache.logging.log4j.ThreadContext;
3535
import org.apache.logging.log4j.core.Appender;
3636
import org.apache.logging.log4j.core.filter.Filterable;
3737
import org.apache.velocity.runtime.RuntimeConstants;
@@ -45,6 +45,7 @@
4545
import com.mirth.connect.model.ResourceProperties;
4646
import com.mirth.connect.model.ResourcePropertiesList;
4747
import com.mirth.connect.model.ServerEvent;
48+
import com.mirth.connect.model.ServerSettings;
4849
import com.mirth.connect.model.UpdateSettings;
4950
import com.mirth.connect.model.converters.ObjectXMLSerializer;
5051
import com.mirth.connect.model.util.MigrationException;
@@ -344,9 +345,11 @@ public void startup() {
344345
((org.apache.logging.log4j.core.Logger) velocityLogger).setLevel(Level.OFF);
345346
}
346347

348+
updateServerSettingsFromEnvironment();
349+
347350
eventController.dispatchEvent(new ServerEvent(configurationController.getServerId(), "Server startup"));
348351

349-
// Start web server before starting the engine in case there is a
352+
// Start web server before starting the engine in case there is a
350353
// problem starting the engine that causes it to hang
351354
startWebServer();
352355

@@ -404,6 +407,39 @@ public void startup() {
404407
timer.schedule(new UsageSenderTask(), 0, ConnectServiceUtil.MILLIS_PER_DAY);
405408
}
406409

410+
private void updateServerSettingsFromEnvironment() {
411+
Optional<String> newServerName = getEnvironmentVariable("MC_SERVER_NAME");
412+
Optional<String> newEnvName = getEnvironmentVariable("MC_ENV_NAME");
413+
414+
if (newServerName.isPresent() || newEnvName.isPresent()) {
415+
try {
416+
ServerSettings serverSettings = configurationController.getServerSettings();
417+
418+
if (newServerName.isPresent()) {
419+
serverSettings.setServerName(newServerName.get());
420+
}
421+
if (newEnvName.isPresent()) {
422+
serverSettings.setEnvironmentName(newEnvName.get());
423+
}
424+
425+
configurationController.setServerSettings(serverSettings);
426+
} catch (ControllerException e) {
427+
logger.error("Failed to update server settings via environment variables", e);
428+
}
429+
}
430+
}
431+
432+
/**
433+
* Pull an environment variable. Values are trimmed, and only non-empty values are returned.
434+
*
435+
* @param envName the environment variable name
436+
* @return the property's value
437+
*/
438+
private Optional<String> getEnvironmentVariable(String envName) {
439+
String propValue = StringUtils.trimToEmpty(System.getenv(envName));
440+
return StringUtils.isNotBlank(propValue) ? Optional.of(propValue) : Optional.empty();
441+
}
442+
407443
/**
408444
* Shuts down the server.
409445
*

0 commit comments

Comments
 (0)