@@ -94,7 +94,7 @@ public class ConnectionFactory implements Cloneable {
94
94
private int handshakeTimeout = DEFAULT_HANDSHAKE_TIMEOUT ;
95
95
private int shutdownTimeout = DEFAULT_SHUTDOWN_TIMEOUT ;
96
96
private Map <String , Object > _clientProperties = AMQConnection .defaultClientProperties ();
97
- private SocketFactory factory = SocketFactory . getDefault () ;
97
+ private SocketFactory factory = null ;
98
98
private SaslConfig saslConfig = DefaultSaslConfig .PLAIN ;
99
99
private ExecutorService sharedExecutor ;
100
100
private ThreadFactory threadFactory = Executors .defaultThreadFactory ();
@@ -119,7 +119,7 @@ public class ConnectionFactory implements Cloneable {
119
119
private FrameHandlerFactory frameHandlerFactory ;
120
120
private NioParams nioParams = new NioParams ();
121
121
122
- private SSLContext sslContext ;
122
+ private SslContextFactory sslContextFactory ;
123
123
124
124
/**
125
125
* Continuation timeout on RPC calls.
@@ -449,7 +449,8 @@ public SocketFactory getSocketFactory() {
449
449
* Set the socket factory used to make connections with. Can be
450
450
* used to enable SSL connections by passing in a
451
451
* javax.net.ssl.SSLSocketFactory instance.
452
- *
452
+ * Note this applies only to blocking IO, not to
453
+ * NIO, as the NIO API doesn't use the SocketFactory API.
453
454
* @see #useSslProtocol
454
455
*/
455
456
public void setSocketFactory (SocketFactory factory ) {
@@ -556,7 +557,7 @@ public void setExceptionHandler(ExceptionHandler exceptionHandler) {
556
557
}
557
558
558
559
public boolean isSSL (){
559
- return getSocketFactory () instanceof SSLSocketFactory ;
560
+ return getSocketFactory () instanceof SSLSocketFactory || sslContextFactory != null ;
560
561
}
561
562
562
563
/**
@@ -572,6 +573,10 @@ public void useSslProtocol()
572
573
/**
573
574
* Convenience method for setting up a SSL socket factory/engine, using
574
575
* the supplied protocol and a very trusting TrustManager.
576
+ * The produced {@link SSLContext} instance will be shared by all
577
+ * the connections created by this connection factory. Use
578
+ * {@link #setSslContextFactory(SslContextFactory)} for more flexibility.
579
+ * @see #setSslContextFactory(SslContextFactory)
575
580
*/
576
581
public void useSslProtocol (String protocol )
577
582
throws NoSuchAlgorithmException , KeyManagementException
@@ -582,8 +587,11 @@ public void useSslProtocol(String protocol)
582
587
/**
583
588
* Convenience method for setting up an SSL socket factory/engine.
584
589
* Pass in the SSL protocol to use, e.g. "TLSv1" or "TLSv1.2".
585
- *
590
+ * The produced {@link SSLContext} instance will be shared with all
591
+ * the connections created by this connection factory. Use
592
+ * {@link #setSslContextFactory(SslContextFactory)} for more flexibility.
586
593
* @param protocol SSL protocol to use.
594
+ * @see #setSslContextFactory(SslContextFactory)
587
595
*/
588
596
public void useSslProtocol (String protocol , TrustManager trustManager )
589
597
throws NoSuchAlgorithmException , KeyManagementException
@@ -596,12 +604,15 @@ public void useSslProtocol(String protocol, TrustManager trustManager)
596
604
/**
597
605
* Convenience method for setting up an SSL socket factory/engine.
598
606
* Pass in an initialized SSLContext.
599
- *
607
+ * The {@link SSLContext} instance will be shared with all
608
+ * the connections created by this connection factory. Use
609
+ * {@link #setSslContextFactory(SslContextFactory)} for more flexibility.
600
610
* @param context An initialized SSLContext
611
+ * @see #setSslContextFactory(SslContextFactory)
601
612
*/
602
613
public void useSslProtocol (SSLContext context ) {
614
+ this .sslContextFactory = name -> context ;
603
615
setSocketFactory (context .getSocketFactory ());
604
- this .sslContext = context ;
605
616
}
606
617
607
618
public static String computeDefaultTlsProcotol (String [] supportedProtocols ) {
@@ -667,11 +678,11 @@ protected synchronized FrameHandlerFactory createFrameHandlerFactory() throws IO
667
678
if (this .nioParams .getNioExecutor () == null && this .nioParams .getThreadFactory () == null ) {
668
679
this .nioParams .setThreadFactory (getThreadFactory ());
669
680
}
670
- this .frameHandlerFactory = new SocketChannelFrameHandlerFactory (connectionTimeout , nioParams , isSSL (), sslContext );
681
+ this .frameHandlerFactory = new SocketChannelFrameHandlerFactory (connectionTimeout , nioParams , isSSL (), sslContextFactory );
671
682
}
672
683
return this .frameHandlerFactory ;
673
684
} else {
674
- return new SocketFrameHandlerFactory (connectionTimeout , factory , socketConf , isSSL (), this .shutdownExecutor );
685
+ return new SocketFrameHandlerFactory (connectionTimeout , factory , socketConf , isSSL (), this .shutdownExecutor , sslContextFactory );
675
686
}
676
687
677
688
}
@@ -915,7 +926,7 @@ public Connection newConnection(ExecutorService executor, AddressResolver addres
915
926
Exception lastException = null ;
916
927
for (Address addr : addrs ) {
917
928
try {
918
- FrameHandler handler = fhFactory .create (addr );
929
+ FrameHandler handler = fhFactory .create (addr , clientProvidedName );
919
930
AMQConnection conn = createConnection (params , handler , metricsCollector );
920
931
conn .start ();
921
932
this .metricsCollector .newConnection (conn );
@@ -1124,4 +1135,20 @@ public void setChannelRpcTimeout(int channelRpcTimeout) {
1124
1135
public int getChannelRpcTimeout () {
1125
1136
return channelRpcTimeout ;
1126
1137
}
1138
+
1139
+ /**
1140
+ * The factory to create SSL contexts.
1141
+ * This provides more flexibility to create {@link SSLContext}s
1142
+ * for different connections than sharing the {@link SSLContext}
1143
+ * with all the connections produced by the connection factory
1144
+ * (which is the case with the {@link #useSslProtocol()} methods).
1145
+ * This way, different connections with a different certificate
1146
+ * for each of them is a possible scenario.
1147
+ * @param sslContextFactory
1148
+ * @see #useSslProtocol(SSLContext)
1149
+ * @since 5.0.0
1150
+ */
1151
+ public void setSslContextFactory (SslContextFactory sslContextFactory ) {
1152
+ this .sslContextFactory = sslContextFactory ;
1153
+ }
1127
1154
}
0 commit comments