diff options
author | md_5 <git@md-5.net> | 2017-12-28 14:49:21 +1100 |
---|---|---|
committer | md_5 <git@md-5.net> | 2017-12-28 14:49:21 +1100 |
commit | c29791a82d9350af40164b501f6ca26d00e2ccba (patch) | |
tree | 58a15280226cb386e6c7460f88b549496f4ee0e0 | |
parent | c1a0994aa668087b94784576ea9fb45505db9f1d (diff) | |
download | bukkit-c29791a82d9350af40164b501f6ca26d00e2ccba.tar bukkit-c29791a82d9350af40164b501f6ca26d00e2ccba.tar.gz bukkit-c29791a82d9350af40164b501f6ca26d00e2ccba.tar.lz bukkit-c29791a82d9350af40164b501f6ca26d00e2ccba.tar.xz bukkit-c29791a82d9350af40164b501f6ca26d00e2ccba.zip |
Backport some PluginClassLoader changes for soak testing.
-rw-r--r-- | pom.xml | 4 | ||||
-rw-r--r-- | src/main/java/org/bukkit/plugin/java/PluginClassLoader.java | 55 |
2 files changed, 55 insertions, 4 deletions
@@ -13,8 +13,8 @@ <description>A plugin API for Minecraft servers.</description> <properties> - <maven.compiler.source>1.6</maven.compiler.source> - <maven.compiler.target>1.6</maven.compiler.target> + <maven.compiler.source>1.7</maven.compiler.source> + <maven.compiler.target>1.7</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> diff --git a/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java b/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java index 13f86338..205468cf 100644 --- a/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java +++ b/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java @@ -1,12 +1,20 @@ package org.bukkit.plugin.java; +import com.google.common.io.ByteStreams; import java.io.File; +import java.io.IOException; +import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; +import java.security.CodeSigner; +import java.security.CodeSource; import java.util.HashMap; import java.util.Map; import java.util.Set; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; +import java.util.jar.Manifest; import org.apache.commons.lang.Validate; import org.bukkit.plugin.InvalidPluginException; @@ -21,11 +29,14 @@ final class PluginClassLoader extends URLClassLoader { private final PluginDescriptionFile description; private final File dataFolder; private final File file; + private final JarFile jar; + private final Manifest manifest; + private final URL url; final JavaPlugin plugin; private JavaPlugin pluginInit; private IllegalStateException pluginState; - PluginClassLoader(final JavaPluginLoader loader, final ClassLoader parent, final PluginDescriptionFile description, final File dataFolder, final File file) throws InvalidPluginException, MalformedURLException { + PluginClassLoader(final JavaPluginLoader loader, final ClassLoader parent, final PluginDescriptionFile description, final File dataFolder, final File file) throws IOException, InvalidPluginException, MalformedURLException { super(new URL[] {file.toURI().toURL()}, parent); Validate.notNull(loader, "Loader cannot be null"); @@ -33,6 +44,9 @@ final class PluginClassLoader extends URLClassLoader { this.description = description; this.dataFolder = dataFolder; this.file = file; + this.jar = new JarFile(file, true); + this.manifest = jar.getManifest(); + this.url = file.toURI().toURL(); try { Class<?> jarClass; @@ -74,7 +88,35 @@ final class PluginClassLoader extends URLClassLoader { } if (result == null) { - result = super.findClass(name); + String path = name.replace('.', '/').concat(".class"); + JarEntry entry = jar.getJarEntry(path); + + if (entry != null) { + byte[] classBytes; + + try (InputStream is = jar.getInputStream(entry)) { + classBytes = ByteStreams.toByteArray(is); + } catch (IOException ex) { + throw new ClassNotFoundException(name, ex); + } + + int dot = name.lastIndexOf('.'); + if (dot != -1) { + String pkgName = name.substring(0, dot); + if (getPackage(pkgName) == null) { + definePackage(pkgName, manifest, url); + } + } + + CodeSigner[] signers = entry.getCodeSigners(); + CodeSource source = new CodeSource(url, signers); + + result = defineClass(name, classBytes, 0, classBytes.length, source); + } + + if (result == null) { + result = super.findClass(name); + } if (result != null) { loader.setClass(name, result); @@ -87,6 +129,15 @@ final class PluginClassLoader extends URLClassLoader { return result; } + @Override + public void close() throws IOException { + try { + super.close(); + } finally { + jar.close(); + } + } + Set<String> getClasses() { return classes.keySet(); } |