Skip to content

Commit

Permalink
Option to load plugins using the system loader.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stéphan Kochen committed Feb 25, 2011
1 parent 0661cc5 commit c650842
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
20 changes: 18 additions & 2 deletions src/main/java/org/bukkit/plugin/SimplePluginManager.java
Expand Up @@ -33,8 +33,10 @@
public final class SimplePluginManager implements PluginManager {
private final Server server;
private final File pluginFolder;
private final List<File> systemPlugins;
private final CommandMap commandMap;
private final List<PluginLoader> pluginLoaders = new ArrayList<PluginLoader>();
private final JavaPluginLoader javaPluginLoader;
private final Map<String, PluginDescription> pluginDescriptions = new HashMap<String, PluginDescription>();
private final Map<String, Plugin> plugins = new HashMap<String, Plugin>();
private final Map<Event.Type, SortedSet<RegisteredListener>> listeners = new EnumMap<Event.Type, SortedSet<RegisteredListener>>(Event.Type.class);
Expand All @@ -50,12 +52,13 @@ public int compare(RegisteredListener i, RegisteredListener j) {
}
};

public SimplePluginManager(Server server, File pluginFolder) {
public SimplePluginManager(Server server, File pluginFolder, List<File> systemPlugins) {
this.server = server;
this.pluginFolder = pluginFolder;
this.systemPlugins = systemPlugins;
this.commandMap = new SimpleCommandMap(server);

registerInterface(JavaPluginLoader.class);
javaPluginLoader = (JavaPluginLoader)registerInterface(JavaPluginLoader.class);
}

/**
Expand Down Expand Up @@ -95,6 +98,19 @@ public void rebuildIndex() {
for (PluginLoader loader : pluginLoaders) {
updateIndexForInterface(loader);
}

for (File file : systemPlugins) {
try {
PluginDescription description = javaPluginLoader.readSystemPluginDescription(file);
String name = description.getName();
if (pluginDescriptions.containsKey(name)) {
throw new InvalidDescriptionException("A plugin with this name already exists: " + name);
}
pluginDescriptions.put(name, description);
} catch (InvalidDescriptionException ex) {
log.log(Level.SEVERE, "Could not load " + file.getPath() + " in " + pluginFolder.getPath() + ": " + ex.getMessage(), ex);
}
}
}

/**
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/org/bukkit/plugin/java/JavaPluginDescription.java
Expand Up @@ -17,11 +17,14 @@

public final class JavaPluginDescription extends YamlPluginDescription {
private static final Logger log = Logger.getLogger(JavaPluginDescription.class.getName());
private final boolean systemPlugin;
private String main;
private ClassLoader classLoader = null;

public JavaPluginDescription(final PluginLoader loader, final File file, final InputStream stream) throws InvalidDescriptionException {
public JavaPluginDescription(final PluginLoader loader, final File file,
final InputStream stream, final boolean systemPlugin) throws InvalidDescriptionException {
super(loader, file, stream);
this.systemPlugin = systemPlugin;
}

/**
Expand All @@ -43,11 +46,13 @@ protected ClassLoader getClassLoader() {
final JavaPluginLoader pluginLoader = (JavaPluginLoader)getLoader();
final ClassLoader parentLoader = getClass().getClassLoader();
final ArrayList<URL> urls = new ArrayList<URL>();
try {
urls.add(getFile().toURI().toURL());
}
catch (MalformedURLException ex) {
log.log(Level.SEVERE, "Unable to turn JAR-path into URL", ex);
if (!systemPlugin) {
try {
urls.add(getFile().toURI().toURL());
}
catch (MalformedURLException ex) {
log.log(Level.SEVERE, "Unable to turn JAR-path into URL", ex);
}
}
classLoader = new PluginClassLoader(pluginLoader, urls.toArray(new URL[0]), parentLoader);
}
Expand Down
32 changes: 31 additions & 1 deletion src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
@@ -1,6 +1,7 @@
package org.bukkit.plugin.java;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -348,6 +349,9 @@ public void execute( Listener listener, Event event ) {
throw new IllegalArgumentException( "Event " + type + " is not supported" );
}

/**
* {@inheritDoc}
*/
public PluginDescription readDescription(File file) throws InvalidDescriptionException {
JavaPluginDescription result = null;

Expand All @@ -362,7 +366,7 @@ public PluginDescription readDescription(File file) throws InvalidDescriptionExc
}

InputStream stream = jar.getInputStream(entry);
result = new JavaPluginDescription(this, file, stream);
result = new JavaPluginDescription(this, file, stream, false);

stream.close();
jar.close();
Expand All @@ -372,6 +376,32 @@ public PluginDescription readDescription(File file) throws InvalidDescriptionExc
return result;
}

/**
* Similar to readDescription, but for system plugins.
*
* This builds a PluginDescription for a system plugin. Because a system
* plugin does not have a containing JAR file, the file points directly
* to the YAML description file.
*
* @param file The YAML description file
* @return A filled PluginDescription object
* @throws InvalidDescriptionException Thrown when the metadata was not understood
*/
public PluginDescription readSystemPluginDescription(File file) throws InvalidDescriptionException {
if (!file.exists()) {
throw new InvalidDescriptionException(new FileNotFoundException(String.format("%s does not exist", file.getPath())));
}
JavaPluginDescription result = null;
try {
InputStream stream = new FileInputStream(file);
result = new JavaPluginDescription(this, file, stream, true);
stream.close();
} catch (IOException ex) {
throw new InvalidDescriptionException(ex);
}
return result;
}

public Plugin enablePlugin(PluginDescription abstractDescription) throws InvalidPluginException {
JavaPluginDescription description = (JavaPluginDescription)abstractDescription;
JavaPlugin plugin = null;
Expand Down

0 comments on commit c650842

Please sign in to comment.