forked from eclipse-vertx/vertx-sql-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQLConnectionFactory.java
More file actions
157 lines (145 loc) · 6.95 KB
/
MySQLConnectionFactory.java
File metadata and controls
157 lines (145 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Copyright (c) 2011-2020 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.mysqlclient.impl;
import io.vertx.core.Context;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.internal.ContextInternal;
import io.vertx.core.internal.VertxInternal;
import io.vertx.core.internal.net.NetSocketInternal;
import io.vertx.core.net.*;
import io.vertx.core.spi.metrics.ClientMetrics;
import io.vertx.core.spi.metrics.VertxMetrics;
import io.vertx.mysqlclient.MySQLAuthenticationPlugin;
import io.vertx.mysqlclient.MySQLConnectOptions;
import io.vertx.mysqlclient.SslMode;
import io.vertx.sqlclient.SqlConnection;
import io.vertx.sqlclient.impl.ConnectionFactoryBase;
import io.vertx.sqlclient.internal.Connection;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.function.Predicate;
import static io.vertx.mysqlclient.impl.protocol.CapabilitiesFlag.*;
public class MySQLConnectionFactory extends ConnectionFactoryBase<MySQLConnectOptions> {
public MySQLConnectionFactory(VertxInternal vertx) {
super(vertx);
}
public MySQLConnectionFactory(VertxInternal vertx, NetClientOptions tranportOptions) {
super(vertx, tranportOptions);
}
@Override
protected Future<Connection> doConnectInternal(MySQLConnectOptions options, ContextInternal context) {
SslMode sslMode = options.isUsingDomainSocket() ? SslMode.DISABLED : options.getSslMode();
ClientSSLOptions sslOptions = options.getSslOptions();
switch (sslMode) {
case VERIFY_IDENTITY:
String hostnameVerificationAlgorithm = sslOptions.getHostnameVerificationAlgorithm();
if (hostnameVerificationAlgorithm == null || hostnameVerificationAlgorithm.isEmpty()) {
return context.failedFuture(new IllegalArgumentException("Host verification algorithm must be specified under VERIFY_IDENTITY ssl-mode."));
}
break;
case VERIFY_CA:
TrustOptions trustOptions = sslOptions.getTrustOptions();
if (trustOptions == null) {
return context.failedFuture(new IllegalArgumentException("Trust options must be specified under " + sslMode.name() + " ssl-mode."));
}
break;
case DISABLED:
sslOptions = null;
break;
}
if (sslOptions != null && sslOptions.getHostnameVerificationAlgorithm() == null) {
sslOptions.setHostnameVerificationAlgorithm("");
}
int capabilitiesFlag = capabilitiesFlags(options);
if (sslMode == SslMode.PREFERRED) {
return doConnect(options, sslMode, sslOptions, capabilitiesFlag, context).recover(err -> doConnect(options, SslMode.DISABLED, null, capabilitiesFlag, context));
} else {
return doConnect(options, sslMode, sslOptions, capabilitiesFlag, context);
}
}
private int capabilitiesFlags(MySQLConnectOptions options) {
int capabilitiesFlags = CLIENT_SUPPORTED_CAPABILITIES_FLAGS;
if (options.getDatabase() != null && !options.getDatabase().isEmpty()) {
capabilitiesFlags |= CLIENT_CONNECT_WITH_DB;
}
if (options.getProperties() != null && !options.getProperties().isEmpty()) {
capabilitiesFlags |= CLIENT_CONNECT_ATTRS;
}
if (!options.isUseAffectedRows()) {
capabilitiesFlags |= CLIENT_FOUND_ROWS;
}
return capabilitiesFlags;
}
private Future<Connection> doConnect(MySQLConnectOptions options, SslMode sslMode, ClientSSLOptions sslOptions, int initialCapabilitiesFlags, ContextInternal context) {
String username = options.getUser();
String password = options.getPassword();
String database = options.getDatabase();
SocketAddress server = options.getSocketAddress();
boolean cachePreparedStatements = options.getCachePreparedStatements();
int preparedStatementCacheMaxSize = options.getPreparedStatementCacheMaxSize();
Predicate<String> preparedStatementCacheSqlFilter = options.getPreparedStatementCacheSqlFilter();
Map<String, String> properties = options.getProperties();
MySQLCollation collation;
Charset charsetEncoding;
if (options.getCollation() != null) {
// override the collation if configured
collation = MySQLCollation.valueOfName(options.getCollation());
charsetEncoding = Charset.forName(collation.mappedJavaCharsetName());
} else {
String charset = options.getCharset();
if (charset == null) {
collation = MySQLCollation.DEFAULT_COLLATION;
} else {
collation = MySQLCollation.valueOfName(MySQLCollation.getDefaultCollationFromCharsetName(charset));
}
String characterEncoding = options.getCharacterEncoding();
if (characterEncoding == null) {
charsetEncoding = Charset.defaultCharset();
} else {
charsetEncoding = Charset.forName(options.getCharacterEncoding());
}
}
Buffer serverRsaPublicKey;
if (options.getServerRsaPublicKeyValue() != null) {
serverRsaPublicKey = options.getServerRsaPublicKeyValue();
} else if (options.getServerRsaPublicKeyPath() != null) {
serverRsaPublicKey = vertx.fileSystem().readFileBlocking(options.getServerRsaPublicKeyPath());
} else {
serverRsaPublicKey = null;
}
int pipeliningLimit = options.getPipeliningLimit();
MySQLAuthenticationPlugin authenticationPlugin = options.getAuthenticationPlugin();
ConnectOptions connectOptions = new ConnectOptions().setRemoteAddress(server);
Future<NetSocket> fut = client.connect(connectOptions);
return fut.flatMap(so -> {
VertxMetrics vertxMetrics = vertx.metrics();
ClientMetrics metrics = vertxMetrics != null ? vertxMetrics.createClientMetrics(options.getSocketAddress(), "sql", options.getMetricsName()) : null;
MySQLSocketConnection conn = new MySQLSocketConnection((NetSocketInternal) so, metrics, options, cachePreparedStatements, preparedStatementCacheMaxSize, preparedStatementCacheSqlFilter, pipeliningLimit, context);
conn.init();
return Future.future(promise -> conn.sendStartupMessage(username, password, database, collation, serverRsaPublicKey, properties, sslMode, sslOptions, initialCapabilitiesFlags, charsetEncoding, authenticationPlugin, promise));
});
}
@Override
public Future<SqlConnection> connect(Context context, MySQLConnectOptions options) {
ContextInternal contextInternal = (ContextInternal) context;
Promise<SqlConnection> promise = contextInternal.promise();
connect(asEventLoopContext(contextInternal), options)
.map(conn -> {
MySQLConnectionImpl mySQLConnection = new MySQLConnectionImpl(contextInternal, this, conn);
conn.init(mySQLConnection);
return (SqlConnection)mySQLConnection;
})
.onComplete(promise);
return promise.future();
}
}