|
41 | 41 | import java.util.Locale; |
42 | 42 | import java.util.Map; |
43 | 43 | import java.util.Map.Entry; |
| 44 | +import java.util.Optional; |
44 | 45 | import java.util.Properties; |
45 | 46 | import java.util.Set; |
46 | 47 | import java.util.SortedMap; |
|
75 | 76 | import org.apache.ibatis.session.SqlSessionManager; |
76 | 77 | import org.apache.logging.log4j.LogManager; |
77 | 78 | import org.apache.logging.log4j.Logger; |
78 | | -import org.bouncycastle.asn1.ASN1Sequence; |
79 | 79 | import org.bouncycastle.asn1.x500.X500Name; |
80 | 80 | import org.bouncycastle.asn1.x509.AuthorityKeyIdentifier; |
81 | 81 | import org.bouncycastle.asn1.x509.BasicConstraints; |
@@ -1704,4 +1704,38 @@ public ConnectionTestResponse sendTestEmail(Properties properties) throws Except |
1704 | 1704 | return new ConnectionTestResponse(ConnectionTestResponse.Type.FAILURE, e.getMessage()); |
1705 | 1705 | } |
1706 | 1706 | } |
| 1707 | + |
| 1708 | + @Override |
| 1709 | + public void updateServerSettingsFromEnvironment() { |
| 1710 | + Optional<String> newServerName = getEnvironmentVariable("MC_SERVER_NAME"); |
| 1711 | + Optional<String> newEnvName = getEnvironmentVariable("MC_ENV_NAME"); |
| 1712 | + |
| 1713 | + if (newServerName.isPresent() || newEnvName.isPresent()) { |
| 1714 | + try { |
| 1715 | + ServerSettings serverSettings = getServerSettings(); |
| 1716 | + |
| 1717 | + if (newServerName.isPresent()) { |
| 1718 | + serverSettings.setServerName(newServerName.get()); |
| 1719 | + } |
| 1720 | + if (newEnvName.isPresent()) { |
| 1721 | + serverSettings.setEnvironmentName(newEnvName.get()); |
| 1722 | + } |
| 1723 | + |
| 1724 | + setServerSettings(serverSettings); |
| 1725 | + } catch (ControllerException e) { |
| 1726 | + logger.error("Failed to update server settings via environment variables", e); |
| 1727 | + } |
| 1728 | + } |
| 1729 | + } |
| 1730 | + |
| 1731 | + /** |
| 1732 | + * Pull an environment variable. Values are trimmed, and only non-empty values are returned. |
| 1733 | + * |
| 1734 | + * @param envName the environment variable name |
| 1735 | + * @return the property's value |
| 1736 | + */ |
| 1737 | + private Optional<String> getEnvironmentVariable(String envName) { |
| 1738 | + String propValue = StringUtils.trimToEmpty(System.getenv(envName)); |
| 1739 | + return StringUtils.isNotBlank(propValue) ? Optional.of(propValue) : Optional.empty(); |
| 1740 | + } |
1707 | 1741 | } |
0 commit comments