diff options
author | Wesley Wolfe <weswolf@aol.com> | 2012-06-11 01:45:41 -0500 |
---|---|---|
committer | Wesley Wolfe <weswolf@aol.com> | 2012-06-15 23:48:09 -0500 |
commit | d21cc6a9997c1ae36d1c0a16de158dca1f3059e0 (patch) | |
tree | 395b7403aa5ffc4481588ff3ebbc1f1bea61ecaa /src/main/java | |
parent | d7ff46b10c2be998645b4937094359b8d7c928d4 (diff) | |
download | bukkit-d21cc6a9997c1ae36d1c0a16de158dca1f3059e0.tar bukkit-d21cc6a9997c1ae36d1c0a16de158dca1f3059e0.tar.gz bukkit-d21cc6a9997c1ae36d1c0a16de158dca1f3059e0.tar.lz bukkit-d21cc6a9997c1ae36d1c0a16de158dca1f3059e0.tar.xz bukkit-d21cc6a9997c1ae36d1c0a16de158dca1f3059e0.zip |
Shift plugin initialization; Addresses BUKKIT-1788
Diffstat (limited to 'src/main/java')
3 files changed, 37 insertions, 4 deletions
diff --git a/src/main/java/org/bukkit/plugin/java/JavaPlugin.java b/src/main/java/org/bukkit/plugin/java/JavaPlugin.java index b7ecc181..188a6143 100644 --- a/src/main/java/org/bukkit/plugin/java/JavaPlugin.java +++ b/src/main/java/org/bukkit/plugin/java/JavaPlugin.java @@ -49,7 +49,12 @@ public abstract class JavaPlugin extends PluginBase { private File configFile = null; private PluginLogger logger = null; - public JavaPlugin() {} + public JavaPlugin() { + ClassLoader classLoader = this.getClass().getClassLoader(); + if (classLoader instanceof PluginClassLoader) { + ((PluginClassLoader) classLoader).initialize(this); + } + } /** * Returns the folder that the plugin data's files are located in. The diff --git a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java index 38ca9422..6ddcc1d5 100644 --- a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java +++ b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java @@ -134,7 +134,7 @@ public class JavaPluginLoader implements PluginLoader { loader = loaders.get(description.getClassLoaderOf()); loader.addURL(urls[0]); } else { - loader = new PluginClassLoader(this, urls, getClass().getClassLoader()); + loader = new PluginClassLoader(this, urls, getClass().getClassLoader(), server, description, dataFolder, file); } Class<?> jarClass = Class.forName(description.getMain(), true, loader); @@ -143,8 +143,6 @@ public class JavaPluginLoader implements PluginLoader { Constructor<? extends JavaPlugin> constructor = plugin.getConstructor(); result = constructor.newInstance(); - - result.initialize(this, server, description, dataFolder, file, loader); } catch (InvocationTargetException ex) { throw new InvalidPluginException(ex.getCause()); } catch (Throwable ex) { diff --git a/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java b/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java index dd7bffc5..6a9b4bba 100644 --- a/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java +++ b/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java @@ -1,22 +1,43 @@ package org.bukkit.plugin.java; +import java.io.File; import java.net.URL; import java.net.URLClassLoader; import java.util.HashMap; import java.util.Map; import java.util.Set; +import org.apache.commons.lang.Validate; +import org.bukkit.Server; +import org.bukkit.plugin.PluginDescriptionFile; + /** * A ClassLoader for plugins, to allow shared classes across multiple plugins */ public class PluginClassLoader extends URLClassLoader { private final JavaPluginLoader loader; private final Map<String, Class<?>> classes = new HashMap<String, Class<?>>(); + private final PluginDescriptionFile description; + private final Server server; + private final File dataFolder; + private final File file; + /** + * Changed in favor of class loader that initializes plugins + */ + @Deprecated public PluginClassLoader(final JavaPluginLoader loader, final URL[] urls, final ClassLoader parent) { + this(loader, urls, parent, null, null, null, null); + } + + public PluginClassLoader(final JavaPluginLoader loader, final URL[] urls, final ClassLoader parent, final Server server, final PluginDescriptionFile description, final File dataFolder, final File file) { super(urls, parent); this.loader = loader; + this.server = server; + this.description = description; + this.dataFolder = dataFolder; + this.file = file; } @Override @@ -54,4 +75,13 @@ public class PluginClassLoader extends URLClassLoader { public Set<String> getClasses() { return classes.keySet(); } + + void initialize(JavaPlugin javaPlugin) { + Validate.notNull(javaPlugin, "Initializing plugin cannot be null"); + Validate.isTrue(javaPlugin.getClass().getClassLoader() == this, "Cannot initialize plugin outside of this class loader"); + + if (server != null && description != null && dataFolder != null && file != null) { + javaPlugin.initialize(loader, server, description, dataFolder, file, this); + } + } } |