Package rs.baselib.sql
Class AbstractJdbcConnectionProvider
java.lang.Object
rs.baselib.sql.AbstractJdbcConnectionProvider
- All Implemented Interfaces:
IDataSourceProvider
,IHibernateDialectProvider
,IJdbcConnectionProvider
,IJdbcConnectionProvider2
- Direct Known Subclasses:
HyperSqlFileJdbcConnectionProvider
,HyperSqlServerJdbcConnectionProvider
,MySql5JdbcConnectionProvider
public abstract class AbstractJdbcConnectionProvider
extends Object
implements IJdbcConnectionProvider2, IDataSourceProvider, IHibernateDialectProvider
Abstract implementation for JdbcConnectionProviders.
The implementation already takes care of most purposes. A common usage scenario is to
provide a URL template as follows:
jdbc:mysql://{0}:{1}/{2}The URL template is a format as for
MessageFormat
with following arguments:
- {0} - DB Host
- {1} - DB Port
- {2} - DB Name
- {3} - DB Login
- {4} - DB Password
- {5}...{n} - Additional arguments (addOnArgs)
Developers can override getDriverUrl(String, String, String, String, String, String...)
to accommodate
for more sophisticated URL templates.
Clients and developers can control whether each of the arguments is enabled and the default value of an argument. The default implementation will automatically use the default value for an argument when the argument is not enabled or is empty.
- Author:
- ralph
-
Constructor Summary
ConstructorDescriptionAbstractJdbcConnectionProvider
(String display) Constructor.AbstractJdbcConnectionProvider
(String display, String urlTemplate) Constructor.AbstractJdbcConnectionProvider
(String display, String dbDriverClassName, String urlTemplate) Constructor. -
Method Summary
Modifier and TypeMethodDescriptiongetAdditionalArgument
(int index, String s) Returns the additional argument to be used for URL construction.int
Returns the number of additional arguments this provider knows about.getAdditionalArgumentName
(int index) Returns the name of the additional argument.String[]
Returns the names of additional arguments.getConnection
(String host, String port, String dbName, String dbLogin, String dbPassword, String... addOnArgs) Provide and open the connection for the given arguments.Returns the name of theDataSource
.Returns the class name of the database driver.getDbLogin
(String s) Returns the DB login argument to be used for URL construction.Returns the DB name argument to be used for URL construction.Returns the DB password argument to be used for URL construction.getDefaultAdditionalArgument
(int index) Returns the default additional argument to be used when it is disabled or actual value is empty.Returns the default DB login argument to be used when DB login argument is disabled or actual value is empty.Returns the default DB name argument to be used when DB name argument is disabled or actual value is empty.Returns the default DB password argument to be used when DB password argument is disabled or actual value is empty.Returns the default host argument to be used when host argument is disabled or actual value is empty.Returns the default port argument to be used when port argument is disabled or actual value is empty.Returns the display.getDriverParameter
(String name) Returns the value of th edriver parameter.int
Returns the number of additional driver parameters.Returns the URL driver parameter string to be used inurl
.getDriverUrl
(String host, String port, String dbName, String dbLogin, String dbPassword, String... addOnArgs) Constructs the driver's URL from the arguments.Returns the dialect to be used by Hibernate.Returns the host argument to be used for URL construction.Returns the port argument to be used for URL construction.Returns theurlTemplate
.boolean
isAdditionalArgumentEnabled
(int index) Returns whether additional argument is enabled.boolean
Returns whether DB login argument is enabled.boolean
Returns whether DB name argument is enabled.boolean
Returns whether DB password argument is enabled.boolean
Returns whether host argument is enabled.boolean
Returns whether port argument is enabled.void
setAdditionalArgument
(int index, String name, boolean enabled, String defaultValue) void
setAdditionalArgumentEnabled
(int index, boolean b) Sets whether additional argument is enabled.void
setAdditionalArgumentName
(int index, String s) Sets the name of the additional argument.protected void
setDataSource
(String dataSource) Sets thedataSource
.void
setDbLogin
(boolean enabled, String defaultValue) A shortcut method forIJdbcConnectionProvider2.setDbLoginEnabled(boolean)
andIJdbcConnectionProvider2.setDefaultDbLogin(String)
.void
setDbLoginEnabled
(boolean b) Sets whether DB login argument is enabled.void
A shortcut method forIJdbcConnectionProvider2.setDbNameEnabled(boolean)
andIJdbcConnectionProvider2.setDefaultDbName(String)
.void
setDbNameEnabled
(boolean b) Sets whether DB name argument is enabled.void
setDbPassword
(boolean enabled, String defaultValue) A shortcut method forIJdbcConnectionProvider2.setDbPasswordEnabled(boolean)
andIJdbcConnectionProvider2.setDefaultDbPassword(String)
.void
setDbPasswordEnabled
(boolean b) Sets whether DB password argument is enabled.void
setDefaultAdditionalArgument
(int index, String s) Sets the default additional argument to be used when it is disabled or actual value is empty.void
Sets the default DB login argument to be used when DB login argument is disabled or actual value is empty.void
Sets the default DB name argument to be used when DB name argument is disabled or actual value is empty.void
Sets the default DB password argument to be used when DB password argument is disabled or actual value is empty.void
Sets the default host argument to be used when host argument is disabled or actual value is empty.void
Sets the default port argument to be used when port argument is disabled or actual value is empty.void
setDriverParameter
(String name, String value) Sets a driver parameter value or deletes it.void
setHibernateDialect
(String hibernateDialect) Sets thehibernateDialect
.void
A shortcut method forIJdbcConnectionProvider2.setHostEnabled(boolean)
andIJdbcConnectionProvider2.setDefaultHost(String)
.void
setHostEnabled
(boolean b) Sets whether host argument is enabled.void
A shortcut method forIJdbcConnectionProvider2.setPortEnabled(boolean)
andIJdbcConnectionProvider2.setDefaultPort(String)
.void
setPortEnabled
(boolean b) Sets whether port argument is enabled.toString()
-
Constructor Details
-
AbstractJdbcConnectionProvider
Constructor. Descendants must overridegetDbDriverClassName()
andgetDriverUrl(String, String, String, String, String, String...)
for a class.- Parameters:
display
- a name to be displayed to users
-
AbstractJdbcConnectionProvider
Constructor. Descendants must overridegetDbDriverClassName()
for a working class.- Parameters:
display
- a name to be displayed to usersurlTemplate
- the URL template (see class description for details)
-
AbstractJdbcConnectionProvider
Constructor.- Parameters:
display
- a name to be displayed to usersdbDriverClassName
- the name of the DB driver (can benull
if you overridegetDbDriverClassName()
)urlTemplate
- the URL template (see class description for details)
-
-
Method Details
-
getConnection
public Connection getConnection(String host, String port, String dbName, String dbLogin, String dbPassword, String... addOnArgs) throws SQLException Provide and open the connection for the given arguments.- Specified by:
getConnection
in interfaceIJdbcConnectionProvider
- Parameters:
host
- the host of the databaseport
- the port of the database (some DB use names as port information)dbName
- name of databasedbLogin
- database logindbPassword
- database passwordaddOnArgs
- additional arguments to be processed by the driver- Returns:
- an opened connection if successful
- Throws:
SQLException
- when the connection cannot be opened
-
getDbDriverClassName
Returns the class name of the database driver. The method shall returnnull
in case of any errors.- Specified by:
getDbDriverClassName
in interfaceIJdbcConnectionProvider
- Returns:
- the dbDriverClassName
-
getUrlTemplate
Returns theurlTemplate
.- Returns:
- the urlTemplate
-
getDriverUrl
public String getDriverUrl(String host, String port, String dbName, String dbLogin, String dbPassword, String... addOnArgs) Constructs the driver's URL from the arguments. The method shall returnnull
in case of any problems or throw an exception.- Specified by:
getDriverUrl
in interfaceIJdbcConnectionProvider
- Parameters:
host
- the host of the databaseport
- the port of the database (some DB use names as port information)dbName
- name of databasedbLogin
- database logindbPassword
- database passwordaddOnArgs
- additional arguments for the driver URL- Returns:
- the URL for the driver or
null
-
getDataSource
Returns the name of theDataSource
.- Specified by:
getDataSource
in interfaceIDataSourceProvider
- Returns:
- the source
-
setDataSource
Sets thedataSource
.- Parameters:
dataSource
- the dataSource to set
-
getHibernateDialect
Returns the dialect to be used by Hibernate.- Specified by:
getHibernateDialect
in interfaceIHibernateDialectProvider
- Returns:
- the name of the dialect class.
-
setHibernateDialect
Sets thehibernateDialect
.- Parameters:
hibernateDialect
- the hibernateDialect to set
-
getHost
Returns the host argument to be used for URL construction. Descendants can override this method when they have more sophisticated algorithms to determine the host argument. This implementation returns the default host when the passed string is either empty or host argument was disabled.- Specified by:
getHost
in interfaceIJdbcConnectionProvider2
- Parameters:
s
- the string containing an actual host argument- Returns:
- the argument to be used in URL construction
- See Also:
-
isHostEnabled
public boolean isHostEnabled()Returns whether host argument is enabled. The information can be used for UI developers to control the input flow.- Specified by:
isHostEnabled
in interfaceIJdbcConnectionProvider2
- Returns:
true
whenhost
argument can be passed toIJdbcConnectionProvider.getConnection(String, String, String, String, String, String...)
-
setHostEnabled
public void setHostEnabled(boolean b) Sets whether host argument is enabled. The information can be used for UI developers to control the input flow.- Specified by:
setHostEnabled
in interfaceIJdbcConnectionProvider2
- Parameters:
b
-true
whenhost
argument can be passed toIJdbcConnectionProvider.getConnection(String, String, String, String, String, String...)
-
setHost
A shortcut method forIJdbcConnectionProvider2.setHostEnabled(boolean)
andIJdbcConnectionProvider2.setDefaultHost(String)
.- Specified by:
setHost
in interfaceIJdbcConnectionProvider2
- Parameters:
enabled
- whether to enable host argumentdefaultValue
- the default value
-
getDefaultHost
Returns the default host argument to be used when host argument is disabled or actual value is empty.- Specified by:
getDefaultHost
in interfaceIJdbcConnectionProvider2
- Returns:
- default value
-
setDefaultHost
Sets the default host argument to be used when host argument is disabled or actual value is empty.- Specified by:
setDefaultHost
in interfaceIJdbcConnectionProvider2
- Parameters:
s
- default value
-
getPort
Returns the port argument to be used for URL construction. Descendants can override this method when they have more sophisticated algorithms to determine the port argument. This implementation returns the default port when the passed string is either empty or port argument was disabled.- Specified by:
getPort
in interfaceIJdbcConnectionProvider2
- Parameters:
s
- the string containing an actual port argument- Returns:
- the argument to be used in URL construction
- See Also:
-
isPortEnabled
public boolean isPortEnabled()Returns whether port argument is enabled. The information can be used for UI developers to control the input flow.- Specified by:
isPortEnabled
in interfaceIJdbcConnectionProvider2
- Returns:
true
whenport
argument can be passed toIJdbcConnectionProvider.getConnection(String, String, String, String, String, String...)
-
setPortEnabled
public void setPortEnabled(boolean b) Sets whether port argument is enabled. The information can be used for UI developers to control the input flow.- Specified by:
setPortEnabled
in interfaceIJdbcConnectionProvider2
- Parameters:
b
-true
whenport
argument can be passed toIJdbcConnectionProvider.getConnection(String, String, String, String, String, String...)
-
setPort
A shortcut method forIJdbcConnectionProvider2.setPortEnabled(boolean)
andIJdbcConnectionProvider2.setDefaultPort(String)
.- Specified by:
setPort
in interfaceIJdbcConnectionProvider2
- Parameters:
enabled
- whether to enable port argumentdefaultValue
- the default value
-
getDefaultPort
Returns the default port argument to be used when port argument is disabled or actual value is empty.- Specified by:
getDefaultPort
in interfaceIJdbcConnectionProvider2
- Returns:
- default value
-
setDefaultPort
Sets the default port argument to be used when port argument is disabled or actual value is empty.- Specified by:
setDefaultPort
in interfaceIJdbcConnectionProvider2
- Parameters:
s
- default value
-
getDbName
Returns the DB name argument to be used for URL construction. Descendants can override this method when they have more sophisticated algorithms to determine the DB name argument. This implementation returns the default DB name when the passed string is either empty or DB name argument was disabled.- Specified by:
getDbName
in interfaceIJdbcConnectionProvider2
- Parameters:
s
- the string containing an actual DB name argument- Returns:
- the argument to be used in URL construction
- See Also:
-
isDbNameEnabled
public boolean isDbNameEnabled()Returns whether DB name argument is enabled. The information can be used for UI developers to control the input flow.- Specified by:
isDbNameEnabled
in interfaceIJdbcConnectionProvider2
- Returns:
true
whendbName
argument can be passed toIJdbcConnectionProvider.getConnection(String, String, String, String, String, String...)
-
setDbNameEnabled
public void setDbNameEnabled(boolean b) Sets whether DB name argument is enabled. The information can be used for UI developers to control the input flow.- Specified by:
setDbNameEnabled
in interfaceIJdbcConnectionProvider2
- Parameters:
b
-true
whendbName
argument can be passed toIJdbcConnectionProvider.getConnection(String, String, String, String, String, String...)
-
setDbName
A shortcut method forIJdbcConnectionProvider2.setDbNameEnabled(boolean)
andIJdbcConnectionProvider2.setDefaultDbName(String)
.- Specified by:
setDbName
in interfaceIJdbcConnectionProvider2
- Parameters:
enabled
- whether to enable DB name argumentdefaultValue
- the default value
-
getDefaultDbName
Returns the default DB name argument to be used when DB name argument is disabled or actual value is empty.- Specified by:
getDefaultDbName
in interfaceIJdbcConnectionProvider2
- Returns:
- default value
-
setDefaultDbName
Sets the default DB name argument to be used when DB name argument is disabled or actual value is empty.- Specified by:
setDefaultDbName
in interfaceIJdbcConnectionProvider2
- Parameters:
s
- default value
-
getDbLogin
Returns the DB login argument to be used for URL construction. Descendants can override this method when they have more sophisticated algorithms to determine the DB login argument. This implementation returns the default DB login when the passed string is either empty or DB login argument was disabled.- Specified by:
getDbLogin
in interfaceIJdbcConnectionProvider2
- Parameters:
s
- the string containing an actual DB login argument- Returns:
- the argument to be used in URL construction
- See Also:
-
isDbLoginEnabled
public boolean isDbLoginEnabled()Returns whether DB login argument is enabled. The information can be used for UI developers to control the input flow.- Specified by:
isDbLoginEnabled
in interfaceIJdbcConnectionProvider2
- Returns:
true
whendbLogin
argument can be passed toIJdbcConnectionProvider.getConnection(String, String, String, String, String, String...)
-
setDbLoginEnabled
public void setDbLoginEnabled(boolean b) Sets whether DB login argument is enabled. The information can be used for UI developers to control the input flow.- Specified by:
setDbLoginEnabled
in interfaceIJdbcConnectionProvider2
- Parameters:
b
-true
whendbLogin
argument can be passed toIJdbcConnectionProvider.getConnection(String, String, String, String, String, String...)
-
setDbLogin
A shortcut method forIJdbcConnectionProvider2.setDbLoginEnabled(boolean)
andIJdbcConnectionProvider2.setDefaultDbLogin(String)
.- Specified by:
setDbLogin
in interfaceIJdbcConnectionProvider2
- Parameters:
enabled
- whether to enable DB login argumentdefaultValue
- the default value
-
getDefaultDbLogin
Returns the default DB login argument to be used when DB login argument is disabled or actual value is empty.- Specified by:
getDefaultDbLogin
in interfaceIJdbcConnectionProvider2
- Returns:
- default value
-
setDefaultDbLogin
Sets the default DB login argument to be used when DB login argument is disabled or actual value is empty.- Specified by:
setDefaultDbLogin
in interfaceIJdbcConnectionProvider2
- Parameters:
s
- default value
-
getDbPassword
Returns the DB password argument to be used for URL construction. Descendants can override this method when they have more sophisticated algorithms to determine the DB password argument. This implementation returns the default DB password when the passed string is either empty or DB password argument was disabled.- Specified by:
getDbPassword
in interfaceIJdbcConnectionProvider2
- Parameters:
s
- the string containing an actual DB password argument- Returns:
- the argument to be used in URL construction
- See Also:
-
isDbPasswordEnabled
public boolean isDbPasswordEnabled()Returns whether DB password argument is enabled. The information can be used for UI developers to control the input flow.- Specified by:
isDbPasswordEnabled
in interfaceIJdbcConnectionProvider2
- Returns:
true
whendbPassword
argument can be passed toIJdbcConnectionProvider.getConnection(String, String, String, String, String, String...)
-
setDbPasswordEnabled
public void setDbPasswordEnabled(boolean b) Sets whether DB password argument is enabled. The information can be used for UI developers to control the input flow.- Specified by:
setDbPasswordEnabled
in interfaceIJdbcConnectionProvider2
- Parameters:
b
-true
whendbPassword
argument can be passed toIJdbcConnectionProvider.getConnection(String, String, String, String, String, String...)
-
setDbPassword
A shortcut method forIJdbcConnectionProvider2.setDbPasswordEnabled(boolean)
andIJdbcConnectionProvider2.setDefaultDbPassword(String)
.- Specified by:
setDbPassword
in interfaceIJdbcConnectionProvider2
- Parameters:
enabled
- whether to enable DB password argumentdefaultValue
- the default value
-
getDefaultDbPassword
Returns the default DB password argument to be used when DB password argument is disabled or actual value is empty.- Specified by:
getDefaultDbPassword
in interfaceIJdbcConnectionProvider2
- Returns:
- default value
-
setDefaultDbPassword
Sets the default DB password argument to be used when DB password argument is disabled or actual value is empty.- Specified by:
setDefaultDbPassword
in interfaceIJdbcConnectionProvider2
- Parameters:
s
- default value
-
getAdditionalArgument
Returns the additional argument to be used for URL construction. Descendants can override this method when they have more sophisticated algorithms to determine the additional argument. This implementation returns the default additional argument when the passed string is either empty or the additional argument was disabled.- Specified by:
getAdditionalArgument
in interfaceIJdbcConnectionProvider2
- Parameters:
index
- index of additional arguments
- the string containing an actual additional argument- Returns:
- the argument to be used in URL construction
- See Also:
-
isAdditionalArgumentEnabled
public boolean isAdditionalArgumentEnabled(int index) Returns whether additional argument is enabled. The information can be used for UI developers to control the input flow.- Specified by:
isAdditionalArgumentEnabled
in interfaceIJdbcConnectionProvider2
- Parameters:
index
- index of additional argument- Returns:
true
when additional argument can be passed toIJdbcConnectionProvider.getConnection(String, String, String, String, String, String...)
-
setAdditionalArgumentEnabled
public void setAdditionalArgumentEnabled(int index, boolean b) Sets whether additional argument is enabled. The information can be used for UI developers to control the input flow.- Specified by:
setAdditionalArgumentEnabled
in interfaceIJdbcConnectionProvider2
- Parameters:
index
- index of additional argumentb
-true
when additional argument can be passed toIJdbcConnectionProvider.getConnection(String, String, String, String, String, String...)
-
setAdditionalArgument
A shortcut method forIJdbcConnectionProvider2.setAdditionalArgumentEnabled(int, boolean)
,IJdbcConnectionProvider2.setAdditionalArgumentName(int, String)
andIJdbcConnectionProvider2.setDefaultAdditionalArgument(int, String)
.- Specified by:
setAdditionalArgument
in interfaceIJdbcConnectionProvider2
- Parameters:
index
- index of additional argumentname
- name of additional argumentenabled
- whether to enable additional argumentdefaultValue
- the default value
-
getDefaultAdditionalArgument
Returns the default additional argument to be used when it is disabled or actual value is empty.- Specified by:
getDefaultAdditionalArgument
in interfaceIJdbcConnectionProvider2
- Parameters:
index
- index of additional argument- Returns:
- default value
-
setDefaultAdditionalArgument
Sets the default additional argument to be used when it is disabled or actual value is empty.- Specified by:
setDefaultAdditionalArgument
in interfaceIJdbcConnectionProvider2
- Parameters:
index
- index of additional arguments
- default value
-
getAdditionalArgumentName
Returns the name of the additional argument. The name can be used for labels.- Specified by:
getAdditionalArgumentName
in interfaceIJdbcConnectionProvider2
- Parameters:
index
- index of argument- Returns:
- name of argument
-
setAdditionalArgumentName
Sets the name of the additional argument. The name can be used for labels.- Specified by:
setAdditionalArgumentName
in interfaceIJdbcConnectionProvider2
- Parameters:
index
- index of arguments
- name of argument
-
getAdditionalArgumentNames
Returns the names of additional arguments.- Specified by:
getAdditionalArgumentNames
in interfaceIJdbcConnectionProvider2
- Returns:
- array of names in order (array is empty when no additional arguments are used)
-
getAdditionalArgumentCount
public int getAdditionalArgumentCount()Returns the number of additional arguments this provider knows about.- Specified by:
getAdditionalArgumentCount
in interfaceIJdbcConnectionProvider2
- Returns:
- number of additional arguments
-
getDriverParameterCount
public int getDriverParameterCount()Returns the number of additional driver parameters.- Specified by:
getDriverParameterCount
in interfaceIJdbcConnectionProvider2
- Returns:
- number of driver parameters.
-
setDriverParameter
Sets a driver parameter value or deletes it.- Specified by:
setDriverParameter
in interfaceIJdbcConnectionProvider2
- Parameters:
name
- name of parametervalue
- value of parameter ornull
if parameter shall be removed
-
getDriverParameter
Returns the value of th edriver parameter.- Specified by:
getDriverParameter
in interfaceIJdbcConnectionProvider2
- Parameters:
name
- name of parameter- Returns:
- value of parameter
-
getDriverParametersString
Returns the URL driver parameter string to be used inurl
.- Specified by:
getDriverParametersString
in interfaceIJdbcConnectionProvider2
- Returns:
- the parameter string
-
getDisplay
Returns the display.- Returns:
- the display
-
toString
-