sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1814305 - in /sis/branches/JDK8/core: sis-referencing/src/main/java/org/apache/sis/referencing/ sis-referencing/src/main/java/org/apache/sis/referencing/factory/ sis-referencing/src/main/java/org/apache/sis/referencing/factory/sql/ sis-uti...
Date Sat, 04 Nov 2017 16:36:20 GMT
Author: desruisseaux
Date: Sat Nov  4 16:36:20 2017
New Revision: 1814305

URL: http://svn.apache.org/viewvc?rev=1814305&view=rev
Log:
Do not log a scary stack trace when the SIS_DATA environment variable is set but the EPSG
SQL script are not available.
Instead, log a more consolidated message (less repetition).

Modified:
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/AuthorityFactories.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/ConcurrentAuthorityFactory.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-utility/src/main/java/org/apache/sis/util/logging/Logging.java

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/AuthorityFactories.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/AuthorityFactories.java?rev=1814305&r1=1814304&r2=1814305&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/AuthorityFactories.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/AuthorityFactories.java
[UTF-8] Sat Nov  4 16:36:20 2017
@@ -194,8 +194,10 @@ final class AuthorityFactories<T extends
             message = e.toString();
         }
         final LogRecord record = new LogRecord(isWarning ? Level.WARNING : Level.CONFIG,
message);
+        if (isWarning && !(e instanceof UnavailableFactoryException)) {
+            record.setThrown(e);
+        }
         record.setLoggerName(Loggers.CRS_FACTORY);
-        if (isWarning) record.setThrown(e);
         Logging.log(CRS.class, "getAuthorityFactory", record);
     }
 

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/ConcurrentAuthorityFactory.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/ConcurrentAuthorityFactory.java?rev=1814305&r1=1814304&r2=1814305&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/ConcurrentAuthorityFactory.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/ConcurrentAuthorityFactory.java
[UTF-8] Sat Nov  4 16:36:20 2017
@@ -28,6 +28,7 @@ import java.util.IdentityHashMap;
 import java.util.concurrent.Callable;
 import java.util.concurrent.TimeUnit;
 import java.util.logging.LogRecord;
+import java.util.logging.Level;
 import java.lang.ref.WeakReference;
 import java.lang.ref.PhantomReference;
 import java.io.PrintWriter;
@@ -50,6 +51,7 @@ import org.apache.sis.util.Disposable;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.logging.Logging;
 import org.apache.sis.util.collection.Cache;
+import org.apache.sis.internal.simple.SimpleCitation;
 import org.apache.sis.internal.system.ReferenceQueueConsumer;
 import org.apache.sis.internal.system.DelayedExecutor;
 import org.apache.sis.internal.system.DelayedRunnable;
@@ -111,6 +113,12 @@ public abstract class ConcurrentAuthorit
     private static final long DURATION_FOR_LOGGING = 10_000_000L;       // 10 milliseconds.
 
     /**
+     * Sentinel value when {@link #authority} can not be determined because the data access
object
+     * can not be constructed.
+     */
+    private static final Citation UNAVAILABLE = new SimpleCitation("unavailable");
+
+    /**
      * The authority, cached after first requested.
      */
     private transient volatile Citation authority;
