Class LocalDataSource
java.lang.Object
org.apache.sis.internal.metadata.sql.LocalDataSource
- All Implemented Interfaces:
Comparable<LocalDataSource>,Wrapper,CommonDataSource,DataSource
public final class LocalDataSource
extends Object
implements DataSource, Comparable<LocalDataSource>
A data source for a database stored locally in the
$SIS_DATA directory.
This class wraps the database-provided DataSource with the addition of a shutdown method.
It provides our starting point for initiating the system-wide connection.- Since:
- 1.1
- Version:
- 1.2
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) final booleanWhether the database needs to be created.private StringPath to the database to open on the local file system.private static final StringThe property name for the home of Derby databases.private final DialectThe database product to use.private DataSourceThe database-provided data source. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateLocalDataSource(Dialect dialect, String dbFile, boolean create) Prepares a new data source for the given database file. -
Method Summary
Modifier and TypeMethodDescriptionintcompareTo(LocalDataSource other) Compares this data source with the given one for preference order.(package private) static LocalDataSource[]Prepares potential data source for the spatial metadata database.(package private) final voidCreates the database if needed.(package private) static LocalDataSourcefindDriver(LocalDataSource[] sources) Returns the first data source from the given array that can be initialized.Attempts to establish a connection.getConnection(String username, String password) Attempts to establish a connection.intReturns the maximum time in seconds that this data source will wait while attempting to connect to a database.Returns the output stream to which all logging and tracing messages for this data source will be printed.Return the parent of all loggers used by this data source.private voidCreates the data source using the context class loader.static booleanReturnstrueif the given exception is the one that we expect in successful shutdown of a Derby database.booleanisWrapperFor(Class<?> type) Returns whetherunwrap(Class)can be invoked for the given type.voidsetLoginTimeout(int seconds) Sets the maximum time in seconds that this data source will wait while attempting to connect to a database.voidsetLogWriter(PrintWriter out) Sets the output stream to which all logging and tracing messages for this data source will be printed.(package private) final voidshutdown()Shutdowns the database used by this data source.toString()Returns a string representation for debugging purpose.<T> TReturns an object of the given type to allow access to non-standard methods.(package private) static DataSourcewrap(DataSource ds) Wraps an existing data source for adding a shutdown method to it.
-
Field Details
-
DERBY_HOME_KEY
The property name for the home of Derby databases.- See Also:
-
dialect
The database product to use. Currently supported values areDialect.DERBYandDialect.HSQL. -
dbFile
Path to the database to open on the local file system. This is the argument to give to theDataSource.setDatabaseName(String)method. This value is set tonullaftersourcecreation because not needed anymore. -
source
The database-provided data source. -
create
final boolean createWhether the database needs to be created.
-
-
Constructor Details
-
LocalDataSource
Prepares a new data source for the given database file. This construction is incomplete; after return eitherinitialize()shall be invoked or thisLocalDataSourceis discarded.- Parameters:
dialect-Dialect.DERBYorDialect.HSQL.dbFile- path to the database to open on the local file system.create- whether the database needs to be created.
-
-
Method Details
-
create
Prepares potential data source for the spatial metadata database. This constructor prepares the path to local file(s), but actualDataSourceconstruction must be done by a call toinitialize()after construction.- Parameters:
database- database name (usually "SpatialMetadata").dialects-Dialect.DERBYand/orDialect.HSQL.- Returns:
- the local data sources (not yet initialized), or
nullif none. If non-null, then the array is guaranteed to contain at least one element.
-
wrap
Wraps an existing data source for adding a shutdown method to it. This method is used for source of data embedded in a separated JAR file.- Parameters:
ds- the data source, usually given byInitializer.embedded().- Returns:
- the data source wrapped with a shutdown method, or
ds.
-
initialize
Creates the data source using the context class loader. It is caller's responsibility to shutdown the database after usage.- Throws:
ClassNotFoundException- if the database driver is not on the classpath.ReflectiveOperationException- if an error occurred while setting the properties on the data source.
-
findDriver
Returns the first data source from the given array that can be initialized. The database may be located in the$SIS_DATA/Databases/SpatialMetadatadirectory, or in theSpatialMetadatasub-directory of the path given by thederby.system.homeproperty.This method does not create the database if it does not exist, because this method does not know if we are inside the
$SIS_DATAdirectory.It is caller's responsibility to shutdown the database after usage.
- Parameters:
sources- the data sources to try.- Returns:
- the first data source for which a driver is available.
- Throws:
ClassNotFoundException- if no database driver is not on the classpath.Exception- if the operation failed for another reason.
-
createDatabase
Creates the database if needed. For Derby we need to explicitly allow creation. For HSQLDB the creation is enabled by default.- Throws:
Exception
-
shutdown
Shutdowns the database used by this data source.- Throws:
ReflectiveOperationException- if an error occurred while setting the shutdown property on the Derby data source.
-
isSuccessfulShutdown
Returnstrueif the given exception is the one that we expect in successful shutdown of a Derby database. While this method is primarily used for Derby shutdown, the error code tested may be applicable to other systems.Note: this method is public for the needs ofnon-free:sis-embedded-datamodule.- Parameters:
e- the exception thrown by Derby.- Returns:
trueif the exception indicates a successful shutdown.
-
compareTo
Compares this data source with the given one for preference order. The preferred data sources are the ones for a database that already exists.- Specified by:
compareToin interfaceComparable<LocalDataSource>- Parameters:
other- the other data source to compare with this one.- Returns:
- -1 if this data source is preferred to
other, +1 ifotheris preferred tothis, or 0 if no preference.
-
isWrapperFor
Returns whetherunwrap(Class)can be invoked for the given type.- Specified by:
isWrapperForin interfaceWrapper- Parameters:
type- the interface or implementation type of desired wrapped object.- Returns:
- whether
unwrap(Class)can be invoked for the given type. - Throws:
SQLException- if an error occurs while checking wrappers.
-
unwrap
Returns an object of the given type to allow access to non-standard methods. The type can be eitherLocalDataSourceor any type supported by the wrapped data source.- Specified by:
unwrapin interfaceWrapper- Type Parameters:
T- compile-time value oftype.- Parameters:
type- the interface or implementation type of desired wrapped object.- Returns:
- an object of the given type.
- Throws:
SQLException- if there is no object of the given type.
-
getConnection
Attempts to establish a connection.- Specified by:
getConnectionin interfaceDataSource- Returns:
- a connection to the locally installed database.
- Throws:
SQLException- if a database access error occurs.
-
getConnection
Attempts to establish a connection.- Specified by:
getConnectionin interfaceDataSource- Parameters:
username- the database user.password- the user's password.- Returns:
- a connection to the locally installed database.
- Throws:
SQLException- if a database access error occurs.
-
getLoginTimeout
Returns the maximum time in seconds that this data source will wait while attempting to connect to a database. Initial value is 0, meaning default timeout or no timeout.- Specified by:
getLoginTimeoutin interfaceCommonDataSource- Returns:
- the data source login time limit, or 0 for the default.
- Throws:
SQLException- if a database access error occurs.
-
setLoginTimeout
Sets the maximum time in seconds that this data source will wait while attempting to connect to a database.- Specified by:
setLoginTimeoutin interfaceCommonDataSource- Parameters:
seconds- the data source login time limit, or 0 for the default.- Throws:
SQLException- if a database access error occurs.
-
getParentLogger
Return the parent of all loggers used by this data source. Can be used for configuring log messages.- Specified by:
getParentLoggerin interfaceCommonDataSource- Returns:
- the parent of all loggers used by this data source.
- Throws:
SQLFeatureNotSupportedException- if the data source does not use logging.
-
getLogWriter
Returns the output stream to which all logging and tracing messages for this data source will be printed. The default writer isnull(logging disabled).- Specified by:
getLogWriterin interfaceCommonDataSource- Returns:
- the log writer, or null if logging is disabled.
- Throws:
SQLException- if a database access error occurs.
-
setLogWriter
Sets the output stream to which all logging and tracing messages for this data source will be printed. This method needs to be invoked for enabling logging.- Specified by:
setLogWriterin interfaceCommonDataSource- Parameters:
out- the log writer, or null if logging is disabled.- Throws:
SQLException- if a database access error occurs.
-
toString
Returns a string representation for debugging purpose.
-