sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1726714 [1/3] - in /sis/branches/JDK8: core/sis-referencing/src/main/java/org/apache/sis/referencing/ core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/ core/sis-referencing/src/main/java/org/apache/sis/referencing/oper...
Date Tue, 26 Jan 2016 00:02:36 GMT
Author: desruisseaux
Date: Tue Jan 26 00:02:35 2016
New Revision: 1726714

URL: http://svn.apache.org/viewvc?rev=1726714&view=rev
Log:
CRS.forCode(String) now delegate to MultiAuthoritiesFactory.

Added:
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/AuthorityFactories.java
  (with props)
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/EPSGFactoryFallback.java
      - copied, changed from r1725652, sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/CommonCRS.java
    sis/branches/JDK8/core/sis-referencing/src/main/resources/META-INF/services/org.opengis.referencing.crs.CRSAuthorityFactory
  (with props)
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/EPSGFactoryFallbackTest.java
      - copied, changed from r1725652, sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/CommonCRSTest.java
Modified:
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/CRS.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/CommonCRS.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/MultiAuthoritiesFactory.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/DefaultMathTransformFactory.java
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/CRSTest.java
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/CommonCRSTest.java
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/LazySet.java
    sis/branches/JDK8/ide-project/NetBeans/build.xml

Added: 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=1726714&view=auto
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/AuthorityFactories.java
(added)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/AuthorityFactories.java
[UTF-8] Tue Jan 26 00:02:35 2016
@@ -0,0 +1,120 @@
+/*
+ * 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.referencing;
+
+import java.util.ServiceLoader;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+import org.opengis.util.FactoryException;
+import org.opengis.referencing.AuthorityFactory;
+import org.opengis.referencing.cs.CSAuthorityFactory;
+import org.opengis.referencing.crs.CRSAuthorityFactory;
+import org.opengis.referencing.datum.DatumAuthorityFactory;
+import org.opengis.referencing.operation.CoordinateOperationAuthorityFactory;
+import org.apache.sis.internal.util.LazySet;
+import org.apache.sis.internal.system.Loggers;
+import org.apache.sis.internal.system.Modules;
+import org.apache.sis.internal.system.SystemListener;
+import org.apache.sis.referencing.factory.MultiAuthoritiesFactory;
+import org.apache.sis.referencing.factory.sql.EPSGFactory;
+import org.apache.sis.util.logging.Logging;
+
+
+/**
+ * Provides the CRS, CS, datum and coordinate operation authority factories.
+ * Provides also the system-wide {@link MultiAuthoritiesFactory} instance used by {@link
CRS#forCode(String)}.
+ * Current version handles the EPSG factory in a special way, but we may try to avoid doing
special cases in a
+ * future SIS version (this may require more help from {@link ServiceLoader}).
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.7
+ * @version 0.7
+ * @module
+ */
+final class AuthorityFactories<T extends AuthorityFactory> extends LazySet<T>
{
+    /**
+     * An array containing only the EPSG factory. Content of this array is initially null.
+     * The EPSG factory will be created when first needed by {@link #initialValues()}.
+     */
+    private static final AuthorityFactory[] EPSG = new AuthorityFactory[1];
+
+    /**
+     * The unique system-wide authority factory instance that contains all factories found
on the classpath,
+     * plus the EPSG factory.
+     */
+    static final MultiAuthoritiesFactory ALL = new MultiAuthoritiesFactory(
+            new AuthorityFactories<>(CRSAuthorityFactory.class),
+            new AuthorityFactories<>(CSAuthorityFactory.class),
+            new AuthorityFactories<>(DatumAuthorityFactory.class),
+            new AuthorityFactories<>(CoordinateOperationAuthorityFactory.class))
+    {
+        @Override
+        public void reload() {
+            synchronized (EPSG) {
+                EPSG[0] = null;
+            }
+            super.reload();
+        }
+    };
+
+    /**
+     * Registers a hook for forcing {@code ALL} to reload all CRS, CS, datum and coordinate
operation factories
+     * when the classpath changed.
+     */
+    static {
+        SystemListener.add(new SystemListener(Modules.REFERENCING) {
+            @Override protected void classpathChanged() {ALL.reload();}
+        });
+    }
+
+    /**
+     * Creates a new provider for factories of the given type.
+     */
+    private AuthorityFactories(final Class<T> type) {
+        super(ServiceLoader.load(type));
+    }
+
+    /**
+     * Returns the EPSG factory.
+     */
+    static AuthorityFactory EPSG() {
+        synchronized (EPSG) {
+            AuthorityFactory factory = EPSG[0];
+            if (factory == null) try {
+                factory = new EPSGFactory();
+            } catch (FactoryException e) {
+                final LogRecord record = new LogRecord(Level.CONFIG, e.getLocalizedMessage());
+                record.setLoggerName(Loggers.CRS_FACTORY);
+                Logging.log(CRS.class, "getAuthorityFactory", record);
+                factory = EPSGFactoryFallback.INSTANCE;
+            }
+            EPSG[0] = factory;
+            return factory;
+        }
+    }
+
+    /**
+     * Returns the EPSG factory. This method tries to instantiate an {@link EPSGFactory}
if possible,
+     * or an {@link EPSGFactoryFallback} otherwise.
+     */
+    @Override
+    @SuppressWarnings("unchecked")
+    protected T[] initialValues() {
+        EPSG();                         // Force creation of EPSG factory if not already
done.
+        return (T[]) EPSG;
+    }
+}