@@ -700,7 +708,7 @@ public abstract class ConcurrentAuthorit
     @Override
     public Citation getAuthority() {
         Citation c = authority;
-        if (c == null) try {
+        if (c == null || c == UNAVAILABLE) try {
             final DAO factory = getDataAccess();
             try {
                 /*
@@ -712,8 +720,19 @@ public abstract class ConcurrentAuthorit
                 release("getAuthority", Citation.class, null);
             }
         } catch (FactoryException e) {
-            Logging.unexpectedException(Logging.getLogger(Loggers.CRS_FACTORY),
-                    ConcurrentAuthorityFactory.class, "getAuthority", e);
+            authority = UNAVAILABLE;
+            /*
+             * Use the warning level only on the first failure, then the fine level on all
subsequent failures.
+             * Do not log the stack trace if we failed because of UnavailableFactoryException
since it may be
+             * normal (the EPSG geodetic dataset is optional, even if strongly recommended).
+             */
+            final LogRecord record = new LogRecord(c == null ? Level.WARNING : Level.FINE,
e.getLocalizedMessage());
+            if (!(e instanceof UnavailableFactoryException)) {
+                record.setThrown(e);
+            }
+            record.setLoggerName(Loggers.CRS_FACTORY);
+            Logging.log(ConcurrentAuthorityFactory.class, "getAuthority", record);
+            c = null;
         }
         return c;
     }

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=1814305&r1=1814304&r2=1814305&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] Sat Nov  4 16:36:20 2017
@@ -45,6 +45,7 @@ import org.apache.sis.internal.system.De
 import org.apache.sis.internal.util.Constants;
 import org.apache.sis.referencing.factory.ConcurrentAuthorityFactory;
 import org.apache.sis.referencing.factory.UnavailableFactoryException;
+import org.apache.sis.util.resources.Messages;
 import org.apache.sis.util.logging.Logging;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.Classes;
@@ -270,7 +271,7 @@ public class EPSGFactory extends Concurr
                 throw new UnavailableFactoryException(Initializer.unspecified(locale));
             }
         } catch (Exception e) {
-            throw new UnavailableFactoryException(message(e), e);
+            throw new UnavailableFactoryException(canNotUse(e), e);
         }
         dataSource   = ds;
         nameFactory  = factory(NameFactory.class,                "nameFactory",  properties);
@@ -293,12 +294,19 @@ public class EPSGFactory extends Concurr
     /**
      * Returns the message to put in an {@link UnavailableFactoryException} having the given
exception as its cause.
      */
-    private String message(final Exception e) {
+    private String canNotUse(final Exception e) {
         String message = Exceptions.getLocalizedMessage(e, locale);
         if (message == null) {
             message = Classes.getShortClassName(e);
         }
-        return Resources.forLocale(locale).getString(Resources.Keys.CanNotUseGeodeticParameters_2,
Constants.EPSG, message);
+        return canNotUse(message);
+    }
+
+    /**
+     * Returns the message to put in an {@link UnavailableFactoryException} having the given
cause.
+     */
+    private String canNotUse(final String cause) {
+        return Resources.forLocale(locale).getString(Resources.Keys.CanNotUseGeodeticParameters_2,
Constants.EPSG, cause);
     }
 
     /**
@@ -367,15 +375,18 @@ public class EPSGFactory extends Concurr
      * See <a href="https://issues.apache.org/jira/browse/LEGAL-183">LEGAL-183</a>
for more information.</p>
      *
      * @param  connection  connection to the database where to create the EPSG schema.
-     * @throws FileNotFoundException if a SQL script has not been found,
-     *         typically because a required resource is not on the classpath.
-     * @throws IOException  if an I/O error occurred while reading a SQL script.
-     * @throws SQLException if an error occurred while writing to the database.
+     * @throws UnavailableFactoryException if installation failed. The exception will have
a
+     *         {@link FileNotFoundException} cause if a SQL script has not been found
+     *         (typically because a required resource is not on the classpath), an
+     *         {@link IOException} if an I/O error occurred while reading a SQL script, or
a
+     *         {@link SQLException} if an error occurred while writing to the database.
      *
      * @see InstallationScriptProvider
      */
-    public synchronized void install(final Connection connection) throws IOException, SQLException
{
+    public synchronized void install(final Connection connection) throws UnavailableFactoryException
{
         ArgumentChecks.ensureNonNull("connection", connection);
+        String    message = null;
+        Exception failure = null;
         try (EPSGInstaller installer = new EPSGInstaller(connection)) {
             final boolean ac = connection.getAutoCommit();
             if (ac) {
@@ -403,9 +414,21 @@ public class EPSGFactory extends Concurr
                     }
                 }
             } catch (IOException | SQLException e) {
-                installer.logFailure(locale, e);
-                throw e;
+                message = installer.failure(locale, e);
+                failure = e;
             }
+        } catch (SQLException e) {
+            message = Messages.getResources(locale).getString(Messages.Keys.CanNotCreateSchema_1,
Constants.EPSG);
+            failure = e;
+        }
+        if (failure != null) {
+            /*
+             * Derby sometime wraps SQLException into another SQLException.  For making the
stack strace a
+             * little bit simpler, keep only the root cause provided that the exception type
is compatible.
+             */
+            UnavailableFactoryException exception = new UnavailableFactoryException(message,
Exceptions.unwrap(failure));
+            exception.setUnavailableFactory(this);
+            throw exception;
         }
     }
 
