Skip to content

Commit

Permalink
[misc] connection refactoring for simplification
Browse files Browse the repository at this point in the history
This will a future proxy redirection evolution
  • Loading branch information
rusher committed Sep 13, 2019
1 parent 924d362 commit 90b684c
Show file tree
Hide file tree
Showing 10 changed files with 373 additions and 320 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<artifactId>mariadb-java-client</artifactId>
<packaging>jar</packaging>
<name>mariadb-java-client</name>
<version>2.4.3</version>
<version>2.5.0-SNAPSHOT</version>
<description>JDBC driver for MariaDB and MySQL</description>
<url>https://mariadb.com/kb/en/mariadb/about-mariadb-connector-j/</url>

Expand All @@ -70,9 +70,9 @@
<checkstyleVersion>8.18</checkstyleVersion>
<checkstyle.plugin.version>2.17</checkstyle.plugin.version>
<driver.version.major>2</driver.version.major>
<driver.version.minor>4</driver.version.minor>
<driver.version.patch>3</driver.version.patch>
<driver.version.qualifier></driver.version.qualifier>
<driver.version.minor>5</driver.version.minor>
<driver.version.patch>0</driver.version.patch>
<driver.version.qualifier>-SNAPSHOT</driver.version.qualifier>
</properties>

<licenses>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,9 @@
import java.util.StringTokenizer;
import java.util.function.Supplier;

import org.mariadb.jdbc.HostAddress;
import org.mariadb.jdbc.MariaDbDatabaseMetaData;
import org.mariadb.jdbc.internal.MariaDbServerCapabilities;
import org.mariadb.jdbc.internal.com.read.Buffer;
import org.mariadb.jdbc.internal.com.read.ReadInitialHandShakePacket;
import org.mariadb.jdbc.internal.io.output.PacketOutputStream;
import org.mariadb.jdbc.internal.protocol.authentication.DefaultAuthenticationProvider;
import org.mariadb.jdbc.internal.util.Options;
Expand Down Expand Up @@ -90,40 +88,41 @@ public class SendHandshakeResponsePacket {
* @param pos output stream
* @param username user name
* @param password password
* @param currentHost current hostname
* @param host current hostname
* @param database database name
* @param clientCapabilities client capabilities
* @param serverCapabilities server capabilities
* @param serverLanguage server language (utf8 / utf8mb4 collation)
* @param packetSeq packet sequence
* @param options user options
* @param greetingPacket server handshake packet information
* @param pluginName plugin name
* @param seed seed
* @throws IOException if socket exception occur
* @see <a href="https://mariadb.com/kb/en/mariadb/1-connecting-connecting/#handshake-response-packet">protocol
* documentation</a>
*/
public static void send(final PacketOutputStream pos,
final String username,
final String password,
final HostAddress currentHost,
final String host,
final String database,
final long clientCapabilities,
final long serverCapabilities,
final byte serverLanguage,
final byte packetSeq,
final Options options,
final ReadInitialHandShakePacket greetingPacket) throws IOException {
final String pluginName,
final byte[] seed) throws IOException {

pos.startPacket(packetSeq);

final byte[] authData;
switch (greetingPacket.getPluginName()) {
switch (pluginName) {
case "": //CONJ-274 : permit connection mysql 5.1 db
case DefaultAuthenticationProvider.MYSQL_NATIVE_PASSWORD:
pos.permitTrace(false);
try {
authData = Utils.encryptPassword(password, greetingPacket.getSeed(),
options.passwordCharacterEncoding);
authData = Utils.encryptPassword(password, seed, options.passwordCharacterEncoding);
break;
} catch (NoSuchAlgorithmException e) {
//cannot occur :
Expand Down Expand Up @@ -174,20 +173,20 @@ public static void send(final PacketOutputStream pos,
}

if ((serverCapabilities & MariaDbServerCapabilities.PLUGIN_AUTH) != 0) {
pos.write(greetingPacket.getPluginName());
pos.write(pluginName);
pos.write((byte) 0);
}

if ((serverCapabilities & MariaDbServerCapabilities.CONNECT_ATTRS) != 0) {
writeConnectAttributes(pos, options.connectionAttributes, currentHost);
writeConnectAttributes(pos, options.connectionAttributes, host);
}

pos.flush();
pos.permitTrace(true);
}

private static void writeConnectAttributes(PacketOutputStream pos, String connectionAttributes,
HostAddress currentHost) throws IOException {
String host) throws IOException {
Buffer buffer = new Buffer(new byte[200]);

buffer.writeStringSmallLength(_CLIENT_NAME);
Expand All @@ -197,7 +196,7 @@ private static void writeConnectAttributes(PacketOutputStream pos, String connec
buffer.writeStringLength(Version.version);

buffer.writeStringSmallLength(_SERVER_HOST);
buffer.writeStringLength((currentHost != null) ? currentHost.host : "");
buffer.writeStringLength((host != null) ? host : "");

buffer.writeStringSmallLength(_OS);
buffer.writeStringLength(System.getProperty("os.name"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import java.io.IOException;
import java.net.Socket;
import org.mariadb.jdbc.UrlParser;
import org.mariadb.jdbc.internal.util.Options;

@FunctionalInterface
public interface SocketHandlerFunction {

Socket apply(UrlParser urlParser, String host) throws IOException;
Socket apply(Options options, String host) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,29 @@ public static SocketHandlerFunction getSocketHandler() {
//forcing use of JNA to ensure AOT compilation
Platform.getOSType();

return (urlParser, host) -> {
if (urlParser.getOptions().pipe != null) {
return new NamedPipeSocket(host, urlParser.getOptions().pipe);
} else if (urlParser.getOptions().localSocket != null) {
return (options, host) -> {
if (options.pipe != null) {
return new NamedPipeSocket(host, options.pipe);
} else if (options.localSocket != null) {
try {
return new UnixDomainSocket(urlParser.getOptions().localSocket);
return new UnixDomainSocket(options.localSocket);
} catch (RuntimeException re) {
throw new IOException(re.getMessage(), re.getCause());
}
} else if (urlParser.getOptions().sharedMemory != null) {
} else if (options.sharedMemory != null) {
try {
return new SharedMemorySocket(urlParser.getOptions().sharedMemory);
return new SharedMemorySocket(options.sharedMemory);
} catch (RuntimeException re) {
throw new IOException(re.getMessage(), re.getCause());
}
} else {
return Utils.standardSocket(urlParser, host);
return Utils.standardSocket(options, host);
}

};
} catch (Throwable cle) {
//jna jar's are not in classpath
}
return (urlParser, host) -> Utils.standardSocket(urlParser, host);
return (options, host) -> Utils.standardSocket(options, host);
}
}
Loading

0 comments on commit 90b684c

Please sign in to comment.