Propchange: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/AuthorityFactories.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/AuthorityFactories.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/CRS.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/CRS.java?rev=1726714&r1=1726713&r2=1726714&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/CRS.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/CRS.java
[UTF-8] Tue Jan 26 00:02:35 2016
@@ -22,7 +22,6 @@ import java.util.ArrayList;
 import java.util.Collections;
 import javax.measure.unit.NonSI;
 import org.opengis.util.FactoryException;
-import org.opengis.util.NoSuchIdentifierException;
 import org.opengis.referencing.NoSuchAuthorityCodeException;
 import org.opengis.referencing.cs.EllipsoidalCS;
 import org.opengis.referencing.cs.AxisDirection;
@@ -41,11 +40,9 @@ import org.opengis.referencing.crs.Verti
 import org.opengis.referencing.crs.EngineeringCRS;
 import org.opengis.metadata.extent.Extent;
 import org.opengis.metadata.extent.GeographicBoundingBox;
-import org.apache.sis.internal.util.DefinitionURI;
 import org.apache.sis.internal.metadata.AxisDirections;
 import org.apache.sis.internal.referencing.ReferencingUtilities;
 import org.apache.sis.internal.system.DefaultFactories;
-import org.apache.sis.internal.system.Loggers;
 import org.apache.sis.referencing.cs.DefaultVerticalCS;
 import org.apache.sis.referencing.cs.DefaultEllipsoidalCS;
 import org.apache.sis.referencing.crs.DefaultGeographicCRS;
@@ -53,9 +50,6 @@ import org.apache.sis.referencing.crs.De
 import org.apache.sis.referencing.crs.DefaultCompoundCRS;
 import org.apache.sis.metadata.iso.extent.Extents;
 import org.apache.sis.util.ArgumentChecks;
-import org.apache.sis.util.resources.Errors;
-import org.apache.sis.util.logging.Logging;
-import org.apache.sis.util.CharSequences;
 import org.apache.sis.util.Utilities;
 import org.apache.sis.util.Static;
 
