Module org.apache.sis.referencing
Class InstallationScriptProvider
Object
InstallationResources
InstallationScriptProvider
Provides SQL scripts needed for creating a local copy of a dataset. This class allows Apache SIS users
to bundle the EPSG or other datasets in their own product for automatic installation when first needed.
Implementations of this class are discovered automatically by
EPSGFactory
if they are declared
in module-info.java
as providers of the org.apache.sis.setup.InstallationResources
service.
How this class is used
The first time that anEPSGDataAccess
needs to be instantiated,
EPSGFactory
verifies if the EPSG database exists. If it does not, then:
EPSGFactory.install(Connection)
searches for the first instance ofInstallationResources
(the parent of this class) for which the set of authorities contains"EPSG"
.- The license may be shown to the user if the application allows that (for example when running as a console application).
- If the installation process is allowed to continue, it will iterate over all readers provided by
openScript(String, int)
and execute the SQL statements (not necessarily verbatim; the installation process may adapt to the target database).
- Since:
- 0.7
-
Field Summary
Modifier and TypeFieldDescriptionprotected static final String
A sentinel value for the content of the script to execute after the SQL scripts provided by the authority.protected static final String
A sentinel value for the content of the script to execute before the SQL scripts provided by the authority. -
Constructor Summary
ModifierConstructorDescriptionprotected
InstallationScriptProvider
(String authority, String... resources) Creates a new provider which will read script files of the given names in that order. -
Method Summary
Modifier and TypeMethodDescriptionReturns the identifiers of the dataset installed by the SQL scripts.String[]
getResourceNames
(String authority) Returns the names of all SQL scripts to execute.openScript
(String authority, int resource) Returns a reader for the SQL script at the given index.protected abstract InputStream
openStream
(String name) Opens the input stream for the SQL script of the given name.Methods inherited from class InstallationResources
getInstructionURL, getLicense, getResource, load
-
Field Details
-
PREPARE
A sentinel value for the content of the script to execute before the SQL scripts provided by the authority. This is an Apache SIS build-in script for constraining the values of someVARCHAR
columns to enumerations of values recognized byEPSGDataAccess
. Those enumerations are not required for proper working ofEPSGFactory
, but can improve data integrity.- See Also:
-
FINISH
A sentinel value for the content of the script to execute after the SQL scripts provided by the authority. This is an Apache SIS build-in script for creating indexes or performing any other manipulation that help SIS to use the dataset. Those indexes are not required for proper working ofEPSGFactory
, but can significantly improve performances.- See Also:
-
-
Constructor Details
-
InstallationScriptProvider
Creates a new provider which will read script files of the given names in that order. The given names are often filenames, but not necessarily (it is okay to use those names only as labels).Typical argument values Authority Argument values EPSG
{PREPARE, "EPSG_Tables.sql", "EPSG_Data.sql", "EPSG_FKeys.sql", FINISH}
- Parameters:
authority
- the authority (typically"EPSG"
), ornull
if not available.resources
- names of the SQL scripts to read.- See Also:
-
-
Method Details
-
getAuthorities
Returns the identifiers of the dataset installed by the SQL scripts. The values currently recognized by SIS are:"EPSG"
for the EPSG geodetic dataset.
null
. An empty set means that the provider does not have all needed resources or does not have permission to distribute the installation scripts.- Specified by:
getAuthorities
in classInstallationResources
- Returns:
- identifiers of SQL scripts that this instance can distribute.
-
getResourceNames
Returns the names of all SQL scripts to execute. This is a copy of the array of names given to the constructor. Those names are often filenames, but not necessarily (they may be just labels).- Specified by:
getResourceNames
in classInstallationResources
- Parameters:
authority
- the value given at construction time (e.g."EPSG"
).- Returns:
- the names of all SQL scripts to execute.
- Throws:
IllegalArgumentException
- if the givenauthority
argument is not the expected value.IOException
- if fetching the script names required an I/O operation and that operation failed.
-
openScript
Returns a reader for the SQL script at the given index. Contents may be read from files in a local directory, or from resources in a JAR file, or from entries in a ZIP file, or any other means at implementer choice. TheBufferedReader
instances shall be closed by the caller.EPSG case
In the EPSG dataset case, the iterator should returnBufferedReader
instances for the following files (replace<version>
by the EPSG version number and<product>
by the target database) in same order. The first and last files are provided by Apache SIS. All other files can be downloaded from https://epsg.org/.- Content of
PREPARE
, an optional data definition script that define the enumerations expected byEPSGDataAccess
. - Content of
"EPSG_<version>.mdb_Tables_<product>.sql"
, a data definition script that create empty tables. - Content of
"EPSG_<version>.mdb_Data_<product>.sql"
, a data manipulation script that populate the tables. - Content of
"EPSG_<version>.mdb_FKeys_<product>.sql"
, a data definition script that create foreigner key constraints. - Content of
FINISH
, an optional data definition and data control script that create indexes and set permissions.
Default implementation
The default implementation invokesopenStream(String)
– except forPREPARE
andFINISH
in which case an Apache SIS build-in script is used – and wrap the result in aLineNumberReader
. The file encoding is UTF-8 (the encoding used in the scripts distributed by EPSG since version 9.4).- Specified by:
openScript
in classInstallationResources
- Parameters:
authority
- the value given at construction time (e.g."EPSG"
).resource
- index of the SQL script to read, from 0 inclusive togetResourceNames(authority).length
exclusive.- Returns:
- a reader for the content of SQL script to execute.
- Throws:
IllegalArgumentException
- if the givenauthority
argument is not the expected value.IndexOutOfBoundsException
- if the givenresource
argument is out of bounds.FileNotFoundException
- if the SQL script of the given name has not been found.IOException
- if an error occurred while creating the reader.
- Content of
-
openStream
Opens the input stream for the SQL script of the given name. This method is invoked by the default implementation ofopenScript(String, int)
for all scripts exceptPREPARE
andFINISH
.Example 1
if thisInstallationScriptProvider
instance gets the SQL scripts from files in a well-known directory and if the names given at construction time are the filenames in that directory, then this method can be implemented as below:protected InputStream openStream(String name) throws IOException { return Files.newInputStream(directory.resolve(name)); }
Example 2
if thisInstallationScriptProvider
instance rather gets the SQL scripts from resources bundled in the same JAR files than and in the same package, then this method can be implemented as below:protected InputStream openStream(String name) { return MyClass.getResourceAsStream(name); }
- Parameters:
name
- name of the script file to open. Can benull
if the resource is not found.- Returns:
- an input stream opened of the given script file.
- Throws:
IOException
- if an error occurred while opening the file.
-