Skip to content

Commit

Permalink
Don't try to load a candidate plugin with the same ID more than once
Browse files Browse the repository at this point in the history
Fixes #1372. Honestly, this was an oversight that I'm somewhat surprised that nobody caught until recently.
  • Loading branch information
astei committed Jul 6, 2024
1 parent 4eae510 commit 6224adf
Showing 1 changed file with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -107,14 +107,25 @@ public void loadPlugins(Path directory) throws IOException {

List<PluginDescription> sortedPlugins = PluginDependencyUtils.sortCandidates(found);

Set<String> loadedPluginsById = new HashSet<>();
Map<String, PluginDescription> loadedCandidates = new HashMap<>();
Map<PluginContainer, Module> pluginContainers = new LinkedHashMap<>();
// Now load the plugins
pluginLoad:
for (PluginDescription candidate : sortedPlugins) {
// If we found a duplicate candidate (with the same ID), don't load it.
PluginDescription existingCandidate = loadedCandidates.get(candidate.getId());
if (existingCandidate != null) {
logger.error("Refusing to load plugin at path {} since we already "
+ "loaded a plugin with the same ID {} from {}",
candidate.getSource().map(Objects::toString).orElse("<UNKNOWN>"),
candidate.getId(),
existingCandidate.getSource().map(Objects::toString).orElse("<UNKNOWN>"));
continue;
}

// Verify dependencies
for (PluginDependency dependency : candidate.getDependencies()) {
if (!dependency.isOptional() && !loadedPluginsById.contains(dependency.getId())) {
if (!dependency.isOptional() && !loadedCandidates.containsKey(dependency.getId())) {
logger.error("Can't load plugin {} due to missing dependency {}", candidate.getId(),
dependency.getId());
continue pluginLoad;
Expand All @@ -125,7 +136,7 @@ public void loadPlugins(Path directory) throws IOException {
PluginDescription realPlugin = loader.createPluginFromCandidate(candidate);
VelocityPluginContainer container = new VelocityPluginContainer(realPlugin);
pluginContainers.put(container, loader.createModule(container));
loadedPluginsById.add(realPlugin.getId());
loadedCandidates.put(realPlugin.getId(), realPlugin);
} catch (Throwable e) {
logger.error("Can't create module for plugin {}", candidate.getId(), e);
}
Expand Down

0 comments on commit 6224adf

Please sign in to comment.