sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1733470 - in /sis/branches/JDK8/core/sis-referencing/src: main/java/org/apache/sis/referencing/factory/sql/ test/java/org/apache/sis/referencing/factory/sql/
Date Thu, 03 Mar 2016 14:42:35 GMT
Author: desruisseaux
Date: Thu Mar  3 14:42:35 2016
New Revision: 1733470

URL: http://svn.apache.org/viewvc?rev=1733470&view=rev
Log:
Allow users to keep the filenames used by EPSG (e.g. "EPSG_v8_9.mdb_Data_PostgreSQL.sql" instead
of "EPSG_Data.sql").

Modified:
    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

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=1733470&r1=1733469&r2=1733470&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] Thu Mar  3 14:42:35 2016
@@ -350,8 +350,9 @@ public class EPSGFactory extends Concurr
      *     If no provider is specified, then this method will search on the classpath (with
{@link java.util.ServiceLoader})
      *     for user-provided implementations of {@code InstallationScriptProvider}.
      *     If no user-specified provider is found, then this method will search for
-     *     {@code "EPSG_Tables.sql"}, {@code "EPSG_Data.sql"} and {@code "EPSG_FKeys.sql"}
files in the
-     *     {@code $SIS_DATA/Databases/ExternalSources} directory.</li>
+     *     {@code "EPSG_*Tables.sql"}, {@code "EPSG_*Data.sql"} and {@code "EPSG_*FKeys.sql"}
files in the
+     *     {@code $SIS_DATA/Databases/ExternalSources} directory where {@code *} stands for
any characters
+     *     provided that there is no ambiguity.</li>
      * </ul>
      *
      * <p><b>Legal constraint:</b>
@@ -381,7 +382,7 @@ public class EPSGFactory extends Concurr
                         installer.prependNamespace(catalog);
                     }
                 }