@@ -146,40 +140,7 @@ public final class CRS extends Static {
             throws NoSuchAuthorityCodeException, FactoryException
     {
         ArgumentChecks.ensureNonNull("code", code);
-        final String authority;
-        final String value;
-        final DefinitionURI uri = DefinitionURI.parse(code);
-        if (uri != null && uri.code != null) {
-            final String type = uri.type;
-            if (type != null && !type.equalsIgnoreCase("crs")) {
-                throw new NoSuchIdentifierException(Errors.format(Errors.Keys.UnknownType_1,
type), type);
-            }
-            authority = uri.authority;
-            value = uri.code;
-        } else {
-            final int s = code.indexOf(DefinitionURI.SEPARATOR);
-            authority = CharSequences.trimWhitespaces(code.substring(0, Math.max(0, s)));
-            value = CharSequences.trimWhitespaces(code.substring(s + 1));
-        }
-        if (authority == null || authority.isEmpty()) {
-            throw new NoSuchIdentifierException(Errors.format(Errors.Keys.MissingAuthority_1,
code), code);
-        }
-        /*
-         * Delegate to the factory for the code space of the given code. If no authority
factory
-         * is available, or if the factory failed to create the CRS, delegate to CommonCRS.
Note
-         * that CommonCRS is not expected to succeed if the real EPSG factory threw an exception,
-         * so we will log a message at the warning level in such case.
-         */
-        final CRSAuthorityFactory factory = DefaultFactories.forClass(CRSAuthorityFactory.class);
-        if (factory != null) try {
-            return factory.createCoordinateReferenceSystem(value);
-        } catch (FactoryException failure) {
-            final CoordinateReferenceSystem crs = CommonCRS.forCode(authority, value, failure);
-            Logging.unexpectedException(Logging.getLogger(Loggers.CRS_FACTORY), CRS.class,
"forCode", failure); // See above comment.
-            return crs;
-        } else {
-            return CommonCRS.forCode(authority, value, null);
-        }
+        return AuthorityFactories.ALL.createCoordinateReferenceSystem(code);
     }
 
     /**
@@ -291,6 +252,7 @@ public final class CRS extends Static {
          */
         final boolean isGeodetic = (crs instanceof GeodeticCRS);
         if (isGeodetic || crs instanceof ProjectedCRS || crs instanceof EngineeringCRS) {
+            @SuppressWarnings("null")
             final CoordinateSystem cs = crs.getCoordinateSystem();
             if (cs.getDimension() == 2) {
                 return !isGeodetic || (cs instanceof EllipsoidalCS);

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/CommonCRS.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/CommonCRS.java?rev=1726714&r1=1726713&r2=1726714&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/CommonCRS.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/CommonCRS.java
[UTF-8] Tue Jan 26 00:02:35 2016
@@ -26,16 +26,14 @@ import javax.measure.unit.Unit;
 import javax.measure.quantity.Duration;
 import org.opengis.util.FactoryException;
 import org.opengis.util.InternationalString;
-import org.opengis.util.NoSuchIdentifierException;
-import org.opengis.referencing.NoSuchAuthorityCodeException;
 import org.opengis.referencing.IdentifiedObject;
+import org.opengis.referencing.AuthorityFactory;
 import org.opengis.referencing.crs.GeodeticCRS;
 import org.opengis.referencing.crs.VerticalCRS;
 import org.opengis.referencing.crs.TemporalCRS;
 import org.opengis.referencing.crs.GeographicCRS;
 import org.opengis.referencing.crs.GeocentricCRS;
 import org.opengis.referencing.crs.ProjectedCRS;
-import org.opengis.referencing.crs.CoordinateReferenceSystem;
 import org.opengis.referencing.crs.CRSAuthorityFactory;
 import org.opengis.referencing.cs.TimeCS;
 import org.opengis.referencing.cs.VerticalCS;
@@ -66,7 +64,6 @@ import org.apache.sis.internal.system.Sy
 import org.apache.sis.internal.system.Modules;
 import org.apache.sis.internal.system.Loggers;
 import org.apache.sis.util.resources.Vocabulary;
-import org.apache.sis.util.resources.Errors;
 import org.apache.sis.util.logging.Logging;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.math.MathFunctions;
@@ -75,11 +72,6 @@ import org.apache.sis.measure.Units;
 
 import static java.util.Collections.singletonMap;
 import static org.opengis.referencing.IdentifiedObject.NAME_KEY;
-import static org.apache.sis.internal.util.Constants.CRS;
-import static org.apache.sis.internal.util.Constants.EPSG;
-import static org.apache.sis.internal.util.Constants.CRS27;
-import static org.apache.sis.internal.util.Constants.CRS83;
-import static org.apache.sis.internal.util.Constants.CRS84;
 
 
 /**
@@ -920,26 +912,6 @@ public enum CommonCRS {
         return crs;
     }
 
-    /**
-     * Returns the CRS for the given EPSG code, or {@code null} if none.
-     * This is a helper method for {@link #forCode(String, String, FactoryException)} only.
-     */
-    private CoordinateReferenceSystem forCode(final int code) {
-        if (code == geographic) return geographic();
-        if (code == geocentric) return geocentric();
-        if (code == geo3D)      return geographic3D();
-        final double latitude;
-        int zone;
-        if (northUTM != 0 && (zone = code - northUTM) >= firstZone &&
zone <= lastZone) {
-            latitude = +1;
-        } else if (southUTM != 0 && (zone = code - southUTM) >= firstZone &&
zone <= lastZone) {
-            latitude = -1;
-        } else {
-            return null;
-        }
-        return UTM(latitude, TransverseMercator.centralMeridian(zone));
-    }
-
 
 
 
@@ -1537,7 +1509,13 @@ public enum CommonCRS {
      * If this method returns {@code null}, then the caller will silently fallback on hard-coded
values.
      */
     static CRSAuthorityFactory crsFactory() {
-        return null; // TODO
+        if (!EPSGFactoryFallback.FORCE_HARDCODED) {
+            final AuthorityFactory factory = AuthorityFactories.EPSG();
+            if (!(factory instanceof EPSGFactoryFallback)) {
+                return (CRSAuthorityFactory) factory;
+            }
+        }
+        return null;
     }
 
     /**
@@ -1545,7 +1523,13 @@ public enum CommonCRS {
      * If this method returns {@code null}, then the caller will silently fallback on hard-coded
values.
      */
     static DatumAuthorityFactory datumFactory() {
-        return null; // TODO
+        if (!EPSGFactoryFallback.FORCE_HARDCODED) {
+            final AuthorityFactory factory = AuthorityFactories.EPSG();
+            if (!(factory instanceof EPSGFactoryFallback)) {
+                return (DatumAuthorityFactory) factory;
+            }
+        }
+        return null;
     }
 
     /**
@@ -1555,61 +1539,4 @@ public enum CommonCRS {
     static void failure(final Object caller, final String method, final FactoryException
e) {
         Logging.unexpectedException(Logging.getLogger(Loggers.CRS_FACTORY), caller.getClass(),
method, e);
     }
-
-    /**
-     * Returns a coordinate reference system for the given authority code.
-     * This method is invoked as a fallback when {@link CRS#forCode(String)}
-     * can not create a CRS for a given code.
-     *
-     * @param authority The authority, either {@code "CRS"} or {@code "EPSG"} (case-insensitive).
-     * @param code      The code, to be parsed as an integer.
-     * @param failure   The exception to throw in case of failure, or {@code null} for creating
our own.
-     *                  A non-null value is provided when a real EPSG factory exists but
failed to create the CRS.
-     *                  In such case, we want to report the error from the real factory instead
than from this fallback.
-     *
-     * @since 0.5
-     */
-    static CoordinateReferenceSystem forCode(final String authority, final String code, final
FactoryException failure)
-            throws FactoryException
-    {
-        NumberFormatException cause = null;
-        try {
-            if (authority.equalsIgnoreCase(CRS)) {
-                switch (Integer.parseInt(code)) {
-                    case CRS27: return CommonCRS.NAD27.normalizedGeographic();
-                    case CRS83: return CommonCRS.NAD83.normalizedGeographic();
-                    case CRS84: return CommonCRS.WGS84.normalizedGeographic();
-                }
-            } else if (authority.equalsIgnoreCase(EPSG)) {
-                final int n = Integer.parseInt(code);
-                if (n != 0) { // CommonCRS uses 0 as a sentinel value for "no EPSG code".
-                    for (final CommonCRS candidate : CommonCRS.values()) {
-                        final CoordinateReferenceSystem crs = candidate.forCode(n);
-                        if (crs != null) {
-                            return crs;
-                        }
-                    }
-                    for (final CommonCRS.Vertical candidate : CommonCRS.Vertical.values())
{
-                        if (candidate.isEPSG && candidate.crs == n) {
-                            return candidate.crs();
-                        }
-                    }
-                }
-            } else if (failure != null) {
-                throw failure;
-            } else {
-                throw new NoSuchIdentifierException(Errors.format(Errors.Keys.UnknownAuthority_1,
authority), authority);
-            }
-        } catch (NumberFormatException e) {
-            cause = e;
-        }
-        if (failure != null) {
-            throw failure;
-        }
-        final NoSuchAuthorityCodeException e = new NoSuchAuthorityCodeException(
-                Errors.format(Errors.Keys.NoSuchAuthorityCode_3, authority, CoordinateReferenceSystem.class,
code),
-                authority, code, code);
-        e.initCause(cause);
-        throw e;
-    }
 }



Mime
View raw message