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 844534c6eb7d7504d634fda1e14b70bc44a905b3 Author: Martin Desruisseaux <[email protected]> AuthorDate: Wed Oct 13 10:59:35 2021 +0200 Add a `CanNotProbeException` for allowing user to know which `DataStoreProvider` failed to probe a file. It makes possible to exclude that provider from next `probeContent(…)` calls if desired. --- .../sis/storage/landsat/LandsatStoreProvider.java | 4 +- .../sis/storage/netcdf/NetcdfStoreProvider.java | 17 +++-- .../apache/sis/storage/sql/SQLStoreProvider.java | 5 +- .../sis/internal/storage/csv/StoreProvider.java | 4 +- .../sis/internal/storage/wkt/FirstKeywordPeek.java | 11 ++- .../org/apache/sis/internal/storage/wkt/Store.java | 4 +- .../sis/internal/storage/wkt/StoreProvider.java | 4 +- .../sis/internal/storage/xml/AbstractProvider.java | 5 +- .../apache/sis/storage/CanNotProbeException.java | 78 ++++++++++++++++++++++ .../org/apache/sis/storage/StorageConnector.java | 2 +- 10 files changed, 109 insertions(+), 25 deletions(-) diff --git a/storage/sis-earth-observation/src/main/java/org/apache/sis/storage/landsat/LandsatStoreProvider.java b/storage/sis-earth-observation/src/main/java/org/apache/sis/storage/landsat/LandsatStoreProvider.java index 5a716d6..448b396 100644 --- a/storage/sis-earth-observation/src/main/java/org/apache/sis/storage/landsat/LandsatStoreProvider.java +++ b/storage/sis-earth-observation/src/main/java/org/apache/sis/storage/landsat/LandsatStoreProvider.java @@ -40,7 +40,7 @@ import org.apache.sis.internal.storage.wkt.FirstKeywordPeek; * the part of the caller. However the {@link LandsatStore} instances created by this factory are not thread-safe. * * @author Martin Desruisseaux (Geomatys) - * @version 1.1 + * @version 1.2 * @since 1.1 * @module */ @@ -202,7 +202,7 @@ public class LandsatStoreProvider extends DataStoreProvider { */ @Override public ProbeResult probeContent(final StorageConnector connector) throws DataStoreException { - return new Peek().probeContent(connector); + return new Peek().probeContent(this, connector); } /** diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStoreProvider.java b/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStoreProvider.java index 6da72f9..cb6acba 100644 --- a/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStoreProvider.java +++ b/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStoreProvider.java @@ -18,10 +18,8 @@ package org.apache.sis.storage.netcdf; import java.util.logging.Level; import java.util.logging.LogRecord; -import java.io.FileNotFoundException; import java.io.IOException; import java.nio.ByteBuffer; -import java.nio.file.NoSuchFileException; import java.lang.reflect.Method; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; @@ -43,6 +41,8 @@ import org.apache.sis.storage.DataStore; import org.apache.sis.storage.DataStoreProvider; import org.apache.sis.storage.StorageConnector; import org.apache.sis.storage.DataStoreException; +import org.apache.sis.storage.InternalDataStoreException; +import org.apache.sis.storage.CanNotProbeException; import org.apache.sis.storage.GridCoverageResource; import org.apache.sis.storage.FeatureSet; import org.apache.sis.storage.Aggregate; @@ -64,7 +64,7 @@ import org.apache.sis.util.Version; * the part of the caller. However the {@link NetcdfStore} instances created by this factory are not thread-safe. * * @author Martin Desruisseaux (Geomatys) - * @version 1.1 + * @version 1.2 * * @see NetcdfStore * @@ -223,10 +223,9 @@ public class NetcdfStoreProvider extends DataStoreProvider { throw (Error) new IncompatibleClassChangeError("canOpen").initCause(e); } catch (InvocationTargetException e) { final Throwable cause = e.getCause(); - if (cause instanceof DataStoreException) throw (DataStoreException) cause; - if (cause instanceof RuntimeException) throw (RuntimeException) cause; - if (cause instanceof Error) throw (Error) cause; - if (cause instanceof FileNotFoundException || cause instanceof NoSuchFileException) { + if (cause instanceof DataStoreException) { + throw (DataStoreException) cause; + } else if (cause instanceof IOException) { /* * Happen if the String argument uses any protocol not recognized by the UCAR library, * in which case UCAR tries to open it as a file even if it is not a file. For example @@ -235,7 +234,7 @@ public class NetcdfStoreProvider extends DataStoreProvider { Logging.recoverableException(getLogger(), NetcdfStoreProvider.class, "probeContent", cause); return ProbeResult.UNSUPPORTED_STORAGE; } - throw new DataStoreException(e); // The cause may be IOException. + throw new CanNotProbeException(this, connector, cause); } } else { /* @@ -360,7 +359,7 @@ public class NetcdfStoreProvider extends DataStoreProvider { if (cause instanceof Error) throw (Error) cause; throw new UndeclaredThrowableException(cause); // Should never happen actually. } catch (ReflectiveOperationException e) { - throw new AssertionError(e); // Should never happen (shall be verified by the JUnit tests). + throw new InternalDataStoreException(e); // Should never happen (shall be verified by the JUnit tests). } } diff --git a/storage/sis-sqlstore/src/main/java/org/apache/sis/storage/sql/SQLStoreProvider.java b/storage/sis-sqlstore/src/main/java/org/apache/sis/storage/sql/SQLStoreProvider.java index 4bb02fb..8761c3a 100644 --- a/storage/sis-sqlstore/src/main/java/org/apache/sis/storage/sql/SQLStoreProvider.java +++ b/storage/sis-sqlstore/src/main/java/org/apache/sis/storage/sql/SQLStoreProvider.java @@ -31,6 +31,7 @@ import org.apache.sis.internal.storage.StoreMetadata; import org.apache.sis.storage.DataStore; import org.apache.sis.storage.DataStoreProvider; import org.apache.sis.storage.DataStoreException; +import org.apache.sis.storage.CanNotProbeException; import org.apache.sis.storage.IllegalOpenParameterException; import org.apache.sis.storage.StorageConnector; import org.apache.sis.storage.ProbeResult; @@ -49,7 +50,7 @@ import static org.apache.sis.internal.sql.feature.Database.WILDCARD; * * @author Johann Sorel (Geomatys) * @author Martin Desruisseaux (Geomatys) - * @version 1.1 + * @version 1.2 * @since 1.0 * @module */ @@ -185,7 +186,7 @@ public class SQLStoreProvider extends DataStoreProvider { } catch (SQLException e) { final String state = e.getSQLState(); if (!"08001".equals(state) || !"3D000".equals(state)) { - throw new DataStoreException(e); + throw new CanNotProbeException(this, connector, e); } } } diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/csv/StoreProvider.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/csv/StoreProvider.java index a36bdef..b6ada72 100644 --- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/csv/StoreProvider.java +++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/csv/StoreProvider.java @@ -45,7 +45,7 @@ import org.apache.sis.util.ArgumentChecks; * the part of the caller. However the {@link Store} instances created by this factory are not thread-safe. * * @author Martin Desruisseaux (Geomatys) - * @version 1.1 + * @version 1.2 * @since 0.8 * @module */ @@ -152,7 +152,7 @@ public final class StoreProvider extends URIDataStore.Provider { */ @Override public ProbeResult probeContent(final StorageConnector connector) throws DataStoreException { - return new Peek().probeContent(connector); + return new Peek().probeContent(this, connector); } /** diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/wkt/FirstKeywordPeek.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/wkt/FirstKeywordPeek.java index 5c8c896..6409ca8 100644 --- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/wkt/FirstKeywordPeek.java +++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/wkt/FirstKeywordPeek.java @@ -23,7 +23,9 @@ import java.nio.file.Path; import java.nio.file.Files; import java.nio.channels.SeekableByteChannel; import org.apache.sis.internal.storage.io.IOUtilities; +import org.apache.sis.storage.CanNotProbeException; import org.apache.sis.storage.DataStoreException; +import org.apache.sis.storage.DataStoreProvider; import org.apache.sis.storage.StorageConnector; import org.apache.sis.storage.ProbeResult; import org.apache.sis.util.Characters; @@ -33,7 +35,7 @@ import org.apache.sis.util.Characters; * Inspects the type of a text file based on the first keyword. * * @author Martin Desruisseaux (Geomatys) - * @version 1.1 + * @version 1.2 * @since 0.8 * @module */ @@ -138,11 +140,14 @@ public abstract class FirstKeywordPeek { * only that there appears to be a reasonable chance of success based on a brief inspection of the storage * header. * + * @param provider the data store provider which is performing the probe operation. * @param connector information about the storage (URL, stream, <i>etc</i>). * @return {@link ProbeResult#SUPPORTED} if the given storage seems to be readable. * @throws DataStoreException if an I/O error occurred. */ - public final ProbeResult probeContent(final StorageConnector connector) throws DataStoreException { + public final ProbeResult probeContent(final DataStoreProvider provider, final StorageConnector connector) + throws DataStoreException + { try { final ByteBuffer buffer = connector.getStorageAs(ByteBuffer.class); final Reader reader; @@ -168,7 +173,7 @@ public abstract class FirstKeywordPeek { } return probeContent(buffer, reader); } catch (IOException e) { - throw new DataStoreException(e); + throw new CanNotProbeException(provider, connector, e); } } diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/wkt/Store.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/wkt/Store.java index 00c9308..b85681b 100644 --- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/wkt/Store.java +++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/wkt/Store.java @@ -136,9 +136,9 @@ final class Store extends URIDataStore { parser.validate(obj); } while (pos.getIndex() < wkt.length()); } catch (ParseException e) { - throw new DataStoreContentException(getLocale(), "WKT", getDisplayName(), in).initCause(e); + throw new DataStoreContentException(getLocale(), StoreProvider.NAME, getDisplayName(), in).initCause(e); } catch (IOException e) { - throw new DataStoreException(getLocale(), "WKT", getDisplayName(), in).initCause(e); + throw new DataStoreException(getLocale(), StoreProvider.NAME, getDisplayName(), in).initCause(e); } } diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/wkt/StoreProvider.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/wkt/StoreProvider.java index 35c236e..c8534e3 100644 --- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/wkt/StoreProvider.java +++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/wkt/StoreProvider.java @@ -34,7 +34,7 @@ import org.apache.sis.util.Version; * The provider of WKT {@link Store} instances. * * @author Martin Desruisseaux (Geomatys) - * @version 1.0 + * @version 1.2 * @since 0.7 * @module */ @@ -173,7 +173,7 @@ public final class StoreProvider extends URIDataStore.Provider { */ @Override public ProbeResult probeContent(final StorageConnector connector) throws DataStoreException { - return Peek.INSTANCE.probeContent(connector); + return Peek.INSTANCE.probeContent(this, connector); } /** diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/AbstractProvider.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/AbstractProvider.java index 7f5a483..c0d19b4 100644 --- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/AbstractProvider.java +++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/AbstractProvider.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.nio.ByteBuffer; import org.apache.sis.storage.DataStore; import org.apache.sis.storage.DataStoreException; +import org.apache.sis.storage.CanNotProbeException; import org.apache.sis.storage.StorageConnector; import org.apache.sis.storage.ProbeResult; import org.apache.sis.internal.storage.io.IOUtilities; @@ -35,7 +36,7 @@ import org.apache.sis.internal.storage.DocumentedStoreProvider; * (JAXB, StAX, <i>etc</i>). * * @author Martin Desruisseaux (Geomatys) - * @version 1.0 + * @version 1.2 * @since 0.8 * @module */ @@ -160,7 +161,7 @@ public abstract class AbstractProvider extends DocumentedStoreProvider { reader.reset(); return result; } catch (IOException e) { - throw new DataStoreException(e); + throw new CanNotProbeException(this, connector, e); } return ProbeResult.UNSUPPORTED_STORAGE; } diff --git a/storage/sis-storage/src/main/java/org/apache/sis/storage/CanNotProbeException.java b/storage/sis-storage/src/main/java/org/apache/sis/storage/CanNotProbeException.java new file mode 100644 index 0000000..66d8d07 --- /dev/null +++ b/storage/sis-storage/src/main/java/org/apache/sis/storage/CanNotProbeException.java @@ -0,0 +1,78 @@ +/* + * 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.storage; + + +/** + * Thrown when an unrecoverable error occurred during the probing of a file. + * This exception contains a reference to the provider that failed. + * + * @author Martin Desruisseaux (Geomatys) + * @version 1.2 + * + * @see DataStoreProvider#probeContent(StorageConnector) + * + * @since 1.2 + * @module + */ +public class CanNotProbeException extends DataStoreException { + /** + * For cross-version compatibility. + */ + private static final long serialVersionUID = -7183214487330030125L; + + /** + * The data store provider that failed to probe a file. + */ + private final DataStoreProvider provider; + + /** + * Creates an exception with the specified details message and cause. + * + * @param provider the data store provider that failed to probe a file. + * @param message the detail message in the default locale. + * @param cause the cause for this exception. + */ + public CanNotProbeException(final DataStoreProvider provider, final String message, final Throwable cause) { + super(message, cause); + this.provider = provider; + } + + /** + * Creates a localized exception with a message saying that the given store can not be processed. + * + * @param provider the data store provider that failed to probe a file. + * @param connector the stream, file or other kind of resource that the store provider tried to probe. + * @param cause the reason why the data store can not be probed. + */ + public CanNotProbeException(final DataStoreProvider provider, final StorageConnector connector, final Throwable cause) { + super(null, provider.getShortName(), connector.getStorageName(), connector.storage); + this.provider = provider; + super.initCause(cause); + } + + /** + * Returns the data store provider that failed to probe a file. + * + * @return the data store provider that failed to probe a file. + * + * @see DataStore#getProvider() + */ + public DataStoreProvider getProvider() { + return provider; + } +} diff --git a/storage/sis-storage/src/main/java/org/apache/sis/storage/StorageConnector.java b/storage/sis-storage/src/main/java/org/apache/sis/storage/StorageConnector.java index b0b9e5f..d22cd81 100644 --- a/storage/sis-storage/src/main/java/org/apache/sis/storage/StorageConnector.java +++ b/storage/sis-storage/src/main/java/org/apache/sis/storage/StorageConnector.java @@ -199,7 +199,7 @@ public class StorageConnector implements Serializable { * * @see #getStorage() */ - private final Object storage; + final Object storage; /** * A name for the input/output object, or {@code null} if none.