-                installer.run(scriptProvider);
+                installer.run(scriptProvider, locale);
                 success = true;
             } finally {
                 if (ac) {

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=1733470&r1=1733469&r2=1733470&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] Thu Mar  3 14:42:35 2016
@@ -32,11 +32,9 @@ import java.io.BufferedReader;
 import org.apache.sis.util.StringBuilders;
 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.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.setup.InstallationResources;
 
@@ -244,12 +242,12 @@ 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(InstallationResources scriptProvider) throws SQLException, IOException
{
+    public void run(InstallationResources scriptProvider, final Locale locale) throws SQLException,
IOException {
         long time = System.nanoTime();
-        log(Messages.getResources(null).getLogRecord(Level.INFO, Messages.Keys.CreatingSchema_2,
EPSG,
-                SQLUtilities.getSimplifiedURL(getConnection().getMetaData())));
+        InstallationScriptProvider.log(Messages.getResources(locale).getLogRecord(Level.INFO,
+                Messages.Keys.CreatingSchema_2, EPSG, SQLUtilities.getSimplifiedURL(getConnection().getMetaData())));
         if (scriptProvider == null) {
-            scriptProvider = lookupProvider();
+            scriptProvider = lookupProvider(locale);
         }
         final String[] scripts = scriptProvider.getResourceNames(EPSG);
         int numRows = 0;
@@ -259,7 +257,7 @@ final class EPSGInstaller extends Script
             }
         }
         time = System.nanoTime() - time;
-        log(Messages.getResources(null).getLogRecord(
+        InstallationScriptProvider.log(Messages.getResources(locale).getLogRecord(
                 PerformanceLevel.forDuration(time, TimeUnit.NANOSECONDS),
                 Messages.Keys.InsertDuration_2, numRows, time / 1E9f));
     }
@@ -267,7 +265,7 @@ final class EPSGInstaller extends Script
     /**
      * Searches for a SQL script provider on the classpath before to fallback on the default
provider.
      */
-    private static InstallationResources lookupProvider() {
+    private static InstallationResources lookupProvider(final Locale locale) throws IOException
{
         InstallationResources fallback = null;
         for (final InstallationResources provider : ServiceLoader.load(InstallationResources.class))
{
             if (provider.getAuthorities().contains(EPSG)) {
@@ -277,7 +275,7 @@ final class EPSGInstaller extends Script
                 fallback = provider;
             }
         }
-        return (fallback != null) ? fallback : new InstallationScriptProvider.Default();
+        return (fallback != null) ? fallback : new InstallationScriptProvider.Default(locale);
     }
 
     /**
@@ -292,15 +290,6 @@ final class EPSGInstaller extends Script
         if (status != null) {
             message = message + ' ' + status;
         }
-        log(new LogRecord(Level.WARNING, message));
-    }
-
-    /**
-     * Logs the given record. This method pretend that the record has been logged by
-     * {@code EPSGFactory.install(…)} because it is the public API using this class.
-     */
-    private static void log(final LogRecord record) {
-        record.setLoggerName(Loggers.CRS_FACTORY);
-        Logging.log(EPSGFactory.class, "install", record);
+        InstallationScriptProvider.log(new LogRecord(Level.WARNING, message));
     }
 }

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=1733470&r1=1733469&r2=1733470&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] Thu Mar  3 14:42:35 2016
@@ -19,6 +19,8 @@ package org.apache.sis.referencing.facto
 import java.util.Set;
 import java.util.Locale;
 import java.util.Collections;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
 import java.sql.Connection;
 import java.io.BufferedReader;
 import java.io.LineNumberReader;
@@ -28,14 +30,17 @@ import java.io.IOException;
 import java.io.FileNotFoundException;
 import java.nio.charset.Charset;
 import org.apache.sis.util.resources.Errors;
+import org.apache.sis.util.logging.Logging;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.setup.InstallationResources;
 import org.apache.sis.internal.system.DataDirectory;
+import org.apache.sis.internal.system.Loggers;
 import org.apache.sis.internal.util.CollectionsExt;
 import org.apache.sis.internal.util.Constants;
 
 // Branch-dependent imports
 import java.nio.charset.StandardCharsets;
+import java.nio.file.DirectoryStream;
 import java.nio.file.Files;
 import java.nio.file.Path;
 
@@ -261,17 +266,27 @@ public abstract class InstallationScript
      */
     protected abstract InputStream openStream(final String name) throws IOException;
 
+    /**
+     * Logs the given record. This method pretend that the record has been logged by
+     * {@code EPSGFactory.install(…)} because it is the public API using this class.
+     */
+    static void log(final LogRecord record) {
+        record.setLoggerName(Loggers.CRS_FACTORY);
+        Logging.log(EPSGFactory.class, "install", record);
+    }
+
 
 
 
     /**
      * The default implementation which use the scripts in the {@code $SIS_DATA/Databases/ExternalSources}
-     * directory, if present. This class expects the files to have those exact names:
+     * directory, if present. This class expects the files to have those exact names where
{@code *} stands
+     * for any characters provided that there is no ambiguity:
      *
      * <ul>
-     *   <li>{@code EPSG_Tables.sql}</li>
-     *   <li>{@code EPSG_Data.sql}</li>
-     *   <li>{@code EPSG_FKeys.sql}</li>
+     *   <li>{@code EPSG_*Tables.sql}</li>
+     *   <li>{@code EPSG_*Data.sql}</li>
+     *   <li>{@code EPSG_*FKeys.sql}</li>
      * </ul>
      *
      * @author  Martin Desruisseaux (Geomatys)
@@ -283,27 +298,58 @@ public abstract class InstallationScript
         /**
          * The directory containing the scripts, or {@code null} if it does not exist.
          */
-        private final Path directory;
+        private Path directory;
+
+        /**
+         * Index of the first real file in the array given to the constructor.
+         * We set the value to 1 for skipping the {@code PREPARE} pseudo-file.
+         */
+        private static final int FIRST_FILE = 1;
 
         /**
          * Creates a default provider.
          */
-        Default() {
+        Default(final Locale locale) throws IOException {
             super(Constants.EPSG,
                     PREPARE,
-                    "EPSG_Tables.sql",
-                    "EPSG_Data.sql",
-                    "EPSG_FKeys.sql",
+                    "Tables",
+                    "Data",
+                    "FKeys",
                     FINISH);
 
             Path dir = DataDirectory.DATABASES.getDirectory();
             if (dir != null) {
                 dir = dir.resolve("ExternalSources");
-                if (!Files.isRegularFile(dir.resolve("EPSG_Tables.sql"))) {
-                    dir = null;
+                if (Files.isDirectory(dir)) {
+                    final String[] resources = super.resources;
+                    final String[] found = new String[resources.length - FIRST_FILE - 1];
+                    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir,
"EPSG_*.sql")) {
+                        for (final Path path : stream) {
+                            final String name = path.getFileName().toString();
+                            for (int i=0; i<found.length; i++) {
+                                final String part = resources[FIRST_FILE + i];
+                                if (name.contains(part)) {
+                                    if (found[i] != null) {
+                                        log(Errors.getResources(locale)
+                                                  .getLogRecord(Level.WARNING, Errors.Keys.DuplicatedElement_1,
part));
+                                        return;                         // Stop the search
because of duplicated file.
+                                    }
+                                    found[i] = name;
+                                }
+                            }
+                        }
+                    }
+                    for (int i=0; i<found.length; i++) {
+                        final String file = found[i];
+                        if (file != null) {
+                            resources[FIRST_FILE + i] = file;
+                        } else {
+                            dir = null;
+                        }
+                    }
+                    directory = dir;
                 }
             }
-            directory = dir;
         }
 
         /**

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=1733470&r1=1733469&r2=1733470&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] Thu Mar  3 14:42:35 2016
@@ -21,6 +21,7 @@ import java.util.Set;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.regex.Pattern;
+import java.io.IOException;
 import javax.sql.DataSource;
 import java.sql.Connection;
 import java.sql.Statement;
@@ -112,8 +113,8 @@ public final strictfp class EPSGInstalle
      * Returns the SQL scripts needed for testing the database creation,
      * or skip the JUnit test if those scripts are not found.
      */
-    private static InstallationScriptProvider getScripts() {
-        final InstallationScriptProvider scripts = new InstallationScriptProvider.Default();
+    private static InstallationScriptProvider getScripts() throws IOException {
+        final InstallationScriptProvider scripts = new InstallationScriptProvider.Default(null);
         assumeTrue("EPSG scripts not found in Databases/ExternalSources directory.",
                 scripts.getAuthorities().contains(Constants.EPSG));
         return scripts;



Mime
View raw message