Class ConnectionUrl

java.lang.Object
com.mysql.cj.conf.ConnectionUrl
All Implemented Interfaces:
DatabaseUrlContainer
Direct Known Subclasses:
AwsSingleConnectionUrl, FailoverConnectionUrl, FailoverDnsSrvConnectionUrl, LoadBalanceConnectionUrl, LoadBalanceDnsSrvConnectionUrl, ReplicationConnectionUrl, ReplicationDnsSrvConnectionUrl, SingleConnectionUrl, XDevApiConnectionUrl, XDevApiDnsSrvConnectionUrl

public abstract class ConnectionUrl
extends java.lang.Object
implements DatabaseUrlContainer
A container for a database URL and a collection of given connection arguments. The connection string is parsed and split by its components, each of which is then processed and fixed according to the needs of the connection type. This abstract class holds all common behavior to all connection string types. Its subclasses must implement their own specifics such as classifying hosts by type or apply validation rules.
  • Nested Class Summary

    Nested Classes 
    Modifier and Type Class Description
    static class  ConnectionUrl.HostsCardinality
    The rules describing the number of hosts a database URL may contain.
    static class  ConnectionUrl.Type
    The database URL type which is determined by the scheme section of the connection string.
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static java.lang.String DEFAULT_HOST  
    static int DEFAULT_PORT  
    protected java.util.List<HostInfo> hosts  
    protected java.lang.String originalConnStr  
    protected java.lang.String originalDatabase  
    protected java.util.Map<java.lang.String,​java.lang.String> properties  
    protected ConnectionUrl.Type type  
  • Constructor Summary

    Constructors 
    Modifier Constructor Description
    protected ConnectionUrl()
    Empty constructor.
    protected ConnectionUrl​(ConnectionUrlParser connStrParser, java.util.Properties info)
    Constructs an instance of ConnectionUrl, performing all the required initializations.
      ConnectionUrl​(java.lang.String origUrl)
    Constructor for unsupported URLs
  • Method Summary

    Modifier and Type Method Description
    static boolean acceptsUrl​(java.lang.String connString)
    Checks if this ConnectionUrl is able to process the given database URL.
    protected HostInfo buildHostInfo​(java.lang.String host, int port, java.lang.String user, java.lang.String password, boolean isDefaultPwd, java.util.Map<java.lang.String,​java.lang.String> hostProps)
    Creates a new HostInfo structure with the given components, passing through the properties transformer if there is one defined in this connection string;
    protected void collectHostsInfo​(ConnectionUrlParser connStrParser)
    Collects the hosts information from the ConnectionUrlParser.
    protected void collectProperties​(ConnectionUrlParser connStrParser, java.util.Properties info)
    Joins the connection arguments from the connection string with the ones from the given connection arguments map collecting them in a single map.
    protected void expandPropertiesFromConfigFiles​(java.util.Map<java.lang.String,​java.lang.String> props)
    Expands the connection argument "useConfig" by reading the mentioned configuration files.
    protected HostInfo fixHostInfo​(HostInfo hi)
    Fixes the host information by moving data around and filling in missing data.
    protected void fixProtocolDependencies​(java.util.Map<java.lang.String,​java.lang.String> hostProps)
    Fixes the protocol (TCP vs PIPE) dependencies for the given host properties map.
    java.util.Properties getConnectionArgumentsAsProperties()
    Returns a Properties instance containing the connection arguments extracted from the URL query section, i.e., per host attributes are excluded.
    static ConnectionUrl getConnectionUrlInstance​(java.lang.String connString, java.util.Properties info)
    Static factory method that returns either a new instance of a ConnectionUrl or a cached one.
    java.lang.String getDatabase()
    Returns the database from this connection URL.
    java.lang.String getDatabaseUrl()
    Returns the original database URL that produced this connection string.
    java.lang.String getDefaultHost()
    Returns the default host.
    java.lang.String getDefaultPassword()
    Returns the default password.
    int getDefaultPort()
    Returns the default port.
    java.lang.String getDefaultUser()
    Returns the default user.
    HostInfo getHostOrSpawnIsolated​(java.lang.String hostPortPair)
    Returns an existing host info with the same host:port part or spawns a new isolated host info based on this connection URL if none was found.
    HostInfo getHostOrSpawnIsolated​(java.lang.String hostPortPair, java.util.List<HostInfo> hostsList)
    Returns an existing host info with the same host:port part or spawns a new isolated host info based on this connection URL if none was found.
    java.util.List<HostInfo> getHostsList()
    Returns a list of the hosts in this connection URL.
    java.util.List<HostInfo> getHostsList​(HostsListView view)
    Returns a list of the hosts in this connection URL, filtered for the given view.
    java.util.List<HostInfo> getHostsListFromDnsSrv​(HostInfo srvHost)
    Returns a hosts list built from the result of the DNS SRV lookup for the original host name.
    HostInfo getMainHost()
    Returns the single or first host info structure.
    java.util.Map<java.lang.String,​java.lang.String> getOriginalProperties()
    Returns the original (common to all hosts) connection arguments as provided in the connection string query section.
    static java.util.Properties getPropertiesFromConfigFiles​(java.lang.String configFiles)
    Returns a map containing the properties read from the given configuration files.
    ConnectionUrl.Type getType()
    Returns this connection URL type.
    int hostsCount()
    Returns the number of hosts in this connection URL.
    protected void injectPerTypeProperties​(java.util.Map<java.lang.String,​java.lang.String> props)
    Subclasses must override this method if they need to inject additional properties in the connection arguments map while it's being constructed.
    protected void preprocessPerTypeHostProperties​(java.util.Map<java.lang.String,​java.lang.String> hostProps)
    Subclasses should override this to perform any required pre-processing on the host information properties.
    protected void replaceLegacyPropertyValues​(java.util.Map<java.lang.String,​java.lang.String> props)
    Some acceptable property values have changed in c/J 8.0 but old values remain hardcoded in a widely used software.
    protected void setupPropertiesTransformer()
    Sets up the ConnectionPropertiesTransform if one was provided.
    java.lang.String toString()
    Returns a string representation of this object.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • ConnectionUrl

      protected ConnectionUrl()
      Empty constructor. Required for subclasses initialization.
    • ConnectionUrl

      public ConnectionUrl​(java.lang.String origUrl)
      Constructor for unsupported URLs
      Parameters:
      origUrl - URLs
    • ConnectionUrl

      protected ConnectionUrl​(ConnectionUrlParser connStrParser, java.util.Properties info)
      Constructs an instance of ConnectionUrl, performing all the required initializations.
      Parameters:
      connStrParser - a ConnectionUrlParser instance containing the parsed version of the original connection string
      info - the connection arguments map
  • Method Details

    • getConnectionUrlInstance

      public static ConnectionUrl getConnectionUrlInstance​(java.lang.String connString, java.util.Properties info)
      Static factory method that returns either a new instance of a ConnectionUrl or a cached one. Returns "null" it can't handle the connection string.
      Parameters:
      connString - the connection string
      info - the connection arguments map
      Returns:
      an instance of a ConnectionUrl or "null" if isn't able to handle the connection string
    • acceptsUrl

      public static boolean acceptsUrl​(java.lang.String connString)
      Checks if this ConnectionUrl is able to process the given database URL.
      Parameters:
      connString - the connection string
      Returns:
      true if this class is able to process the given URL, false otherwise
    • collectProperties

      protected void collectProperties​(ConnectionUrlParser connStrParser, java.util.Properties info)
      Joins the connection arguments from the connection string with the ones from the given connection arguments map collecting them in a single map. Additionally may also collect other connection arguments from configuration files.
      Parameters:
      connStrParser - the ConnectionUrlParser from where to collect the properties
      info - the connection arguments map
    • setupPropertiesTransformer

      protected void setupPropertiesTransformer()
      Sets up the ConnectionPropertiesTransform if one was provided.
    • expandPropertiesFromConfigFiles

      protected void expandPropertiesFromConfigFiles​(java.util.Map<java.lang.String,​java.lang.String> props)
      Expands the connection argument "useConfig" by reading the mentioned configuration files.
      Parameters:
      props - a connection arguments map from where to read the "useConfig" property and where to save the loaded properties.
    • getPropertiesFromConfigFiles

      public static java.util.Properties getPropertiesFromConfigFiles​(java.lang.String configFiles)
      Returns a map containing the properties read from the given configuration files. Multiple files can be referenced using a comma as separator.
      Parameters:
      configFiles - the list of the configuration files to read
      Returns:
      the map containing all the properties read
    • injectPerTypeProperties

      protected void injectPerTypeProperties​(java.util.Map<java.lang.String,​java.lang.String> props)
      Subclasses must override this method if they need to inject additional properties in the connection arguments map while it's being constructed.
      Parameters:
      props - the properties already containing all known connection arguments
    • replaceLegacyPropertyValues

      protected void replaceLegacyPropertyValues​(java.util.Map<java.lang.String,​java.lang.String> props)
      Some acceptable property values have changed in c/J 8.0 but old values remain hardcoded in a widely used software. So we need to accept old values and translate them to new ones.
      Parameters:
      props - the host properties map to fix
    • collectHostsInfo

      protected void collectHostsInfo​(ConnectionUrlParser connStrParser)
      Collects the hosts information from the ConnectionUrlParser.
      Parameters:
      connStrParser - the ConnectionUrlParser from where to collect the hosts information
    • fixHostInfo

      protected HostInfo fixHostInfo​(HostInfo hi)
      Fixes the host information by moving data around and filling in missing data. Applies properties transformations to the collected properties if ConnectionPropertiesTransform was declared in the connection arguments.
      Parameters:
      hi - the host information data to fix
      Returns:
      a new HostInfo with all required data
    • preprocessPerTypeHostProperties

      protected void preprocessPerTypeHostProperties​(java.util.Map<java.lang.String,​java.lang.String> hostProps)
      Subclasses should override this to perform any required pre-processing on the host information properties.
      Parameters:
      hostProps - the host properties map to process
    • getDefaultHost

      public java.lang.String getDefaultHost()
      Returns the default host. Subclasses must override this method if they have different default host value.
      Returns:
      the default host
    • getDefaultPort

      public int getDefaultPort()
      Returns the default port. Subclasses must override this method if they have different default port value.
      Returns:
      the default port
    • getDefaultUser

      public java.lang.String getDefaultUser()
      Returns the default user. Usually the one provided in the method DriverManager.getConnection(String, String, String) or as connection argument.
      Returns:
      the default user
    • getDefaultPassword

      public java.lang.String getDefaultPassword()
      Returns the default password. Usually the one provided in the method DriverManager.getConnection(String, String, String) or as connection argument.
      Returns:
      the default password
    • fixProtocolDependencies

      protected void fixProtocolDependencies​(java.util.Map<java.lang.String,​java.lang.String> hostProps)
      Fixes the protocol (TCP vs PIPE) dependencies for the given host properties map.
      Parameters:
      hostProps - the host properties map to fix
    • getType

      public ConnectionUrl.Type getType()
      Returns this connection URL type.
      Returns:
      the connection URL type
    • getDatabaseUrl

      public java.lang.String getDatabaseUrl()
      Returns the original database URL that produced this connection string.
      Specified by:
      getDatabaseUrl in interface DatabaseUrlContainer
      Returns:
      the original database URL
    • getDatabase

      public java.lang.String getDatabase()
      Returns the database from this connection URL. Note that a "DBNAME" property overrides the database identified in the connection string.
      Returns:
      the database name
    • hostsCount

      public int hostsCount()
      Returns the number of hosts in this connection URL.
      Returns:
      the number of hosts
    • getMainHost

      public HostInfo getMainHost()
      Returns the single or first host info structure.
      Returns:
      the first host info structure
    • getHostsList

      public java.util.List<HostInfo> getHostsList()
      Returns a list of the hosts in this connection URL.
      Returns:
      the hosts list from this connection URL
    • getHostsList

      public java.util.List<HostInfo> getHostsList​(HostsListView view)
      Returns a list of the hosts in this connection URL, filtered for the given view. By default returns all hosts. Subclasses should override this method in order to implement support for different views, usually by splitting the global hosts into smaller sub-lists.
      Parameters:
      view - the type of the view to use in the returned list of hosts. This argument is ignored in this implementation.
      Returns:
      the hosts list from this connection URL, filtered for the given view.
    • getHostOrSpawnIsolated

      public HostInfo getHostOrSpawnIsolated​(java.lang.String hostPortPair)
      Returns an existing host info with the same host:port part or spawns a new isolated host info based on this connection URL if none was found.
      Parameters:
      hostPortPair - the host:port part to search for
      Returns:
      the existing host info or a new independent one
    • getHostOrSpawnIsolated

      public HostInfo getHostOrSpawnIsolated​(java.lang.String hostPortPair, java.util.List<HostInfo> hostsList)
      Returns an existing host info with the same host:port part or spawns a new isolated host info based on this connection URL if none was found.
      Parameters:
      hostPortPair - the host:port part to search for
      hostsList - the hosts list from where to search the host list
      Returns:
      the existing host info or a new independent one
    • buildHostInfo

      protected HostInfo buildHostInfo​(java.lang.String host, int port, java.lang.String user, java.lang.String password, boolean isDefaultPwd, java.util.Map<java.lang.String,​java.lang.String> hostProps)
      Creates a new HostInfo structure with the given components, passing through the properties transformer if there is one defined in this connection string;
      Parameters:
      host - the host
      port - the port
      user - the user name
      password - the password
      isDefaultPwd - no password was provided in the connection URL or arguments?
      hostProps - the host properties map
      Returns:
      a new instance of HostInfo
    • getOriginalProperties

      public java.util.Map<java.lang.String,​java.lang.String> getOriginalProperties()
      Returns the original (common to all hosts) connection arguments as provided in the connection string query section.
      Returns:
      the original (common to all hosts) connection arguments
    • getConnectionArgumentsAsProperties

      public java.util.Properties getConnectionArgumentsAsProperties()
      Returns a Properties instance containing the connection arguments extracted from the URL query section, i.e., per host attributes are excluded. Applies properties transformations to the collected properties if ConnectionPropertiesTransform was declared in the connection arguments.
      Returns:
      a Properties instance containing the common connection arguments.
    • getHostsListFromDnsSrv

      public java.util.List<HostInfo> getHostsListFromDnsSrv​(HostInfo srvHost)
      Returns a hosts list built from the result of the DNS SRV lookup for the original host name.
      Parameters:
      srvHost - the HostInfo from where to get the DNS SRV service name to lookup.
      Returns:
      the hosts list from the result of the DNS SRV lookup, filtered for the given view.
    • toString

      public java.lang.String toString()
      Returns a string representation of this object.
      Overrides:
      toString in class java.lang.Object
      Returns:
      a string representation of this object