# Class Configuration

Object
Configuration

public final class Configuration extends Object
Provides system-wide configuration for Apache SIS library. Methods in this class can be used for overriding SIS default values. Those methods can be used in final applications, but should not be used by libraries in order to avoid interfering with user's settings.

## Other system-wide configuration

The following methods have system-wide effects on Apache SIS configuration, but are not yet controlled through this `Configuration` class:

- `MonolineFormatter​.install()`
- `PerformanceLevel​.setMinDuration(long, TimeUnit)`

The following properties are defined by the standard Java environment. Apache SIS read those properties but does not modify them:

- `Locale​.getDefault()` (sometimes using `Locale​.Category`)
- `Charset​.defaultCharset()`
- `TimeZone​.getDefault()`
- `System​.lineSeparator()`
- `File​.pathSeparator`
- `File​.separator`

Since:
  1.0

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static Configuration`
  `current()`
  Returns the current configuration.
  `Optional<DataSource>`
  `getDatabase()`
  Returns the data source for the SIS-wide "SpatialMetadata" database.
  `void`
  `setDatabase(Supplier<DataSource> source)`
  Specifies the data source to use if no `"jdbc/SpatialMetadata"` source is binded to a JNDI environment.
  `void`
  `shutdown()`
  Shutdowns the Apache SIS library.

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### current

    public static Configuration current()
    Returns the current configuration.
    Returns:
      the current configuration.

  - ### getDatabase

    public Optional<DataSource> getDatabase() throws SQLException
    Returns the data source for the SIS-wide "SpatialMetadata" database. This method returns the first of the following steps that succeed:
    1. If a JNDI context exists, use the data source registered under the `"jdbc/SpatialMetadata"` name.
    2. Otherwise if a default data source has been supplied, use that data source.
    3. 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.
    4. Otherwise if the `org​.apache​.sis​.referencing​.database` module is present on the module path, use the embedded database.
    5. Otherwise if the "`derby​.system​.home`" property is defined, use the data source for `"jdbc:derby:SpatialMetadata"` database. This database will not be created if it does not exist.
    Returns:
      the data source for the `"SpatialMetadata"` database.
    Throws:
      `SQLException` - if an error occurred while fetching the database source.

  - ### setDatabase

    public void setDatabase(Supplier<DataSource> source)
    Specifies the data source to use if no `"jdbc/SpatialMetadata"` source is binded to a JNDI environment. Data source specified by JNDI has precedence over data source specified by this method in order to let users control their data source. The following example shows how to setup a connection to a PostgreSQL database:

        import org.postgresql.ds.PGSimpleDataSource;

        class MyClass {
            private static DataSource createDataSource() {
                PGSimpleDataSource ds = new PGSimpleDataSource();
                ds.setDatabaseName("SpatialMetadata");
                // Server default to "localhost".
                return ds;
            }

            static initialize() {
                if (WANT_TO_CONFIGURE_JNDI) {
                    // Registration assuming that a JNDI implementation is available
                    Context env = (Context) InitialContext.doLookup("java:comp/env");
                    env.bind("jdbc/SpatialMetadata", createDataSource());
                }

                // Fallback if there is no JNDI or no "SpatialMetadata" entry.
                Configuration.current().setDatabase(MyClass::createDataSource);
            }
        }

    This method can be invoked only before the first attempt to get the database. If the `DataSource` has already be obtained, then this method throws `IllegalStateException`.
    Parameters:
      `source` - supplier of data source to set. The supplier may return `null`, in which case it will be ignored.
    Throws:
      `IllegalStateException` - if `DataSource` has already be obtained before this method call.
    See Also:
      - How to use EPSG geodetic dataset

  - ### shutdown

    public void shutdown()
    Shutdowns the Apache SIS library. This method closes database connections and stops the daemon threads that were started by SIS. The Apache SIS library shall not be used anymore after this method call. Any use of Apache SIS after this method call may have unexpected effects. In particular, it may cause memory leaks.

    #### When to use

    This method should generally not be invoked, because Apache SIS registers itself a shutdown hook to the Java Virtual Machine. This method may be useful in embedded environments that do not allow the use of shutdown hooks, or when waiting for the JVM shutdown is overly conservative.

    #### Complete shutdown

    This method shutdowns only the databases used by Apache SIS. If Apache Derby is used for the EPSG database, some Derby daemon threads may still be running. Those daemons can be ignored in standalone applications, but may need to be stopped in embedded environments. A complete Derby shutdown can be requested with the following code:

        Configuration.current().shutdown();
        try {
            DriverManager.getConnection("jdbc:derby:;shutdown=true");
        } catch (SQLException e) {
            // Expected exception as per Derby documentation.
        }

    Since:
      1.5
    See Also:
      - Shutting down Derby