|
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 */ |
@@ -85,31 +99,30 @@ public class ConnectionFactory implements Cloneable { |
85 | 99 |
|
86 | 100 | private static final String FALLBACK_TLS_PROTOCOL = "TLSv1"; |
87 | 101 |
|
88 | | - private String username = DEFAULT_USER; |
89 | | - private String password = DEFAULT_PASS; |
90 | | - private String virtualHost = DEFAULT_VHOST; |
91 | | - private String host = DEFAULT_HOST; |
92 | | - private int port = USE_DEFAULT_PORT; |
93 | | - private int requestedChannelMax = DEFAULT_CHANNEL_MAX; |
94 | | - private int requestedFrameMax = DEFAULT_FRAME_MAX; |
95 | | - private int requestedHeartbeat = DEFAULT_HEARTBEAT; |
96 | | - private int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT; |
97 | | - private int handshakeTimeout = DEFAULT_HANDSHAKE_TIMEOUT; |
98 | | - private int shutdownTimeout = DEFAULT_SHUTDOWN_TIMEOUT; |
99 | | - private Map<String, Object> _clientProperties = AMQConnection.defaultClientProperties(); |
100 | | - private SocketFactory factory = SocketFactory.getDefault(); |
101 | | - private SaslConfig saslConfig = DefaultSaslConfig.PLAIN; |
| 102 | + private String virtualHost = DEFAULT_VHOST; |
| 103 | + private String host = DEFAULT_HOST; |
| 104 | + private int port = USE_DEFAULT_PORT; |
| 105 | + private int requestedChannelMax = DEFAULT_CHANNEL_MAX; |
| 106 | + private int requestedFrameMax = DEFAULT_FRAME_MAX; |
| 107 | + private int requestedHeartbeat = DEFAULT_HEARTBEAT; |
| 108 | + private int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT; |
| 109 | + private int handshakeTimeout = DEFAULT_HANDSHAKE_TIMEOUT; |
| 110 | + private int shutdownTimeout = DEFAULT_SHUTDOWN_TIMEOUT; |
| 111 | + private Map<String, Object> _clientProperties = AMQConnection.defaultClientProperties(); |
| 112 | + private SocketFactory factory = SocketFactory.getDefault(); |
| 113 | + private SaslConfig saslConfig = DefaultSaslConfig.PLAIN; |
102 | 114 | private ExecutorService sharedExecutor; |
103 | | - private ThreadFactory threadFactory = Executors.defaultThreadFactory(); |
| 115 | + private ThreadFactory threadFactory = Executors.defaultThreadFactory(); |
104 | 116 | // minimises the number of threads rapid closure of many |
105 | 117 | // connections uses, see rabbitmq/rabbitmq-java-client#86 |
106 | 118 | private ExecutorService shutdownExecutor; |
107 | 119 | private ScheduledExecutorService heartbeatExecutor; |
108 | | - private SocketConfigurator socketConf = new DefaultSocketConfigurator(); |
109 | | - private ExceptionHandler exceptionHandler = new DefaultExceptionHandler(); |
| 120 | + private SocketConfigurator socketConf = new DefaultSocketConfigurator(); |
| 121 | + private ExceptionHandler exceptionHandler = new DefaultExceptionHandler(); |
| 122 | + private CredentialsProvider credentialsProvider = new DefaultCredentialsProvider(DEFAULT_USER, DEFAULT_PASS); |
110 | 123 |
|
111 | | - private boolean automaticRecovery = true; |
112 | | - private boolean topologyRecovery = true; |
| 124 | + private boolean automaticRecovery = true; |
| 125 | + private boolean topologyRecovery = true; |
113 | 126 |
|
114 | 127 | // long is used to make sure the users can use both ints |
115 | 128 | // and longs safely. It is unlikely that anybody'd need |
@@ -172,33 +185,50 @@ public void setPort(int port) { |
172 | 185 | * @return the AMQP user name to use when connecting to the broker |
173 | 186 | */ |
174 | 187 | public String getUsername() { |
175 | | - return this.username; |
| 188 | + return credentialsProvider.getUsername(); |
176 | 189 | } |
177 | 190 |
|
178 | 191 | /** |
179 | 192 | * Set the user name. |
180 | 193 | * @param username the AMQP user name to use when connecting to the broker |
181 | 194 | */ |
182 | 195 | public void setUsername(String username) { |
183 | | - this.username = username; |
| 196 | + this.credentialsProvider = new DefaultCredentialsProvider( |
| 197 | + username, |
| 198 | + this.credentialsProvider.getPassword() |
| 199 | + ); |
184 | 200 | } |
185 | 201 |
|
186 | 202 | /** |
187 | 203 | * Retrieve the password. |
188 | 204 | * @return the password to use when connecting to the broker |
189 | 205 | */ |
190 | 206 | public String getPassword() { |
191 | | - return this.password; |
| 207 | + return credentialsProvider.getPassword(); |
192 | 208 | } |
193 | 209 |
|
194 | 210 | /** |
195 | 211 | * Set the password. |
196 | 212 | * @param password the password to use when connecting to the broker |
197 | 213 | */ |
198 | 214 | public void setPassword(String password) { |
199 | | - this.password = password; |
| 215 | + this.credentialsProvider = new DefaultCredentialsProvider( |
| 216 | + this.credentialsProvider.getUsername(), |
| 217 | + password |
| 218 | + ); |
200 | 219 | } |
201 | 220 |
|
| 221 | + /** |
| 222 | + * Set a custom credentials provider. |
| 223 | + * Default implementation uses static username and password. |
| 224 | + * @param credentialsProvider The custom implementation of CredentialsProvider to use when connecting to the broker. |
| 225 | + * @see com.rabbitmq.client.impl.DefaultCredentialsProvider |
| 226 | + * @since 4.5.0 |
| 227 | + */ |
| 228 | + public void setCredentialsProvider(CredentialsProvider credentialsProvider) { |
| 229 | + this.credentialsProvider = credentialsProvider; |
| 230 | + } |
| 231 | + |
202 | 232 | /** |
203 | 233 | * Retrieve the virtual host. |
204 | 234 | * @return the virtual host to use when connecting to the broker |
@@ -954,8 +984,7 @@ public Connection newConnection(ExecutorService executor, AddressResolver addres |
954 | 984 | public ConnectionParams params(ExecutorService consumerWorkServiceExecutor) { |
955 | 985 | ConnectionParams result = new ConnectionParams(); |
956 | 986 |
|
957 | | - result.setUsername(username); |
958 | | - result.setPassword(password); |
| 987 | + result.setCredentialsProvider(credentialsProvider); |
959 | 988 | result.setConsumerWorkServiceExecutor(consumerWorkServiceExecutor); |
960 | 989 | result.setVirtualHost(virtualHost); |
961 | 990 | result.setClientProperties(getClientProperties()); |
@@ -1053,7 +1082,8 @@ protected AddressResolver createAddressResolver(List<Address> addresses) { |
1053 | 1082 |
|
1054 | 1083 | @Override public ConnectionFactory clone(){ |
1055 | 1084 | try { |
1056 | | - return (ConnectionFactory)super.clone(); |
| 1085 | + ConnectionFactory clone = (ConnectionFactory)super.clone(); |
| 1086 | + return clone; |
1057 | 1087 | } catch (CloneNotSupportedException e) { |
1058 | 1088 | throw new Error(e); |
1059 | 1089 | } |
|
0 commit comments