@@ -455,7 +478,7 @@ public class EPSGFactory extends Concurr
                 return newDataAccess(connection, tr);
             } else {
                 connection.close();
-                exception = new UnavailableFactoryException(SQLTranslator.tableNotFound(locale));
+                exception = new UnavailableFactoryException(canNotUse(SQLTranslator.tableNotFound(locale)));
             }
         } catch (Exception e) {                     // Really want to catch all exceptions
here.
             if (connection != null) try {
@@ -463,11 +486,14 @@ public class EPSGFactory extends Concurr
             } catch (SQLException e2) {
                 e.addSuppressed(e2);
             }
+            if (e instanceof FactoryException) {
+                throw (FactoryException) e;
+            }
             /*
              * Derby sometime wraps SQLException into another SQLException.  For making the
stack strace a
              * little bit simpler, keep only the root cause provided that the exception type
is compatible.
              */
-            exception = new UnavailableFactoryException(message(e), Exceptions.unwrap(e));
+            exception = new UnavailableFactoryException(canNotUse(e), Exceptions.unwrap(e));
         }
         exception.setUnavailableFactory(this);
         throw exception;

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=1814305&r1=1814304&r2=1814305&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] Sat Nov  4 16:36:20 2017
@@ -25,7 +25,6 @@ import java.sql.SQLException;
 import java.util.StringTokenizer;
 import java.util.concurrent.TimeUnit;
 import java.util.logging.Level;
-import java.util.logging.LogRecord;
 import java.io.BufferedReader;
 import org.apache.sis.util.StringBuilders;
 import org.apache.sis.internal.metadata.sql.ScriptRunner;
@@ -275,18 +274,16 @@ final class EPSGInstaller extends Script
     }
 
     /**
-     * Logs a message reporting the failure to create EPSG database. This method is invoked
when {@link EPSGFactory}
-     * caught an exception. This log completes rather than replaces the exception message
since {@code EPSGFactory}
-     * lets the exception propagate. Another code (for example {@link org.apache.sis.referencing.CRS#forCode(String)})
-     * may catch that exception and log another record with the exception message.
+     * Creates a message reporting the failure to create EPSG database. This method is invoked
when {@link EPSGFactory}
+     * caught an exception. This method completes the exception message with the file name
and line number where the
+     * error occurred, if such information is available.
      */
-    final void logFailure(final Locale locale, final Exception cause) {
+    final String failure(final Locale locale, final Exception cause) {
         String message = Messages.getResources(locale).getString(Messages.Keys.CanNotCreateSchema_1,
EPSG);
         String status = status(locale);
         if (status != null) {
             message = message + ' ' + status;
         }
-        message = Exceptions.formatChainedMessages(locale, message, cause);
-        InstallationScriptProvider.log(new LogRecord(Level.WARNING, message));
+        return Exceptions.formatChainedMessages(locale, message, cause);
     }
 }

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/logging/Logging.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/logging/Logging.java?rev=1814305&r1=1814304&r2=1814305&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/logging/Logging.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/logging/Logging.java
[UTF-8] Sat Nov  4 16:36:20 2017
@@ -241,13 +241,16 @@ public final class Logging extends Stati
      * @param  record  the record where to set the class and method names.
      * @return the record to use for logging the record.
      */
-    static Logger inferCaller(Logger logger, String classe, String method,
+    private static Logger inferCaller(Logger logger, String classe, String method,
             final StackTraceElement[] trace, final LogRecord record)
     {
         for (final StackTraceElement element : trace) {
             /*
              * Search for the first stack trace element with a classname matching the expected
one.
              * We compare against the name of the class given in argument if it was non-null.
+             *
+             * Note: a previous version also compared logger name with package name.
+             * This has been removed because those names are only loosely related.
              */
             final String classname = element.getClassName();
             if (classe != null) {



Mime
View raw message