Apply dependency remapping using an isolated classloader

* Fixes the issue which prevented LP from loading on Java 9
* Should also fix #697
This commit is contained in:
Luck 2018-01-19 19:27:08 +00:00
parent 04ffd00c0e
commit 03a93b1186
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
10 changed files with 241 additions and 127 deletions

View File

@ -50,10 +50,6 @@
<pattern>net.kyori.text</pattern>
<shadedPattern>me.lucko.luckperms.lib.text</shadedPattern>
</relocation>
<relocation>
<pattern>me.lucko.jarrelocator</pattern>
<shadedPattern>me.lucko.luckperms.lib.jarrelocator</shadedPattern>
</relocation>
<!-- relocated dependencies -->
<relocation>
@ -93,9 +89,13 @@
<shadedPattern>me.lucko.luckperms.lib.bson</shadedPattern>
</relocation>
<relocation>
<pattern>redis.clients.jedis.shaded</pattern>
<pattern>redis.clients.jedis</pattern>
<shadedPattern>me.lucko.luckperms.lib.jedis</shadedPattern>
</relocation>
<relocation>
<pattern>org.apache.commons.pool2</pattern>
<shadedPattern>me.lucko.luckperms.lib.commonspool2</shadedPattern>
</relocation>
<relocation>
<pattern>ninja.leaping.configurate</pattern>
<shadedPattern>me.lucko.luckperms.lib.configurate</shadedPattern>

View File

@ -50,10 +50,6 @@
<pattern>net.kyori.text</pattern>
<shadedPattern>me.lucko.luckperms.lib.text</shadedPattern>
</relocation>
<relocation>
<pattern>me.lucko.jarrelocator</pattern>
<shadedPattern>me.lucko.luckperms.lib.jarrelocator</shadedPattern>
</relocation>
<!-- relocated dependencies -->
<relocation>
@ -93,9 +89,13 @@
<shadedPattern>me.lucko.luckperms.lib.bson</shadedPattern>
</relocation>
<relocation>
<pattern>redis.clients.jedis.shaded</pattern>
<pattern>redis.clients.jedis</pattern>
<shadedPattern>me.lucko.luckperms.lib.jedis</shadedPattern>
</relocation>
<relocation>
<pattern>org.apache.commons.pool2</pattern>
<shadedPattern>me.lucko.luckperms.lib.commonspool2</shadedPattern>
</relocation>
<relocation>
<pattern>ninja.leaping.configurate</pattern>
<shadedPattern>me.lucko.luckperms.lib.configurate</shadedPattern>

View File

@ -44,24 +44,6 @@
<scope>provided</scope>
</dependency>
<!-- jar-relocator -->
<dependency>
<groupId>me.lucko</groupId>
<artifactId>jar-relocator</artifactId>
<version>1.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
</exclusion>
<exclusion>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-commons</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- text -->
<dependency>
<groupId>net.kyori</groupId>
@ -101,7 +83,7 @@
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
<scope>provided</scope>
<scope>compile</scope>
</dependency>
<!-- caffeine -->
<dependency>
@ -169,7 +151,7 @@
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.1-shaded</version>
<version>2.9.0</version>
<scope>provided</scope>
</dependency>
<!-- MongoDB -->

View File

