This is an automated email from the ASF dual-hosted git repository.

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

commit 61e956a326aa868f60397501f49af71d578a599a
Author: Martin Desruisseaux <martin.desruisse...@geomatys.com>
AuthorDate: Sat May 13 04:00:42 2023 +0200

    Delegate the loading of GeoAPI resources to GeoAPI itself.
    This is necessary in the context of named Jigsaw modules.
---
 .../apache/sis/internal/jaxb/cat/CodeListUID.java  |  5 +-
 .../apache/sis/internal/jaxb/cat/package-info.java |  2 +-
 .../main/java/org/apache/sis/util/iso/Types.java   | 54 +++++++++++-----------
 .../main/java/org/apache/sis/xml/LegacyCodes.java  |  2 +-
 .../java/org/apache/sis/util/iso/TypesTest.java    |  8 ++--
 .../org/apache/sis/internal/util/CodeLists.java    |  9 +---
 .../apache/sis/internal/util/MetadataServices.java |  7 ++-
 7 files changed, 40 insertions(+), 47 deletions(-)

diff --git 
a/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/cat/CodeListUID.java
 
b/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/cat/CodeListUID.java
index 9dd9b769a6..01ba3becd3 100644
--- 
a/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/cat/CodeListUID.java
+++ 
b/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/cat/CodeListUID.java
@@ -17,13 +17,13 @@
 package org.apache.sis.internal.jaxb.cat;
 
 import java.util.Locale;
-import java.util.ResourceBundle;
 import java.util.MissingResourceException;
 import jakarta.xml.bind.annotation.XmlAttribute;
 import jakarta.xml.bind.annotation.XmlType;
 import jakarta.xml.bind.annotation.XmlValue;
 import org.opengis.util.CodeList;
 import org.opengis.util.ControlledVocabulary;
+import org.opengis.annotation.ResourceBundles;
 import org.apache.sis.util.iso.Types;
 import org.apache.sis.internal.jaxb.Context;
 
