类 ConnectionUrl
- java.lang.Object
-
- com.mysql.cj.conf.ConnectionUrl
-
- 所有已实现的接口:
DatabaseUrlContainer
- 直接已知子类:
FailoverConnectionUrl,FailoverDnsSrvConnectionUrl,LoadBalanceConnectionUrl,LoadBalanceDnsSrvConnectionUrl,ReplicationConnectionUrl,ReplicationDnsSrvConnectionUrl,SingleConnectionUrl,XDevApiConnectionUrl,XDevApiDnsSrvConnectionUrl
public abstract class ConnectionUrl extends 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.
-
-
嵌套类概要
嵌套类 修饰符和类型 类 说明 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.
-
字段概要
字段 修饰符和类型 字段 说明 static StringDEFAULT_HOSTstatic intDEFAULT_PORTprotected List<HostInfo>hostsprotected StringoriginalConnStrprotected StringoriginalDatabaseprotected Map<String,String>propertiesprotected ConnectionUrl.Typetype
-
构造器概要
构造器 限定符 构造器 说明 protectedConnectionUrl()Empty constructor.protectedConnectionUrl(ConnectionUrlParser connStrParser, Properties info)Constructs an instance ofConnectionUrl, performing all the required initializations.ConnectionUrl(String origUrl)Constructor for unsupported URLs
-
方法概要
所有方法 静态方法 实例方法 具体方法 修饰符和类型 方法 说明 static booleanacceptsUrl(String connString)Checks if thisConnectionUrlis able to process the given database URL.protected HostInfobuildHostInfo(String host, int port, String user, String password, Map<String,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, 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(Map<String,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(Map<String,String> hostProps)Fixes the protocol (TCP vs PIPE) dependencies for the given host properties map.PropertiesgetConnectionArgumentsAsProperties()Returns aPropertiesinstance containing the connection arguments extracted from the URL query section, i.e., per host attributes are excluded.static ConnectionUrlgetConnectionUrlInstance(String connString, Properties info)Static factory method that returns either a new instance of aConnectionUrlor a cached one.StringgetDatabase()Returns the database from this connection URL.StringgetDatabaseUrl()Returns the original database URL that produced this connection string.StringgetDefaultHost()Returns the default host.StringgetDefaultPassword()Returns the default password.intgetDefaultPort()Returns the default port.StringgetDefaultUser()Returns the default user.HostInfogetHostOrSpawnIsolated(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(String hostPortPair, 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.List<HostInfo>getHostsList()Returns a list of the hosts in this connection URL.List<HostInfo>getHostsList(HostsListView view)Returns a list of the hosts in this connection URL, filtered for the given view.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.Map<String,String>getOriginalProperties()Returns the original (common to all hosts) connection arguments as provided in the connection string query section.static PropertiesgetPropertiesFromConfigFiles(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(Map<String,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(Map<String,String> hostProps)Subclasses should override this to perform any required pre-processing on the host information properties.protected voidreplaceLegacyPropertyValues(Map<String,String> props)Some acceptable property values have changed in c/J 8.0 but old values remain hard-coded in widely used software.protected voidsetupPropertiesTransformer()Sets up theConnectionPropertiesTransformif one was provided.StringtoString()Returns a string representation of this object.
-
-
-
字段详细资料
-
DEFAULT_PORT
public static final int DEFAULT_PORT
- 另请参阅:
- 常量字段值
-
type
protected ConnectionUrl.Type type
-
originalConnStr
protected String originalConnStr
-
originalDatabase
protected String originalDatabase
-
-
构造器详细资料
-
ConnectionUrl
protected ConnectionUrl()
Empty constructor. Required for subclasses initialization.
-
ConnectionUrl
public ConnectionUrl(String origUrl)
Constructor for unsupported URLs- 参数:
origUrl- URLs
-
ConnectionUrl
protected ConnectionUrl(ConnectionUrlParser connStrParser, Properties info)
Constructs an instance ofConnectionUrl, performing all the required initializations.- 参数:
connStrParser- aConnectionUrlParserinstance containing the parsed version of the original connection stringinfo- the connection arguments map
-
-
方法详细资料
-
getConnectionUrlInstance
public static ConnectionUrl getConnectionUrlInstance(String connString, 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.- 参数:
connString- the connection stringinfo- the connection arguments map- 返回:
- an instance of a
ConnectionUrlor "null" if isn't able to handle the connection string
-
acceptsUrl
public static boolean acceptsUrl(String connString)
Checks if thisConnectionUrlis able to process the given database URL.- 参数:
connString- the connection string- 返回:
- true if this class is able to process the given URL, false otherwise
-
collectProperties
protected void collectProperties(ConnectionUrlParser connStrParser, 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.- 参数:
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(Map<String,String> props)
Expands the connection argument "useConfig" by reading the mentioned configuration files.- 参数:
props- a connection arguments map from where to read the "useConfig" property and where to save the loaded properties.
-
getPropertiesFromConfigFiles
public static Properties getPropertiesFromConfigFiles(String configFiles)
Returns a map containing the properties read from the given configuration files. Multiple files can be referenced using a comma as separator.- 参数:
configFiles- the list of the configuration files to read- 返回:
- the map containing all the properties read
-
injectPerTypeProperties
protected void injectPerTypeProperties(Map<String,String> props)
Subclasses must override this method if they need to inject additional properties in the connection arguments map while it's being constructed.- 参数:
props- the properties already containing all known connection arguments
-
replaceLegacyPropertyValues
protected void replaceLegacyPropertyValues(Map<String,String> props)
Some acceptable property values have changed in c/J 8.0 but old values remain hard-coded in widely used software. So, old values must be accepted and translated to new ones.- 参数:
props- the host properties map to fix
-
collectHostsInfo
protected void collectHostsInfo(ConnectionUrlParser connStrParser)
Collects the hosts information from theConnectionUrlParser.- 参数:
connStrParser- theConnectionUrlParserfrom 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 ifConnectionPropertiesTransformwas declared in the connection arguments.- 参数:
hi- the host information data to fix- 返回:
- a new
HostInfowith all required data
-
preprocessPerTypeHostProperties
protected void preprocessPerTypeHostProperties(Map<String,String> hostProps)
Subclasses should override this to perform any required pre-processing on the host information properties.- 参数:
hostProps- the host properties map to process
-
getDefaultHost
public String getDefaultHost()
Returns the default host. Subclasses must override this method if they have different default host value.- 返回:
- the default host
-
getDefaultPort
public int getDefaultPort()
Returns the default port. Subclasses must override this method if they have different default port value.- 返回:
- the default port
-
getDefaultUser
public String getDefaultUser()
Returns the default user. Usually the one provided in the methodDriverManager.getConnection(String, String, String)or as connection argument.- 返回:
- the default user
-
getDefaultPassword
public String getDefaultPassword()
Returns the default password. Usually the one provided in the methodDriverManager.getConnection(String, String, String)or as connection argument.- 返回:
- the default password
-
fixProtocolDependencies
protected void fixProtocolDependencies(Map<String,String> hostProps)
Fixes the protocol (TCP vs PIPE) dependencies for the given host properties map.- 参数:
hostProps- the host properties map to fix
-
getType
public ConnectionUrl.Type getType()
Returns this connection URL type.- 返回:
- the connection URL type
-
getDatabaseUrl
public String getDatabaseUrl()
Returns the original database URL that produced this connection string.- 指定者:
getDatabaseUrl在接口中DatabaseUrlContainer- 返回:
- the original database URL
-
getDatabase
public String getDatabase()
Returns the database from this connection URL. Note that a "DBNAME" property overrides the database identified in the connection string.- 返回:
- the database name
-
hostsCount
public int hostsCount()
Returns the number of hosts in this connection URL.- 返回:
- the number of hosts
-
getMainHost
public HostInfo getMainHost()
Returns the single or first host info structure.- 返回:
- the first host info structure
-
getHostsList
public List<HostInfo> getHostsList()
Returns a list of the hosts in this connection URL.- 返回:
- the hosts list from this connection URL
-
getHostsList
public 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.- 参数:
view- the type of the view to use in the returned list of hosts. This argument is ignored in this implementation.- 返回:
- the hosts list from this connection URL, filtered for the given view.
-
getHostOrSpawnIsolated
public HostInfo getHostOrSpawnIsolated(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.- 参数:
hostPortPair- the host:port part to search for- 返回:
- the existing host info or a new independent one
-
getHostOrSpawnIsolated
public HostInfo getHostOrSpawnIsolated(String hostPortPair, 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.- 参数:
hostPortPair- the host:port part to search forhostsList- the hosts list from where to search the host list- 返回:
- the existing host info or a new independent one
-
buildHostInfo
protected HostInfo buildHostInfo(String host, int port, String user, String password, Map<String,String> hostProps)
Creates a newHostInfostructure with the given components, passing through the properties transformer if there is one defined in this connection string;- 参数:
host- the hostport- the portuser- the user namepassword- the passwordhostProps- the host properties map- 返回:
- a new instance of
HostInfo
-
getOriginalProperties
public Map<String,String> getOriginalProperties()
Returns the original (common to all hosts) connection arguments as provided in the connection string query section.- 返回:
- the original (common to all hosts) connection arguments
-
getConnectionArgumentsAsProperties
public 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.- 返回:
- a
Propertiesinstance containing the common connection arguments.
-
getHostsListFromDnsSrv
public List<HostInfo> getHostsListFromDnsSrv(HostInfo srvHost)
Returns a hosts list built from the result of the DNS SRV lookup for the original host name.- 参数:
srvHost- theHostInfofrom where to get the DNS SRV service name to lookup.- 返回:
- the hosts list from the result of the DNS SRV lookup, filtered for the given view.
-
-