@ -28,8 +28,7 @@ package me.lucko.luckperms.common.dependencies;
import com.google.common.collect.ImmutableList;
import com.google.common.io.ByteStreams;
import me.lucko.jarrelocator.Relocation;
import me.lucko.luckperms.common.dependencies.relocation.Relocations;
import me.lucko.luckperms.common.dependencies.relocation.Relocation;
import java.io.InputStream;
import java.net.URL;
@ -53,55 +52,61 @@ public enum Dependency {
"5.2",
"zBMYiX4sdxy3l6aNX06mQcI6UfBDfKUXq+z5ZN2yZAs="
),
JAR_RELOCATOR(
"me.lucko",
"jar-relocator",
"1.2",
"ECR0wrAMwmM0dpmuY1ifCG+2rpObOIlSI127jBbSrbI="
),
CAFFEINE(
"com{}github{}ben-manes{}caffeine",
"caffeine",
"2.6.0",
"JmT16VQnCnVBAjRJCQkkkjmSVx2jajpzeBuKwpbzDA8=",
Relocations.of("caffeine", "com{}github{}benmanes{}caffeine")
Relocation.of("caffeine", "com{}github{}benmanes{}caffeine")
),
MARIADB_DRIVER(
"org{}mariadb{}jdbc",
"mariadb-java-client",
"2.2.0",
"/q0LPGHrp3L9rvKr7TuA6urbtXBqvXis92mP4KhxzUw=",
Relocations.of("mariadb", "org{}mariadb{}jdbc")
Relocation.of("mariadb", "org{}mariadb{}jdbc")
),
MYSQL_DRIVER(
"mysql",
"mysql-connector-java",
"5.1.44",
"d4RZVzTeWpoHBPB/tQP3mSafNy7L9MDUSOt4Ku9LGCc=",
Relocations.of("mysql", "com{}mysql")
Relocation.of("mysql", "com{}mysql")
),
POSTGRESQL_DRIVER(
"org{}postgresql",
"postgresql",
"9.4.1212",
"DLKhWL4xrPIY4KThjI89usaKO8NIBkaHc/xECUsMNl0=",
Relocations.of("postgresql", "org{}postgresql")
Relocation.of("postgresql", "org{}postgresql")
),
H2_DRIVER(
"com.h2database",
"h2",
"1.4.196",
"CgX0oNW4WEAUiq3OY6QjtdPDbvRHVjibT6rQjScz+vU=",
Relocations.of("h2", "org{}h2")
Relocation.of("h2", "org{}h2")
),
SQLITE_DRIVER(
"org.xerial",
"sqlite-jdbc",
"3.21.0",
"bglRaH4Y+vQFZV7TfOdsVLO3rJpauJ+IwjuRULAb45Y=",
Relocations.of("sqlite", "org{}sqlite")
Relocation.of("sqlite", "org{}sqlite")
),
HIKARI(
"com{}zaxxer",
"HikariCP",
"2.7.4",
"y9JE6/VmbydCqlV1z468+oqdkBswBk6aw+ECT178AT4=",
Relocations.of("hikari", "com{}zaxxer{}hikari")
Relocation.of("hikari", "com{}zaxxer{}hikari")
),
SLF4J_SIMPLE(
"org.slf4j",
@ -121,36 +126,47 @@ public enum Dependency {
"3.5.0",
"gxrbKVSI/xM6r+6uL7g7I0DzNV+hlNTtfw4UL13XdK8=",
ImmutableList.<Relocation>builder()
.addAll(Relocations.of("mongodb", "com{}mongodb"))
.addAll(Relocations.of("bson", "org{}bson"))
.addAll(Relocation.of("mongodb", "com{}mongodb"))
.addAll(Relocation.of("bson", "org{}bson"))
.build()
),
JEDIS(
"https://github.com/lucko/jedis/releases/download/jedis-2.9.1-shaded/jedis-2.9.1-shaded.jar",
"2.9.1-shaded",
"mM19X6LyD97KP4RSbcCR5BTRAwQ0x9y02voX7ePOSjE=",
Relocations.of("jedis", "redis{}clients{}jedis{}shaded")
"redis.clients",
"jedis",
"2.9.0",
"HqqWy45QVeTVF0Z/DzsrPLvGKn2dHotqI8YX7GDThvo=",
ImmutableList.<Relocation>builder()
.addAll(Relocation.of("jedis", "redis{}clients{}jedis"))
.addAll(Relocation.of("commonspool2", "org{}apache{}commons{}pool2"))
.build()
),
COMMONS_POOL_2(
"org.apache.commons",
"commons-pool2",
"2.4.2",
"IREqpnNzPfzQRTVN33WzHh1GS5nI5RWXQ0myUyJUzFM=",
Relocation.of("commonspool2", "org{}apache{}commons{}pool2")
),
CONFIGURATE_CORE(
"ninja{}leaping{}configurate",
"configurate-core",
"3.3",
"4leBJEqj1kVszaifZeKNl4hgHxG5M+Nk5TJKkPW2s4Y=",
Relocations.of("configurate", "ninja{}leaping{}configurate")
Relocation.of("configurate", "ninja{}leaping{}configurate")
),
CONFIGURATE_GSON(
"ninja{}leaping{}configurate",
"configurate-gson",
"3.3",
"4HxrW3/ZKdn095x/W4gylQMNskdmteXYVxVv0UKGJA4=",
Relocations.of("configurate", "ninja{}leaping{}configurate")
Relocation.of("configurate", "ninja{}leaping{}configurate")
),
CONFIGURATE_YAML(
"ninja{}leaping{}configurate",
"configurate-yaml",
"3.3",
"hgADp3g+xHHPD34bAuxMWtB+OQ718Tlw69jVp2KPJNk=",
Relocations.of("configurate", "ninja{}leaping{}configurate")
Relocation.of("configurate", "ninja{}leaping{}configurate")
),
CONFIGURATE_HOCON(
"ninja{}leaping{}configurate",
@ -158,8 +174,8 @@ public enum Dependency {
"3.3",
"UIy5FVmsBUG6+Z1mpIEE2EXgtOI1ZL0p/eEW+BbtGLU=",
ImmutableList.<Relocation>builder()
.addAll(Relocations.of("configurate", "ninja{}leaping{}configurate"))
.addAll(Relocations.of("hocon", "com{}typesafe{}config"))
.addAll(Relocation.of("configurate", "ninja{}leaping{}configurate"))
.addAll(Relocation.of("hocon", "com{}typesafe{}config"))
.build()
),
HOCON_CONFIG(
@ -167,7 +183,7 @@ public enum Dependency {
"config",
"1.3.1",
"5vrfxhCCINOmuGqn5OFsnnu4V7pYlViGMIuxOXImSvA=",
Relocations.of("hocon", "com{}typesafe{}config")
Relocation.of("hocon", "com{}typesafe{}config")
);
private final String url;

View File

@ -27,8 +27,8 @@ package me.lucko.luckperms.common.dependencies;
import com.google.common.io.ByteStreams;
import me.lucko.jarrelocator.JarRelocator;
import me.lucko.jarrelocator.Relocation;
import me.lucko.luckperms.common.dependencies.relocation.Relocation;
import me.lucko.luckperms.common.dependencies.relocation.RelocationHandler;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.storage.StorageType;
@ -53,6 +53,7 @@ public class DependencyManager {
private final MessageDigest digest;
private final DependencyRegistry registry;
private final EnumSet<Dependency> alreadyLoaded = EnumSet.noneOf(Dependency.class);
private RelocationHandler relocationHandler = null;
public DependencyManager(LuckPermsPlugin plugin) {
this.plugin = plugin;
@ -64,23 +65,29 @@ public class DependencyManager {
this.registry = new DependencyRegistry(plugin);
}
private synchronized RelocationHandler getRelocationHandler() {
if (this.relocationHandler == null) {
this.relocationHandler = new RelocationHandler(this);
}
return this.relocationHandler;
}
public File getSaveDirectory() {
File saveDirectory = new File(this.plugin.getDataDirectory(), "lib");
if (!(saveDirectory.exists() || saveDirectory.mkdirs())) {
throw new RuntimeException("Unable to create lib dir - " + saveDirectory.getPath());
}
return saveDirectory;
}
public void loadStorageDependencies(Set<StorageType> storageTypes) {
loadDependencies(this.registry.resolveStorageDependencies(storageTypes));
}
public void loadDependencies(Set<Dependency> dependencies) {
loadDependencies(dependencies, true);
}
public void loadDependencies(Set<Dependency> dependencies, boolean applyRemapping) {
if (applyRemapping) {
this.plugin.getLog().info("Identified the following dependencies: " + dependencies.toString());
}
File saveDirectory = new File(this.plugin.getDataDirectory(), "lib");
if (!(saveDirectory.exists() || saveDirectory.mkdirs())) {
throw new RuntimeException("Unable to create lib dir - " + saveDirectory.getPath());
}
this.plugin.getLog().info("Identified the following dependencies: " + dependencies.toString());
File saveDirectory = getSaveDirectory();
// create a list of file sources
List<Source> sources = new ArrayList<>();
@ -102,53 +109,36 @@ public class DependencyManager {
// apply any remapping rules to the files
List<File> remappedJars = new ArrayList<>(sources.size());
if (applyRemapping) {
for (Source source : sources) {
try {
// apply remap rules
List<Relocation> relocations = source.dependency.getRelocations();
for (Source source : sources) {
try {
// apply remap rules
List<Relocation> relocations = source.dependency.getRelocations();
if (relocations.isEmpty()) {
remappedJars.add(source.file);
continue;
}
File input = source.file;
File output = new File(input.getParentFile(), "remap-" + input.getName());
// if the remapped file exists already, just use that.
if (output.exists()) {
remappedJars.add(output);
continue;
}
// make sure ASM is loaded
Set<Dependency> asmDepends = EnumSet.noneOf(Dependency.class);
if (!DependencyRegistry.asmPresent()) {
asmDepends.add(Dependency.ASM);
}
if (!DependencyRegistry.asmCommonsPresent()) {
asmDepends.add(Dependency.ASM_COMMONS);
}
if (!asmDepends.isEmpty()) {
// load asm before calling the jar relocator
loadDependencies(asmDepends, false);
}
// attempt to remap the jar.
this.plugin.getLog().info("Attempting to remap " + input.getName() + "...");
JarRelocator relocator = new JarRelocator(input, output, relocations);
relocator.run();
remappedJars.add(output);
} catch (Throwable e) {
this.plugin.getLog().severe("Unable to remap the source file '" + source.dependency.name() + "'.");
e.printStackTrace();
if (relocations.isEmpty()) {
remappedJars.add(source.file);
continue;
}
}
} else {
for (Source source : sources) {
remappedJars.add(source.file);
File input = source.file;
File output = new File(input.getParentFile(), "remap-" + input.getName());
// if the remapped file exists already, just use that.
if (output.exists()) {
remappedJars.add(output);
continue;
}
// init the relocation handler
RelocationHandler relocationHandler = getRelocationHandler();
// attempt to remap the jar.
this.plugin.getLog().info("Attempting to apply relocations to " + input.getName() + "...");
relocationHandler.remap(input, output, relocations);
remappedJars.add(output);
} catch (Throwable e) {
this.plugin.getLog().severe("Unable to remap the source file '" + source.dependency.name() + "'.");
e.printStackTrace();
}
}
@ -163,7 +153,7 @@ public class DependencyManager {
}
}
private File downloadDependency(File saveDirectory, Dependency dependency) throws Exception {
public File downloadDependency(File saveDirectory, Dependency dependency) throws Exception {
String fileName = dependency.name().toLowerCase() + "-" + dependency.getVersion() + ".jar";
File file = new File(saveDirectory, fileName);

View File

@ -65,6 +65,7 @@ public class DependencyRegistry {
}
if (this.plugin.getConfiguration().get(ConfigKeys.REDIS_ENABLED)) {
dependencies.add(Dependency.COMMONS_POOL_2);
dependencies.add(Dependency.JEDIS);
}

View File

@ -25,20 +25,33 @@
package me.lucko.luckperms.common.dependencies.relocation;
import me.lucko.jarrelocator.Relocation;
import java.util.ArrayList;
import java.util.List;
public final class Relocations {
public final class Relocation {
public static List<Relocation> of(String name, String... packages) {
List<Relocation> relocations = new ArrayList<>();
List<Relocation> ret = new ArrayList<>();
for (String p : packages) {
relocations.add(new Relocation(p.replace("{}", "."), "me.lucko.luckperms.lib." + name));
ret.add(new Relocation(p.replace("{}", "."), "me.lucko.luckperms.lib." + name));
}
return relocations;
return ret;
}
private final String pattern;
private final String relocatedPattern;
public Relocation(String pattern, String relocatedPattern) {
this.pattern = pattern;
this.relocatedPattern = relocatedPattern;
}
public String getPattern() {
return this.pattern;
}
public String getRelocatedPattern() {
return this.relocatedPattern;
}
private Relocations() {}
}

View File

@ -0,0 +1,112 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.dependencies.relocation;
import me.lucko.luckperms.common.dependencies.Dependency;
import me.lucko.luckperms.common.dependencies.DependencyManager;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Handles class runtime relocation of packages in downloaded dependencies
*/
public class RelocationHandler implements AutoCloseable {
private final DependencyManager dependencyManager;
private URLClassLoader classLoader;
private Constructor<?> relocatorConstructor;
private Method relocatorRunMethod;
public RelocationHandler(DependencyManager dependencyManager) {
this.dependencyManager = dependencyManager;
try {
setup();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void setup() throws Exception {
File saveDirectory = this.dependencyManager.getSaveDirectory();
// download the required dependencies for the remapping.
File asm = this.dependencyManager.downloadDependency(saveDirectory, Dependency.ASM);
File asmCommons = this.dependencyManager.downloadDependency(saveDirectory, Dependency.ASM_COMMONS);
File jarRelocator = this.dependencyManager.downloadDependency(saveDirectory, Dependency.JAR_RELOCATOR);
URL[] urls = new URL[]{
asm.toURI().toURL(),
asmCommons.toURI().toURL(),
jarRelocator.toURI().toURL()
};
// construct an isolated classloader instance containing the dependencies needed
this.classLoader = new URLClassLoader(urls, null);
// load the relocator class
Class<?> relocatorClass = this.classLoader.loadClass("me.lucko.jarrelocator.JarRelocator");
// prepare the the reflected constructor & method instances
this.relocatorConstructor = relocatorClass.getDeclaredConstructor(File.class, File.class, Map.class);
this.relocatorConstructor.setAccessible(true);
this.relocatorRunMethod = relocatorClass.getDeclaredMethod("run");
this.relocatorRunMethod.setAccessible(true);
}
public void remap(File input, File output, List<Relocation> relocations) throws Exception {
if (this.classLoader == null) {
throw new IllegalStateException("ClassLoader is closed");
}
Map<String, String> mappings = new HashMap<>();
for (Relocation relocation : relocations) {
mappings.put(relocation.getPattern(), relocation.getRelocatedPattern());
}
// create and invoke a new relocator
Object relocator = this.relocatorConstructor.newInstance(input, output, mappings);
this.relocatorRunMethod.invoke(relocator);
}
@Override
public void close() throws IOException {
if (this.classLoader == null) {
return;
}
this.classLoader.close();
this.classLoader = null;
}
}

View File

@ -27,10 +27,10 @@ package me.lucko.luckperms.common.messaging;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import redis.clients.jedis.shaded.Jedis;
import redis.clients.jedis.shaded.JedisPool;
import redis.clients.jedis.shaded.JedisPoolConfig;
import redis.clients.jedis.shaded.JedisPubSub;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisPubSub;
/**
* An implementation of {@link me.lucko.luckperms.api.MessagingService} using Redis.

View File

@ -50,10 +50,6 @@
<pattern>net.kyori.text</pattern>
<shadedPattern>me.lucko.luckperms.lib.text</shadedPattern>
</relocation>
<relocation>
<pattern>me.lucko.jarrelocator</pattern>
<shadedPattern>me.lucko.luckperms.lib.jarrelocator</shadedPattern>
</relocation>
<!-- relocated dependencies -->
<relocation>
@ -89,9 +85,13 @@
<shadedPattern>me.lucko.luckperms.lib.bson</shadedPattern>
</relocation>
<relocation>
<pattern>redis.clients.jedis.shaded</pattern>
<pattern>redis.clients.jedis</pattern>
<shadedPattern>me.lucko.luckperms.lib.jedis</shadedPattern>
</relocation>
<relocation>
<pattern>org.apache.commons.pool2</pattern>
<shadedPattern>me.lucko.luckperms.lib.commonspool2</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>