1
1
/*
2
- * Copyright 2010-2011 the original author or authors.
2
+ * Copyright 2010-2013 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
15
15
*/
16
16
package org .springframework .data .mongodb .core ;
17
17
18
- import com . mongodb . MongoOptions ;
18
+ import javax . net . ssl . SSLSocketFactory ;
19
19
20
20
import org .springframework .beans .factory .FactoryBean ;
21
21
import org .springframework .beans .factory .InitializingBean ;
22
22
23
+ import com .mongodb .MongoOptions ;
24
+
23
25
/**
24
- * A factory bean for construction of a MongoOptions instance
26
+ * A factory bean for construction of a {@link MongoOptions} instance.
25
27
*
26
28
* @author Graeme Rocher
27
- * @Author Mark Pollack
29
+ * @author Mark Pollack
30
+ * @author Mike Saavedra
31
+ * @author Thomas Darimont
28
32
*/
29
33
public class MongoOptionsFactoryBean implements FactoryBean <MongoOptions >, InitializingBean {
30
34
31
- private static final MongoOptions MONGO_OPTIONS = new MongoOptions ();
35
+ private final MongoOptions MONGO_OPTIONS = new MongoOptions ();
36
+
32
37
/**
33
- * number of connections allowed per host will block if run out
38
+ * The number of connections allowed per host will block if run out.
34
39
*/
35
40
private int connectionsPerHost = MONGO_OPTIONS .connectionsPerHost ;
36
41
37
42
/**
38
- * multiplier for connectionsPerHost for # of threads that can block if connectionsPerHost is 10, and
39
- * threadsAllowedToBlockForConnectionMultiplier is 5, then 50 threads can block more than that and an exception will
40
- * be throw
43
+ * A multiplier for connectionsPerHost for # of threads that can block a connection.
44
+ * <p>
45
+ * If connectionsPerHost is {@literal 10}, and threadsAllowedToBlockForConnectionMultiplier is {@literal 5}, then
46
+ * {@literal 50} threads can block. If more threads try to block an exception will be thrown.
41
47
*/
42
48
private int threadsAllowedToBlockForConnectionMultiplier = MONGO_OPTIONS .threadsAllowedToBlockForConnectionMultiplier ;
43
49
44
50
/**
45
- * max wait time of a blocking thread for a connection
51
+ * Max wait time of a blocking thread for a connection.
46
52
*/
47
53
private int maxWaitTime = MONGO_OPTIONS .maxWaitTime ;
48
54
49
55
/**
50
- * connect timeout in milliseconds. 0 is default and infinite
56
+ * Connect timeout in milliseconds. {@literal 0} is default and means infinite time.
51
57
*/
52
58
private int connectTimeout = MONGO_OPTIONS .connectTimeout ;
53
59
54
60
/**
55
- * socket timeout. 0 is default and infinite
61
+ * The socket timeout. {@literal 0} is default and means infinite time.
56
62
*/
57
63
private int socketTimeout = MONGO_OPTIONS .socketTimeout ;
58
64
59
65
/**
60
- * This controls whether or not to have socket keep alive turned on (SO_KEEPALIVE).
61
- *
62
- * defaults to false
66
+ * This controls whether or not to have socket keep alive turned on (SO_KEEPALIVE). This defaults to {@literal false}.
63
67
*/
64
68
public boolean socketKeepAlive = MONGO_OPTIONS .socketKeepAlive ;
65
69
66
70
/**
67
- * this controls whether or not on a connect, the system retries automatically
71
+ * This controls whether or not the system retries automatically on a failed connect. This defaults to
72
+ * {@literal false}.
68
73
*/
69
74
private boolean autoConnectRetry = MONGO_OPTIONS .autoConnectRetry ;
70
75
71
76
private long maxAutoConnectRetryTime = MONGO_OPTIONS .maxAutoConnectRetryTime ;
72
77
73
78
/**
74
- * This specifies the number of servers to wait for on the write operation, and exception raising behavior.
75
- *
76
- * Defaults to 0.
79
+ * This specifies the number of servers to wait for on the write operation, and exception raising behavior. This
80
+ * defaults to {@literal 0}.
77
81
*/
78
82
private int writeNumber ;
79
83
80
84
/**
81
- * This controls timeout for write operations in milliseconds.
82
- *
83
- * Defaults to 0 (indefinite). Greater than zero is number of milliseconds to wait.
85
+ * This controls timeout for write operations in milliseconds. This defaults to {@literal 0} (indefinite). Greater
86
+ * than zero is number of milliseconds to wait.
84
87
*/
85
88
private int writeTimeout ;
86
89
87
90
/**
88
- * This controls whether or not to fsync.
89
- *
90
- * Defaults to false.
91
+ * This controls whether or not to fsync. This defaults to {@literal false}.
91
92
*/
92
93
private boolean writeFsync ;
93
94
94
95
/**
95
- * Specifies if the driver is allowed to read from secondaries or slaves.
96
- *
97
- * Defaults to false
96
+ * Specifies if the driver is allowed to read from secondaries or slaves. This defaults to {@literal false}.
98
97
*/
99
- @ SuppressWarnings ("deprecation" )
100
- private boolean slaveOk = MONGO_OPTIONS .slaveOk ;
98
+ @ SuppressWarnings ("deprecation" ) private boolean slaveOk = MONGO_OPTIONS .slaveOk ;
101
99
102
100
/**
103
- * number of connections allowed per host will block if run out
101
+ * This controls SSL support via SSLSocketFactory. This defaults to {@literal false}.
102
+ */
103
+ private boolean ssl ;
104
+
105
+ /**
106
+ * Specifies the {@link SSLSocketFactory} to use. This defaults to {@link SSLSocketFactory#getDefault()}
107
+ */
108
+ private SSLSocketFactory sslSocketFactory ;
109
+
110
+ /**
111
+ * The maximum number of connections allowed per host until we will block.
112
+ *
113
+ * @param connectionsPerHost
104
114
*/
105
115
public void setConnectionsPerHost (int connectionsPerHost ) {
106
116
this .connectionsPerHost = connectionsPerHost ;
107
117
}
108
118
109
119
/**
110
- * multiplier for connectionsPerHost for # of threads that can block if connectionsPerHost is 10, and
111
- * threadsAllowedToBlockForConnectionMultiplier is 5, then 50 threads can block more than that and an exception will
112
- * be throw
120
+ * A multiplier for connectionsPerHost for # of threads that can block a connection.
121
+ *
122
+ * @see #threadsAllowedToBlockForConnectionMultiplier
123
+ * @param threadsAllowedToBlockForConnectionMultiplier
113
124
*/
114
125
public void setThreadsAllowedToBlockForConnectionMultiplier (int threadsAllowedToBlockForConnectionMultiplier ) {
115
126
this .threadsAllowedToBlockForConnectionMultiplier = threadsAllowedToBlockForConnectionMultiplier ;
116
127
}
117
128
118
129
/**
119
- * max wait time of a blocking thread for a connection
130
+ * Max wait time of a blocking thread for a connection.
131
+ *
132
+ * @param maxWaitTime
120
133
*/
121
134
public void setMaxWaitTime (int maxWaitTime ) {
122
135
this .maxWaitTime = maxWaitTime ;
123
136
}
124
137
125
138
/**
126
- * connect timeout in milliseconds. 0 is default and infinite
139
+ * The connect timeout in milliseconds. {@literal 0} is default and infinite
140
+ *
141
+ * @param connectTimeout
127
142
*/
128
143
public void setConnectTimeout (int connectTimeout ) {
129
144
this .connectTimeout = connectTimeout ;
130
145
}
131
146
132
147
/**
133
- * socket timeout. 0 is default and infinite
148
+ * The socket timeout. {@literal 0} is default and infinite.
149
+ *
150
+ * @param socketTimeout
134
151
*/
135
152
public void setSocketTimeout (int socketTimeout ) {
136
153
this .socketTimeout = socketTimeout ;
137
154
}
138
155
139
156
/**
140
- * This controls whether or not to have socket keep alive
157
+ * This controls whether or not to have socket keep alive.
141
158
*
142
159
* @param socketKeepAlive
143
160
*/
@@ -147,12 +164,12 @@ public void setSocketKeepAlive(boolean socketKeepAlive) {
147
164
148
165
/**
149
166
* This specifies the number of servers to wait for on the write operation, and exception raising behavior. The 'w'
150
- * option to the getlasterror command. Defaults to 0 .
167
+ * option to the getlasterror command. Defaults to {@literal 0} .
151
168
* <ul>
152
- * <li>-1 = don't even report network errors</li>
153
- * <li>0 = default, don't call getLastError by default</li>
154
- * <li>1 = basic, call getLastError, but don't wait for slaves</li>
155
- * <li>2 += wait for slaves</li>
169
+ * <li>{@literal -1} = don't even report network errors</li>
170
+ * <li>{@literal 0} = default, don't call getLastError by default</li>
171
+ * <li>{@literal 1} = basic, call getLastError, but don't wait for slaves</li>
172
+ * <li>{@literal 2} += wait for slaves</li>
156
173
* </ul>
157
174
*
158
175
* @param writeNumber the number of servers to wait for on the write operation, and exception raising behavior.
@@ -164,31 +181,33 @@ public void setWriteNumber(int writeNumber) {
164
181
/**
165
182
* This controls timeout for write operations in milliseconds. The 'wtimeout' option to the getlasterror command.
166
183
*
167
- * @param writeTimeout Defaults to 0 (indefinite). Greater than zero is number of milliseconds to wait.
184
+ * @param writeTimeout Defaults to {@literal 0} (indefinite). Greater than zero is number of milliseconds to wait.
168
185
*/
169
186
public void setWriteTimeout (int writeTimeout ) {
170
187
this .writeTimeout = writeTimeout ;
171
188
}
172
189
173
190
/**
174
- * This controls whether or not to fsync. The 'fsync' option to the getlasterror command. Defaults to false.
191
+ * This controls whether or not to fsync. The 'fsync' option to the getlasterror command. Defaults to {@literal false}
175
192
*
176
- * @param writeFsync to fsync on write (true), otherwise false.
193
+ * @param writeFsync to fsync on <code> write (true)<code> , otherwise {@literal false} .
177
194
*/
178
195
public void setWriteFsync (boolean writeFsync ) {
179
196
this .writeFsync = writeFsync ;
180
197
}
181
198
182
199
/**
183
- * this controls whether or not on a connect, the system retries automatically
200
+ * Controls whether or not the system retries automatically, on a failed connect.
201
+ *
202
+ * @param autoConnectRetry
184
203
*/
185
204
public void setAutoConnectRetry (boolean autoConnectRetry ) {
186
205
this .autoConnectRetry = autoConnectRetry ;
187
206
}
188
207
189
208
/**
190
- * The maximum amount of time in millisecons to spend retrying to open connection to the same server. Default is 0,
191
- * which means to use the default 15s if autoConnectRetry is on.
209
+ * The maximum amount of time in millisecons to spend retrying to open connection to the same server. This defaults to
210
+ * {@literal 0}, which means to use the default {@literal 15s} if {@link # autoConnectRetry} is on.
192
211
*
193
212
* @param maxAutoConnectRetryTime the maxAutoConnectRetryTime to set
194
213
*/
@@ -197,16 +216,37 @@ public void setMaxAutoConnectRetryTime(long maxAutoConnectRetryTime) {
197
216
}
198
217
199
218
/**
200
- * Specifies if the driver is allowed to read from secondaries or slaves. Defaults to false.
219
+ * Specifies if the driver is allowed to read from secondaries or slaves. This defaults to {@literal false} .
201
220
*
202
221
* @param slaveOk true if the driver should read from secondaries or slaves.
203
222
*/
204
223
public void setSlaveOk (boolean slaveOk ) {
205
224
this .slaveOk = slaveOk ;
206
225
}
207
226
227
+ /**
228
+ * Specifies if the driver should use an SSL connection to Mongo. This defaults to {@literal false}.
229
+ *
230
+ * @param ssl true if the driver should use an SSL connection.
231
+ */
232
+ public void setSsl (boolean ssl ) {
233
+ this .ssl = ssl ;
234
+ }
235
+
236
+ /**
237
+ * Specifies the SSLSocketFactory to use for creating SSL connections to Mongo.
238
+ *
239
+ * @param sslSocketFactory the sslSocketFactory to use.
240
+ */
241
+ public void setSslSocketFactory (SSLSocketFactory sslSocketFactory ) {
242
+
243
+ setSsl (sslSocketFactory != null );
244
+ this .sslSocketFactory = sslSocketFactory ;
245
+ }
246
+
208
247
@ SuppressWarnings ("deprecation" )
209
248
public void afterPropertiesSet () {
249
+
210
250
MONGO_OPTIONS .connectionsPerHost = connectionsPerHost ;
211
251
MONGO_OPTIONS .threadsAllowedToBlockForConnectionMultiplier = threadsAllowedToBlockForConnectionMultiplier ;
212
252
MONGO_OPTIONS .maxWaitTime = maxWaitTime ;
@@ -219,6 +259,9 @@ public void afterPropertiesSet() {
219
259
MONGO_OPTIONS .w = writeNumber ;
220
260
MONGO_OPTIONS .wtimeout = writeTimeout ;
221
261
MONGO_OPTIONS .fsync = writeFsync ;
262
+ if (ssl ) {
263
+ MONGO_OPTIONS .setSocketFactory (sslSocketFactory != null ? sslSocketFactory : SSLSocketFactory .getDefault ());
264
+ }
222
265
}
223
266
224
267
public MongoOptions getObject () {
@@ -232,5 +275,4 @@ public Class<?> getObjectType() {
232
275
public boolean isSingleton () {
233
276
return true ;
234
277
}
235
-
236
278
}
0 commit comments