From e1e07d2865c89dafe33115afa7a5831d4651b38c Mon Sep 17 00:00:00 2001 From: Adam Gent Date: Thu, 12 Dec 2024 13:05:17 -0500 Subject: [PATCH] Allow NoSuchFileException to indicate missing --- .../ezkv/kvs/DefaultKeyValuesSourceLoader.java | 3 ++- .../io/jstach/ezkv/kvs/KeyValuesEnvironment.java | 4 ++-- .../java/io/jstach/ezkv/kvs/KeyValuesLoader.java | 10 ++++++++-- .../java/io/jstach/ezkv/kvs/KeyValuesResource.java | 14 ++++++++++++-- .../jstach/ezkv/kvs/KeyValuesServiceProvider.java | 7 ++++--- 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/ezkv-kvs/src/main/java/io/jstach/ezkv/kvs/DefaultKeyValuesSourceLoader.java b/ezkv-kvs/src/main/java/io/jstach/ezkv/kvs/DefaultKeyValuesSourceLoader.java index c4f6ff6..41459cf 100644 --- a/ezkv-kvs/src/main/java/io/jstach/ezkv/kvs/DefaultKeyValuesSourceLoader.java +++ b/ezkv-kvs/src/main/java/io/jstach/ezkv/kvs/DefaultKeyValuesSourceLoader.java @@ -2,6 +2,7 @@ import java.io.FileNotFoundException; import java.io.IOException; +import java.nio.file.NoSuchFileException; import java.util.ArrayList; import java.util.EnumSet; import java.util.HashSet; @@ -216,7 +217,7 @@ KeyValues load(Node node, InternalKeyValuesResource resource, Set flag catch (KeyValuesException e) { throw new IOException("Resource has key value errors. resource: " + describe(node), e); } - catch (FileNotFoundException e) { + catch (FileNotFoundException | NoSuchFileException e) { logger.missing(resource, e); if (LoadFlag.NO_REQUIRE.isSet(flags)) { return KeyValues.empty(); diff --git a/ezkv-kvs/src/main/java/io/jstach/ezkv/kvs/KeyValuesEnvironment.java b/ezkv-kvs/src/main/java/io/jstach/ezkv/kvs/KeyValuesEnvironment.java index 198dbeb..af898f2 100644 --- a/ezkv-kvs/src/main/java/io/jstach/ezkv/kvs/KeyValuesEnvironment.java +++ b/ezkv-kvs/src/main/java/io/jstach/ezkv/kvs/KeyValuesEnvironment.java @@ -224,7 +224,7 @@ default void loaded(KeyValuesResource resource) { * @param resource the resource that was not found * @param exception the exception that occurred when the resource was not found */ - default void missing(KeyValuesResource resource, FileNotFoundException exception) { + default void missing(KeyValuesResource resource, Exception exception) { debug(KeyValueReference.describe(new StringBuilder("Missing "), resource, false).toString()); } @@ -294,7 +294,7 @@ public void loaded(KeyValuesResource resource) { } @Override - public void missing(KeyValuesResource resource, FileNotFoundException exception) { + public void missing(KeyValuesResource resource, Exception exception) { } } diff --git a/ezkv-kvs/src/main/java/io/jstach/ezkv/kvs/KeyValuesLoader.java b/ezkv-kvs/src/main/java/io/jstach/ezkv/kvs/KeyValuesLoader.java index 2205535..c4b40a9 100644 --- a/ezkv-kvs/src/main/java/io/jstach/ezkv/kvs/KeyValuesLoader.java +++ b/ezkv-kvs/src/main/java/io/jstach/ezkv/kvs/KeyValuesLoader.java @@ -3,6 +3,7 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.net.URI; +import java.nio.file.NoSuchFileException; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; @@ -150,10 +151,15 @@ public KeyValuesLoader build() { * Loads key-values using the current builder configuration. * @return a {@link KeyValues} instance containing the loaded key-value pairs * @throws IOException if an I/O error occurs during loading - * @throws FileNotFoundException if a specified resource is not found + * @throws FileNotFoundException if a specified resource is not found. This + * exception gets special treatment if thrown if the resource is optional it will + * not be an error. + * @throws NoSuchFileException if a specified resource is not found. This + * exception gets special treatment if thrown if the resource is optional it will + * not be an error. */ @Override - public KeyValues load() throws IOException, FileNotFoundException { + public KeyValues load() throws IOException, FileNotFoundException, NoSuchFileException { return build().load(); } diff --git a/ezkv-kvs/src/main/java/io/jstach/ezkv/kvs/KeyValuesResource.java b/ezkv-kvs/src/main/java/io/jstach/ezkv/kvs/KeyValuesResource.java index c145f9d..89f89d1 100644 --- a/ezkv-kvs/src/main/java/io/jstach/ezkv/kvs/KeyValuesResource.java +++ b/ezkv-kvs/src/main/java/io/jstach/ezkv/kvs/KeyValuesResource.java @@ -1,7 +1,9 @@ package io.jstach.ezkv.kvs; +import java.io.FileNotFoundException; import java.net.URI; import java.nio.charset.StandardCharsets; +import java.nio.file.NoSuchFileException; import java.util.ArrayList; import java.util.Base64; import java.util.EnumSet; @@ -210,7 +212,13 @@ public sealed interface KeyValuesResource extends NamedKeyValuesSource, KeyValue /** * Indicates that the resource is optional and no error should occur if it is not - * found. + * found. {@linkplain KeyValuesLoader Loaders} can indicate not found by throwing + * either {@link FileNotFoundException} or {@link NoSuchFileException}. If the + * resource is missing during a full load it will be logged with + * {@link KeyValuesEnvironment.Logger#missing(KeyValuesResource, Exception)}. Note + * that a resource may just not have any key values but is still found. By default + * resources are normally required to exist. + * @see #FLAG_NO_EMPTY */ public static final String FLAG_NO_REQUIRE = "NO_REQUIRE"; @@ -226,7 +234,9 @@ public sealed interface KeyValuesResource extends NamedKeyValuesSource, KeyValue /** * Indicates that the resource should not be empty. Typically used to enforce that the - * resource contains at least one key-value pair. + * resource contains at least one key-value pair. This is different than + * {@link #FLAG_NO_REQUIRE} which is if the resource does not exist at all. By default + * empty resources are normally allowed. */ public static final String FLAG_NO_EMPTY = "NO_EMPTY"; diff --git a/ezkv-kvs/src/main/java/io/jstach/ezkv/kvs/KeyValuesServiceProvider.java b/ezkv-kvs/src/main/java/io/jstach/ezkv/kvs/KeyValuesServiceProvider.java index b68d9c4..51842f6 100644 --- a/ezkv-kvs/src/main/java/io/jstach/ezkv/kvs/KeyValuesServiceProvider.java +++ b/ezkv-kvs/src/main/java/io/jstach/ezkv/kvs/KeyValuesServiceProvider.java @@ -2,6 +2,7 @@ import java.io.FileNotFoundException; import java.net.URI; +import java.nio.file.NoSuchFileException; import java.util.Optional; import java.util.function.BiConsumer; @@ -103,9 +104,9 @@ default KeyValuesMedia.Parser requireParser(KeyValuesResource resource) throws K * Finds a {@link KeyValuesLoader} capable of loading the specified * {@link KeyValuesResource} using the provided {@link LoaderContext}. If * the resource cannot be found the returned {@link KeyValuesLoader} should throw - * a {@link FileNotFoundException} to indicate not found and not Optional.empty - * which conversely means no matching loader could be found based on the resource. - * + * a {@link FileNotFoundException} or {@link NoSuchFileException} to indicate not + * found and not Optional.empty which conversely means no matching loader could be + * found based on the resource. * @param context the context containing dependencies and services * @param resource the resource for which a loader is sought * @return an {@link Optional} containing the loader if found, or empty if not