Properly cleanup JarInJarClassLoader when plugin disables

This commit is contained in:
Luck 2021-05-23 11:27:45 +01:00
parent f5cdb98b01
commit 05c9ca5951
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
4 changed files with 34 additions and 1 deletions

View File

@ -33,6 +33,7 @@ import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
/**
@ -64,6 +65,20 @@ public class JarInJarClassLoader extends URLClassLoader {
addURL(url);
}
public void deleteJarResource() {
URL[] urls = getURLs();
if (urls.length == 0) {
return;
}
try {
Path path = Paths.get(urls[0].toURI());
Files.deleteIfExists(path);
} catch (Exception e) {
// ignore
}
}
/**
* Creates a new plugin instance.
*

View File

@ -255,6 +255,9 @@ public abstract class AbstractLuckPermsPlugin implements LuckPermsPlugin {
// shutdown async executor pool
getBootstrap().getScheduler().shutdownExecutor();
// close classpath appender
getBootstrap().getClassPathAppender().close();
getLogger().info("Goodbye!");
}

View File

@ -30,8 +30,12 @@ import java.nio.file.Path;
/**
* Interface which allows access to add URLs to the plugin classpath at runtime.
*/
public interface ClassPathAppender {
public interface ClassPathAppender extends AutoCloseable {
void addJarToClasspath(Path file);
@Override
default void close() {
}
}

View File

@ -27,6 +27,7 @@ package me.lucko.luckperms.common.plugin.classpath;
import me.lucko.luckperms.common.loader.JarInJarClassLoader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Path;
@ -48,4 +49,14 @@ public class JarInJarClassPathAppender implements ClassPathAppender {
throw new RuntimeException(e);
}
}
@Override
public void close() {
this.classLoader.deleteJarResource();
try {
this.classLoader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}