Skip to content

Commit

Permalink
Makes plugins directory independent of current directory (fixes brail…
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Håkansson committed Oct 15, 2018
1 parent e3d0047 commit 84ab3bf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/Plugins.md
Original file line number Diff line number Diff line change
@@ -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.
Plugins can be added by creating a directory called `plugins` inside the `lib` folder and placing the plugin (.jar) in that folder.
35 changes: 27 additions & 8 deletions src/org/daisy/dotify/cli/DotifyCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -113,14 +116,21 @@ protected void putCommand(String cmd, String desc, Class<? extends CommandDetail
* @param dir the directory to search for jar-files.
*/
public void setPluginsDir(File dir) {
// list jars and convert to URL's
URL[] jars = FileIO.toURL(dir.listFiles((parent, name)->name.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));
}
}
}

Expand All @@ -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.<File>empty();
}
})
.ifPresent(parent->setPluginsDir(new File(parent, "plugins")));
if (HELP.equalsIgnoreCase(args[0])) {
if (args.length>=2) {
Class<? extends CommandDetails> clazz = commands.get(args[1]);
Expand Down

0 comments on commit 84ab3bf

Please sign in to comment.