Class ConnectionHelper


  • public final class ConnectionHelper
    extends java.lang.Object
    Connection creation helper class
    • Constructor Detail

      • ConnectionHelper

        public ConnectionHelper()
    • Method Detail

      • createSocket

        public static java.net.Socket createSocket​(Configuration conf,
                                                   HostAddress hostAddress)
                                            throws java.io.IOException,
                                                   java.sql.SQLException
        Create socket accordingly to options.
        Parameters:
        conf - Url options
        hostAddress - host ( mandatory but for named pipe / unix socket)
        Returns:
        a nex socket
        Throws:
        java.io.IOException - if connection error occur
        java.sql.SQLException - in case of configuration error
      • standardSocket

        public static java.net.Socket standardSocket​(Configuration conf,
                                                     HostAddress hostAddress)
                                              throws java.io.IOException,
                                                     java.sql.SQLException
        Use standard socket implementation.
        Parameters:
        conf - url options
        hostAddress - host to connect
        Returns:
        socket
        Throws:
        java.io.IOException - in case of error establishing socket.
        java.sql.SQLException - in case host is null
      • assertValidClassName

        static void assertValidClassName​(java.lang.String className)
                                  throws java.io.IOException
        Ensure a socketFactory option is a plain Java binary class name, i.e. a dot-separated sequence of ASCII Java identifiers (org.example.MyFactory, com.foo.Bar$Baz). This rejects resource locators such as jar:file:/proc/self/fd/N!/Evil that would otherwise be passed verbatim to Class.forName(java.lang.String), preventing bytecode from being loaded from an attacker-controlled location.
        Parameters:
        className - socketFactory option value
        Throws:
        java.io.IOException - if the value is not a valid class name
      • connectSocket

        public static java.net.Socket connectSocket​(Configuration conf,
                                                    HostAddress hostAddress)
                                             throws java.sql.SQLException
        Connect socket
        Parameters:
        conf - configuration
        hostAddress - host to connect
        Returns:
        socket
        Throws:
        java.sql.SQLException - if hostname is required and not provided, or socket cannot be created
      • initializeClientCapabilities

        public static long initializeClientCapabilities​(Configuration configuration,
                                                        long serverCapabilities,
                                                        HostAddress hostAddress)
        Initialize client capability according to configuration and server capabilities.
        Parameters:
        configuration - configuration
        serverCapabilities - server capabilities
        hostAddress - host address server
        Returns:
        client capabilities
      • initializeBaseCapabilities

        private static long initializeBaseCapabilities()
      • applyOptionalCapabilities

        private static long applyOptionalCapabilities​(long capabilities,
                                                      Configuration configuration)
      • applyTechnicalCapabilities

        private static long applyTechnicalCapabilities​(long capabilities,
                                                       Configuration configuration)
      • applyConnectionCapabilities

        private static long applyConnectionCapabilities​(long capabilities,
                                                        Configuration configuration,
                                                        HostAddress hostAddress)
      • getBooleanProperty

        private static boolean getBooleanProperty​(Configuration configuration,
                                                  java.lang.String propertyName,
                                                  boolean defaultValue)
      • shouldEnableMetadataCache

        private static boolean shouldEnableMetadataCache​(Configuration configuration)
      • shouldConnectWithDb

        private static boolean shouldConnectWithDb​(Configuration configuration,
                                                   HostAddress hostAddress)
      • loadCredential

        public static Credential loadCredential​(CredentialPlugin credentialPlugin,
                                                Configuration configuration,
                                                HostAddress hostAddress)
                                         throws java.sql.SQLException
        Load user/password plugin if configured to.
        Parameters:
        credentialPlugin - configuration credential plugin
        configuration - configuration
        hostAddress - current connection host address
        Returns:
        credentials
        Throws:
        java.sql.SQLException - if configured credential plugin fail
      • enabledSslProtocolSuites

        static void enabledSslProtocolSuites​(javax.net.ssl.SSLSocket sslSocket,
                                             Configuration conf)
                                      throws java.sql.SQLException
        Return possible protocols : values of option enabledSslProtocolSuites is set, or default to "TLSv1,TLSv1.1". MariaDB versions ≥ 10.0.15 and ≥ 5.5.41 supports TLSv1.2 if compiled with openSSL (default). MySQL's community versions ≥ 5.7.10 is compiled with yaSSL, so max TLS is TLSv1.1.
        Parameters:
        sslSocket - current sslSocket
        Throws:
        java.sql.SQLException - if protocol isn't a supported protocol
      • enabledSslCipherSuites

        static void enabledSslCipherSuites​(javax.net.ssl.SSLSocket sslSocket,
                                           Configuration conf)
                                    throws java.sql.SQLException
        Set ssl socket cipher according to options.
        Parameters:
        sslSocket - current ssl socket
        conf - configuration
        Throws:
        java.sql.SQLException - if a cipher isn't known