|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectuk.ac.ic.doc.automed.DSR
public class DSR
Reads data source definitions from a configuration file and provides methods
to get a Connection
for a data souce.
Configuration File
The default configuration file
"uk.ac.ic.doc.automed.DSR::data_source_repository.cfg" is read and
checked for syntax errors when the class is first loaded.
Blank lines (lines containing only white space) are ignored. Comments can
appear anywhere, they start with a '#' character and extend to the end of
their line. A directive is a line that starts with a keyword (preceeded onlly
by white space) which is followed by the keyword's value. The value starts
from the first non white space character after the keyword and extends to the
end of the line or first comment character. All white space within the value
is preserved. Trailing white space and white space between a value and a
comment are stripped. Multline values can be specified using '\' as the last,
non white space character before the the beginning of a comment or the end of
the line. The newlines seperating the lines are preserved.
A data source definition must begin with the DataSource directive. The order of the directives after the DataSource directive is not significant.
Directives
Keyword | Argument Type | Description | |
---|---|---|---|
DataSource | String | The name of the data source for which all directives up to the next DataSouce apply | Required |
ConnectionType | String | Currently only type "JDBC" is supported | Required |
JdbcURL | String | The JDBC URL used to connect to the data source | Required |
JdbcDriver | String | The fully qualified name of the JDBC driver class needed to connect to the data source | Required |
Username | String | Username needed to connect to data source | Optional |
Password | String | Password needed to connect to data source | Optional |
ZeroForNull | String | A table name and field name seperated by a period. Certain commands such as display and exportdb will use this to convert values of the field to null when they are zero in a result set quering the table. This is get round a bug in the postgresql driver where integer foreign key fields return 0 when their value is null. | Optional |
SerialID | String | A table name and field name seperated by a period. A field marked as SerialID will still have it's values written by the exportdb command but the importdb command will not try to set a value for the field when inserting rows. It is assumed that the underlying database will supply values (presumably sequential). | Optional |
SerialRef | String | A table name and field name seperated by a period followed by a colon and another table name and field. The first field marked as a SerialRef should be a field that is a foreign key to the second field named which itself should be a SerialID field. importdb will use the value supplied by the DBS for the SerialID field when inserting the SerialRef field. | Optional |
A property can be defined using the same syntax, any unrecognized keyword is interpreted as a property name and the value is assumed to be of type String. The difference between properties and keywords is that keywords are those properties that the DSR "knows about"
Nested Class Summary |
---|
Nested classes/interfaces inherited from interface uk.ac.ic.doc.automed.xml.XMLifyConstants |
---|
XMLifyConstants.IgnoreNextElementException |
Field Summary | |
---|---|
private static java.lang.Class |
commandClass
The Class commandClass is reflected upon for getters, setters etc. |
private static java.lang.String[][] |
commandData
The names of the commands to add using {@link #addCommand(String, Class) |
private static java.util.Map |
commandHelp
|
private static java.util.Map |
commandParamsHelp
|
private static java.util.Map |
commands
(String)Command -> (Method)do |
private static int |
CONNECTION_TYPE_JDBC
|
private static int |
CONNECTION_TYPE_UNKNOWN
|
private static java.util.Map |
creaters
|
private static java.lang.String |
currentConfigFile
|
private static java.lang.String |
currentConfigFileFullPath
|
private static java.util.Map |
dataSources
(String)Name -> HashMap of (String)Property -> (Object)Value |
private static java.util.Map |
dbas
(String)DataSource -> Connection |
private static java.lang.String |
defaultDataSource
For users of the command line dispatcher to set if they don't want their user to have to specify a data source for each command |
private static java.lang.String |
defaultUserName
|
private static java.util.Map |
getters
|
private static java.util.Map |
isers
(String)Property -> Method |
private static java.lang.Object[][] |
keywordData
definition of keywords and their types a keyword's type is either null, in which case the keyword takes no argument or it must be Character.class or the class must have a constructor that takes a string argument. |
private static java.util.Map |
keywords
(String)Keyword -> (Class)Type |
private static java.util.List |
reloadHooks
|
private static java.util.Map |
removers
|
private static java.util.Map |
serialIDFields
|
private static java.util.Map |
serialRefFields
of (String)DataSource -> HashMap of (String)Table -> HashMap of (String)Field->{(String)Table, (String)Field} |
private static java.util.Map |
setters
|
private static java.util.Map |
zeroForNullFields
of (String)DataSource -> HashMap of (String)Table -> HashSet of (String)Field |
Constructor Summary | |
---|---|
DSR()
Should never be instantiated |
Method Summary | |
---|---|
static java.lang.reflect.Method |
addCommand(java.lang.String command,
java.lang.Class methodClass,
java.lang.String help)
Looks for a public method with the signature do |
static void |
addReloadHook(java.lang.reflect.Method m)
|
private static void |
checkDataSource(java.util.Map dataSource)
Checks the syntax of the given data source's directives. |
static void |
close()
Close all open connections. |
static void |
commit()
Use this method when auto-commit is set to false |
static void |
dispatchCommand(java.lang.String[] args)
Invokes the method registered for the command args[0] with #addCommand(String, Class) . |
static void |
doCreateCommand(java.lang.String[] args)
|
static void |
doCreatedbCommand(java.lang.String[] args)
Reads the property CreateDB property for the data source and executes the value as an update on the databse connection |
static void |
doDefaultCommand(java.lang.String[] args)
|
static void |
doDisplayCommand(java.lang.String[] args)
|
static void |
doDropdbCommand(java.lang.String[] args)
|
static void |
doGetCommand(java.lang.String[] args)
|
private static void |
doGetSetCreateRemoveCommand(java.lang.String[] args,
java.util.Map methods,
java.lang.String prefix)
|
static void |
doHelpCommand(java.lang.String[] args)
|
static void |
doIsCommand(java.lang.String[] args)
|
static void |
doPopulatedbCommand(java.lang.String[] args)
|
static void |
doPrintCommand(java.lang.String[] args)
|
static void |
doReloadCommand(java.lang.String[] args)
|
static void |
doRemoveCommand(java.lang.String[] args)
|
static void |
doSetCommand(java.lang.String[] args)
|
static java.lang.Object |
executeStatement(java.lang.String dataSource,
java.lang.String sql,
boolean display)
|
static java.lang.Object |
executeStatement(java.lang.String dataSource,
java.lang.String sql,
boolean display,
java.util.Set nullifyZeros)
|
static java.lang.Object[] |
executeStatements(java.lang.String dataSource,
java.lang.String sql,
boolean display)
|
static java.lang.Object[] |
executeStatements(java.lang.String dataSource,
java.lang.String sql,
boolean display,
java.util.Set nullifyZeros)
|
private static java.lang.String |
extractDataSource(java.lang.String[] args)
|
private static java.lang.String |
extractDataSource(java.lang.String[] args,
int start)
|
(package private) static java.sql.Connection |
getCachedConnectionToDataSource(java.lang.String name)
|
static java.lang.Class |
getCommandClass()
|
static java.lang.String |
getConfigFileName()
Returns the name of the file used for data source definitions. |
static java.lang.String |
getConnectionJDBCURL(java.lang.String connectionName)
Return the JDBC URL assocaited with a DSR identified by its name |
static java.lang.String |
getConnectionUsername(java.lang.String connectionName)
Return the username associated with a DSR identified by its name |
private static java.lang.String |
getConnectionValue(java.lang.String connectionName,
java.lang.String key)
Lookup a key value for a connection, as read in from a .cfg file |
static java.sql.Connection |
getDataSourceConnection(java.lang.String name)
Gets a connection to the named data souce. |
static DBAccess |
getDataSourceConnection(java.lang.String name,
java.lang.String username,
java.lang.String password)
Gets a connection to the named data souce. |
static java.lang.Object |
getDataSourceProperty(java.lang.String dataSourceName,
java.lang.String propertyName)
Returns the named property for the named data source as defined in the configuration file. |
static DBAccess |
getDBAccessObject(java.lang.String dsname)
Return the DBAccess object that manages the connections to a data source |
static java.lang.String |
getDefaultDataSource()
|
static java.lang.String |
getDefaultUserName()
|
private static java.lang.String |
getParametersString(java.lang.reflect.Method m)
|
static java.sql.PreparedStatement |
getPreparedStatement(java.lang.String dsname,
java.lang.String sql)
Create a prepared statement object of the data source. |
private static java.lang.String[] |
getTableNames(java.lang.String dataSourceName)
|
static void |
main(java.lang.String[] args)
Provides a simple command line interface to enable the configuration file to be checked. |
private static void |
printDataSource(java.util.HashMap dataSource)
|
private static void |
printDataSource(java.lang.String dataSource)
|
protected static void |
readConfigFile()
Reads the current configuration file. |
protected static void |
readConfigFile(java.lang.String configFile)
Reads the directives for the data sources defined in configFile. |
private static void |
readConfigFile(java.lang.String configFile,
java.util.Map currentDataSource)
|
static void |
setCommandClass(java.lang.Class c)
|
static void |
setCommandHelp(java.lang.String[][] commandHelp)
|
static void |
setConfigFileName(java.lang.String filename)
Sets the name of the file used for data source defintiions, this resets any data cached in the MDR or STR and invalidates all objects returned by them prior to this call. |
static void |
setDefaultDataSource(java.lang.String name)
|
static void |
setDefaultUserName(java.lang.String name)
|
static void |
shutdown()
Tear down all connections and shutdown embedded databases. |
static void |
testDataSource()
Useful for auto-start, embedded data source when we just want to load it |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final int CONNECTION_TYPE_UNKNOWN
private static final int CONNECTION_TYPE_JDBC
private static final java.util.Map keywords
private static final java.util.Map dataSources
private static final java.util.Map commands
private static final java.util.Map dbas
private static final java.util.Map commandHelp
private static java.lang.String currentConfigFile
private static java.lang.String currentConfigFileFullPath
private static java.util.Map isers
private static java.util.Map getters
private static java.util.Map setters
private static java.util.Map creaters
private static java.util.Map removers
private static final java.util.Map zeroForNullFields
private static final java.util.Map serialIDFields
private static final java.util.Map serialRefFields
private static final java.util.Map commandParamsHelp
private static java.lang.String defaultDataSource
private static java.lang.String defaultUserName
private static java.lang.Class commandClass
private static final java.lang.Object[][] keywordData
private static final java.lang.String[][] commandData
private static java.util.List reloadHooks
Constructor Detail |
---|
DSR()
Method Detail |
---|
public static java.lang.String getDefaultDataSource()
public static void setDefaultDataSource(java.lang.String name)
public static java.lang.String getDefaultUserName()
public static void setDefaultUserName(java.lang.String name)
public static void setCommandHelp(java.lang.String[][] commandHelp)
public static java.lang.Class getCommandClass()
public static void setCommandClass(java.lang.Class c)
protected static void readConfigFile() throws java.io.IOException, DSRException
readConfigFile(String)
with the name of the last configuration
file read.
java.io.IOException
DSRException
readConfigFile(String configFile)
protected static void readConfigFile(java.lang.String configFile) throws java.io.IOException, DSRException
configFile
- The name of the file to read data source definitions from.
java.io.IOException
- If there's a problem reading the file.
DSRException
- If there's an error in the configuration file.private static void readConfigFile(java.lang.String configFile, java.util.Map currentDataSource) throws java.io.IOException, DSRException
java.io.IOException
DSRException
private static void checkDataSource(java.util.Map dataSource) throws DSRException
DSRException
public static java.lang.Object getDataSourceProperty(java.lang.String dataSourceName, java.lang.String propertyName) throws DSRException
DSRException
public static java.sql.Connection getDataSourceConnection(java.lang.String name) throws java.io.IOException, java.sql.SQLException, DSRException
getDataSourceConnection(String name, String username, String password)
supplying as username and password the values defined for the data source
or null if they aren't defined.
java.io.IOException
java.sql.SQLException
DSRException
getDataSourceConnection(String name, String username, String
password)
public static DBAccess getDBAccessObject(java.lang.String dsname)
dsname
-
public static java.sql.PreparedStatement getPreparedStatement(java.lang.String dsname, java.lang.String sql) throws java.sql.SQLException
dsname
-
java.sql.SQLException
public static DBAccess getDataSourceConnection(java.lang.String name, java.lang.String username, java.lang.String password) throws java.io.IOException, java.sql.SQLException, DSRException
name
- The name of the data source as defined in the configuration
file.username
- The username to use when establishing the connection or null if
not required or to use the defined value for the data source or
prompt the user if not defined.password
- The password to use when establishing the connection or null if
not required or to use the defined value for the data source or
to prompt the user if not defined.
Connection
to the named data source.
java.ioIOException
- if there's a problem reading input from the user.
java.sql.SQLException
- from the DriverManager.getConnection(String,
String, String)
.
DSRException
- if there's no definition for the named data source or the
connection type is unknown or (for connection type
"JDBC") the JdbcDriver can't be found or doesn't
implement Driver
.
java.io.IOException
static java.sql.Connection getCachedConnectionToDataSource(java.lang.String name) throws java.io.IOException, java.sql.SQLException, DSRException
java.io.IOException
java.sql.SQLException
DSRException
public static void close() throws DSRException
shutdown()
when your application
is closing but other applications are still using the database!
DSRException
public static void commit() throws DSRException
DSRException
public static void shutdown() throws DSRException
DSRException
private static java.lang.String getConnectionValue(java.lang.String connectionName, java.lang.String key) throws NotFoundException
NotFoundException
public static java.lang.String getConnectionUsername(java.lang.String connectionName)
public static java.lang.String getConnectionJDBCURL(java.lang.String connectionName) throws NotFoundException
NotFoundException
private static java.lang.String[] getTableNames(java.lang.String dataSourceName) throws DSRException
DSRException
public static java.lang.Object executeStatement(java.lang.String dataSource, java.lang.String sql, boolean display) throws DSRException, java.io.IOException, java.sql.SQLException
DSRException
java.io.IOException
java.sql.SQLException
public static java.lang.Object executeStatement(java.lang.String dataSource, java.lang.String sql, boolean display, java.util.Set nullifyZeros) throws DSRException, java.io.IOException, java.sql.SQLException
DSRException
java.io.IOException
java.sql.SQLException
public static java.lang.Object[] executeStatements(java.lang.String dataSource, java.lang.String sql, boolean display) throws DSRException, java.io.IOException, java.sql.SQLException
DSRException
java.io.IOException
java.sql.SQLException
public static java.lang.Object[] executeStatements(java.lang.String dataSource, java.lang.String sql, boolean display, java.util.Set nullifyZeros) throws DSRException, java.io.IOException, java.sql.SQLException
DSRException
java.io.IOException
java.sql.SQLException
public static void main(java.lang.String[] args)
public static void dispatchCommand(java.lang.String[] args) throws java.lang.Exception
#addCommand(String, Class)
.
DSRException
- if the command is not registered
java.lang.Exception
- the target exception if invoke throghs
InvocationTargetExceptionpublic static java.lang.reflect.Method addCommand(java.lang.String command, java.lang.Class methodClass, java.lang.String help) throws java.lang.NoSuchMethodException
java.lang.NoSuchMethodException
public static void doDefaultCommand(java.lang.String[] args)
public static void doHelpCommand(java.lang.String[] args)
public static void doPrintCommand(java.lang.String[] args)
public static void doReloadCommand(java.lang.String[] args) throws java.io.IOException
java.io.IOException
public static void doCreatedbCommand(java.lang.String[] args) throws DSRException, java.io.IOException, java.sql.SQLException
DSRException
java.io.IOException
java.sql.SQLException
public static void doDropdbCommand(java.lang.String[] args) throws DSRException, java.io.IOException, java.sql.SQLException
DSRException
java.io.IOException
java.sql.SQLException
public static void doDisplayCommand(java.lang.String[] args) throws DSRException, java.io.IOException, java.sql.SQLException
DSRException
java.io.IOException
java.sql.SQLException
public static void doPopulatedbCommand(java.lang.String[] args) throws DSRException, java.sql.SQLException, java.io.IOException
DSRException
java.sql.SQLException
java.io.IOException
public static void doIsCommand(java.lang.String[] args) throws java.lang.Exception
java.lang.Exception
public static void doGetCommand(java.lang.String[] args) throws java.lang.Exception
java.lang.Exception
public static void doSetCommand(java.lang.String[] args) throws java.lang.Exception
java.lang.Exception
public static void doCreateCommand(java.lang.String[] args) throws java.lang.Exception
java.lang.Exception
public static void doRemoveCommand(java.lang.String[] args) throws java.lang.Exception
java.lang.Exception
private static void doGetSetCreateRemoveCommand(java.lang.String[] args, java.util.Map methods, java.lang.String prefix) throws java.lang.Exception
java.lang.Exception
private static java.lang.String getParametersString(java.lang.reflect.Method m)
private static void printDataSource(java.lang.String dataSource)
private static void printDataSource(java.util.HashMap dataSource)
private static java.lang.String extractDataSource(java.lang.String[] args)
private static java.lang.String extractDataSource(java.lang.String[] args, int start)
public static java.lang.String getConfigFileName()
public static void setConfigFileName(java.lang.String filename) throws java.io.IOException, DSRException
~/.automed/data_source_repository.cfg
by default.
filename
- the name of the file used for data source definitions.
java.io.IOException
DSRException
public static void addReloadHook(java.lang.reflect.Method m)
public static void testDataSource() throws DSRException
DSRException
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |