sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1733108 - in /sis/branches/JDK8: application/sis-console/src/main/java/org/apache/sis/console/ application/sis-console/src/main/resources/META-INF/services/ core/sis-referencing/src/main/java/org/apache/sis/referencing/ core/sis-referencin...
Date Tue, 01 Mar 2016 17:06:30 GMT
Author: desruisseaux
Date: Tue Mar  1 17:06:30 2016
New Revision: 1733108

URL: http://svn.apache.org/viewvc?rev=1733108&view=rev
Log:
Refactoring: transfer more responsibility from InstallationScriptProvider to InstallationResources.
The intend is to get more flexibility for applying the same mechanism to resources provided
by other authorities than EPSG.

Added:
    sis/branches/JDK8/application/sis-console/src/main/resources/META-INF/services/org.apache.sis.setup.InstallationResources
      - copied unchanged from r1733107, sis/branches/JDK8/application/sis-console/src/main/resources/META-INF/services/org.apache.sis.referencing.factory.sql.InstallationScriptProvider
Removed:
    sis/branches/JDK8/application/sis-console/src/main/resources/META-INF/services/org.apache.sis.referencing.factory.sql.InstallationScriptProvider
Modified:
    sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/LicensedDataProvider.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/IdentifiedObjects.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/IdentifiedObjectFinder.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/sql/EPSGFactory.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/sql/EPSGInstaller.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/sql/InstallationScriptProvider.java
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/sql/EPSGInstallerTest.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/setup/InstallationResources.java

Modified: sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/LicensedDataProvider.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/LicensedDataProvider.java?rev=1733108&r1=1733107&r2=1733108&view=diff
==============================================================================
--- sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/LicensedDataProvider.java
[UTF-8] (original)
+++ sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/LicensedDataProvider.java
[UTF-8] Tue Mar  1 17:06:30 2016
@@ -16,10 +16,11 @@
  */
 package org.apache.sis.console;
 
+import java.util.Collections;
 import java.util.Locale;
+import java.util.Set;
 import java.io.Console;
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.BufferedReader;
 import java.net.URL;
 import java.net.URLClassLoader;
@@ -27,14 +28,21 @@ import java.util.ServiceLoader;
 import java.io.FileNotFoundException;
 import java.nio.file.AccessDeniedException;
 import java.sql.Connection;                             // For javadoc.
-import org.apache.sis.internal.util.Constants;
-import org.apache.sis.referencing.factory.sql.InstallationScriptProvider;
+import org.apache.sis.util.resources.Errors;
 import org.apache.sis.internal.util.Fallback;
+import org.apache.sis.setup.InstallationResources;
+
+import static org.apache.sis.internal.util.Constants.EPSG;
 
 
 /**
  * A provider for data licensed under different terms of use than the Apache license.
- * This class is in charge of asking user's agreement before to install those data.
+ * This class is in charge of downloading the data if necessary and asking user's agreement
+ * before to install them. Authorities managed by the current implementation are:
+ *
+ * <ul>
+ *   <li>{@code "EPSG"} for the EPSG geodetic dataset.</li>
+ * </ul>
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.7
@@ -42,7 +50,7 @@ import org.apache.sis.internal.util.Fall
  * @module
  */
 @Fallback
