1515
1616package com .rabbitmq .client ;
1717
18- import com .rabbitmq .client .impl .*;
18+ import static java .util .concurrent .TimeUnit .*;
19+
20+ import com .rabbitmq .client .impl .AMQConnection ;
21+ import com .rabbitmq .client .impl .ConnectionParams ;
22+ import com .rabbitmq .client .impl .CredentialsProvider ;
23+ import com .rabbitmq .client .impl .DefaultCredentialsProvider ;
24+ import com .rabbitmq .client .impl .DefaultExceptionHandler ;
25+ import com .rabbitmq .client .impl .ErrorOnWriteListener ;
26+ import com .rabbitmq .client .impl .FrameHandler ;
27+ import com .rabbitmq .client .impl .FrameHandlerFactory ;
28+ import com .rabbitmq .client .impl .SocketFrameHandlerFactory ;
1929import com .rabbitmq .client .impl .nio .NioParams ;
2030import com .rabbitmq .client .impl .nio .SocketChannelFrameHandlerFactory ;
2131import com .rabbitmq .client .impl .recovery .AutorecoveringConnection ;
22-
23- import javax .net .SocketFactory ;
24- import javax .net .ssl .SSLContext ;
25- import javax .net .ssl .SSLSocketFactory ;
26- import javax .net .ssl .TrustManager ;
2732import java .io .IOException ;
2833import java .net .URI ;
2934import java .net .URISyntaxException ;
3035import java .net .URLDecoder ;
3136import java .security .KeyManagementException ;
3237import java .security .NoSuchAlgorithmException ;
33- import java .util .*;
34- import java .util .concurrent .*;
35-
36- import static java .util .concurrent .TimeUnit .*;
38+ import java .util .Arrays ;
39+ import java .util .Collections ;
40+ import java .util .HashMap ;
41+ import java .util .List ;
42+ import java .util .Map ;
43+ import java .util .Properties ;
44+ import java .util .concurrent .ExecutorService ;
45+ import java .util .concurrent .Executors ;
46+ import java .util .concurrent .ScheduledExecutorService ;
47+ import java .util .concurrent .ThreadFactory ;
48+ import java .util .concurrent .TimeoutException ;
49+ import javax .net .SocketFactory ;
50+ import javax .net .ssl .SSLContext ;
51+ import javax .net .ssl .SSLSocketFactory ;
52+ import javax .net .ssl .TrustManager ;
3753
3854/**
3955 * Convenience factory class to facilitate opening a {@link Connection} to a RabbitMQ node.
4258 * Some settings that apply to connections can also be configured here
4359 * and will apply to all connections produced by this factory.
4460 */
45-
4661public class ConnectionFactory implements Cloneable {
4762
4863 /** Default user name */
@@ -92,8 +107,6 @@ public class ConnectionFactory implements Cloneable {
92107
93108 private static final String FALLBACK_TLS_PROTOCOL = "TLSv1" ;
94109
95- private String username = DEFAULT_USER ;
96- private String password = DEFAULT_PASS ;
97110 private String virtualHost = DEFAULT_VHOST ;
98111 private String host = DEFAULT_HOST ;
99112 private int port = USE_DEFAULT_PORT ;
@@ -106,17 +119,19 @@ public class ConnectionFactory implements Cloneable {
106119 private Map <String , Object > _clientProperties = AMQConnection .defaultClientProperties ();
107120 private SocketFactory socketFactory = null ;
108121 private SaslConfig saslConfig = DefaultSaslConfig .PLAIN ;
122+
109123 private ExecutorService sharedExecutor ;
110- private ThreadFactory threadFactory = Executors .defaultThreadFactory ();
124+ private ThreadFactory threadFactory = Executors .defaultThreadFactory ();
111125 // minimises the number of threads rapid closure of many
112126 // connections uses, see rabbitmq/rabbitmq-java-client#86
113127 private ExecutorService shutdownExecutor ;
114128 private ScheduledExecutorService heartbeatExecutor ;
115- private SocketConfigurator socketConf = new DefaultSocketConfigurator ();
116- private ExceptionHandler exceptionHandler = new DefaultExceptionHandler ();
129+ private SocketConfigurator socketConf = new DefaultSocketConfigurator ();
130+ private ExceptionHandler exceptionHandler = new DefaultExceptionHandler ();
131+ private CredentialsProvider credentialsProvider = new DefaultCredentialsProvider (DEFAULT_USER , DEFAULT_PASS );
117132
118- private boolean automaticRecovery = true ;
119- private boolean topologyRecovery = true ;
133+ private boolean automaticRecovery = true ;
134+ private boolean topologyRecovery = true ;
120135
121136 // long is used to make sure the users can use both ints
122137 // and longs safely. It is unlikely that anybody'd need
@@ -193,33 +208,50 @@ public void setPort(int port) {
193208 * @return the AMQP user name to use when connecting to the broker
194209 */
195210 public String getUsername () {
196- return this . username ;
211+ return credentialsProvider . getUsername () ;
197212 }
198213
199214 /**
200215 * Set the user name.
201216 * @param username the AMQP user name to use when connecting to the broker
202217 */
203218 public void setUsername (String username ) {
204- this .username = username ;
219+ this .credentialsProvider = new DefaultCredentialsProvider (
220+ username ,
221+ this .credentialsProvider .getPassword ()
222+ );
205223 }
206224
207225 /**
208226 * Retrieve the password.
209227 * @return the password to use when connecting to the broker
210228 */
211229 public String getPassword () {
212- return this . password ;
230+ return credentialsProvider . getPassword () ;
213231 }
214232
215233 /**
216234 * Set the password.
217235 * @param password the password to use when connecting to the broker
218236 */
219237 public void setPassword (String password ) {
220- this .password = password ;
238+ this .credentialsProvider = new DefaultCredentialsProvider (
239+ this .credentialsProvider .getUsername (),
240+ password
241+ );
221242 }
222243
244+ /**
245+ * Set a custom credentials provider.
246+ * Default implementation uses static username and password.
247+ * @param credentialsProvider The custom implementation of CredentialsProvider to use when connecting to the broker.
248+ * @see com.rabbitmq.client.impl.DefaultCredentialsProvider
249+ * @since 4.5.0
250+ */
251+ public void setCredentialsProvider (CredentialsProvider credentialsProvider ) {
252+ this .credentialsProvider = credentialsProvider ;
253+ }
254+
223255 /**
224256 * Retrieve the virtual host.
225257 * @return the virtual host to use when connecting to the broker
@@ -994,8 +1026,7 @@ public Connection newConnection(ExecutorService executor, AddressResolver addres
9941026 public ConnectionParams params (ExecutorService consumerWorkServiceExecutor ) {
9951027 ConnectionParams result = new ConnectionParams ();
9961028
997- result .setUsername (username );
998- result .setPassword (password );
1029+ result .setCredentialsProvider (credentialsProvider );
9991030 result .setConsumerWorkServiceExecutor (consumerWorkServiceExecutor );
10001031 result .setVirtualHost (virtualHost );
10011032 result .setClientProperties (getClientProperties ());
@@ -1095,7 +1126,8 @@ protected AddressResolver createAddressResolver(List<Address> addresses) {
10951126
10961127 @ Override public ConnectionFactory clone (){
10971128 try {
1098- return (ConnectionFactory )super .clone ();
1129+ ConnectionFactory clone = (ConnectionFactory )super .clone ();
1130+ return clone ;
10991131 } catch (CloneNotSupportedException e ) {
11001132 throw new RuntimeException (e );
11011133 }
@@ -1331,7 +1363,7 @@ public boolean isChannelShouldCheckRpcResponseType() {
13311363
13321364 /**
13331365 * Timeout (in ms) for work pool enqueueing.
1334- * The {@link WorkPool} dispatches several types of responses
1366+ * The {@link com.rabbitmq.client.impl. WorkPool} dispatches several types of responses
13351367 * from the broker (e.g. deliveries). A high-traffic
13361368 * client with slow consumers can exhaust the work pool and
13371369 * compromise the whole connection (by e.g. letting the broker
0 commit comments