]> source.dussan.org Git - pf4j.git/commitdiff
Optional for add path/file in PluginClassLoader
authorDecebal Suiu <decebal.suiu@gmail.com>
Sun, 23 Jan 2022 22:42:20 +0000 (00:42 +0200)
committerDecebal Suiu <decebal.suiu@gmail.com>
Sun, 23 Jan 2022 22:42:20 +0000 (00:42 +0200)
pf4j/src/main/java/org/pf4j/PluginClassLoader.java

index 0106acbeb6c9fb817937932c6a7c095ae3490668..b69c5dca4b1f60c0108b192cf9a6e0ccf8712f94 100644 (file)
@@ -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);
+            }
         }
     }