|
19 | 19 | import java.sql.DriverManager; |
20 | 20 | import java.sql.SQLException; |
21 | 21 | import java.util.ArrayList; |
22 | | -import java.util.Calendar; |
23 | 22 | import java.util.List; |
| 23 | +import java.util.Optional; |
24 | 24 | import java.util.Timer; |
25 | 25 | import java.util.TimerTask; |
26 | 26 |
|
27 | 27 | import org.apache.commons.configuration2.PropertiesConfiguration; |
28 | 28 | import org.apache.commons.io.IOUtils; |
29 | 29 | import org.apache.commons.lang3.StringUtils; |
30 | 30 | import org.apache.commons.lang3.math.NumberUtils; |
31 | | -import org.apache.logging.log4j.ThreadContext; |
32 | 31 | import org.apache.logging.log4j.Level; |
33 | 32 | import org.apache.logging.log4j.LogManager; |
34 | 33 | import org.apache.logging.log4j.Logger; |
| 34 | +import org.apache.logging.log4j.ThreadContext; |
35 | 35 | import org.apache.logging.log4j.core.Appender; |
36 | 36 | import org.apache.logging.log4j.core.filter.Filterable; |
37 | 37 | import org.apache.velocity.runtime.RuntimeConstants; |
|
45 | 45 | import com.mirth.connect.model.ResourceProperties; |
46 | 46 | import com.mirth.connect.model.ResourcePropertiesList; |
47 | 47 | import com.mirth.connect.model.ServerEvent; |
| 48 | +import com.mirth.connect.model.ServerSettings; |
48 | 49 | import com.mirth.connect.model.UpdateSettings; |
49 | 50 | import com.mirth.connect.model.converters.ObjectXMLSerializer; |
50 | 51 | import com.mirth.connect.model.util.MigrationException; |
@@ -344,9 +345,11 @@ public void startup() { |
344 | 345 | ((org.apache.logging.log4j.core.Logger) velocityLogger).setLevel(Level.OFF); |
345 | 346 | } |
346 | 347 |
|
| 348 | + updateServerSettingsFromEnvironment(); |
| 349 | + |
347 | 350 | eventController.dispatchEvent(new ServerEvent(configurationController.getServerId(), "Server startup")); |
348 | 351 |
|
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 |
350 | 353 | // problem starting the engine that causes it to hang |
351 | 354 | startWebServer(); |
352 | 355 |
|
@@ -404,6 +407,39 @@ public void startup() { |
404 | 407 | timer.schedule(new UsageSenderTask(), 0, ConnectServiceUtil.MILLIS_PER_DAY); |
405 | 408 | } |
406 | 409 |
|
| 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 | + |
407 | 443 | /** |
408 | 444 | * Shuts down the server. |
409 | 445 | * |
|
0 commit comments