-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathMySQLConnectionFactory.java
More file actions
151 lines (140 loc) · 6.79 KB
/
Copy pathMySQLConnectionFactory.java
File metadata and controls
151 lines (140 loc) · 6.79 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
/*
* 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.impl.ContextInternal;
import io.vertx.core.impl.VertxInternal;
import io.vertx.core.net.NetClientOptions;
import io.vertx.core.net.NetSocket;
import io.vertx.core.net.SocketAddress;
import io.vertx.core.net.TrustOptions;
import io.vertx.core.net.impl.NetSocketInternal;
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.SqlConnectOptions;
import io.vertx.sqlclient.SqlConnection;
import io.vertx.sqlclient.impl.Connection;
import io.vertx.sqlclient.impl.ConnectionFactoryBase;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.function.Predicate;
import java.util.function.Supplier;
import static io.vertx.mysqlclient.impl.protocol.CapabilitiesFlag.*;
public class MySQLConnectionFactory extends ConnectionFactoryBase {
public MySQLConnectionFactory(VertxInternal vertx, Supplier<? extends Future<? extends SqlConnectOptions>> options) {
super(vertx, options);
}
@Override
protected Future<Connection> doConnectInternal(SqlConnectOptions options, ContextInternal context) {
MySQLConnectOptions mySQLOptions = MySQLConnectOptions.wrap(options);
SslMode sslMode = mySQLOptions.isUsingDomainSocket() ? SslMode.DISABLED : mySQLOptions.getSslMode();
switch (sslMode) {
case VERIFY_IDENTITY:
String hostnameVerificationAlgorithm = options.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 = options.getTrustOptions();
if (trustOptions == null) {
return context.failedFuture(new IllegalArgumentException("Trust options must be specified under " + sslMode.name() + " ssl-mode."));
}
break;
}
int capabilitiesFlag = capabilitiesFlags(mySQLOptions);
if (sslMode == SslMode.PREFERRED) {
return doConnect(mySQLOptions, sslMode, capabilitiesFlag, context).recover(err -> doConnect(mySQLOptions, SslMode.DISABLED, capabilitiesFlag, context));
} else {
return doConnect(mySQLOptions, sslMode, 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, 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();
Future<NetSocket> fut = netClient(new NetClientOptions(options).setSsl(false)).connect(server);
return fut.flatMap(so -> {
VertxMetrics vertxMetrics = vertx.metricsSPI();
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, initialCapabilitiesFlags, charsetEncoding, authenticationPlugin, promise));
});
}
@Override
public Future<SqlConnection> connect(Context context, SqlConnectOptions 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();
}
}