Package com.mysql.cj.conf
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 classConnectionUrl.HostsCardinalityThe rules describing the number of hosts a database URL may contain.static classConnectionUrl.TypeThe 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.StringDEFAULT_HOSTstatic intDEFAULT_PORTprotected java.util.List<HostInfo>hostsprotected java.lang.StringoriginalConnStrprotected java.lang.StringoriginalDatabaseprotected java.util.Map<java.lang.String,java.lang.String>propertiesprotected ConnectionUrl.Typetype -
Constructor Summary
Constructors Modifier Constructor Description protectedConnectionUrl()Empty constructor.protectedConnectionUrl(ConnectionUrlParser connStrParser, java.util.Properties info)Constructs an instance ofConnectionUrl, performing all the required initializations.ConnectionUrl(java.lang.String origUrl)Constructor for unsupported URLs -
Method Summary
Modifier and Type Method Description static booleanacceptsUrl(java.lang.String connString)Checks if thisConnectionUrlis able to process the given database URL.protected HostInfobuildHostInfo(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 newHostInfostructure with the given components, passing through the properties transformer if there is one defined in this connection string;protected voidcollectHostsInfo(ConnectionUrlParser connStrParser)Collects the hosts information from theConnectionUrlParser.protected voidcollectProperties(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 voidexpandPropertiesFromConfigFiles(java.util.Map<java.lang.String,java.lang.String> props)Expands the connection argument "useConfig" by reading the mentioned configuration files.protected HostInfofixHostInfo(HostInfo hi)Fixes the host information by moving data around and filling in missing data.protected voidfixProtocolDependencies(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.PropertiesgetConnectionArgumentsAsProperties()Returns aPropertiesinstance containing the connection arguments extracted from the URL query section, i.e., per host attributes are excluded.static ConnectionUrlgetConnectionUrlInstance(java.lang.String connString, java.util.Properties info)Static factory method that returns either a new instance of aConnectionUrlor a cached one.java.lang.StringgetDatabase()Returns the database from this connection URL.java.lang.StringgetDatabaseUrl()Returns the original database URL that produced this connection string.java.lang.StringgetDefaultHost()Returns the default host.java.lang.StringgetDefaultPassword()Returns the default password.intgetDefaultPort()Returns the default port.java.lang.StringgetDefaultUser()Returns the default user.HostInfogetHostOrSpawnIsolated(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.HostInfogetHostOrSpawnIsolated(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.HostInfogetMainHost()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.PropertiesgetPropertiesFromConfigFiles(java.lang.String configFiles)Returns a map containing the properties read from the given configuration files.ConnectionUrl.TypegetType()Returns this connection URL type.inthostsCount()Returns the number of hosts in this connection URL.protected voidinjectPerTypeProperties(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 voidpreprocessPerTypeHostProperties(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 voidreplaceLegacyPropertyValues(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 voidsetupPropertiesTransformer()Sets up theConnectionPropertiesTransformif one was provided.java.lang.StringtoString()Returns a string representation of this object.
-
Field Details
-
DEFAULT_HOST
public static final java.lang.String DEFAULT_HOST- See Also:
- Constant Field Values
-
DEFAULT_PORT
public static final int DEFAULT_PORT- See Also:
- Constant Field Values
-
type
-
originalConnStr
protected java.lang.String originalConnStr -
originalDatabase
protected java.lang.String originalDatabase -
hosts
-
properties
protected java.util.Map<java.lang.String,java.lang.String> properties
-
-
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
Constructs an instance ofConnectionUrl, performing all the required initializations.- Parameters:
connStrParser- aConnectionUrlParserinstance containing the parsed version of the original connection stringinfo- 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 aConnectionUrlor a cached one. Returns "null" it can't handle the connection string.- Parameters:
connString- the connection stringinfo- the connection arguments map- Returns:
- an instance of a
ConnectionUrlor "null" if isn't able to handle the connection string
-
acceptsUrl
public static boolean acceptsUrl(java.lang.String connString)Checks if thisConnectionUrlis 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
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- theConnectionUrlParserfrom where to collect the propertiesinfo- the connection arguments map
-
setupPropertiesTransformer
protected void setupPropertiesTransformer()Sets up theConnectionPropertiesTransformif 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
Collects the hosts information from theConnectionUrlParser.- Parameters:
connStrParser- theConnectionUrlParserfrom where to collect the hosts information
-
fixHostInfo
Fixes the host information by moving data around and filling in missing data. Applies properties transformations to the collected properties ifConnectionPropertiesTransformwas declared in the connection arguments.- Parameters:
hi- the host information data to fix- Returns:
- a new
HostInfowith 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 methodDriverManager.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 methodDriverManager.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
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:
getDatabaseUrlin interfaceDatabaseUrlContainer- 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
Returns the single or first host info structure.- Returns:
- the first host info structure
-
getHostsList
Returns a list of the hosts in this connection URL.- Returns:
- the hosts list from this connection URL
-
getHostsList
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
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 forhostsList- 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 newHostInfostructure with the given components, passing through the properties transformer if there is one defined in this connection string;- Parameters:
host- the hostport- the portuser- the user namepassword- the passwordisDefaultPwd- 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 aPropertiesinstance containing the connection arguments extracted from the URL query section, i.e., per host attributes are excluded. Applies properties transformations to the collected properties ifConnectionPropertiesTransformwas declared in the connection arguments.- Returns:
- a
Propertiesinstance containing the common connection arguments.
-
getHostsListFromDnsSrv
Returns a hosts list built from the result of the DNS SRV lookup for the original host name.- Parameters:
srvHost- theHostInfofrom 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:
toStringin classjava.lang.Object- Returns:
- a string representation of this object
-