@@ -86,10 +86,20 @@ public class DBPort implements Connection {
8686 */
8787 @ SuppressWarnings ("deprecation" )
8888 public DBPort ( ServerAddress addr ){
89- this ( addr , null , new MongoOptions (), 0 );
89+ this ( addr , new MongoOptions ());
9090 }
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 ) {
93103 _options = options ;
94104 _sa = addr ;
95105 _addr = addr ;
@@ -98,8 +108,8 @@ public DBPort( ServerAddress addr ){
98108
99109 _logger = Logger .getLogger ( _rootLogger .getName () + "." + addr .toString () );
100110 try {
101- ensureOpen ();
102111 _decoder = _options .dbDecoderFactory .create ();
112+ ensureOpen (mongo );
103113 openedAt = System .currentTimeMillis ();
104114 lastUsedAt = openedAt ;
105115 } catch (IOException e ) {
@@ -248,6 +258,15 @@ InputStream getInputStream() throws IOException {
248258 * @throws IOException
249259 */
250260 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 {
251270
252271 if ( _socket != null )
253272 return ;
@@ -271,6 +290,9 @@ public synchronized void ensureOpen() throws IOException {
271290 _socket .setSoTimeout ( _options .socketTimeout );
272291 _in = new BufferedInputStream ( _socket .getInputStream () );
273292 _out = _socket .getOutputStream ();
293+ if (mongo != null ) {
294+ _serverVersion = ServerMonitor .getVersion (runCommand (mongo .getDB ("admin" ), new BasicDBObject ("buildinfo" , 1 )));
295+ }
274296 successfullyConnected = true ;
275297 }
276298 catch ( IOException e ){
@@ -364,7 +386,7 @@ CommandResult authenticate(Mongo mongo, final MongoCredential credentials) {
364386 Authenticator authenticator ;
365387 MongoCredential actualCredentials ;
366388 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 ) {
368390 actualCredentials = MongoCredential .createScramSha1Credential (credentials .getUserName (), credentials .getSource (),
369391 credentials .getPassword ());
370392 } else {
@@ -415,6 +437,10 @@ public long getUsageCount() {
415437 return usageCount ;
416438 }
417439
440+ ServerVersion getServerVersion () {
441+ return _serverVersion ;
442+ }
443+
418444 PooledConnectionProvider getProvider () {
419445 return provider ;
420446 }
@@ -440,6 +466,7 @@ Set<String> getAuthenticatedDatabases() {
440466 private volatile Socket _socket ;
441467 private volatile InputStream _in ;
442468 private volatile OutputStream _out ;
469+ private volatile ServerVersion _serverVersion ;
443470
444471 // needs synchronization to ensure that modifications are published.
445472 private final Set <String > authenticatedDatabases = Collections .synchronizedSet (new HashSet <String >());
0 commit comments