Fixed basic dependency functionality - If class can't be found, all other .jar files are scanned

By: Raphfrk <raphfrk@gmail.com>
This commit is contained in:
Bukkit/Spigot 2011-03-09 22:14:13 +00:00
parent b4e1f91936
commit 65e79b2885
2 changed files with 36 additions and 16 deletions

View file

@ -39,7 +39,7 @@ public final class JavaPluginLoader implements PluginLoader {
Pattern.compile("\\.jar$"), Pattern.compile("\\.jar$"),
}; };
private final Map<String, Class<?>> classes = new HashMap<String, Class<?>>(); private final Map<String, Class<?>> classes = new HashMap<String, Class<?>>();
private final Map<String, File> files = new HashMap<String, File>(); private final Map<String, PluginClassLoader> loaders = new HashMap<String, PluginClassLoader>();
public JavaPluginLoader(Server instance) { public JavaPluginLoader(Server instance) {
server = instance; server = instance;
@ -81,27 +81,21 @@ public final class JavaPluginLoader implements PluginLoader {
throw new InvalidPluginException(ex); throw new InvalidPluginException(ex);
} }
ArrayList<File> dependFiles = new ArrayList<File>();
for(String pluginName : depend) { for(String pluginName : depend) {
if(files == null) { if(loaders == null) {
throw new UnknownDependencyException(pluginName); throw new UnknownDependencyException(pluginName);
} }
File current = files.get(pluginName); PluginClassLoader current = loaders.get(pluginName);
if(current == null) { if(current == null) {
throw new UnknownDependencyException(pluginName); throw new UnknownDependencyException(pluginName);
} }
dependFiles.add(current);
} }
PluginClassLoader loader = null;
try { try {
URL[] urls = new URL[dependFiles.size() + 1]; URL[] urls = new URL[1];
urls[0] = file.toURI().toURL(); urls[0] = file.toURI().toURL();
int cnt = 1; loader = new PluginClassLoader(this, urls, getClass().getClassLoader());
for(File f : dependFiles) {
urls[cnt++] = f.toURI().toURL();
}
ClassLoader loader = new PluginClassLoader(this, urls, getClass().getClassLoader());
Class<?> jarClass = Class.forName(description.getMain(), true, loader); Class<?> jarClass = Class.forName(description.getMain(), true, loader);
Class<? extends JavaPlugin> plugin = jarClass.asSubclass(JavaPlugin.class); Class<? extends JavaPlugin> plugin = jarClass.asSubclass(JavaPlugin.class);
@ -113,7 +107,7 @@ public final class JavaPluginLoader implements PluginLoader {
throw new InvalidPluginException(ex); throw new InvalidPluginException(ex);
} }
files.put(description.getName(), file); loaders.put(description.getName(), (PluginClassLoader)loader);
return (Plugin)result; return (Plugin)result;
} }
@ -143,7 +137,22 @@ public final class JavaPluginLoader implements PluginLoader {
} }
public Class<?> getClassByName(final String name) { public Class<?> getClassByName(final String name) {
return classes.get(name); Class<?> cachedClass = classes.get(name);
if(cachedClass != null) {
return cachedClass;
} else {
for(String current : loaders.keySet()) {
PluginClassLoader loader = loaders.get(current);
try {
cachedClass = loader.findClass(name, false);
} catch (ClassNotFoundException cnfe) {
}
if(cachedClass != null) {
return cachedClass;
}
}
}
return null;
} }
public void setClass(final String name, final Class<?> clazz) { public void setClass(final String name, final Class<?> clazz) {
@ -450,6 +459,11 @@ public final class JavaPluginLoader implements PluginLoader {
if (!plugin.isEnabled()) { if (!plugin.isEnabled()) {
JavaPlugin jPlugin = (JavaPlugin)plugin; JavaPlugin jPlugin = (JavaPlugin)plugin;
String pluginName = jPlugin.getDescription().getName();
if(!loaders.containsKey(pluginName)) {
loaders.put(pluginName, (PluginClassLoader)jPlugin.getClassLoader());
}
jPlugin.setEnabled(true); jPlugin.setEnabled(true);
server.getPluginManager().callEvent(new PluginEvent(Event.Type.PLUGIN_ENABLE, plugin)); server.getPluginManager().callEvent(new PluginEvent(Event.Type.PLUGIN_ENABLE, plugin));
} }
@ -468,7 +482,7 @@ public final class JavaPluginLoader implements PluginLoader {
server.getPluginManager().callEvent(new PluginEvent(Event.Type.PLUGIN_DISABLE, plugin)); server.getPluginManager().callEvent(new PluginEvent(Event.Type.PLUGIN_DISABLE, plugin));
files.remove(jPlugin.getDescription().getName()); loaders.remove(jPlugin.getDescription().getName());
if (cloader instanceof PluginClassLoader) { if (cloader instanceof PluginClassLoader) {
PluginClassLoader loader = (PluginClassLoader)cloader; PluginClassLoader loader = (PluginClassLoader)cloader;

View file

@ -21,10 +21,16 @@ public class PluginClassLoader extends URLClassLoader {
@Override @Override
protected Class<?> findClass(String name) throws ClassNotFoundException { protected Class<?> findClass(String name) throws ClassNotFoundException {
return findClass(name, true);
}
protected Class<?> findClass(String name, boolean checkGlobal) throws ClassNotFoundException {
Class<?> result = classes.get(name); Class<?> result = classes.get(name);
if (result == null) { if (result == null) {
if(checkGlobal) {
result = loader.getClassByName(name); result = loader.getClassByName(name);
}
if (result == null) { if (result == null) {
result = super.findClass(name); result = super.findClass(name);