Always load dependencies into the plugin classloader, and not it's parent - fixes #479

This commit is contained in:
Luck 2017-09-17 19:30:43 +01:00
parent 214929e5e5
commit a3d78f4d0e
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 22 additions and 13 deletions

View File

@ -39,7 +39,7 @@ import javax.annotation.Nonnull;
* <p>Any changes made to log entries will only apply to this instance of the log. * <p>Any changes made to log entries will only apply to this instance of the log.
* You can add to the log using the {@link Storage}, and then request an updated copy.</p> * You can add to the log using the {@link Storage}, and then request an updated copy.</p>
* *
* <p>All methods are thread safe, and return immutable & thread safe collections.</p> * <p>All methods are thread safe, and return immutable and thread safe collections.</p>
*/ */
public interface Log { public interface Log {

View File

@ -31,14 +31,15 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import me.lucko.luckperms.api.PlatformType;
import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.storage.StorageType; import me.lucko.luckperms.common.storage.StorageType;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.nio.file.Files; import java.nio.file.Files;
@ -49,14 +50,17 @@ import java.util.Set;
@UtilityClass @UtilityClass
public class DependencyManager { public class DependencyManager {
private static Method ADD_URL_METHOD; private static final Method ADD_URL_METHOD;
static { static {
Method addUrlMethod = null;
try { try {
ADD_URL_METHOD = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); addUrlMethod = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
ADD_URL_METHOD.setAccessible(true); addUrlMethod.setAccessible(true);
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
e.printStackTrace(); e.printStackTrace();
} }
ADD_URL_METHOD = addUrlMethod;
} }
public static final Map<StorageType, List<Dependency>> STORAGE_DEPENDENCIES = ImmutableMap.<StorageType, List<Dependency>>builder() public static final Map<StorageType, List<Dependency>> STORAGE_DEPENDENCIES = ImmutableMap.<StorageType, List<Dependency>>builder()
@ -104,9 +108,9 @@ public class DependencyManager {
for (Map.Entry<Dependency, File> e : toLoad) { for (Map.Entry<Dependency, File> e : toLoad) {
try { try {
loadJar(plugin, e.getValue()); loadJar(plugin, e.getValue());
} catch (Throwable e1) { } catch (Throwable t) {
plugin.getLog().severe("Failed to load jar for dependency " + e.getKey().name()); plugin.getLog().severe("Failed to load jar for dependency " + e.getKey().name());
e1.printStackTrace(); t.printStackTrace();
} }
} }
} }
@ -134,14 +138,19 @@ public class DependencyManager {
} }
} }
private static void loadJar(LuckPermsPlugin plugin, File file) throws Exception { private static void loadJar(LuckPermsPlugin plugin, File file) {
URLClassLoader classLoader = (URLClassLoader) plugin.getClass().getClassLoader(); // get the classloader to load into
ClassLoader classLoader = plugin.getClass().getClassLoader();
if (plugin.getServerType() != PlatformType.SPONGE && !plugin.getServerName().equals("KCauldron") && !plugin.getServerName().equals("Uranium")) { if (classLoader instanceof URLClassLoader) {
classLoader = (URLClassLoader) classLoader.getParent(); try {
ADD_URL_METHOD.invoke(classLoader, file.toURI().toURL());
} catch (IllegalAccessException | InvocationTargetException | MalformedURLException e) {
throw new RuntimeException("Unable to invoke URLClassLoader#addURL", e);
}
} else {
throw new RuntimeException("Unknown classloader: " + classLoader.getClass());
} }
ADD_URL_METHOD.invoke(classLoader, file.toURI().toURL());
} }
} }