From 6b2b28796bee203ebdfc7a05052ebb65a9823307 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sun, 31 Oct 2021 23:24:39 -0400 Subject: [PATCH] Clean up and clarify plugin loader logic. --- .../velocitypowered/proxy/VelocityServer.java | 1 - .../proxy/plugin/VelocityPluginManager.java | 15 +++++++-------- .../proxy/plugin/loader/PluginLoader.java | 18 ++++++++++++++++-- .../plugin/loader/java/JavaPluginLoader.java | 16 ++++++++-------- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java index aedc2b627..2e5ffa9dd 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java @@ -22,7 +22,6 @@ import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import com.velocitypowered.api.event.EventManager; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.event.proxy.ProxyReloadEvent; import com.velocitypowered.api.event.proxy.ProxyShutdownEvent; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityPluginManager.java b/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityPluginManager.java index 682d5bc78..16886e2f3 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityPluginManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityPluginManager.java @@ -43,7 +43,6 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; import java.util.IdentityHashMap; import java.util.LinkedHashMap; @@ -58,7 +57,7 @@ public class VelocityPluginManager implements PluginManager { private static final Logger logger = LogManager.getLogger(VelocityPluginManager.class); - private final Map plugins = new LinkedHashMap<>(); + private final Map pluginsById = new LinkedHashMap<>(); private final Map pluginInstances = new IdentityHashMap<>(); private final VelocityServer server; @@ -67,7 +66,7 @@ public class VelocityPluginManager implements PluginManager { } private void registerPlugin(PluginContainer plugin) { - plugins.put(plugin.getDescription().getId(), plugin); + pluginsById.put(plugin.getDescription().getId(), plugin); Optional instance = plugin.getInstance(); instance.ifPresent(o -> pluginInstances.put(o, plugin)); } @@ -90,7 +89,7 @@ public class VelocityPluginManager implements PluginManager { p -> p.toFile().isFile() && p.toString().endsWith(".jar"))) { for (Path path : stream) { try { - found.add(loader.loadPluginDescription(path)); + found.add(loader.loadCandidate(path)); } catch (Exception e) { logger.error("Unable to load plugin {}", path, e); } @@ -119,7 +118,7 @@ public class VelocityPluginManager implements PluginManager { } try { - PluginDescription realPlugin = loader.loadPlugin(candidate); + PluginDescription realPlugin = loader.createPluginFromCandidate(candidate); VelocityPluginContainer container = new VelocityPluginContainer(realPlugin); pluginContainers.put(container, loader.createModule(container)); loadedPluginsById.add(realPlugin.getId()); @@ -175,17 +174,17 @@ public class VelocityPluginManager implements PluginManager { @Override public Optional getPlugin(String id) { checkNotNull(id, "id"); - return Optional.ofNullable(plugins.get(id)); + return Optional.ofNullable(pluginsById.get(id)); } @Override public Collection getPlugins() { - return Collections.unmodifiableCollection(plugins.values()); + return Collections.unmodifiableCollection(pluginsById.values()); } @Override public boolean isLoaded(String id) { - return plugins.containsKey(id); + return pluginsById.containsKey(id); } @Override diff --git a/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/PluginLoader.java b/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/PluginLoader.java index 5c1087221..8b176a75b 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/PluginLoader.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/PluginLoader.java @@ -27,9 +27,23 @@ import java.nio.file.Path; */ public interface PluginLoader { - PluginDescription loadPluginDescription(Path source) throws Exception; + /** + * Loads a candidate description from the given {@code source}. + * + * @param source the source to load the candidate from + * @return a plugin candidate description + * @throws Exception if anything goes wrong + */ + PluginDescription loadCandidate(Path source) throws Exception; - PluginDescription loadPlugin(PluginDescription source) throws Exception; + /** + * Materializes a "real" plugin description from the given {@code candidate}. + * + * @param candidate the candidate to materialize + * @return a plugin description + * @throws Exception if anything goes wrong + */ + PluginDescription createPluginFromCandidate(PluginDescription candidate) throws Exception; /** * Creates a {@link Module} for the provided {@link PluginContainer} diff --git a/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/java/JavaPluginLoader.java b/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/java/JavaPluginLoader.java index a3043a93b..1756c2677 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/java/JavaPluginLoader.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/java/JavaPluginLoader.java @@ -57,7 +57,7 @@ public class JavaPluginLoader implements PluginLoader { } @Override - public PluginDescription loadPluginDescription(Path source) throws Exception { + public PluginDescription loadCandidate(Path source) throws Exception { Optional serialized = getSerializedPluginInfo(source); if (!serialized.isPresent()) { @@ -73,20 +73,20 @@ public class JavaPluginLoader implements PluginLoader { } @Override - public PluginDescription loadPlugin(PluginDescription source) throws Exception { - if (!(source instanceof JavaVelocityPluginDescriptionCandidate)) { + public PluginDescription createPluginFromCandidate(PluginDescription candidate) throws Exception { + if (!(candidate instanceof JavaVelocityPluginDescriptionCandidate)) { throw new IllegalArgumentException("Description provided isn't of the Java plugin loader"); } - URL pluginJarUrl = source.getSource().get().toUri().toURL(); + URL pluginJarUrl = candidate.getSource().get().toUri().toURL(); PluginClassLoader loader = AccessController.doPrivileged( (PrivilegedAction) () -> new PluginClassLoader(new URL[]{pluginJarUrl})); loader.addToClassloaders(); - JavaVelocityPluginDescriptionCandidate candidate = - (JavaVelocityPluginDescriptionCandidate) source; - Class mainClass = loader.loadClass(candidate.getMainClass()); - return createDescription(candidate, mainClass); + JavaVelocityPluginDescriptionCandidate candidateInst = + (JavaVelocityPluginDescriptionCandidate) candidate; + Class mainClass = loader.loadClass(candidateInst.getMainClass()); + return createDescription(candidateInst, mainClass); } @Override