|
| 1 | +package app; |
| 2 | + |
| 3 | +import com.ibm.mq.constants.CMQC; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStream; |
| 7 | +import java.nio.file.Files; |
| 8 | +import java.nio.file.Path; |
| 9 | +import java.util.Hashtable; |
| 10 | +import java.util.Properties; |
| 11 | + |
| 12 | +final class MQCommon { |
| 13 | + private static final String CIPHER_SUITE = "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256"; |
| 14 | + |
| 15 | + private static final String ENV_USER = "MQ_USER"; |
| 16 | + private static final String ENV_PASSWORD = "MQ_PASSWORD"; |
| 17 | + private static final String ENV_TRUSTSTORE_PASSWORD = "TRUSTSTORE_PASSWORD"; |
| 18 | + |
| 19 | + private MQCommon() { |
| 20 | + } |
| 21 | + |
| 22 | + static Config loadConfig(Path propertiesPath) throws IOException { |
| 23 | + Properties properties = new Properties(); |
| 24 | + try (InputStream input = Files.newInputStream(propertiesPath)) { |
| 25 | + properties.load(input); |
| 26 | + } |
| 27 | + |
| 28 | + String host = getRequiredProperty(properties, "HOST"); |
| 29 | + int port = Integer.parseInt(getRequiredProperty(properties, "PORT")); |
| 30 | + String channel = getRequiredProperty(properties, "CHANNEL"); |
| 31 | + String qmgr = getRequiredProperty(properties, "QMGR"); |
| 32 | + String queue = getRequiredProperty(properties, "QUEUE"); |
| 33 | + String truststorePath = getRequiredProperty(properties, "TRUSTSTORE_PATH"); |
| 34 | + String truststoreType = getRequiredProperty(properties, "TRUSTSTORE_TYPE"); |
| 35 | + |
| 36 | + String user = getRequiredEnv(ENV_USER); |
| 37 | + String password = getRequiredEnv(ENV_PASSWORD); |
| 38 | + String truststorePassword = getRequiredEnv(ENV_TRUSTSTORE_PASSWORD); |
| 39 | + |
| 40 | + return new Config( |
| 41 | + host, |
| 42 | + port, |
| 43 | + channel, |
| 44 | + qmgr, |
| 45 | + queue, |
| 46 | + user, |
| 47 | + password, |
| 48 | + truststorePath, |
| 49 | + truststoreType, |
| 50 | + truststorePassword |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + static Hashtable<String, Object> createMqProps(Config config) { |
| 55 | + Hashtable<String, Object> props = new Hashtable<>(); |
| 56 | + props.put(CMQC.HOST_NAME_PROPERTY, config.host); |
| 57 | + props.put(CMQC.PORT_PROPERTY, config.port); |
| 58 | + props.put(CMQC.CHANNEL_PROPERTY, config.channel); |
| 59 | + props.put(CMQC.USER_ID_PROPERTY, config.user); |
| 60 | + props.put(CMQC.PASSWORD_PROPERTY, config.password); |
| 61 | + props.put(CMQC.TRANSPORT_PROPERTY, CMQC.TRANSPORT_MQSERIES_CLIENT); |
| 62 | + props.put(CMQC.SSL_CIPHER_SUITE_PROPERTY, CIPHER_SUITE); |
| 63 | + return props; |
| 64 | + } |
| 65 | + |
| 66 | + static void configureSsl(Config config) { |
| 67 | + System.setProperty("com.ibm.mq.cfg.useIBMCipherMappings", "false"); |
| 68 | + System.setProperty("com.ibm.mq.client.useAuthentication", "true"); |
| 69 | + System.setProperty("javax.net.ssl.trustStore", config.truststorePath); |
| 70 | + System.setProperty("javax.net.ssl.trustStorePassword", config.truststorePassword); |
| 71 | + System.setProperty("javax.net.ssl.trustStoreType", config.truststoreType); |
| 72 | + } |
| 73 | + |
| 74 | + private static String getRequiredProperty(Properties properties, String key) { |
| 75 | + String value = firstNonBlank( |
| 76 | + properties.getProperty(key), |
| 77 | + properties.getProperty(key.toLowerCase()), |
| 78 | + properties.getProperty(key.toUpperCase()) |
| 79 | + ); |
| 80 | + if (value == null) { |
| 81 | + throw new IllegalArgumentException("Missing property: " + key); |
| 82 | + } |
| 83 | + return value.trim(); |
| 84 | + } |
| 85 | + |
| 86 | + private static String getRequiredEnv(String name) { |
| 87 | + String value = System.getenv(name); |
| 88 | + if (value == null || value.isBlank()) { |
| 89 | + throw new IllegalStateException("Missing environment variable: " + name); |
| 90 | + } |
| 91 | + return value; |
| 92 | + } |
| 93 | + |
| 94 | + private static String firstNonBlank(String... values) { |
| 95 | + for (String value : values) { |
| 96 | + if (value != null && !value.isBlank()) { |
| 97 | + return value; |
| 98 | + } |
| 99 | + } |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + static final class Config { |
| 104 | + final String host; |
| 105 | + final int port; |
| 106 | + final String channel; |
| 107 | + final String qmgr; |
| 108 | + final String queue; |
| 109 | + final String user; |
| 110 | + final String password; |
| 111 | + final String truststorePath; |
| 112 | + final String truststoreType; |
| 113 | + final String truststorePassword; |
| 114 | + |
| 115 | + Config( |
| 116 | + String host, |
| 117 | + int port, |
| 118 | + String channel, |
| 119 | + String qmgr, |
| 120 | + String queue, |
| 121 | + String user, |
| 122 | + String password, |
| 123 | + String truststorePath, |
| 124 | + String truststoreType, |
| 125 | + String truststorePassword |
| 126 | + ) { |
| 127 | + this.host = host; |
| 128 | + this.port = port; |
| 129 | + this.channel = channel; |
| 130 | + this.qmgr = qmgr; |
| 131 | + this.queue = queue; |
| 132 | + this.user = user; |
| 133 | + this.password = password; |
| 134 | + this.truststorePath = truststorePath; |
| 135 | + this.truststoreType = truststoreType; |
| 136 | + this.truststorePassword = truststorePassword; |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments