Refactored Plugin into an interface, make Javaplugin and relevant code to attempt to actually load plugins

By: Dinnerbone <dinnerbone@dinnerbone.com>
This commit is contained in:
Bukkit/Spigot
2010-12-24 16:41:51 +00:00
parent e3d491491a
commit a28c9acb1b
10 changed files with 232 additions and 43 deletions

View File

@@ -0,0 +1,91 @@
package org.bukkit.plugin.java;
import java.io.File;
import org.bukkit.Server;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginLoader;
/**
* Represents a Java plugin
*/
public abstract class JavaPlugin implements Plugin {
private boolean isEnabled = false;
private final PluginLoader loader;
private final Server server;
private final File file;
private final PluginDescriptionFile description;
private final ClassLoader classLoader;
/**
* Constructs a new Java plugin instance
*
* @param pluginLoader PluginLoader that is responsible for this plugin
* @param instance Server instance that is running this plugin
* @param desc PluginDescriptionFile containing metadata on this plugin
* @param plugin File containing this plugin
* @param cLoader ClassLoader which holds this plugin
*/
protected JavaPlugin(PluginLoader pluginLoader, Server instance, PluginDescriptionFile desc, File plugin, ClassLoader cLoader) {
loader = pluginLoader;
server = instance;
file = plugin;
description = desc;
classLoader = cLoader;
}
/**
* Gets the associated PluginLoader responsible for this plugin
*
* @return PluginLoader that controls this plugin
*/
public final PluginLoader getPluginLoader() {
return loader;
}
/**
* Returns the Server instance currently running this plugin
*
* @return Server running this plugin
*/
public final Server getServer() {
return server;
}
/**
* Returns a value indicating whether or not this plugin is currently enabled
*
* @return true if this plugin is enabled, otherwise false
*/
public final boolean isEnabled() {
return isEnabled;
}
/**
* Returns the file which contains this plugin
*
* @return File containing this plugin
*/
protected File getFile() {
return file;
}
/**
* Returns the plugin.yaml file containing the details for this plugin
*
* @return Contents of the plugin.yaml file
*/
public PluginDescriptionFile getDescription() {
return description;
}
/**
* Returns the ClassLoader which holds this plugin
*
* @return ClassLoader holding this plugin
*/
protected ClassLoader getClassLoader() {
return classLoader;
}
}

View File

@@ -2,10 +2,19 @@
package org.bukkit.plugin.java;
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Constructor;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginLoader;
import java.util.regex.Pattern;
import org.bukkit.Server;
import org.bukkit.plugin.InvalidPluginException;
import org.bukkit.plugin.PluginDescriptionFile;
/**
* Represents a Java plugin loader, allowing plugins in the form of .jars
@@ -32,8 +41,26 @@ public final class JavaPluginLoader implements PluginLoader {
throw new UnsupportedOperationException("Not supported yet.");
}
public Plugin loadPlugin(File file) {
throw new UnsupportedOperationException("Not supported yet.");
public Plugin loadPlugin(File file) throws InvalidPluginException {
JavaPlugin result = null;
PluginDescriptionFile description = new PluginDescriptionFile("Sample Plugin", "org.bukkit.plugin.sample.main");
if (!file.exists()) {
throw new InvalidPluginException(new FileNotFoundException(String.format("%s does not exist", file.getPath())));
}
try {
ClassLoader loader = URLClassLoader.newInstance(new URL[]{file.toURI().toURL()}, getClass().getClassLoader());
Class<?> jarClass = Class.forName(description.getMain());
Class<? extends JavaPlugin> plugin = jarClass.asSubclass(JavaPlugin.class);
Constructor<? extends JavaPlugin> constructor = plugin.getConstructor(PluginLoader.class, Server.class, PluginDescriptionFile.class, File.class, ClassLoader.class);
result = constructor.newInstance(this, server, description, file, loader);
} catch (Exception ex) {
throw new InvalidPluginException(ex);
}
return (Plugin)result;
}
public Pattern[] getPluginFileFilters() {