Class Initializer
java.lang.Object
org.apache.sis.internal.metadata.sql.Initializer
- Direct Known Subclasses:
DatabaseListener
Manages the unique
DataSource
instance to the $SIS_DATA/Databases/SpatialMetadata
database.
This includes initialization of a new database if none existed. The schemas will be created by subclasses of
this Initializer
class, which must be registered in the following file:
Initializer
implementations should define the following methods:
createSchema(Connection)
— invoked when a new database is created.dataSourceChanged()
— invoked when the data source changed.
DataSource
instance, through JNDI or otherwise.- Since:
- 0.7
- Version:
- 1.2
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
A JNDI listener for being informed of changes in theDataSource
associated to"jdbc/SpatialMetadata"
. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static boolean
true
ifconnected(DatabaseMetaData)
has been invoked at least once.static final String
Name of the database to open in the$SIS_DATA/Databases
directory or the directory given by thederby.system.home
property.static final String
Name of the JNDI resource to lookup in the"java:comp/env"
context.private static DataSource
The unique, SIS-wide, data source to the$SIS_DATA/Databases/SpatialMetadata
database.private static Supplier
<DataSource> Data source specified by the user, to be used if no data source is specified by JNDI. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic LogRecord
connected
(DatabaseMetaData metadata) Prepares a log record saying that a connection to the spatial metadata database has been created.protected abstract void
createSchema
(Connection connection) Invoked for populating an initially empty database.protected abstract void
Invoked when the JNDI data source associated to"jdbc/SpatialMetadata"
changed.private static DataSource
embedded()
If thenon-free:sis-embedded-data
module is present on the classpath, returns the data source for embedded Derby database.static DataSource
Returns the data source for the SIS-wide "SpatialMetadata" database.static boolean
hasJNDI()
Returnstrue
if SIS will try to fetch theDataSource
from JNDI.static boolean
setDefault
(Supplier<DataSource> ds) Specifies the data source to use if there is no JNDI environment or if no data source is binded tojdbc/SpatialMetadata
.private static void
shutdown()
Invoked when the JVM is shutting down, or when the Servlet or OSGi bundle is uninstalled.static Object
unspecified
(Locale locale, boolean asLog) Returns a message for unspecified data source.
-
Field Details
-
DATABASE
Name of the database to open in the$SIS_DATA/Databases
directory or the directory given by thederby.system.home
property.Note: this field is public for the needs ofnon-free:sis-embedded-data
module.- See Also:
-
JNDI
Name of the JNDI resource to lookup in the"java:comp/env"
context.- See Also:
-
supplier
Data source specified by the user, to be used if no data source is specified by JNDI.- See Also:
-
source
The unique, SIS-wide, data source to the$SIS_DATA/Databases/SpatialMetadata
database. Created when first needed, and cleared on shutdown.- See Also:
-
connected
private static boolean connectedtrue
ifconnected(DatabaseMetaData)
has been invoked at least once. This is reset tofalse
if thesource
is changed. We use this information for logging purpose.
-
-
Constructor Details
-
Initializer
protected Initializer()For subclasses only.
-
-
Method Details
-
createSchema
Invoked for populating an initially empty database.- Parameters:
connection
- connection to the empty database.- Throws:
SQLException
- if an error occurred while populating the database.
-
dataSourceChanged
protected abstract void dataSourceChanged()Invoked when the JNDI data source associated to"jdbc/SpatialMetadata"
changed. -
setDefault
Specifies the data source to use if there is no JNDI environment or if no data source is binded tojdbc/SpatialMetadata
. Data source specified by JNDI has precedence over this supplier in order to let users control their data source. This method does nothing if the data source has already been initialized.Initializer
will not register any shutdown process for user supplied data source. We presume that database life cycle is managed by the caller.- Parameters:
ds
- supplier of data source to set, ornull
for removing previous supplier. This supplier may returnnull
, in which case it will be ignored.- Returns:
- whether the given data source supplier has been successfully set.
- Since:
- 1.0
- See Also:
-
getDataSource
Returns the data source for the SIS-wide "SpatialMetadata" database. This method returns the first of the following steps that succeed:- If a JNDI context exists, use the data source registered under the
"jdbc/SpatialMetadata"
name. - Otherwise if a default data source has been supplied, use that data source.
- Otherwise if the
SIS_DATA
environment variable is defined, use the data source for"jdbc:derby:$SIS_DATA/Databases/SpatialMetadata"
. That database will be created if it does not exist. Note that this is the only case where Apache SIS may create the database since it is located in the directory managed by Apache SIS. - Otherwise if the
non-free:sis-embedded-data
module is present on the classpath, use the embedded database. - Otherwise if the
"derby.system.home"
property is defined, use the data source for"jdbc:derby:SpatialMetadata"
. This database will not be created if it does not exist. - Otherwise (no JNDI, no environment variable, no Derby property set),
null
.
- Returns:
- the data source for the
$SIS_DATA/Databases/SpatialMetadata
or equivalent database, ornull
if none. - Throws:
NamingException
- if an error occurred while fetching the data source from a JNDI context.MalformedURLException
- if an error occurred while converting thederby.jar
file to URL.ClassNotFoundException
- ifderby.jar
has not been found on the JDK installation directory.InstantiationException
- if an error occurred while creatingorg.apache.derby.jdbc.EmbeddedDataSource
.NoSuchMethodException
- if a JDBC bean property has not been found on the data source.IllegalAccessException
- if a JDBC bean property of the data source is not public.InvocationTargetException
- if an error occurred while setting a data source bean property.Exception
- for any other kind of errors. This includeRuntimeException
not documented above likeIllegalArgumentException
,ClassCastException
,SecurityException
, etc.- See Also:
- If a JNDI context exists, use the data source registered under the
-
hasJNDI
public static boolean hasJNDI()Returnstrue
if SIS will try to fetch theDataSource
from JNDI.- Returns:
true
if a JNDI environment seems to be present.
-
embedded
If thenon-free:sis-embedded-data
module is present on the classpath, returns the data source for embedded Derby database. Otherwise returnsnull
.- Since:
- 0.8
- See Also:
-
connected
Prepares a log record saying that a connection to the spatial metadata database has been created. This method can be invoked afterDataSource.getConnection()
. When invoked for the first time, the record level is set toLevel.CONFIG
. On next calls, the level becomeLevel.FINE
.- Parameters:
metadata
- the value ofDataSource.getConnection().getMetaData()
or equivalent.- Returns:
- the record to log. Caller should set the source class name and source method name.
- Throws:
SQLException
- if an error occurred while fetching the database URL.- Since:
- 0.8
-
unspecified
Returns a message for unspecified data source. The message will depend on whether a JNDI context exists or not. This message can be used for constructing an exception whengetDataSource()
returnednull
. -
shutdown
Invoked when the JVM is shutting down, or when the Servlet or OSGi bundle is uninstalled. This method shutdowns the Derby database.- Throws:
ReflectiveOperationException
- if an error occurred while setting the shutdown property on the Derby data source.SQLException
- if call toWrapper.unwrap(Class)
failed. This exception should never happen sincesource
should always be an instance ofLocalDataSource
when this method is invoked, andSQLException
thrown by the database are not propagated here.
-