From 84ab3bfeb257a750fdcd32aa9aec82fc8cbfa9eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20H=C3=A5kansson?= Date: Mon, 15 Oct 2018 09:11:15 +0200 Subject: [PATCH] Makes plugins directory independent of current directory (fixes #9) --- docs/Plugins.md | 2 +- src/org/daisy/dotify/cli/DotifyCLI.java | 35 +++++++++++++++++++------ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/docs/Plugins.md b/docs/Plugins.md index 65a06d9..a07b8f9 100644 --- a/docs/Plugins.md +++ b/docs/Plugins.md @@ -1,4 +1,4 @@ [Table of Contents](toc.md) # Plugins # -Plugins can be added by creating a directory called _plugins_ in the software folder and placing the plugin (.jar) in that folder. \ No newline at end of file +Plugins can be added by creating a directory called `plugins` inside the `lib` folder and placing the plugin (.jar) in that folder. \ No newline at end of file diff --git a/src/org/daisy/dotify/cli/DotifyCLI.java b/src/org/daisy/dotify/cli/DotifyCLI.java index d5c9c97..8cf1a3e 100644 --- a/src/org/daisy/dotify/cli/DotifyCLI.java +++ b/src/org/daisy/dotify/cli/DotifyCLI.java @@ -2,12 +2,15 @@ import java.io.File; import java.lang.reflect.Method; +import java.net.URISyntaxException; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; +import java.util.logging.Level; import java.util.logging.Logger; import org.daisy.dotify.SystemProperties; @@ -113,14 +116,21 @@ protected void putCommand(String cmd, String desc, Classname.endsWith(".jar"))); - for (URL u : jars) { - logger.info("Found jar " + u); + if (logger.isLoggable(Level.FINE)) { + logger.fine("Plugins folder: " + dir); } - // set context class loader - if (jars.length>0) { - Thread.currentThread().setContextClassLoader(new URLClassLoader(jars)); + if (dir.exists()) { + // list jars and convert to URL's + URL[] jars = FileIO.toURL(FileIO.listFilesRecursive(dir, ".jar").toArray(new File[]{})); + if (logger.isLoggable(Level.FINE)) { + for (URL u : jars) { + logger.fine("Found jar " + u); + } + } + // set context class loader + if (jars.length>0) { + Thread.currentThread().setContextClassLoader(new URLClassLoader(jars)); + } } } @@ -135,7 +145,16 @@ public void run() throws Exception { parser.displayHelp(System.out); ExitCode.MISSING_ARGUMENT.exitSystem(); } - setPluginsDir(new File("plugins")); + //TODO: check error conditions, such as null + Optional.ofNullable(DotifyCLI.class.getProtectionDomain().getCodeSource()) + .flatMap(v->{ + try { + return Optional.ofNullable(new File((v.getLocation()).toURI()).getParentFile()); + } catch (URISyntaxException e) { + return Optional.empty(); + } + }) + .ifPresent(parent->setPluginsDir(new File(parent, "plugins"))); if (HELP.equalsIgnoreCase(args[0])) { if (args.length>=2) { Class clazz = commands.get(args[1]);