@@ -243,8 +243,7 @@ public final class CodeListUID {
         if (locale != null) {
             final String key = classID + '.' + fieldID;
             try {
-                value = 
ResourceBundle.getBundle("org.opengis.metadata.CodeLists",
-                        locale, 
CodeList.class.getClassLoader()).getString(key);
+                value = ResourceBundles.codeLists(locale).getString(key);
             } catch (MissingResourceException e) {
                 Context.warningOccured(context, CodeListAdapter.class, 
"marshal", e, false);
             }
diff --git 
a/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/cat/package-info.java
 
b/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/cat/package-info.java
index 322e6d979b..f6859f0df9 100644
--- 
a/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/cat/package-info.java
+++ 
b/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/cat/package-info.java
@@ -20,7 +20,7 @@
  * ISO 19115-3:2016 defines them in the {@value 
org.apache.sis.xml.Namespaces#CAT} namespace.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.1
+ * @version 1.4
  * @since   1.0
  */
 package org.apache.sis.internal.jaxb.cat;
diff --git a/core/sis-metadata/src/main/java/org/apache/sis/util/iso/Types.java 
b/core/sis-metadata/src/main/java/org/apache/sis/util/iso/Types.java
index d7f50cec76..7fc10558b4 100644
--- a/core/sis-metadata/src/main/java/org/apache/sis/util/iso/Types.java
+++ b/core/sis-metadata/src/main/java/org/apache/sis/util/iso/Types.java
@@ -28,7 +28,7 @@ import java.util.logging.Level;
 import java.util.logging.Logger;
 import java.util.logging.LogRecord;
 import java.io.IOException;
-import java.io.InputStream;
+import org.opengis.annotation.ResourceBundles;
 import org.opengis.annotation.UML;
 import org.opengis.util.CodeList;
 import org.opengis.util.ControlledVocabulary;
@@ -295,7 +295,7 @@ public final class Types extends Static {
     @OptionalCandidate
     public static InternationalString getDescription(final 
ControlledVocabulary code) {
         if (code != null) {
-            final String resources = getResources(code.getClass().getName());
+            final String resources = toResourceName(code.getClass().getName());
             if (resources != null) {
                 return new Description(resources, 
Description.resourceKey(code));
             }
@@ -316,7 +316,7 @@ public final class Types extends Static {
     public static InternationalString getDescription(final Class<?> type) {
         final String name = getStandardName(type);
         if (name != null) {
-            final String resources = getResources(type.getName());
+            final String resources = toResourceName(type.getName());
             if (resources != null) {
                 return new Description(resources, name);
             }
@@ -338,7 +338,7 @@ public final class Types extends Static {
         if (property != null) {
             final String name = getStandardName(type);
             if (name != null) {
-                final String resources = getResources(type.getName());
+                final String resources = toResourceName(type.getName());
                 if (resources != null) {
                     return new Description(resources, name + SEPARATOR + 
property);
                 }
@@ -360,15 +360,10 @@ public final class Types extends Static {
          */
         private static final long serialVersionUID = -6202647167398898834L;
 
-        /**
-         * The class loader to use for fetching GeoAPI resources.
-         * Since the resources are bundled in the GeoAPI JAR file,
-         * we use the instance that loaded GeoAPI for more determinist 
behavior.
-         */
-        private static final ClassLoader CLASSLOADER = 
UML.class.getClassLoader();
-
         /**
          * Creates a new international string from the specified resource 
bundle and key.
+         * The {@code resources} argument is only informative since this class 
overrides
+         * the {@link #getBundle(Locale)} method.
          *
          * @param resources  the name of the resource bundle, as a fully 
qualified class name.
          * @param key        the key for the resource to fetch.
@@ -378,11 +373,12 @@ public final class Types extends Static {
         }
 
         /**
-         * Loads the resources using the class loader used for loading GeoAPI 
interfaces.
+         * Loads the GeoAPI resources. A cache is managed by {@link 
ResourceBundle}.
+         * Note that the {@link #resources} field value is ignored.
          */
         @Override
-        protected final ResourceBundle getBundle(final Locale locale) {
-            return ResourceBundle.getBundle(resources, locale, CLASSLOADER);
+        protected ResourceBundle getBundle(final Locale locale) {
+            return ResourceBundles.descriptions(locale);
         }
 
         /**
@@ -423,7 +419,7 @@ public final class Types extends Static {
      * The code below is a duplicated - in a different way - of {@code 
CodeListUID(CodeList)}
      * constructor ({@link org.apache.sis.internal.jaxb.code package}). This 
duplication exists
      * because {@code CodeListUID} constructor stores more information in an 
opportunist way.
-     * If this method is updated, please update {@code CodeListUID(CodeList)} 
accordingly.
+     * If this class is updated, please update {@code CodeListUID(CodeList)} 
accordingly.
      *
      * @author  Martin Desruisseaux (Geomatys)
      * @version 0.3
@@ -447,10 +443,19 @@ public final class Types extends Static {
          * @param  code  the code list for which to create a title.
          */
         CodeTitle(final ControlledVocabulary code) {
-            super(CodeLists.RESOURCES, resourceKey(code));
+            super("org.opengis.metadata.CodeLists", resourceKey(code));
             this.code = code;
         }
 
+        /**
+         * Loads the GeoAPI resources. A cache is managed by {@link 
ResourceBundle}.
+         * Note that the {@link #resources} field value is ignored.
+         */
+        @Override
+        protected ResourceBundle getBundle(final Locale locale) {
+            return ResourceBundles.codeLists(locale);
+        }
+
         /**
          * Returns a fallback if no resource is found.
          */
@@ -462,11 +467,14 @@ public final class Types extends Static {
 
     /**
      * Returns the resource name for the given GeoAPI type, or {@code null} if 
none.
+     * The non-null resource name is only informative in this implementation.
+     * However we need {@code null} value is still necessary for telling that
+     * no resource is expected to exist for the given class.
      *
      * @param  classname  the fully qualified name of the GeoAPI type.
      * @return the resource bundle to load, or {@code null} if none.
      */
-    static String getResources(final String classname) {
+    static String toResourceName(final String classname) {
         String resources = "org.opengis.metadata.Descriptions";
         if (classname.regionMatches(0, resources, 0, 21)) {             // 21 
is the location after the last dot.
             return resources;
@@ -531,16 +539,10 @@ public final class Types extends Static {
             return null;
         }
         if (typeForNames == null) {
-            final Class<UML> c = UML.class;
-            final InputStream in = 
c.getResourceAsStream("class-index.properties");
-            if (in == null) {
-                throw new MissingResourceException("class-index.properties", 
c.getName(), identifier);
-            }
-            final Properties props = new Properties();
+            final Properties props;
             try {
-                props.load(in);
-                in.close();
-            } catch (IOException | IllegalArgumentException e) {
+                props = ResourceBundles.classIndex();
+            } catch (IOException e) {
                 throw new BackingStoreException(e);
             }
             /*
diff --git 
a/core/sis-metadata/src/main/java/org/apache/sis/xml/LegacyCodes.java 
b/core/sis-metadata/src/main/java/org/apache/sis/xml/LegacyCodes.java
index cd7e8090e2..2229e1057f 100644
--- a/core/sis-metadata/src/main/java/org/apache/sis/xml/LegacyCodes.java
+++ b/core/sis-metadata/src/main/java/org/apache/sis/xml/LegacyCodes.java
@@ -61,7 +61,7 @@ final class LegacyCodes {
         for (final Map.Entry<Object,Object> entry : codes.entrySet()) {
             final String legacy = ((String) entry.getKey()).intern();
             final String name   = ((String) entry.getValue()).intern();
-            IANA_TO_LEGACY.put(name  .toUpperCase(Locale.US), legacy); // IANA 
names are restricted to US-ASCII.
+            IANA_TO_LEGACY.put(name  .toUpperCase(Locale.US), legacy);      // 
IANA names are restricted to US-ASCII.
             LEGACY_TO_IANA.put(legacy.toLowerCase(Locale.US), name);
             IANA_TO_LEGACY.put(name, legacy);
             LEGACY_TO_IANA.put(legacy, name);
diff --git 
a/core/sis-metadata/src/test/java/org/apache/sis/util/iso/TypesTest.java 
b/core/sis-metadata/src/test/java/org/apache/sis/util/iso/TypesTest.java
index da06d0ef0e..1773da29a2 100644
--- a/core/sis-metadata/src/test/java/org/apache/sis/util/iso/TypesTest.java
+++ b/core/sis-metadata/src/test/java/org/apache/sis/util/iso/TypesTest.java
@@ -45,7 +45,7 @@ import static org.opengis.test.Assert.assertInstanceOf;
  * Tests the {@link Types} class.
  *
  * @author  Martin Desruisseaux (IRD, Geomatys)
- * @version 1.0
+ * @version 1.4
  * @since   0.3
  */
 public final class TypesTest extends TestCase {
@@ -160,12 +160,12 @@ public final class TypesTest extends TestCase {
     }
 
     /**
-     * Tests the {@link Types#getResources(String)} method.
+     * Tests the {@link Types#toResourceName(String)} method.
      */
     @Test
     public void testGetResources() {
-        assertEquals("org.opengis.metadata.Descriptions", 
Types.getResources("org.opengis.metadata.Identifier"));
-        assertNull(Types.getResources("org.opengis.metadata2.Identifier"));
+        assertEquals("org.opengis.metadata.Descriptions", 
Types.toResourceName("org.opengis.metadata.Identifier"));
+        assertNull(Types.toResourceName("org.opengis.metadata2.Identifier"));
     }
 
     /**
diff --git 
a/core/sis-utility/src/main/java/org/apache/sis/internal/util/CodeLists.java 
b/core/sis-utility/src/main/java/org/apache/sis/internal/util/CodeLists.java
index da7f6ec2b8..d46f79a4c1 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/internal/util/CodeLists.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/internal/util/CodeLists.java
@@ -33,17 +33,10 @@ import org.opengis.util.ControlledVocabulary;
  * This class opportunistically implements {@link Predicate} interface, but 
this is an implementation details.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.0
+ * @version 1.4
  * @since   1.0
  */
 public final class CodeLists implements Predicate<CodeList<?>> {
-    /**
-     * The name of bundle resources for code list titles. The resources should 
be loaded with
-     * the same class loader than {@code 
org.opengis.annotation.UML.class.getClassLoader()}.
-     * Keys are {@link CodeList#identifier()}.
-     */
-    public static final String RESOURCES = "org.opengis.metadata.CodeLists";
-
     /**
      * The name to compare during filtering operation.
      */
diff --git 
a/core/sis-utility/src/main/java/org/apache/sis/internal/util/MetadataServices.java
 
b/core/sis-utility/src/main/java/org/apache/sis/internal/util/MetadataServices.java
index 19aa270723..ff6a7f1f12 100644
--- 
a/core/sis-utility/src/main/java/org/apache/sis/internal/util/MetadataServices.java
+++ 
b/core/sis-utility/src/main/java/org/apache/sis/internal/util/MetadataServices.java
@@ -19,12 +19,12 @@ package org.apache.sis.internal.util;
 import java.text.Format;
 import java.util.Locale;
 import java.util.TimeZone;
-import java.util.ResourceBundle;
 import java.util.MissingResourceException;
 import java.util.function.Supplier;
 import javax.sql.DataSource;
 import java.sql.SQLException;
 import org.opengis.annotation.UML;
+import org.opengis.annotation.ResourceBundles;
 import org.opengis.metadata.citation.Citation;
 import org.apache.sis.internal.system.Modules;
 import org.apache.sis.internal.system.OptionalDependency;
@@ -40,7 +40,7 @@ import org.opengis.util.ControlledVocabulary;
  * implementation using Java reflection.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.2
+ * @version 1.4
  * @since   0.6
  */
 public class MetadataServices extends OptionalDependency {
@@ -131,8 +131,7 @@ public class MetadataServices extends OptionalDependency {
          */
         final UML uml = code.getClass().getAnnotation(UML.class);
         if (uml != null) try {
-            return ResourceBundle.getBundle(CodeLists.RESOURCES, locale, 
UML.class.getClassLoader())
-                                 .getString(uml.identifier() + '.' + 
code.identifier());
+            return 
ResourceBundles.codeLists(locale).getString(uml.identifier() + '.' + 
code.identifier());
         } catch (MissingResourceException e) {
             /*
              * Ignore. The reason for not finding the resource may because of 
above code not covering enough cases.

Reply via email to