@@ -86,10 +86,20 @@ public class DBPort implements Connection {
86
86
*/
87
87
@ SuppressWarnings ("deprecation" )
88
88
public DBPort ( ServerAddress addr ){
89
- this ( addr , null , new MongoOptions (), 0 );
89
+ this ( addr , new MongoOptions ());
90
90
}
91
-
92
- DBPort ( ServerAddress addr , PooledConnectionProvider pool , MongoOptions options , int generation ) {
91
+
92
+ // Normal usage
93
+ DBPort ( ServerAddress addr , PooledConnectionProvider pool , Mongo mongo , int generation ) {
94
+ this (addr , pool , mongo , mongo .getMongoOptions (), generation );
95
+ }
96
+
97
+ // Server monitor usage
98
+ DBPort ( ServerAddress addr , MongoOptions options ) {
99
+ this (addr , null , null , options , 0 );
100
+ }
101
+
102
+ private DBPort ( ServerAddress addr , PooledConnectionProvider pool , Mongo mongo , MongoOptions options , int generation ) {
93
103
_options = options ;
94
104
_sa = addr ;
95
105
_addr = addr ;
@@ -98,8 +108,8 @@ public DBPort( ServerAddress addr ){
98
108
99
109
_logger = Logger .getLogger ( _rootLogger .getName () + "." + addr .toString () );
100
110
try {
101
- ensureOpen ();
102
111
_decoder = _options .dbDecoderFactory .create ();
112
+ ensureOpen (mongo );
103
113
openedAt = System .currentTimeMillis ();
104
114
lastUsedAt = openedAt ;
105
115
} catch (IOException e ) {
@@ -248,6 +258,15 @@ InputStream getInputStream() throws IOException {
248
258
* @throws IOException
249
259
*/
250
260
public synchronized void ensureOpen () throws IOException {
261
+ ensureOpen (null );
262
+ }
263
+
264
+ /**
265
+ * makes sure that a connection to the server has been opened
266
+ * @throws IOException
267
+ * @param mongo
268
+ */
269
+ private synchronized void ensureOpen (final Mongo mongo ) throws IOException {
251
270
252
271
if ( _socket != null )
253
272
return ;
@@ -271,6 +290,9 @@ public synchronized void ensureOpen() throws IOException {
271
290
_socket .setSoTimeout ( _options .socketTimeout );
272
291
_in = new BufferedInputStream ( _socket .getInputStream () );
273
292
_out = _socket .getOutputStream ();
293
+ if (mongo != null ) {
294
+ _serverVersion = ServerMonitor .getVersion (runCommand (mongo .getDB ("admin" ), new BasicDBObject ("buildinfo" , 1 )));
295
+ }
274
296
successfullyConnected = true ;
275
297
}
276
298
catch ( IOException e ){
@@ -364,7 +386,7 @@ CommandResult authenticate(Mongo mongo, final MongoCredential credentials) {
364
386
Authenticator authenticator ;
365
387
MongoCredential actualCredentials ;
366
388
if (credentials .getMechanism () == null ) {
367
- if (mongo . getConnector (). getServerDescription ( getAddress ()). getVersion () .compareTo (new ServerVersion (3 , 0 )) >= 0 ) {
389
+ if (_serverVersion .compareTo (new ServerVersion (3 , 0 )) >= 0 ) {
368
390
actualCredentials = MongoCredential .createScramSha1Credential (credentials .getUserName (), credentials .getSource (),
369
391
credentials .getPassword ());
370
392
} else {
@@ -415,6 +437,10 @@ public long getUsageCount() {
415
437
return usageCount ;
416
438
}
417
439
440
+ ServerVersion getServerVersion () {
441
+ return _serverVersion ;
442
+ }
443
+
418
444
PooledConnectionProvider getProvider () {
419
445
return provider ;
420
446
}
@@ -440,6 +466,7 @@ Set<String> getAuthenticatedDatabases() {
440
466
private volatile Socket _socket ;
441
467
private volatile InputStream _in ;
442
468
private volatile OutputStream _out ;
469
+ private volatile ServerVersion _serverVersion ;
443
470
444
471
// needs synchronization to ensure that modifications are published.
445
472
private final Set <String > authenticatedDatabases = Collections .synchronizedSet (new HashSet <String >());
0 commit comments