sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 03/03: Provides a way to setup the "SpatialMetadata" data source programmatically. https://issues.apache.org/jira/browse/SIS-395
Date Mon, 09 Sep 2019 12:43:52 GMT
This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit ced338eff36b9bc3418c739eac07bae92d0a65e6
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Mon Sep 9 14:43:14 2019 +0200

    Provides a way to setup the "SpatialMetadata" data source programmatically.
    https://issues.apache.org/jira/browse/SIS-395
---
 .../java/org/apache/sis/console/package-info.java  |   2 +-
 .../apache/sis/internal/metadata/Resources.java    |   5 +
 .../sis/internal/metadata/Resources.properties     |   1 +
 .../sis/internal/metadata/Resources_fr.properties  |   1 +
 .../sis/internal/metadata/ServicesForUtility.java  |  28 ++++++
 .../sis/internal/metadata/sql/Initializer.java     |  22 +++--
 .../org/apache/sis/internal/system/Supervisor.java |   2 -
 .../apache/sis/internal/util/MetadataServices.java |  25 +++++
 .../sis/internal/util/TemporalUtilities.java       |   2 -
 .../java/org/apache/sis/setup/Configuration.java   | 102 +++++++++++++++++++++
 .../java/org/apache/sis/util/Configuration.java    |   8 +-
 11 files changed, 184 insertions(+), 14 deletions(-)

diff --git a/application/sis-console/src/main/java/org/apache/sis/console/package-info.java
b/application/sis-console/src/main/java/org/apache/sis/console/package-info.java
index 09ee1c4..9fd9235 100644
--- a/application/sis-console/src/main/java/org/apache/sis/console/package-info.java
+++ b/application/sis-console/src/main/java/org/apache/sis/console/package-info.java
@@ -21,7 +21,7 @@
  *
  * <div class="section">SIS installation on remote machines</div>
  * Some sub-commands can operate on SIS installation on remote machines, provided that Apache
