From 00a461f0a7972478a5e88ebdc097924f09111d50 Mon Sep 17 00:00:00 2001 From: Decebal Suiu Date: Mon, 24 Jan 2022 00:42:20 +0200 Subject: [PATCH] Optional for add path/file in PluginClassLoader --- .../main/java/org/pf4j/PluginClassLoader.java | 39 +++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/pf4j/src/main/java/org/pf4j/PluginClassLoader.java b/pf4j/src/main/java/org/pf4j/PluginClassLoader.java index 0106acb..b69c5dc 100644 --- a/pf4j/src/main/java/org/pf4j/PluginClassLoader.java +++ b/pf4j/src/main/java/org/pf4j/PluginClassLoader.java @@ -20,6 +20,7 @@ import org.slf4j.LoggerFactory; import java.io.File; import java.io.IOException; +import java.io.UncheckedIOException; import java.net.URL; import java.net.URLClassLoader; import java.nio.file.Path; @@ -80,21 +81,51 @@ public class PluginClassLoader extends URLClassLoader { super.addURL(url); } + /** + * Shortcut for {@code addPath(path, true)}. + */ public void addPath(Path path) { + addPath(path, true); + } + + /** + * Add a path. + * If {@code optional} is {@code true} then if something is wrong a warning message is added to the log. + * Otherwise, {@link UncheckedIOException} is thrown. + */ + public void addPath(Path path, boolean optional) { try { addURL(path.toUri().toURL()); } catch (IOException e) { -// throw new UncheckedIOException("Cannot add path '" + path + "'", e); - log.warn("Cannot add path '" + path + "'", e); + if (optional) { + log.warn("Cannot add path '" + path + "'", e); + } else { + throw new UncheckedIOException("Cannot add path '" + path + "'", e); + } } } + /** + * Shortcut for {@code addFile(file, true)}. + */ public void addFile(File file) { + addFile(file, true); + } + + /** + * Add a file. + * If {@code optional} is {@code true} then if something is wrong a warning message is added to the log. + * Otherwise, {@link UncheckedIOException} is thrown. + */ + public void addFile(File file, boolean optional) { try { addURL(file.getCanonicalFile().toURI().toURL()); } catch (IOException e) { -// throw new UncheckedIOException("Cannot add file '" + file + "'", e); - log.warn("Cannot add file '" + file + "'", e); + if (optional) { + log.warn("Cannot add file '" + file + "'", e); + } else { + throw new UncheckedIOException("Cannot add file '" + file + "'", e); + } } } -- 2.39.5