Skip to content

Commit

Permalink
Refactor PluginsScanner to improve path handling and error management…
Browse files Browse the repository at this point in the history
… for plugins directory resolution

Ref: #531
  • Loading branch information
mkjsix committed Nov 18, 2024
1 parent 83d17e7 commit 4dd8856
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions core/src/main/java/org/restheart/plugins/PluginsScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -359,7 +361,7 @@ public RuntimeClassGraph() {

var libJars = Arrays.stream(this.jars)
.map(jar -> jar.getPath())
.map(path -> Path.of(path))
.map(path -> Paths.get(path))
.filter(jar -> isLibJar(jar))
.map(path -> path.getFileName().toString())
.toArray(String[]::new);
Expand Down Expand Up @@ -395,31 +397,27 @@ public ClassGraph get() {
}

public static Path getPluginsDirectory() {
var pluginsDir = Bootstrapper.getConfiguration().coreModule().pluginsDirectory();
String pluginsDir = Bootstrapper.getConfiguration().coreModule().pluginsDirectory();

if (pluginsDir == null) {
return null;
}

var pluginsPath = Path.of(pluginsDir);
Path pluginsPath = Paths.get(pluginsDir);

if (pluginsPath.isAbsolute()) {
return Paths.get(pluginsDir);
} else {
// this is to allow specifying the plugins directory path
// relative to the jar (also working when running from classes)
var location = PluginsFactory.class.getProtectionDomain().getCodeSource().getLocation();
return pluginsPath;
}

try {
return Path.of(URLDecoder.decode(location.getPath(), StandardCharsets.UTF_8.toString())) // url -> path
.getParent()
.resolve(pluginsPath);
} catch(UnsupportedEncodingException uee) {
throw new RuntimeException(uee);
}
try {
URI locationUri = PluginsFactory.class.getProtectionDomain().getCodeSource().getLocation().toURI();
return Paths.get(locationUri).getParent().resolve(pluginsPath);
} catch (URISyntaxException e) {
throw new RuntimeException("Failed to resolve plugins directory", e);
}
}


private URL[] findPluginsJars(Path pluginsDirectory) {
return _findPluginsJars(pluginsDirectory, 0);
}
Expand Down

0 comments on commit 4dd8856

Please sign in to comment.