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