|
15 | 15 |
|
16 | 16 | package com.rabbitmq.client; |
17 | 17 |
|
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.FrameHandler; |
| 26 | +import com.rabbitmq.client.impl.FrameHandlerFactory; |
| 27 | +import com.rabbitmq.client.impl.SocketFrameHandlerFactory; |
19 | 28 | import com.rabbitmq.client.impl.nio.NioParams; |
20 | 29 | import com.rabbitmq.client.impl.nio.SocketChannelFrameHandlerFactory; |
21 | 30 | import 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; |
27 | 31 | import java.io.IOException; |
28 | 32 | import java.net.URI; |
29 | 33 | import java.net.URISyntaxException; |
30 | 34 | import java.net.URLDecoder; |
31 | 35 | import java.security.KeyManagementException; |
32 | 36 | import java.security.NoSuchAlgorithmException; |
33 | | -import java.util.*; |
34 | | -import java.util.concurrent.*; |
35 | | - |
36 | | -import static java.util.concurrent.TimeUnit.*; |
| 37 | +import java.util.Arrays; |
| 38 | +import java.util.Collections; |
| 39 | +import java.util.HashMap; |
| 40 | +import java.util.List; |
| 41 | +import java.util.Map; |
| 42 | +import java.util.Properties; |
| 43 | +import java.util.concurrent.ExecutorService; |
| 44 | +import java.util.concurrent.Executors; |
| 45 | +import java.util.concurrent.ScheduledExecutorService; |
| 46 | +import java.util.concurrent.ThreadFactory; |
| 47 | +import java.util.concurrent.TimeoutException; |
| 48 | +import javax.net.SocketFactory; |
| 49 | +import javax.net.ssl.SSLContext; |
| 50 | +import javax.net.ssl.SSLSocketFactory; |
| 51 | +import javax.net.ssl.TrustManager; |
37 | 52 |
|
38 | 53 | /** |
39 | 54 | * Convenience "factory" class to facilitate opening a {@link Connection} to an AMQP broker. |
40 | 55 | */ |
41 | | - |
42 | 56 | public class ConnectionFactory implements Cloneable { |
43 | 57 |
|
44 | 58 | /** Default user name */ |
@@ -88,31 +102,30 @@ public class ConnectionFactory implements Cloneable { |
88 | 102 |
|
89 | 103 | private static final String FALLBACK_TLS_PROTOCOL = "TLSv1"; |
90 | 104 |
|
91 | | - private String username = DEFAULT_USER; |
92 | | - private String password = DEFAULT_PASS; |
93 | | - private String virtualHost = DEFAULT_VHOST; |
94 | | - private String host = DEFAULT_HOST; |
95 | | - private int port = USE_DEFAULT_PORT; |
96 | | - private int requestedChannelMax = DEFAULT_CHANNEL_MAX; |
97 | | - private int requestedFrameMax = DEFAULT_FRAME_MAX; |
98 | | - private int requestedHeartbeat = DEFAULT_HEARTBEAT; |
99 | | - private int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT; |
100 | | - private int handshakeTimeout = DEFAULT_HANDSHAKE_TIMEOUT; |
101 | | - private int shutdownTimeout = DEFAULT_SHUTDOWN_TIMEOUT; |
102 | | - private Map<String, Object> _clientProperties = AMQConnection.defaultClientProperties(); |
103 | | - private SocketFactory factory = SocketFactory.getDefault(); |
104 | | - private SaslConfig saslConfig = DefaultSaslConfig.PLAIN; |
| 105 | + private String virtualHost = DEFAULT_VHOST; |
| 106 | + private String host = DEFAULT_HOST; |
| 107 | + private int port = USE_DEFAULT_PORT; |
| 108 | + private int requestedChannelMax = DEFAULT_CHANNEL_MAX; |
| 109 | + private int requestedFrameMax = DEFAULT_FRAME_MAX; |
| 110 | + private int requestedHeartbeat = DEFAULT_HEARTBEAT; |
| 111 | + private int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT; |
| 112 | + private int handshakeTimeout = DEFAULT_HANDSHAKE_TIMEOUT; |
| 113 | + private int shutdownTimeout = DEFAULT_SHUTDOWN_TIMEOUT; |
| 114 | + private Map<String, Object> _clientProperties = AMQConnection.defaultClientProperties(); |
| 115 | + private SocketFactory factory = SocketFactory.getDefault(); |
| 116 | + private SaslConfig saslConfig = DefaultSaslConfig.PLAIN; |
105 | 117 | private ExecutorService sharedExecutor; |
106 | | - private ThreadFactory threadFactory = Executors.defaultThreadFactory(); |
| 118 | + private ThreadFactory threadFactory = Executors.defaultThreadFactory(); |
107 | 119 | // minimises the number of threads rapid closure of many |
108 | 120 | // connections uses, see rabbitmq/rabbitmq-java-client#86 |
109 | 121 | private ExecutorService shutdownExecutor; |
110 | 122 | private ScheduledExecutorService heartbeatExecutor; |
111 | | - private SocketConfigurator socketConf = new DefaultSocketConfigurator(); |
112 | | - private ExceptionHandler exceptionHandler = new DefaultExceptionHandler(); |
| 123 | + private SocketConfigurator socketConf = new DefaultSocketConfigurator(); |
| 124 | + private ExceptionHandler exceptionHandler = new DefaultExceptionHandler(); |
| 125 | + private CredentialsProvider credentialsProvider = new DefaultCredentialsProvider(DEFAULT_USER, DEFAULT_PASS); |
113 | 126 |
|
114 | | - private boolean automaticRecovery = true; |
115 | | - private boolean topologyRecovery = true; |
| 127 | + private boolean automaticRecovery = true; |
| 128 | + private boolean topologyRecovery = true; |
116 | 129 |
|
117 | 130 | // long is used to make sure the users can use both ints |
118 | 131 | // and longs safely. It is unlikely that anybody'd need |
@@ -189,33 +202,50 @@ public void setPort(int port) { |
189 | 202 | * @return the AMQP user name to use when connecting to the broker |
190 | 203 | */ |
191 | 204 | public String getUsername() { |
192 | | - return this.username; |
| 205 | + return credentialsProvider.getUsername(); |
193 | 206 | } |
194 | 207 |
|
195 | 208 | /** |
196 | 209 | * Set the user name. |
197 | 210 | * @param username the AMQP user name to use when connecting to the broker |
198 | 211 | */ |
199 | 212 | public void setUsername(String username) { |
200 | | - this.username = username; |
| 213 | + this.credentialsProvider = new DefaultCredentialsProvider( |
| 214 | + username, |
| 215 | + this.credentialsProvider.getPassword() |
| 216 | + ); |
201 | 217 | } |
202 | 218 |
|
203 | 219 | /** |
204 | 220 | * Retrieve the password. |
205 | 221 | * @return the password to use when connecting to the broker |
206 | 222 | */ |
207 | 223 | public String getPassword() { |
208 | | - return this.password; |
| 224 | + return credentialsProvider.getPassword(); |
209 | 225 | } |
210 | 226 |
|
211 | 227 | /** |
212 | 228 | * Set the password. |
213 | 229 | * @param password the password to use when connecting to the broker |
214 | 230 | */ |
215 | 231 | public void setPassword(String password) { |
216 | | - this.password = password; |
| 232 | + this.credentialsProvider = new DefaultCredentialsProvider( |
| 233 | + this.credentialsProvider.getUsername(), |
| 234 | + password |
| 235 | + ); |
217 | 236 | } |
218 | 237 |
|
| 238 | + /** |
| 239 | + * Set a custom credentials provider. |
| 240 | + * Default implementation uses static username and password. |
| 241 | + * @param credentialsProvider The custom implementation of CredentialsProvider to use when connecting to the broker. |
| 242 | + * @see com.rabbitmq.client.impl.DefaultCredentialsProvider |
| 243 | + * @since 4.5.0 |
| 244 | + */ |
| 245 | + public void setCredentialsProvider(CredentialsProvider credentialsProvider) { |
| 246 | + this.credentialsProvider = credentialsProvider; |
| 247 | + } |
| 248 | + |
219 | 249 | /** |
220 | 250 | * Retrieve the virtual host. |
221 | 251 | * @return the virtual host to use when connecting to the broker |
@@ -971,8 +1001,7 @@ public Connection newConnection(ExecutorService executor, AddressResolver addres |
971 | 1001 | public ConnectionParams params(ExecutorService consumerWorkServiceExecutor) { |
972 | 1002 | ConnectionParams result = new ConnectionParams(); |
973 | 1003 |
|
974 | | - result.setUsername(username); |
975 | | - result.setPassword(password); |
| 1004 | + result.setCredentialsProvider(credentialsProvider); |
976 | 1005 | result.setConsumerWorkServiceExecutor(consumerWorkServiceExecutor); |
977 | 1006 | result.setVirtualHost(virtualHost); |
978 | 1007 | result.setClientProperties(getClientProperties()); |
@@ -1072,7 +1101,8 @@ protected AddressResolver createAddressResolver(List<Address> addresses) { |
1072 | 1101 |
|
1073 | 1102 | @Override public ConnectionFactory clone(){ |
1074 | 1103 | try { |
1075 | | - return (ConnectionFactory)super.clone(); |
| 1104 | + ConnectionFactory clone = (ConnectionFactory)super.clone(); |
| 1105 | + return clone; |
1076 | 1106 | } catch (CloneNotSupportedException e) { |
1077 | 1107 | throw new Error(e); |
1078 | 1108 | } |
|
0 commit comments