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 06e70ddfca62d751b812397037005fbaa828cc40
Author: Martin Desruisseaux <martin.desruisse...@geomatys.com>
AuthorDate: Mon Sep 30 17:08:26 2024 +0200

    Bug fixes in the support of sub-components in a GDAL store:
    - Fix the driver identifier (short name, not long name).
    - Throw an exception when a component cannot be opened.
    - Do not close the components that were not opened.
---
 .../main/org/apache/sis/storage/gdal/ErrorHandler.java       |  6 ++++++
 .../main/org/apache/sis/storage/gdal/GDALStore.java          | 12 ++++++++----
 .../main/org/apache/sis/storage/gdal/Opener.java             |  5 +++++
 .../main/org/apache/sis/storage/gdal/SubdatasetList.java     |  9 +++++++++
 4 files changed, 28 insertions(+), 4 deletions(-)

diff --git 
a/incubator/src/org.apache.sis.storage.gdal/main/org/apache/sis/storage/gdal/ErrorHandler.java
 
b/incubator/src/org.apache.sis.storage.gdal/main/org/apache/sis/storage/gdal/ErrorHandler.java
index 7432ccfdf5..bb8236e0cc 100644
--- 
a/incubator/src/org.apache.sis.storage.gdal/main/org/apache/sis/storage/gdal/ErrorHandler.java
+++ 
b/incubator/src/org.apache.sis.storage.gdal/main/org/apache/sis/storage/gdal/ErrorHandler.java
@@ -110,6 +110,12 @@ final class ErrorHandler {
             String text = NativeFunctions.toString(message);
             if (text == null || text.isBlank()) {
                 text = "GDAL error #" + code;
+            } else {
+                /*
+                 * GDAL puts line separator in the middle of messages, maybe 
for console output.
+                 * Remove them as line feeds are handled either by JavaFX or 
by our log formatter.
+                 */
+                text = text.replace(System.lineSeparator(), " ");
             }
             messages.add(new ErrorHandler(err, text));
         } catch (Throwable e) {
diff --git 
a/incubator/src/org.apache.sis.storage.gdal/main/org/apache/sis/storage/gdal/GDALStore.java
 
b/incubator/src/org.apache.sis.storage.gdal/main/org/apache/sis/storage/gdal/GDALStore.java
index be76b49eca..748dc7d15c 100644
--- 
a/incubator/src/org.apache.sis.storage.gdal/main/org/apache/sis/storage/gdal/GDALStore.java
+++ 
b/incubator/src/org.apache.sis.storage.gdal/main/org/apache/sis/storage/gdal/GDALStore.java
@@ -238,7 +238,7 @@ public class GDALStore extends DataStore implements 
Aggregate {
         try {
             var result = (MemorySegment) 
gdal.getDatasetDriver.invokeExact(handle());
             if (!GDAL.isNull(result)) {     // Paranoiac check.
-                result = (MemorySegment) gdal.getName.invokeExact(result);
+                result = (MemorySegment) 
gdal.getIdentifier.invokeExact(result);
                 return GDAL.toString(result);
             }
         } catch (Throwable e) {
@@ -478,15 +478,19 @@ public class GDALStore extends DataStore implements 
Aggregate {
             error = e;      // Should not happen even if a listener threw an 
exception, but we want to be safe.
         } finally {
             try {
-                final List<? extends Resource> children;
+                List<? extends Resource> children;
                 synchronized (this) {
                     children = components;
                     components = null;
                 }
+                if (children instanceof SubdatasetList delayed) {
+                    children = delayed.getOpenedComponents();
+                }
                 if (children != null) {
                     for (Resource child : children) {
-                        if (child instanceof Subdataset) try {
-                            ((GDALStore) child).closeRecursively();
+                        // Note: child may be null if not yet opened.
+                        if (child instanceof GDALStore subdataset) try {
+                            subdataset.closeRecursively();
                         } catch (RuntimeException e) {
                             if (error == null) error = e;
                             else error.addSuppressed(e);
diff --git 
a/incubator/src/org.apache.sis.storage.gdal/main/org/apache/sis/storage/gdal/Opener.java
 
b/incubator/src/org.apache.sis.storage.gdal/main/org/apache/sis/storage/gdal/Opener.java
index 45e4314a30..4b737797ec 100644
--- 
a/incubator/src/org.apache.sis.storage.gdal/main/org/apache/sis/storage/gdal/Opener.java
+++ 
b/incubator/src/org.apache.sis.storage.gdal/main/org/apache/sis/storage/gdal/Opener.java
@@ -26,6 +26,7 @@ import org.apache.sis.util.privy.Constants;
 import org.apache.sis.storage.ProbeResult;
 import org.apache.sis.storage.StorageConnector;
 import org.apache.sis.storage.DataStoreException;
+import org.apache.sis.util.resources.Errors;
 
 
 /**
@@ -110,6 +111,10 @@ final class Opener implements Runnable {
         } catch (Throwable e) {
             throw GDAL.propagate(e);
         }
+        if (GDAL.isNull(handle)) {
+            ErrorHandler.throwOnFailure(null, "open");
+            throw new 
DataStoreException(Errors.format(Errors.Keys.CanNotOpen_1, url));
+        }
     }
 
     /**
diff --git 
a/incubator/src/org.apache.sis.storage.gdal/main/org/apache/sis/storage/gdal/SubdatasetList.java
 
b/incubator/src/org.apache.sis.storage.gdal/main/org/apache/sis/storage/gdal/SubdatasetList.java
index 826aa56453..e02924006a 100644
--- 
a/incubator/src/org.apache.sis.storage.gdal/main/org/apache/sis/storage/gdal/SubdatasetList.java
+++ 
b/incubator/src/org.apache.sis.storage.gdal/main/org/apache/sis/storage/gdal/SubdatasetList.java
@@ -16,6 +16,7 @@
  */
 package org.apache.sis.storage.gdal;
 
+import java.util.Arrays;
 import java.util.List;
 import java.util.AbstractList;
 import java.util.LinkedHashMap;
@@ -155,4 +156,12 @@ final class SubdatasetList extends 
AbstractList<Subdataset> {
             return component;
         }
     }
+
+    /**
+     * Returns the components that are open. The returned array may contain 
null elements.
+     * Those null elements are for components that have not been opened yet 
and shall be ignored.
+     */
+    final List<Subdataset> getOpenedComponents() {
+        return Arrays.asList(components);
+    }
 }

Reply via email to