SIS
- * has been <a href="http://sis.apache.org/source.html#master">compiled with MBeans
enabled</a>
+ * has been {@linkplain org.apache.sis.internal.system.Supervisor#ENABLED compiled with MBeans
enabled}
  * and that the remote Java Virtual Machine has been started with the following options:
  *
  * {@preformat shell
diff --git a/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/Resources.java
b/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/Resources.java
index 7323666..f2badce 100644
--- a/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/Resources.java
+++ b/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/Resources.java
@@ -66,6 +66,11 @@ public final class Resources extends IndexedResourceBundle {
         public static final short BoxCrossesAntiMeridian = 3;
 
         /**
+         * Connection to “{0}” database is already initialized.
+         */
+        public static final short ConnectionAlreadyInitialized_1 = 6;
+
+        /**
          * This metadata element is already initialized with value “{0}”.
          */
         public static final short ElementAlreadyInitialized_1 = 2;
diff --git a/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/Resources.properties
b/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/Resources.properties
index 5ed3632..0e80c25 100644
--- a/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/Resources.properties
+++ b/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/Resources.properties
@@ -20,6 +20,7 @@
 # For resources shared by all modules in the Apache SIS project, see "org.apache.sis.util.resources"
package.
 #
 BoxCrossesAntiMeridian            = Bounding box crosses the antimeridian.
+ConnectionAlreadyInitialized_1    = Connection to \u201c{0}\u201d database is already initialized.
 ElementAlreadyInitialized_1       = This metadata element is already initialized with value
\u201c{0}\u201d.
 ElementsOmitted_1                 = \u2026 {0} elements omitted \u2026
 ExpectedInterface_2               = `{1}` is an implementation class. Specify the `{0}` interface
instead.
diff --git a/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/Resources_fr.properties
b/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/Resources_fr.properties
index ff8b409..c8eefe5 100644
--- a/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/Resources_fr.properties
+++ b/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/Resources_fr.properties
@@ -25,6 +25,7 @@
 #   U+00A0 NO-BREAK SPACE         before  :
 #
 BoxCrossesAntiMeridian            = La bo\u00eete englobante traverse l\u2019antim\u00e9ridien.
+ConnectionAlreadyInitialized_1    = La connexion \u00e0 la base de donn\u00e9es \u00ab\u202f{0}\u202f\u00bb
est d\u00e9j\u00e0 initialis\u00e9e.
 ElementAlreadyInitialized_1       = Cet \u00e9l\u00e9ment de m\u00e9ta-donn\u00e9e est d\u00e9j\u00e0
initialis\u00e9 avec la valeur \u00ab\u202f{0}\u202f\u00bb.
 ElementsOmitted_1                 = \u2026 {0} \u00e9l\u00e9ments omis \u2026
 ExpectedInterface_2               = `{1}` est une classe d\u2019impl\u00e9mentation. Sp\u00e9cifiez
l\u2019interface `{0}` \u00e0 la place.
diff --git a/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/ServicesForUtility.java
b/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/ServicesForUtility.java
index 891b375..2cf4b6c 100644
--- a/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/ServicesForUtility.java
+++ b/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/ServicesForUtility.java
@@ -20,6 +20,8 @@ import java.text.Format;
 import java.util.Locale;
 import java.util.TimeZone;
 import javax.sql.DataSource;
+import java.sql.SQLException;
+import java.util.function.Supplier;
 import org.opengis.util.ControlledVocabulary;
 import org.opengis.metadata.citation.Citation;
 import org.apache.sis.internal.util.MetadataServices;
@@ -30,6 +32,7 @@ import org.apache.sis.util.logging.Logging;
 import org.apache.sis.util.iso.Types;
 import org.apache.sis.util.Exceptions;
 import org.apache.sis.util.Classes;
+import org.apache.sis.util.resources.Errors;
 
 
 
@@ -142,4 +145,29 @@ public final class ServicesForUtility extends MetadataServices {
     public Format createCoordinateFormat(final Locale locale, final TimeZone timezone) {
         return ReferencingServices.getInstance().createCoordinateFormat(locale, timezone);
     }
+
+    /**
+     * Returns the data source for the SIS-wide "SpatialMetadata" database.
+     */
+    @Override
+    public DataSource getDataSource() throws SQLException {
+        try {
+            return Initializer.getDataSource();
+        } catch (SQLException e) {
+            throw e;
+        } catch (Exception e) {
+            throw new SQLException(Errors.format(Errors.Keys.CanNotConnectTo_1, Initializer.DATABASE),
e);
+        }
+    }
+
+    /**
+     * Specifies the data source to use if there is no JNDI environment or if no data source
is binded
+     * to {@code jdbc/SpatialMetadata}.
+     */
+    @Override
+    public void setDataSource(final Supplier<DataSource> ds) {
+        if (!Initializer.setDefault(ds)) {
+            throw new IllegalStateException(Resources.format(Resources.Keys.ConnectionAlreadyInitialized_1,
Initializer.DATABASE));
+        }
+    }
 }
diff --git a/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/sql/Initializer.java
b/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/sql/Initializer.java
index 7a74797..288c8a1 100644
--- a/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/sql/Initializer.java
+++ b/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/sql/Initializer.java
@@ -247,6 +247,8 @@ public abstract class Initializer {
      *             This supplier may return {@code null}, in which case it will be ignored.
      * @return whether the given data source supplier has been successfully set.
      *
+     * @see org.apache.sis.setup.Configuration#setDatabase(Supplier)
+     *
      * @since 1.0
      */
     @Configuration
@@ -263,15 +265,17 @@ public abstract class Initializer {
      * Returns the data source for the SIS-wide "SpatialMetadata" database.
      * This method returns the first of the following steps that succeed:
      *
-     * <ol>
-     *   <li>If a JNDI context exists, the data source registered under the {@code
jdbc/SpatialMetadata} name.</li>
+     * <ol class="verbose">
+     *   <li>If a JNDI context exists, use the data source registered under the {@code
"jdbc/SpatialMetadata"} name.</li>
      *   <li>Otherwise if a default data source {@linkplain #setDefault has been supplied},
use that data source.</li>
-     *   <li>Otherwise if the {@code non-free:sis-embedded-data} module is present
on the classpath and there is no
-     *       database already installed in the {@code SIS_DATA} directory, use the embedded
database.</li>
-     *   <li>If the {@code SIS_DATA} environment variable is defined, {@code jdbc:derby:$SIS_DATA/Databases/SpatialMetadata}.
-     *       This database will be created if it does not exist. Note that this is the only
case where we allow database
-     *       creation since we are in the directory managed by SIS.</li>
-     *   <li>If the {@code derby.system.home} property is defined, the data source
for {@code jdbc:derby:SpatialMetadata}.
+     *   <li>Otherwise if the {@code SIS_DATA} environment variable is defined,
+     *       use the data source for {@code "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.</li>
+     *   <li>Otherwise if the {@code non-free:sis-embedded-data} module is present
on the classpath,
+     *       use the embedded database.</li>
+     *   <li>Otherwise if the {@code "derby.system.home"} property is defined,
+     *       use the data source for {@code "jdbc:derby:SpatialMetadata"}.
      *       This database will <strong>not</strong> be created if it does not
exist.</li>
      *   <li>Otherwise (no JNDI, no environment variable, no Derby property set), {@code
null}.</li>
      * </ol>
@@ -286,6 +290,8 @@ public abstract class Initializer {
      * @throws java.lang.reflect.InvocationTargetException if an error occurred while setting
a data source bean property.
      * @throws Exception for any other kind of errors. This include {@link RuntimeException}
not documented above like
      *         {@link IllegalArgumentException}, {@link ClassCastException}, {@link SecurityException},
<i>etc.</i>
+     *
+     * @see org.apache.sis.setup.Configuration#getDatabase()
      */
     public static synchronized DataSource getDataSource() throws Exception {
         if (source == null) {
diff --git a/core/sis-utility/src/main/java/org/apache/sis/internal/system/Supervisor.java
b/core/sis-utility/src/main/java/org/apache/sis/internal/system/Supervisor.java
index 81358b6..8d87e58 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/internal/system/Supervisor.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/internal/system/Supervisor.java
@@ -59,8 +59,6 @@ public final class Supervisor extends StandardMBean implements SupervisorMBean
{
     /**
      * Whatever JMX agent is enabled. Setting this variable to {@code false} allows the
      * Java compiler to omit any dependency to this {@code Supervisor} class.
-     *
-     * @see <a href="http://sis.apache.org/source.html#master">Differences between
SIS master and branches</a>
      */
     static final boolean ENABLED = true;
 
diff --git a/core/sis-utility/src/main/java/org/apache/sis/internal/util/MetadataServices.java
b/core/sis-utility/src/main/java/org/apache/sis/internal/util/MetadataServices.java
index 8baeb26..e5101ec 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/internal/util/MetadataServices.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/internal/util/MetadataServices.java
@@ -21,6 +21,9 @@ import java.util.Locale;
 import java.util.TimeZone;
 import java.util.ResourceBundle;
 import java.util.MissingResourceException;
+import java.util.function.Supplier;
+import javax.sql.DataSource;
+import java.sql.SQLException;
 import org.opengis.annotation.UML;
 import org.opengis.metadata.citation.Citation;
 import org.apache.sis.internal.system.Modules;
@@ -176,4 +179,26 @@ public class MetadataServices extends OptionalDependency {
     public Format createCoordinateFormat(final Locale locale, final TimeZone timezone) {
         throw moduleNotFound();
     }
+
+    /**
+     * Returns the data source for the SIS-wide "SpatialMetadata" database.
+     *
+     * @return the data source for the {@code $SIS_DATA/Databases/SpatialMetadata} or equivalent
database, or {@code null} if none.
+     * @throws SQLException if an error occurred while fetching the database source.
+     */
+    public DataSource getDataSource() throws SQLException {
+        throw moduleNotFound();
+    }
+
+    /**
+     * Specifies the data source to use if there is no JNDI environment or if no data source
is binded
+     * to {@code jdbc/SpatialMetadata}.
+     *
+     * @param  ds  supplier of data source to set, or {@code null} for removing previous
supplier.
+     *             This supplier may return {@code null}, in which case it will be ignored.
+     * @throws IllegalStateException if {@link DataSource} has already be obtained before
this method call.
+     */
+    public void setDataSource(final Supplier<DataSource> ds) {
+        throw moduleNotFound();
+    }
 }
diff --git a/core/sis-utility/src/main/java/org/apache/sis/internal/util/TemporalUtilities.java
b/core/sis-utility/src/main/java/org/apache/sis/internal/util/TemporalUtilities.java
index f741f2a..46b554d 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/internal/util/TemporalUtilities.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/internal/util/TemporalUtilities.java
@@ -43,8 +43,6 @@ public final class TemporalUtilities extends Static {
      * until we can really provide a sis-temporal module.
      *
      * This constant will be removed after SIS release a sis-temporal module.
-     *
-     * @see <a href="http://sis.apache.org/source.html#master">Differences between
SIS master and branches</a>
      */
     public static final boolean REPORT_MISSING_MODULE = true;
 
diff --git a/core/sis-utility/src/main/java/org/apache/sis/setup/Configuration.java b/core/sis-utility/src/main/java/org/apache/sis/setup/Configuration.java
new file mode 100644
index 0000000..a27973d
--- /dev/null
+++ b/core/sis-utility/src/main/java/org/apache/sis/setup/Configuration.java
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.setup;
+
+import java.util.Optional;
+import javax.sql.DataSource;
+import java.sql.SQLException;
+import java.util.function.Supplier;
+import org.apache.sis.util.ArgumentChecks;
+import org.apache.sis.internal.util.MetadataServices;
+
+
+/**
+ * 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.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @version 1.0
+ * @since   1.0
+ * @module
+ */
+public final class Configuration {
+    /**
+     * The default configuration instance. We use instances instead than static methods in
case we want
+     * different configuration modes in a future Apache SIS version (for example a "strict"
mode versus
+     * a "lenient" mode), of for allowing configurations to be saved in a file and restored.
+     */
+    private static final Configuration DEFAULT = new Configuration();
+
+    /**
+     * Do not allow instantiation except by methods in this class.
+     */
+    private Configuration() {
+    }
+
+    /**
+     * Returns the current configuration.
+     *
+     * @return the current configuration.
+     */
+    public static Configuration current() {
+        return DEFAULT;
+    }
+
+    /**
+     * Returns the data source for the SIS-wide "SpatialMetadata" database.
+     * This method returns the first of the following steps that succeed:
+     *
+     * <ol>
+     *   <li>If a JNDI context exists, use the data source registered under the {@code
"jdbc/SpatialMetadata"} name.</li>
+     *   <li>Otherwise if a default data source {@linkplain #setDatabase has been supplied},
use that data source.</li>
+     *   <li>Otherwise if the {@code SIS_DATA} environment variable is defined,
+     *       use the data source for {@code "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.</li>
+     *   <li>Otherwise if the {@code non-free:sis-embedded-data} module is present
on the classpath,
+     *       use the embedded database.</li>
+     *   <li>Otherwise if the {@code "derby.system.home"} property is defined,
+     *       use the data source for {@code "jdbc:derby:SpatialMetadata"} database.
+     *       This database will <strong>not</strong> be created if it does not
exist.</li>
+     * </ol>
+     *
+     * @return the data source for the {@code "SpatialMetadata"} database.
+     * @throws SQLException if an error occurred while fetching the database source.
+     */
+    public Optional<DataSource> getDatabase() throws SQLException {
+        return Optional.of(MetadataServices.getInstance().getDataSource());
+    }
+
+    /**
+     * Specifies the data source to use if no {@code "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.
+     *
+     * <p>This method can be invoked only before the first attempt to {@linkplain #getDatabase()
get the database}.
+     * If the {@link DataSource} has already be obtained, then this method throws {@link
IllegalStateException}.</p>
+     *
+     * @param  source  supplier of data source to set.
+     *         The supplier may return {@code null}, in which case it will be ignored.
+     * @throws IllegalStateException if {@link DataSource} has already be obtained before
this method call.
+     */
+    public void setDatabase(final Supplier<DataSource> source) {
+        ArgumentChecks.ensureNonNull("source", source);
+        MetadataServices.getInstance().setDataSource(source);
+    }
+}
diff --git a/core/sis-utility/src/main/java/org/apache/sis/util/Configuration.java b/core/sis-utility/src/main/java/org/apache/sis/util/Configuration.java
index 03ab7ff..fb913ca 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/util/Configuration.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/util/Configuration.java
@@ -27,9 +27,15 @@ import java.lang.annotation.RetentionPolicy;
  * Annotates methods having a system-wide impact on the configuration of the Apache SIS library.
  * See <cite>"Use"</cite> javadoc link for a list of annotated methods.
  *
+ * <p><b>Do not use.</b> This annotation is for documentation purpose only
+ * and will be replaced by the {@link org.apache.sis.setup.Configuration} class.
+ *
  * @author  Martin Desruisseaux (IRD, Geomatys)
  * @version 0.3
- * @since   0.3
+ *
+ * @see <a href="https://issues.apache.org/jira/browse/SIS-86">SIS-86</a>
+ *
+ * @since 0.3
  * @module
  */
 @Documented


Mime
View raw message