diff options
author | Luck <git@lucko.me> | 2021-10-03 18:50:38 +0100 |
---|---|---|
committer | Luck <git@lucko.me> | 2021-10-03 18:50:38 +0100 |
commit | 347095e3b689ed8cedaaf5e0a76282964058ff12 (patch) | |
tree | 2a764814a9fa0016bde7a9f94ace375c6df3ef9f /spark-waterdog/src/main/java/me/lucko/spark/waterdog/WaterdogClassSourceLookup.java | |
parent | dfd397d90a98c9edb110dcfdf4098a4350fa15ac (diff) | |
download | spark-347095e3b689ed8cedaaf5e0a76282964058ff12.tar.gz spark-347095e3b689ed8cedaaf5e0a76282964058ff12.tar.bz2 spark-347095e3b689ed8cedaaf5e0a76282964058ff12.zip |
Waterdog support
Diffstat (limited to 'spark-waterdog/src/main/java/me/lucko/spark/waterdog/WaterdogClassSourceLookup.java')
-rw-r--r-- | spark-waterdog/src/main/java/me/lucko/spark/waterdog/WaterdogClassSourceLookup.java | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/spark-waterdog/src/main/java/me/lucko/spark/waterdog/WaterdogClassSourceLookup.java b/spark-waterdog/src/main/java/me/lucko/spark/waterdog/WaterdogClassSourceLookup.java new file mode 100644 index 0000000..36e6a57 --- /dev/null +++ b/spark-waterdog/src/main/java/me/lucko/spark/waterdog/WaterdogClassSourceLookup.java @@ -0,0 +1,62 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.waterdog; + +import me.lucko.spark.common.util.ClassSourceLookup; + +import dev.waterdog.waterdogpe.ProxyServer; +import dev.waterdog.waterdogpe.plugin.Plugin; +import dev.waterdog.waterdogpe.plugin.PluginClassLoader; + +import java.util.Map; +import java.util.WeakHashMap; + +public class WaterdogClassSourceLookup extends ClassSourceLookup.ByClassLoader { + private final ProxyServer proxy; + private final Map<ClassLoader, String> cache; + + public WaterdogClassSourceLookup(ProxyServer proxy) { + this.proxy = proxy; + this.cache = new WeakHashMap<>(); + } + + @Override + public String identify(ClassLoader loader) throws ReflectiveOperationException { + if (loader instanceof PluginClassLoader) { + String name = this.cache.get(loader); + if (name != null) { + return name; + } + + for (Plugin plugin : this.proxy.getPluginManager().getPlugins()) { + if (plugin.getClass().getClassLoader() == loader) { + name = plugin.getName(); + break; + } + } + + this.cache.put(loader, name); + return name; + } + return null; + } +} + |