Added Java plugin loader + a plugin manager

By: Dinnerbone <dinnerbone@dinnerbone.com>
This commit is contained in:
Bukkit/Spigot 2010-12-24 10:31:01 +00:00
parent 1df4f74b79
commit 284ae0df7f
3 changed files with 125 additions and 2 deletions

View file

@ -2,6 +2,7 @@
package org.bukkit.plugin;
import java.io.File;
import java.util.regex.Pattern;
/**
* Represents a plugin loader, which provides access and management for all plugins
@ -38,11 +39,14 @@ public interface PluginLoader {
/**
* Loads the plugin contained in the specified file
*
* File must be a .jar and contain a valid plugin.yaml file
*
* @param file File to attempt to load
* @return Plugin that was contained in the specified file, or null if
* unsuccessful
*/
public Plugin loadPlugin(File file);
/**
* Returns a list of all filename filters expected by this PluginLoader
*/
public Pattern[] getPluginFileFilters();
}

View file

@ -0,0 +1,82 @@
package org.bukkit.plugin;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import org.bukkit.Server;
import java.util.regex.Pattern;
/**
* Handles all plugin management from the Server
*/
public final class PluginManager {
private final Server server;
private final HashMap<Pattern, PluginLoader> fileAssociations = new HashMap<Pattern, PluginLoader>();
public PluginManager(Server instance) {
server = instance;
}
/**
* Registers the specified plugin loader
*
* @param loader PluginLoader to register
*/
public void RegisterInterface(PluginLoader loader) {
Pattern[] patterns = loader.getPluginFileFilters();
for (Pattern pattern : patterns) {
fileAssociations.put(pattern, loader);
}
}
/**
* Loads the plugins contained within the specified directory
*
* @param directory Directory to check for plugins
* @return A list of all plugins loaded
*/
public Plugin[] loadPlugins(File directory) {
List<Plugin> result = new ArrayList<Plugin>();
File[] files = directory.listFiles();
for (File file : files) {
Plugin plugin = loadPlugin(file);
if (plugin != null) {
result.add(plugin);
}
}
return (Plugin[])result.toArray();
}
/**
* Loads the plugin in the specified file
*
* File must be valid according to the current enabled Plugin interfaces
*
* @param file File containing the plugin to load
* @return The Plugin loaded, or null if it was invalid
*/
public Plugin loadPlugin(File file) {
Set<Pattern> filters = fileAssociations.keySet();
Plugin result = null;
for (Pattern filter : filters) {
String name = file.getName();
Matcher match = filter.matcher(name);
if (match.find()) {
PluginLoader loader = fileAssociations.get(filter);
result = loader.loadPlugin(file);
}
}
return result;
}
}

View file

@ -0,0 +1,37 @@
package org.bukkit.plugin.java;
import java.io.File;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginLoader;
import java.util.regex.Pattern;
/**
* Represents a Java plugin loader, allowing plugins in the form of .jars
*/
public final class JavaPluginLoader implements PluginLoader {
private final Pattern[] fileFilters = new Pattern[] {
Pattern.compile("\\.jar$"),
};
public Plugin getPlugin(String name) {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean isPluginEnabled(String name) {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean isPluginEnabled(Plugin plugin) {
throw new UnsupportedOperationException("Not supported yet.");
}
public Plugin loadPlugin(File file) {
throw new UnsupportedOperationException("Not supported yet.");
}
public Pattern[] getPluginFileFilters() {
return fileFilters;
}
}