36
36
37
37
import java .util .List ;
38
38
39
+ import static com .mongodb .assertions .Assertions .assertNotNull ;
39
40
import static com .mongodb .assertions .Assertions .notNull ;
41
+ import static com .mongodb .internal .async .AsyncRunnable .beginAsync ;
40
42
import static com .mongodb .internal .connection .CommandHelper .HELLO ;
41
43
import static com .mongodb .internal .connection .CommandHelper .LEGACY_HELLO ;
42
44
import static com .mongodb .internal .connection .CommandHelper .executeCommand ;
@@ -73,9 +75,18 @@ public InternalStreamConnectionInitializer(final ClusterConnectionMode clusterCo
73
75
74
76
@ Override
75
77
public InternalConnectionInitializationDescription startHandshake (final InternalConnection internalConnection ) {
76
- notNull ( "internalConnection" , internalConnection );
78
+ long startTime = System . nanoTime ( );
77
79
78
- return initializeConnectionDescription (internalConnection );
80
+ notNull ("internalConnection" , internalConnection );
81
+ BsonDocument helloCommandDocument = createHelloCommand (authenticator , internalConnection );
82
+ BsonDocument helloResult ;
83
+ try {
84
+ helloResult = executeCommand ("admin" , helloCommandDocument , clusterConnectionMode , serverApi , internalConnection );
85
+ } catch (MongoException e ) {
86
+ throw mapHelloException (e );
87
+ }
88
+ setSpeculativeAuthenticateResponse (helloResult );
89
+ return createInitializationDescription (helloResult , internalConnection , startTime );
79
90
}
80
91
81
92
public InternalConnectionInitializationDescription finishHandshake (final InternalConnection internalConnection ,
@@ -91,15 +102,19 @@ public InternalConnectionInitializationDescription finishHandshake(final Interna
91
102
public void startHandshakeAsync (final InternalConnection internalConnection ,
92
103
final SingleResultCallback <InternalConnectionInitializationDescription > callback ) {
93
104
long startTime = System .nanoTime ();
94
- executeCommandAsync ("admin" , createHelloCommand (authenticator , internalConnection ), clusterConnectionMode , serverApi ,
95
- internalConnection , (helloResult , t ) -> {
96
- if (t != null ) {
97
- callback .onResult (null , t instanceof MongoException ? mapHelloException ((MongoException ) t ) : t );
98
- } else {
99
- setSpeculativeAuthenticateResponse (helloResult );
100
- callback .onResult (createInitializationDescription (helloResult , internalConnection , startTime ), null );
101
- }
102
- });
105
+ beginAsync ().<InternalConnectionInitializationDescription >thenSupply (c -> {
106
+ notNull ("internalConnection" , internalConnection );
107
+ BsonDocument helloCommandDocument = createHelloCommand (authenticator , internalConnection );
108
+
109
+ beginAsync ().<BsonDocument >thenSupply (c2 -> {
110
+ executeCommandAsync ("admin" , helloCommandDocument , clusterConnectionMode , serverApi , internalConnection , c2 );
111
+ }).onErrorIf (e -> e instanceof MongoException , (t , c2 ) -> {
112
+ throw mapHelloException ((MongoException ) assertNotNull (t ));
113
+ }).thenApply ((helloResult , c2 ) -> {
114
+ setSpeculativeAuthenticateResponse (assertNotNull (helloResult ));
115
+ callback .complete (createInitializationDescription (helloResult , internalConnection , startTime ));
116
+ });
117
+ }).finish (callback );
103
118
}
104
119
105
120
@ Override
@@ -121,20 +136,6 @@ public void finishHandshakeAsync(final InternalConnection internalConnection,
121
136
}
122
137
}
123
138
124
- private InternalConnectionInitializationDescription initializeConnectionDescription (final InternalConnection internalConnection ) {
125
- BsonDocument helloResult ;
126
- BsonDocument helloCommandDocument = createHelloCommand (authenticator , internalConnection );
127
-
128
- long start = System .nanoTime ();
129
- try {
130
- helloResult = executeCommand ("admin" , helloCommandDocument , clusterConnectionMode , serverApi , internalConnection );
131
- } catch (MongoException e ) {
132
- throw mapHelloException (e );
133
- }
134
- setSpeculativeAuthenticateResponse (helloResult );
135
- return createInitializationDescription (helloResult , internalConnection , start );
136
- }
137
-
138
139
private MongoException mapHelloException (final MongoException e ) {
139
140
if (checkSaslSupportedMechs && e .getCode () == USER_NOT_FOUND_CODE ) {
140
141
MongoCredential credential = authenticator .getMongoCredential ();
0 commit comments