1717import io .vertx .core .impl .ContextInternal ;
1818import io .vertx .core .impl .EventLoopContext ;
1919import io .vertx .core .impl .VertxInternal ;
20+ import io .vertx .core .net .NetClient ;
2021import io .vertx .core .net .NetClientOptions ;
2122import io .vertx .core .net .NetSocket ;
2223import io .vertx .core .net .SocketAddress ;
2829import io .vertx .sqlclient .impl .ConnectionFactoryBase ;
2930import io .vertx .sqlclient .impl .tracing .QueryTracer ;
3031
32+ import java .util .Map ;
33+ import java .util .function .Supplier ;
34+
3135import static io .vertx .mssqlclient .impl .codec .EncryptionLevel .*;
3236
3337public class MSSQLConnectionFactory extends ConnectionFactoryBase {
3438
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 ) {
3940 super (vertx , options );
40- desiredPacketSize = options .getPacketSize ();
41- clientConfigSsl = options .isSsl ();
4241 }
4342
4443 @ Override
@@ -48,23 +47,27 @@ protected void initializeConfiguration(SqlConnectOptions options) {
4847
4948 @ Override
5049 protected void configureNetClientOptions (NetClientOptions netClientOptions ) {
51- // Always start unencrypted, the connection will be upgraded if client and server agree
5250 netClientOptions .setSsl (false );
5351 }
5452
5553 @ 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 );
5856 }
5957
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 ) {
6159 if (redirections > 1 ) {
6260 return context .failedFuture ("The client can be redirected only once" );
6361 }
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 ));
6467 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 ))
6871 )
6972 .compose (connBase -> {
7073 MSSQLSocketConnection conn = (MSSQLSocketConnection ) connBase ;
@@ -74,39 +77,44 @@ private Future<Connection> connectOrRedirect(SocketAddress server, String userna
7477 }
7578 Promise <Void > closePromise = context .promise ();
7679 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 ));
7881 });
7982 }
8083
81- private MSSQLSocketConnection createSocketConnection (NetSocket so , EventLoopContext context ) {
84+ private MSSQLSocketConnection createSocketConnection (NetSocket so , int desiredPacketSize , EventLoopContext context ) {
8285 MSSQLSocketConnection conn = new MSSQLSocketConnection ((NetSocketInternal ) so , desiredPacketSize , false , 0 , sql -> true , 1 , context );
8386 conn .init ();
8487 return conn ;
8588 }
8689
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 ) {
8993 Promise <Void > closePromise = context .promise ();
9094 conn .close (null , closePromise );
9195 return closePromise .future ().transform (v -> context .failedFuture ("The client is configured for encryption but the server does not support it" ));
9296 }
9397 Future <Void > future ;
9498 if (encryptionLevel != ENCRYPT_NOT_SUP ) {
9599 // Start connection encryption ...
96- future = conn .enableSsl (clientConfigSsl , encryptionLevel , (MSSQLConnectOptions ) options );
100+ future = conn .enableSsl (clientSslConfig , encryptionLevel , (MSSQLConnectOptions ) options );
97101 } else {
98102 // ... unless the client did not require encryption and the server does not support it
99103 future = context .succeededFuture ();
100104 }
105+ String username = options .getUser ();
106+ String password = options .getPassword ();
107+ String database = options .getDatabase ();
108+ Map <String , String > properties = options .getProperties ();
101109 return future .compose (v -> conn .sendLoginMessage (username , password , database , properties ));
102110 }
103111
104112 @ Override
105- public Future <SqlConnection > connect (Context context ) {
113+ public Future <SqlConnection > connect (Context context , SqlConnectOptions options ) {
106114 ContextInternal ctx = (ContextInternal ) context ;
107115 QueryTracer tracer = ctx .tracer () == null ? null : new QueryTracer (ctx .tracer (), options );
108116 Promise <SqlConnection > promise = ctx .promise ();
109- connect (asEventLoopContext (ctx ))
117+ connect (asEventLoopContext (ctx ), options )
110118 .map (conn -> {
111119 MSSQLConnectionImpl msConn = new MSSQLConnectionImpl (ctx , this , conn , tracer , null );
112120 conn .init (msConn );
0 commit comments