-public class LicensedDataProvider extends InstallationScriptProvider {
+public class LicensedDataProvider extends InstallationResources {
     /**
      * Where to download the EPSG scripts after user has approved the terms of use.
      *
@@ -64,7 +72,7 @@ public class LicensedDataProvider extend
     /**
      * The provider to use for fetching the actual licensed data after we got user's agreement.
      */
-    private InstallationScriptProvider provider;
+    private InstallationResources provider;
 
     /**
      * {@code true} if the user has accepted the EPSG terms of use, {@code false} if (s)he
refused,
@@ -84,23 +92,23 @@ public class LicensedDataProvider extend
      * Returns the name of the authority who provides data under non-Apache terms of use.
      * If this {@code LicensedDataProvider} can not ask user's agreement because there is
      * no {@link Console} attached to the current Java virtual machine, then this method
-     * returns {@code "unavailable"}.
+     * returns an empty set.
      *
-     * @return {@code "EPSG"} or {@code "unavailable"}.
+     * @return {@code "EPSG"} or an empty set.
      */
     @Override
-    public String getAuthority() {
-        return (console != null) ? Constants.EPSG : "unavailable";
+    public Set<String> getAuthorities() {
+        return (console != null) ? Collections.singleton(EPSG) : Collections.emptySet();
     }
 
     /**
      * Downloads the provider to use for fetching the actual licensed data after we got user's
agreement.
      */
-    private static InstallationScriptProvider download() throws IOException {
-        for (final InstallationScriptProvider c : ServiceLoader.load(InstallationScriptProvider.class,
+    private static InstallationResources download() throws IOException {
+        for (final InstallationResources c : ServiceLoader.load(InstallationResources.class,
                 new URLClassLoader(new URL[] {new URL(DOWNLOAD_URL)})))
         {
-            if (!c.getClass().isAnnotationPresent(Fallback.class)) {
+            if (!c.getClass().isAnnotationPresent(Fallback.class) && c.getAuthorities().contains(EPSG))
{
                 return c;
             }
         }
@@ -115,7 +123,12 @@ public class LicensedDataProvider extend
      * @throws AccessDeniedException if the user does not accept to install the EPSG dataset.
      * @throws IOException if an error occurred while reading the {@link #DOWNLOAD_URL}.
      */
-    private synchronized InstallationScriptProvider provider(final boolean requireAgreement)
throws IOException {
+    private synchronized InstallationResources provider(final String authority, final boolean
requireAgreement)
+            throws IOException
+    {
+        if (!EPSG.equals(authority)) {
+            throw new IllegalArgumentException(Errors.format(Errors.Keys.IllegalArgumentValue_2,
"authority", authority));
+        }
         if (provider == null) {
             if (console == null) {
                 throw new IllegalStateException();
@@ -129,7 +142,7 @@ public class LicensedDataProvider extend
             provider = download();
         }
         if (requireAgreement && accepted == null) {
-            final String license = getLicense(locale, "text/plain");
+            final String license = getLicense(authority, locale, "text/plain");
             if (license != null) {
                 console.format("%n").writer().write(license);
             }
@@ -163,60 +176,56 @@ public class LicensedDataProvider extend
     }
 
     /**
-     * Returns the terms of use of the dataset, or {@code null} if none.
+     * Returns the terms of use of the dataset provided by the given authority, or {@code
null} if none.
      * The terms of use can be returned in either plain text or HTML.
      *
+     * @param  authority One of the values returned by {@link #getAuthorities()}.
      * @param  mimeType Either {@code "text/plain"} or {@code "text/html"}.
      * @return The terms of use in plain text or HTML, or {@code null} if none.
+     * @throws IllegalArgumentException if the given {@code authority} argument is not one
of the expected values.
      * @throws IOException if an error occurred while reading the license file.
      */
     @Override
-    public String getLicense(Locale locale, String mimeType) throws IOException {
-        return provider(false).getLicense(locale, mimeType);
+    public String getLicense(String authority, Locale locale, String mimeType) throws IOException
{
+        return provider(authority, false).getLicense(authority, locale, mimeType);
     }
 
     /**
-     * Returns the names of all SQL scripts to execute.
+     * Returns the names of installation scripts provided by the given authority.
      * This method is invoked by {@link org.apache.sis.referencing.factory.sql.EPSGFactory#install(Connection)}
      * for listing the SQL scripts to execute during EPSG dataset installation.
      *
      * <p>If that question has not already been asked, this method asks to the user
if (s)he accepts
      * EPSG terms of use. If (s)he refuses, an {@link AccessDeniedException} will be thrown.</p>
      *
+     * @param  authority One of the values returned by {@link #getAuthorities()}.
      * @return The names of all SQL scripts to execute.
+     * @throws IllegalArgumentException if the given {@code authority} argument is not one
of the expected values.
      * @throws IOException if an error occurred while fetching the script names.
      */
     @Override
-    public String[] getScriptNames() throws IOException {
-        return provider(true).getScriptNames();
+    public String[] getResourceNames(final String authority) throws IOException {
+        return provider(authority, true).getResourceNames(authority);
     }
 
     /**
-     * Returns a reader for the SQL script at the given index.
+     * Returns a reader for the installation script at the given index.
      * This method is invoked by {@link org.apache.sis.referencing.factory.sql.EPSGFactory#install(Connection)}
      * for getting the SQL scripts to execute during EPSG dataset installation.
      *
      * <p>If that question has not already been asked, this method asks to the user
if (s)he accepts
      * EPSG terms of use. If (s)he refuses, an {@link AccessDeniedException} will be thrown.</p>
      *
-     * @param  index Index of the SQL script to read, from 0 inclusive to
-     *         <code>{@linkplain #getScriptNames()}.length</code> exclusive.
-     * @return A reader for the content of SQL script to execute.
+     * @param  authority One of the values returned by {@link #getAuthorities()}.
+     * @param  resource Index of the script to open, from 0 inclusive to
+     *         <code>{@linkplain #getResourceNames(String) getResourceNames}(authority).length</code>
exclusive.
+     * @return A reader for the installation script content.
+     * @throws IllegalArgumentException if the given {@code authority} argument is not one
of the expected values.
+     * @throws IndexOutOfBoundsException if the given {@code resource} argument is out of
bounds.
      * @throws IOException if an error occurred while creating the reader.
      */
     @Override
-    public BufferedReader getScriptContent(final int index) throws IOException {
-        return provider(true).getScriptContent(index);
-    }
-
-    /**
-     * Unsupported operation.
-     *
-     * @param name Ignored.
-     * @return {@code null}.
-     */
-    @Override
-    protected InputStream open(String name) {
-        return null;
+    public BufferedReader openScript(final String authority, final int resource) throws IOException
{
+        return provider(authority, true).openScript(authority, resource);
     }
 }

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/IdentifiedObjects.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/IdentifiedObjects.java?rev=1733108&r1=1733107&r2=1733108&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/IdentifiedObjects.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/IdentifiedObjects.java
[UTF-8] Tue Mar  1 17:06:30 2016
@@ -302,7 +302,7 @@ public final class IdentifiedObjects ext
      * @return A string representation of the first identifier or name, or {@code null} if
none.
      *
      * @see #getIdentifier(IdentifiedObject, Citation)
-     * @see #searchIdentifierCode(IdentifiedObject, boolean)
+     * @see #lookupURN(IdentifiedObject, Citation)
      */
     public static String getIdentifierOrName(final IdentifiedObject object) {
         if (object != null) {
@@ -512,6 +512,8 @@ public final class IdentifiedObjects ext
      * @return A finder to use for looking up unidentified objects.
      * @throws FactoryException if the finder can not be created.
      *
+     * @see #lookupEPSG(IdentifiedObject)
+     * @see #lookupURN(IdentifiedObject, Citation)
      * @see org.apache.sis.referencing.factory.GeodeticAuthorityFactory#newIdentifiedObjectFinder()
      * @see IdentifiedObjectFinder#find(IdentifiedObject)
      */

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/IdentifiedObjectFinder.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/IdentifiedObjectFinder.java?rev=1733108&r1=1733107&r2=1733108&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/IdentifiedObjectFinder.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/IdentifiedObjectFinder.java
[UTF-8] Tue Mar  1 17:06:30 2016
@@ -62,7 +62,7 @@ import org.apache.sis.util.Utilities;
  * @module
  *
  * @see GeodeticAuthorityFactory#newIdentifiedObjectFinder()
- * @see IdentifiedObjects#lookupIdentifier(IdentifiedObject, boolean)
+ * @see IdentifiedObjects#newFinder(String)
  */
 public class IdentifiedObjectFinder {
     /**

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/sql/EPSGFactory.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/sql/EPSGFactory.java?rev=1733108&r1=1733107&r2=1733108&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/sql/EPSGFactory.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/sql/EPSGFactory.java
[UTF-8] Tue Mar  1 17:06:30 2016
@@ -349,6 +349,8 @@ public class EPSGFactory extends Concurr
      * @param  connection Connection to the database where to create the EPSG schema.
      * @throws IOException if the SQL script can not be found or an I/O error occurred while
reading them.
      * @throws SQLException if an error occurred while writing to the database.
+     *
+     * @see InstallationScriptProvider
      */
     public synchronized void install(final Connection connection) throws IOException, SQLException
{
         ArgumentChecks.ensureNonNull("connection", connection);

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/sql/EPSGInstaller.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/sql/EPSGInstaller.java?rev=1733108&r1=1733107&r2=1733108&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/sql/EPSGInstaller.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/sql/EPSGInstaller.java
[UTF-8] Tue Mar  1 17:06:30 2016
@@ -33,12 +33,14 @@ import org.apache.sis.util.StringBuilder
 import org.apache.sis.internal.metadata.sql.ScriptRunner;
 import org.apache.sis.internal.metadata.sql.SQLUtilities;
 import org.apache.sis.internal.system.Loggers;
-import org.apache.sis.internal.util.Constants;
+import org.apache.sis.internal.util.Fallback;
 import org.apache.sis.util.CharSequences;
 import org.apache.sis.util.resources.Messages;
 import org.apache.sis.util.logging.Logging;
 import org.apache.sis.util.logging.PerformanceLevel;
-import org.apache.sis.internal.util.Fallback;
+import org.apache.sis.setup.InstallationResources;
+
+import static org.apache.sis.internal.util.Constants.EPSG;
 
 
 /**
@@ -242,17 +244,17 @@ final class EPSGInstaller extends Script
      * @throws IOException if an error occurred while reading an input.
      * @throws SQLException if an error occurred while executing a SQL statement.
      */
-    public void run(InstallationScriptProvider scriptProvider) throws SQLException, IOException
{
+    public void run(InstallationResources scriptProvider) throws SQLException, IOException
{
         long time = System.nanoTime();
-        log(Messages.getResources(null).getLogRecord(Level.INFO, Messages.Keys.CreatingSchema_2,
Constants.EPSG,
+        log(Messages.getResources(null).getLogRecord(Level.INFO, Messages.Keys.CreatingSchema_2,
EPSG,
                 SQLUtilities.getSimplifiedURL(getConnection().getMetaData())));
         if (scriptProvider == null) {
             scriptProvider = lookupProvider();
         }
-        final String[] scripts = scriptProvider.getScriptNames();
+        final String[] scripts = scriptProvider.getResourceNames(EPSG);
         int numRows = 0;
         for (int i=0; i<scripts.length; i++) {
-            try (BufferedReader in = scriptProvider.getScriptContent(i)) {
+            try (BufferedReader in = scriptProvider.openScript(EPSG, i)) {
                 numRows += run(scripts[i], in);
             }
         }
@@ -265,10 +267,10 @@ final class EPSGInstaller extends Script
     /**
      * Searches for a SQL script provider on the classpath before to fallback on the default
provider.
      */
-    private static InstallationScriptProvider lookupProvider() {
-        InstallationScriptProvider fallback = null;
-        for (final InstallationScriptProvider provider : ServiceLoader.load(InstallationScriptProvider.class))
{
-            if (Constants.EPSG.equals(provider.getAuthority())) {
+    private static InstallationResources lookupProvider() {
+        InstallationResources fallback = null;
+        for (final InstallationResources provider : ServiceLoader.load(InstallationResources.class))
{
+            if (provider.getAuthorities().contains(EPSG)) {
                 if (provider.getClass().isAnnotationPresent(Fallback.class)) {
                     return provider;
                 }
@@ -282,7 +284,7 @@ final class EPSGInstaller extends Script
      * Logs a message reporting the failure to create EPSG database.
      */
     final void logFailure(final Locale locale) {
-        String message = Messages.getResources(locale).getString(Messages.Keys.CanNotCreateSchema_1,
Constants.EPSG);
+        String message = Messages.getResources(locale).getString(Messages.Keys.CanNotCreateSchema_1,
EPSG);
         String status = status(locale);
         if (status != null) {
             message = message + ' ' + status;

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/sql/InstallationScriptProvider.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/sql/InstallationScriptProvider.java?rev=1733108&r1=1733107&r2=1733108&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/sql/InstallationScriptProvider.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/sql/InstallationScriptProvider.java
[UTF-8] Tue Mar  1 17:06:30 2016
@@ -16,7 +16,9 @@
  */
 package org.apache.sis.referencing.factory.sql;
 
+import java.util.Set;
 import java.util.Locale;
+import java.util.Collections;
 import java.sql.Connection;
 import java.io.BufferedReader;
 import java.io.LineNumberReader;
@@ -29,6 +31,7 @@ import org.apache.sis.util.resources.Err
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.setup.InstallationResources;
 import org.apache.sis.internal.system.DataDirectory;
+import org.apache.sis.internal.util.CollectionsExt;
 import org.apache.sis.internal.util.Constants;
 
 // Branch-dependent imports
@@ -38,28 +41,24 @@ import java.nio.file.Path;
 
 
 /**
- * Provides SQL scripts needed for creating a local copy of a dataset. This interface allows
Apache SIS users
+ * 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.
- * Some datasets can not included directly in Apache SIS for
- * <a href="https://issues.apache.org/jira/browse/LEGAL-183">licensing reasons</a>.
- *
- * <p>Implementations of this interface can be declared in the following file for automatic
discovery
- * by {@link EPSGFactory} (see {@link java.util.ServiceLoader} for more information):</p>
+ * Implementations of this class can be declared in the following file for automatic discovery
by {@link EPSGFactory}:
  *
  * {@preformat text
- *     META-INF/services/org.apache.sis.referencing.factory.sql.InstallationScriptProvider
+ *     META-INF/services/org.apache.sis.setup.InstallationResources
  * }
  *
  * <div class="section">How this class is used</div>
- * The first time that an {@link EPSGDataAccess} needs to be instantiated, {@link EPSGFactory}
verifies
- * if the EPSG database exists. If it does not, then:
+ * The first time that an {@link EPSGDataAccess} needs to be instantiated,
+ * {@link EPSGFactory} verifies if the EPSG database exists. If it does not, then:
  * <ol>
- *   <li>{@link EPSGFactory#install(Connection)} searches for the first {@code InstallationScriptProvider}
instance
- *       for which {@link #getAuthority()} returns {@code "EPSG"}.</li>
+ *   <li>{@link EPSGFactory#install(Connection)} searches for the first instance of
{@link InstallationResources}
+ *       (the parent of this class) for which the {@linkplain #getAuthorities() set of authorities}
contains {@code "EPSG"}.</li>
  *   <li>The {@linkplain #getLicense license} may be shown to the user if the application
allows that
  *       (for example when running as a {@linkplain org.apache.sis.console console application}).</li>
  *   <li>If the installation process is allowed to continue, it will iterate over all
readers provided by
- *       {@link #getScriptContent(int)} and execute the SQL statements (not necessarily verbatim;
+ *       {@link #openScript(String, int)} and execute the SQL statements (not necessarily
verbatim;
  *       the installation process may adapt to the target database).</li>
  * </ol>
  *
@@ -86,9 +85,14 @@ public abstract class InstallationScript
     protected static final String FINISH = "Finish";
 
     /**
+     * The authorities to be returned by {@link #getAuthorities()}.
+     */
+    private final Set<String> authorities;
+
+    /**
      * The names of the SQL scripts to read.
      */
-    private final String[] names;
+    private final String[] resources;
 
     /**
      * Creates a new provider which will read script files of the given names in that order.
@@ -108,58 +112,61 @@ public abstract class InstallationScript
      *   </tr>
      * </table>
      *
-     * @param names Names of the SQL scripts to read.
+     * @param authority The authority (typically {@code "EPSG"}), or {@code null} if not
available.
+     * @param resources Names of the SQL scripts to read.
      *
-     * @see #getScriptNames()
-     * @see #open(String)
+     * @see #getResourceNames(String)
+     * @see #openStream(String)
      */
-    protected InstallationScriptProvider(final String... names) {
-        ArgumentChecks.ensureNonNull("filenames", names);
-        this.names = names;
+    protected InstallationScriptProvider(final String authority, final String... resources)
{
+        ArgumentChecks.ensureNonNull("resources", resources);
+        authorities = CollectionsExt.singletonOrEmpty(authority);
+        this.resources = resources;
     }
 
     /**
-     * Returns the identifier of the dataset installed by the SQL scripts.
-     * The values recognized by SIS are listed below
-     * (note that this list may be expanded in future SIS versions if more authorities are
supported):
+     * Returns the identifiers of the dataset installed by the SQL scripts.
+     * The values currently recognized by SIS are:
      *
      * <ul>
-     *   <li>{@code "EPSG"}</li>
+     *   <li>{@code "EPSG"} for the EPSG geodetic dataset.</li>
      * </ul>
      *
-     * SIS will ignore any {@code InstallationScriptProvider} instance
-     * for which {@code getAuthority()} returns a value not present in the above list.
-     * It is okay for example to return {@code "unavailable"} if the provider does not
-     * have the resources or the permission for returning the installation scripts.
+     * The default implementation returns the authority given at construction time, or an
empty set
+     * if that authority was {@code null}. An empty set means that the provider does not
have all
+     * needed resources or does not have permission to distribute the installation scripts.
      *
-     * @return {@code "EPSG"} or other value.
-     */
-    public abstract String getAuthority();
+      * @return Identifiers of SQL scripts that this instance can distribute.
+    */
+    @Override
+    @SuppressWarnings("ReturnOfCollectionOrArrayField")
+    public Set<String> getAuthorities() {
+        return authorities;
+    }
 
     /**
-     * Returns the terms of use of the dataset, or {@code null} if none.
-     * The terms of use can be returned in either plain text or HTML.
-     *
-     * <p>For the EPSG dataset, this method should return the content of the
-     * <a href="http://www.epsg.org/TermsOfUse">http://www.epsg.org/TermsOfUse</a>
page.</p>
-     *
-     * @param  mimeType Either {@code "text/plain"} or {@code "text/html"}.
-     * @return The terms of use in plain text or HTML, or {@code null} if none.
-     * @throws IOException if an error occurred while reading the license file.
+     * Verifies that the given authority is one of the expected values.
      */
-    @Override
-    public abstract String getLicense(Locale locale, String mimeType) throws IOException;
+    private void verifyAuthority(final String authority) {
+        if (!authorities.contains(authority)) {
+            throw new IllegalArgumentException(Errors.format(Errors.Keys.IllegalArgumentValue_2,
"authority", authority));
+        }
+    }
 
     /**
      * 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).
      *
+     * @param  authority The value given at construction time (e.g. {@code "EPSG"}).
      * @return The names of all SQL scripts to execute.
+     * @throws IllegalArgumentException if the given {@code authority} argument is not the
expected value.
      * @throws IOException if fetching the script names required an I/O operation and that
operation failed.
      */
-    public String[] getScriptNames() throws IOException {
-        return names.clone();
+    @Override
+    public String[] getResourceNames(String authority) throws IOException {
+        verifyAuthority(authority);
+        return resources.clone();
     }
 
     /**
@@ -170,8 +177,8 @@ public abstract class InstallationScript
      * <div class="section">EPSG case</div>
      * In the EPSG dataset case, the iterator should return {@code BufferedReader} instances
for the following files
      * (replace {@code <version>} by the EPSG version number and {@code <product>}
by the target database) in same order.
-     * The first 3 files can be downloaded from <a href="http://www.epsg.org/">http://www.epsg.org/</a>.
-     * The fourth file is provided by Apache SIS.
+     * The first and last files are provided by Apache SIS.
+     * All other files can be downloaded from <a href="http://www.epsg.org/">http://www.epsg.org/</a>.
      *
      * <ol>
      *   <li>Content of {@link #PREPARE}, an optional data definition script that define
the enumerations expected by {@link EPSGDataAccess}.</li>
@@ -184,21 +191,26 @@ public abstract class InstallationScript
      * Implementors are free to return a different set of scripts with equivalent content.
      *
      * <div class="section">Default implementation</div>
-     * The default implementation invokes {@link #open(String)} – except for {@link #PREPARE}
and {@link #FINISH}
+     * The default implementation invokes {@link #openStream(String)} – except for {@link
#PREPARE} and {@link #FINISH}
      * in which case an Apache SIS build-in script is used – and wrap the result in a {@link
LineNumberReader}.
+     * The scripts are opened with ISO LATIN-1 encoding.
      *
-     * @param  index Index of the SQL script to read, from 0 inclusive to
+     * @param  authority The value given at construction time (e.g. {@code "EPSG"}).
+     * @param  resource Index of the SQL script to read, from 0 inclusive to
      *         <code>{@linkplain #getScriptNames()}.length</code> exclusive.
      * @return A reader for the content of SQL script to execute.
+     * @throws IllegalArgumentException if the given {@code authority} argument is not the
expected value.
+     * @throws IndexOutOfBoundsException if the given {@code resource} argument is out of
bounds.
      * @throws IOException if an error occurred while creating the reader.
      */
-    public BufferedReader getScriptContent(final int index) throws IOException {
-        ArgumentChecks.ensureValidIndex(names.length, index);
-        final String authority = getAuthority();
+    @Override
+    public BufferedReader openScript(final String authority, final int resource) throws IOException
{
+        verifyAuthority(authority);
+        ArgumentChecks.ensureValidIndex(resources.length, resource);
         if (!Constants.EPSG.equals(authority)) {
             throw new IllegalStateException(Errors.format(Errors.Keys.UnknownAuthority_1,
authority));
         }
-        String name = names[index];
+        String name = resources[resource];
         final Charset charset;
         final InputStream in;
         if (PREPARE.equals(name) || FINISH.equals(name)) {
@@ -206,7 +218,7 @@ public abstract class InstallationScript
             in = InstallationScriptProvider.class.getResourceAsStream(name);
             charset = StandardCharsets.UTF_8;
         } else {
-            in = open(name);
+            in = openStream(name);
             charset = StandardCharsets.ISO_8859_1;
         }
         if (in == null) {
@@ -217,7 +229,7 @@ public abstract class InstallationScript
 
     /**
      * Opens the input stream for the SQL script of the given name.
-     * This method is invoked by the default implementation of {@link #getScriptContent(int)}
+     * This method is invoked by the default implementation of {@link #openScript(String,
int)}
      * for all scripts except {@link #PREPARE} and {@link #FINISH}.
      *
      * <div class="note"><b>Example 1:</b>
@@ -226,7 +238,7 @@ public abstract class InstallationScript
      * filenames in that directory, then this method can be implemented as below:
      *
      * {@preformat java
-     *    protected InputStream open(String name) throws IOException {
+     *    protected InputStream openStream(String name) throws IOException {
      *        return Files.newInputStream(directory.resolve(name));
      *    }
      * }
@@ -237,17 +249,17 @@ public abstract class InstallationScript
      * in the same JAR files than and in the same package, then this method can be implemented
as below:
      *
      * {@preformat java
-     *    protected InputStream open(String name) {
+     *    protected InputStream openStream(String name) {
      *        return MyClass.getResourceAsStream(name);
      *    }
      * }
      * </div>
      *
-     * @param  name Name of the script file to open.
+     * @param  name Name of the script file to open. Can be {@code null} if the resource
is not found.
      * @return An input stream opened of the given script file.
      * @throws IOException if an error occurred while opening the file.
      */
-    protected abstract InputStream open(final String name) throws IOException;
+    protected abstract InputStream openStream(final String name) throws IOException;
 
 
 
@@ -276,12 +288,13 @@ public abstract class InstallationScript
         /**
          * Creates a default provider.
          */
-        public Default() {
-            super(PREPARE,
-                  "EPSG_Tables.sql",
-                  "EPSG_Data.sql",
-                  "EPSG_FKeys.sql",
-                  FINISH);
+        Default() {
+            super(Constants.EPSG,
+                    PREPARE,
+                    "EPSG_Tables.sql",
+                    "EPSG_Data.sql",
+                    "EPSG_FKeys.sql",
+                    FINISH);
 
             Path dir = DataDirectory.DATABASES.getDirectory();
             if (dir != null) {
@@ -301,8 +314,8 @@ public abstract class InstallationScript
          *         or {@code "unavailable"} otherwise.
          */
         @Override
-        public String getAuthority() {
-            return (directory != null) ? Constants.EPSG : "unavailable";
+        public Set<String> getAuthorities() {
+            return (directory != null) ? super.getAuthorities() : Collections.emptySet();
         }
 
         /**
@@ -311,7 +324,7 @@ public abstract class InstallationScript
          * @return The terms of use in plain text or HTML, or {@code null} if the license
is presumed already accepted.
          */
         @Override
-        public String getLicense(Locale locale, String mimeType) {
+        public String getLicense(String authority, Locale locale, String mimeType) {
             return null;
         }
 
@@ -323,7 +336,7 @@ public abstract class InstallationScript
          * @throws IOException if an error occurred while opening the file.
          */
         @Override
-        protected InputStream open(final String name) throws IOException {
+        protected InputStream openStream(final String name) throws IOException {
             return Files.newInputStream(directory.resolve(name));
         }
     }

Modified: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/sql/EPSGInstallerTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/sql/EPSGInstallerTest.java?rev=1733108&r1=1733107&r2=1733108&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/sql/EPSGInstallerTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/sql/EPSGInstallerTest.java
[UTF-8] Tue Mar  1 17:06:30 2016
@@ -115,7 +115,7 @@ public final strictfp class EPSGInstalle
     private static InstallationScriptProvider getScripts() {
         final InstallationScriptProvider scripts = new InstallationScriptProvider.Default();
         assumeTrue("EPSG scripts not found in Databases/ExternalSources directory.",
-                Constants.EPSG.equals(scripts.getAuthority()));
+                scripts.getAuthorities().contains(Constants.EPSG));
         return scripts;
     }
 

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/setup/InstallationResources.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/setup/InstallationResources.java?rev=1733108&r1=1733107&r2=1733108&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/setup/InstallationResources.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/setup/InstallationResources.java
[UTF-8] Tue Mar  1 17:06:30 2016
@@ -16,8 +16,10 @@
  */
 package org.apache.sis.setup;
 
+import java.util.Set;
 import java.util.Locale;
 import java.io.IOException;
+import java.io.BufferedReader;
 
 
 /**
@@ -36,6 +38,15 @@ import java.io.IOException;
  * are more restrictive than the Apache license and require that we inform the users about
those conditions.</p>
  * </div>
  *
+ * Some classes that depend on installation resources are:
+ * {@link org.apache.sis.referencing.factory.sql.EPSGFactory}.
+ * In order to allow those classes to discover which resources are available,
+ * {@code InstallationResources} implementations shall be declared in the following file:
+ *
+ * {@preformat text
+ *     META-INF/services/org.apache.sis.setup.InstallationResources
+ * }
+ *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.7
  * @version 0.7
@@ -49,13 +60,68 @@ public abstract class InstallationResour
     }
 
     /**
-     * Returns the terms of use of the resources, or {@code null} if none.
+     * Returns identifiers of the resources provided by this instance.
+     * The values recognized by SIS are listed below
+     * (note that this list may be expanded in any future SIS versions):
+     *
+     * <ul>
+     *   <li>{@code "EPSG"} for the EPSG geodetic dataset.</li>
+     * </ul>
+     *
+     * This method may return an empty set if this {@code InstallationResources} instance
did not find the
+     * resources (for example because of files not found) or does not have the permission
to distribute them.
+     *
+     * @return Identifiers of resources that this instance can distribute.
+     */
+    public abstract Set<String> getAuthorities();
+
+    /**
+     * Returns the terms of use of the resources distributed by the specified authority,
or {@code null} if none.
      * The terms of use can be returned in either plain text or HTML.
      *
-     * @param  locale The preferred locale for the terms of use.
-     * @param  mimeType Either {@code "text/plain"} or {@code "text/html"}.
+     * <div class="note"><b>Example:</b>
+     * For the {@code "EPSG"} authority, this method may return a copy of the
+     * <a href="http://www.epsg.org/TermsOfUse">http://www.epsg.org/TermsOfUse</a>
page.
+     * </div>
+     *
+     * @param  authority One of the values returned by {@link #getAuthorities()}.
+     * @param  locale    The preferred locale for the terms of use.
+     * @param  mimeType  Either {@code "text/plain"} or {@code "text/html"}.
      * @return The terms of use in plain text or HTML, or {@code null} if none.
+     * @throws IllegalArgumentException if the given {@code authority} argument is not one
of the expected values.
      * @throws IOException if an error occurred while reading the license file.
      */
-    public abstract String getLicense(Locale locale, String mimeType) throws IOException;
+    public abstract String getLicense(String authority, Locale locale, String mimeType) throws
IOException;
+
+    /**
+     * Returns the names of all resources of the specified authority that are distributed
by this instance.
+     * The resources will be used in the order they appear in the array.
+     *
+     * <div class="note"><b>Example:</b>
+     * for the {@code "EPSG"} authority, this method may return the filenames of all SQL
scripts to execute.
+     * One of the first script creates tables, followed by a script that populates tables
with data,
+     * followed by a script that creates foreigner keys.
+     * </div>
+     *
+     * @param  authority One of the values returned by {@link #getAuthorities()}.
+     * @return The names of all resources of the given authority that are distributed by
this instance.
+     * @throws IllegalArgumentException if the given {@code authority} argument is not one
of the expected values.
+     * @throws IOException if fetching the resource names required an I/O operation and that
operation failed.
+     */
+    public abstract String[] getResourceNames(String authority) throws IOException;
+
+    /**
+     * Returns a reader for the resources at the given index.
+     * The resource may be a SQL script or any other resources readable as a text.
+     * The returned {@link BufferedReader} instance shall be closed by the caller.
+     *
+     * @param  authority One of the values returned by {@link #getAuthorities()}.
+     * @param  resource Index of the script to open, from 0 inclusive to
+     *         <code>{@linkplain #getResourceNames(String) getResourceNames}(authority).length</code>
exclusive.
+     * @return A reader for the installation script content.
+     * @throws IllegalArgumentException if the given {@code authority} argument is not one
of the expected values.
+     * @throws IndexOutOfBoundsException if the given {@code resource} argument is out of
bounds.
+     * @throws IOException if an error occurred while creating the reader.
+     */
+    public abstract BufferedReader openScript(String authority, int resource) throws IOException;
 }




Mime
View raw message