17
17
import io .vertx .core .impl .ContextInternal ;
18
18
import io .vertx .core .impl .EventLoopContext ;
19
19
import io .vertx .core .impl .VertxInternal ;
20
+ import io .vertx .core .net .NetClient ;
20
21
import io .vertx .core .net .NetClientOptions ;
21
22
import io .vertx .core .net .NetSocket ;
22
23
import io .vertx .core .net .SocketAddress ;
28
29
import io .vertx .sqlclient .impl .ConnectionFactoryBase ;
29
30
import io .vertx .sqlclient .impl .tracing .QueryTracer ;
30
31
32
+ import java .util .Map ;
33
+ import java .util .function .Supplier ;
34
+
31
35
import static io .vertx .mssqlclient .impl .codec .EncryptionLevel .*;
32
36
33
37
public class MSSQLConnectionFactory extends ConnectionFactoryBase {
34
38
35
- private final int desiredPacketSize ;
36
- private final boolean clientConfigSsl ;
37
-
38
- public MSSQLConnectionFactory (VertxInternal vertx , MSSQLConnectOptions options ) {
39
+ public MSSQLConnectionFactory (VertxInternal vertx , Supplier <MSSQLConnectOptions > options ) {
39
40
super (vertx , options );
40
- desiredPacketSize = options .getPacketSize ();
41
- clientConfigSsl = options .isSsl ();
42
41
}
43
42
44
43
@ Override
@@ -48,23 +47,27 @@ protected void initializeConfiguration(SqlConnectOptions options) {
48
47
49
48
@ Override
50
49
protected void configureNetClientOptions (NetClientOptions netClientOptions ) {
51
- // Always start unencrypted, the connection will be upgraded if client and server agree
52
50
netClientOptions .setSsl (false );
53
51
}
54
52
55
53
@ Override
56
- protected Future <Connection > doConnectInternal (SocketAddress server , String username , String password , String database , EventLoopContext context ) {
57
- return connectOrRedirect (server , username , password , database , context , 0 );
54
+ protected Future <Connection > doConnectInternal (SqlConnectOptions options , EventLoopContext context ) {
55
+ return connectOrRedirect (( MSSQLConnectOptions ) options , context , 0 );
58
56
}
59
57
60
- private Future <Connection > connectOrRedirect (SocketAddress server , String username , String password , String database , EventLoopContext context , int redirections ) {
58
+ private Future <Connection > connectOrRedirect (MSSQLConnectOptions options , EventLoopContext context , int redirections ) {
61
59
if (redirections > 1 ) {
62
60
return context .failedFuture ("The client can be redirected only once" );
63
61
}
62
+ SocketAddress server = options .getSocketAddress ();
63
+ boolean clientSslConfig = options .isSsl ();
64
+ int desiredPacketSize = options .getPacketSize ();
65
+ // Always start unencrypted, the connection will be upgraded if client and server agree
66
+ NetClient netClient = netClient (new NetClientOptions (options ).setSsl (false ));
64
67
return netClient .connect (server )
65
- .map (so -> createSocketConnection (so , context ))
66
- .compose (conn -> conn .sendPreLoginMessage (clientConfigSsl )
67
- .compose (encryptionLevel -> login (conn , username , password , database , encryptionLevel , context ))
68
+ .map (so -> createSocketConnection (so , desiredPacketSize , context ))
69
+ .compose (conn -> conn .sendPreLoginMessage (clientSslConfig )
70
+ .compose (encryptionLevel -> login (conn , options , encryptionLevel , context ))
68
71
)
69
72
.compose (connBase -> {
70
73
MSSQLSocketConnection conn = (MSSQLSocketConnection ) connBase ;
@@ -74,39 +77,44 @@ private Future<Connection> connectOrRedirect(SocketAddress server, String userna
74
77
}
75
78
Promise <Void > closePromise = context .promise ();
76
79
conn .close (null , closePromise );
77
- return closePromise .future ().transform (v -> connectOrRedirect (alternateServer , username , password , database , context , redirections + 1 ));
80
+ return closePromise .future ().transform (v -> connectOrRedirect (options , context , redirections + 1 ));
78
81
});
79
82
}
80
83
81
- private MSSQLSocketConnection createSocketConnection (NetSocket so , EventLoopContext context ) {
84
+ private MSSQLSocketConnection createSocketConnection (NetSocket so , int desiredPacketSize , EventLoopContext context ) {
82
85
MSSQLSocketConnection conn = new MSSQLSocketConnection ((NetSocketInternal ) so , desiredPacketSize , false , 0 , sql -> true , 1 , context );
83
86
conn .init ();
84
87
return conn ;
85
88
}
86
89
87
- private Future <Connection > login (MSSQLSocketConnection conn , String username , String password , String database , Byte encryptionLevel , EventLoopContext context ) {
88
- if (clientConfigSsl && encryptionLevel != ENCRYPT_ON && encryptionLevel != ENCRYPT_REQ ) {
90
+ private Future <Connection > login (MSSQLSocketConnection conn , MSSQLConnectOptions options , Byte encryptionLevel , EventLoopContext context ) {
91
+ boolean clientSslConfig = options .isSsl ();
92
+ if (clientSslConfig && encryptionLevel != ENCRYPT_ON && encryptionLevel != ENCRYPT_REQ ) {
89
93
Promise <Void > closePromise = context .promise ();
90
94
conn .close (null , closePromise );
91
95
return closePromise .future ().transform (v -> context .failedFuture ("The client is configured for encryption but the server does not support it" ));
92
96
}
93
97
Future <Void > future ;
94
98
if (encryptionLevel != ENCRYPT_NOT_SUP ) {
95
99
// Start connection encryption ...
96
- future = conn .enableSsl (clientConfigSsl , encryptionLevel , (MSSQLConnectOptions ) options );
100
+ future = conn .enableSsl (clientSslConfig , encryptionLevel , (MSSQLConnectOptions ) options );
97
101
} else {
98
102
// ... unless the client did not require encryption and the server does not support it
99
103
future = context .succeededFuture ();
100
104
}
105
+ String username = options .getUser ();
106
+ String password = options .getPassword ();
107
+ String database = options .getDatabase ();
108
+ Map <String , String > properties = options .getProperties ();
101
109
return future .compose (v -> conn .sendLoginMessage (username , password , database , properties ));
102
110
}
103
111
104
112
@ Override
105
- public Future <SqlConnection > connect (Context context ) {
113
+ public Future <SqlConnection > connect (Context context , SqlConnectOptions options ) {
106
114
ContextInternal ctx = (ContextInternal ) context ;
107
115
QueryTracer tracer = ctx .tracer () == null ? null : new QueryTracer (ctx .tracer (), options );
108
116
Promise <SqlConnection > promise = ctx .promise ();
109
- connect (asEventLoopContext (ctx ))
117
+ connect (asEventLoopContext (ctx ), options )
110
118
.map (conn -> {
111
119
MSSQLConnectionImpl msConn = new MSSQLConnectionImpl (ctx , this , conn , tracer , null );
112
120
conn .init (msConn );
0 commit comments