24
24
import java .util .HashMap ;
25
25
import java .util .Hashtable ;
26
26
import java .util .Map ;
27
- import java .util .Optional ;
28
27
29
28
/**
30
29
* Class represents MQ connection.
@@ -36,7 +35,7 @@ public class MQConnection {
36
35
/**
37
36
* Method creates connection properties Hashtable from connection parameters.
38
37
*
39
- * @param config - config .
38
+ * @param config - object containing different properties .
40
39
* @return - returns prepared structure with all parameters transformed into queue manager's format.
41
40
*/
42
41
public static Map <String , Object > createMQConnectionParams (Config config ) {
@@ -52,30 +51,36 @@ public static Map<String, Object> createMQConnectionParams(Config config) {
52
51
}
53
52
MQSecurityProperties mqSecurityProperties = config .getMqSecurityProperties ();
54
53
if (mqSecurityProperties != null && mqSecurityProperties .isUseTLS ()) {
55
- KeyStore keyStore = getStore (mqSecurityProperties .getKeystorePath (), mqSecurityProperties .getKeystorePassword ());
56
- KeyStore trustStore = getStore (mqSecurityProperties .getTruststorePath (), mqSecurityProperties .getTruststorePassword ());
57
-
58
- SSLContext sslContext = null ;
59
- try {
60
- TrustManagerFactory trustManagerFactory = TrustManagerFactory .getInstance (TrustManagerFactory .getDefaultAlgorithm ());
61
- KeyManagerFactory keyManagerFactory = KeyManagerFactory .getInstance (KeyManagerFactory .getDefaultAlgorithm ());
62
- trustManagerFactory .init (trustStore );
63
- keyManagerFactory .init (keyStore , mqSecurityProperties .getKeystorePassword ().toCharArray ());
64
- sslContext = SSLContext .getInstance (mqSecurityProperties .getSslProtocol ());
65
- sslContext .init (keyManagerFactory .getKeyManagers (), trustManagerFactory .getTrustManagers (), null );
66
- } catch (KeyStoreException | UnrecoverableKeyException | NoSuchAlgorithmException | KeyManagementException e1 ) {
67
- logger .error ("Failed!" , e1 );
68
- }
69
-
70
- SSLSocketFactory sslSocketFactory = sslContext .getSocketFactory ();
71
-
72
54
properties .put (MQConstants .SSL_CIPHER_SUITE_PROPERTY , mqSecurityProperties .getCipherSuite ());
73
- properties .put (MQConstants .SSL_SOCKET_FACTORY_PROPERTY , sslSocketFactory );
55
+ properties .put (MQConstants .SSL_SOCKET_FACTORY_PROPERTY , getSslSocketFactory ( mqSecurityProperties ) );
74
56
System .setProperty ("com.ibm.mq.cfg.useIBMCipherMappings" , "false" );
75
57
}
76
58
return properties ;
77
59
}
78
60
61
+ /**
62
+ * Method creates SSLSocketFactory from connection parameters.
63
+ *
64
+ * @param mqSecurityProperties - object containing security properties.
65
+ * @return - returns prepared SSLSocketFactory.
66
+ */
67
+ private static SSLSocketFactory getSslSocketFactory (MQSecurityProperties mqSecurityProperties ) {
68
+ KeyStore keyStore = getStore (mqSecurityProperties .getKeystorePath (), mqSecurityProperties .getKeystorePassword ());
69
+ KeyStore trustStore = getStore (mqSecurityProperties .getTruststorePath (), mqSecurityProperties .getTruststorePassword ());
70
+ SSLContext sslContext = null ;
71
+ try {
72
+ TrustManagerFactory trustManagerFactory = TrustManagerFactory .getInstance (TrustManagerFactory .getDefaultAlgorithm ());
73
+ KeyManagerFactory keyManagerFactory = KeyManagerFactory .getInstance (KeyManagerFactory .getDefaultAlgorithm ());
74
+ trustManagerFactory .init (trustStore );
75
+ keyManagerFactory .init (keyStore , mqSecurityProperties .getKeystorePassword ().toCharArray ());
76
+ sslContext = SSLContext .getInstance (mqSecurityProperties .getSslProtocol ());
77
+ sslContext .init (keyManagerFactory .getKeyManagers (), trustManagerFactory .getTrustManagers (), null );
78
+ } catch (KeyStoreException | UnrecoverableKeyException | NoSuchAlgorithmException | KeyManagementException e1 ) {
79
+ logger .error ("Failed!" , e1 );
80
+ }
81
+ return sslContext .getSocketFactory ();
82
+ }
83
+
79
84
private static KeyStore getStore (String storePath , String storePassword ) {
80
85
KeyStore keyStore = null ;
81
86
try (FileInputStream keyStoreInput = new FileInputStream (storePath )) {
@@ -91,7 +96,7 @@ private static KeyStore getStore(String storePath, String storePassword) {
91
96
* Method establishes connection with queue manager.
92
97
*
93
98
* @param qmNqme - queue manager's name.
94
- * @param connectionProperties - prepared structure with all parameters transformed into queue manager's format. See {@link #createMQConnectionParams(String, int, String, String, String, boolean )} for more info.
99
+ * @param connectionProperties - prepared structure with all parameters transformed into queue manager's format. See {@link #createMQConnectionParams(Config config )} for more info.
95
100
*/
96
101
public void establish (String qmNqme , Map <String , Object > connectionProperties ) throws MQException {
97
102
queueManager = new MQQueueManager (qmNqme , new Hashtable <>(connectionProperties ));
0 commit comments