-
-
Notifications
You must be signed in to change notification settings - Fork 756
Implement new plugin system using PF4J #1111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DRSchlaubi
wants to merge
19
commits into
lavalink-devs:master
Choose a base branch
from
DRSchlaubi:feature/new-plugin-system
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c3e81e5
Implement prototype, which is able to load legacy plugins
DRSchlaubi a007e2a
Add pf4j to version catalog
DRSchlaubi 964cea3
Update LavalinkServer/src/main/java/lavalink/server/bootstrap/PluginC…
DRSchlaubi 4739ab6
Make plugin manager accessible from API
DRSchlaubi 0aa7664
Add implementation for new plugins
DRSchlaubi 43d609e
Remove javax
DRSchlaubi 8302f7a
Correctly autowire configuration properties
DRSchlaubi 3142d49
Correctly honor extension ordinals
DRSchlaubi 0a144a3
Cleanup
DRSchlaubi b4b53ad
Remove outdated configurable code
DRSchlaubi 28774dc
Remove unused import
DRSchlaubi 6bd1acb
Register lavalink internal extensions
DRSchlaubi 6a83e3a
Add VersionManager
DRSchlaubi 91e65de
Add clarification for ClassLoadingStrategy
DRSchlaubi 1f8c46e
Add support for development mode
DRSchlaubi ece06b4
Add DevelopmentPluginLoader
DRSchlaubi 97a8421
Enable extension dependency checking
DRSchlaubi 9c1a22c
Load configurations from configurations.idx
DRSchlaubi 41f9c1f
Cleanup
DRSchlaubi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
LavalinkServer/src/main/java/lavalink/server/bootstrap/PluginClassWrapper.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package lavalink.server.bootstrap | ||
|
|
||
| import org.pf4j.Plugin | ||
| import org.pf4j.PluginFactory | ||
| import org.pf4j.PluginWrapper | ||
|
|
||
| class PluginClassWrapper(@get:JvmName("getLavalinkContext") val wrapper: PluginWrapper) : Plugin() | ||
|
|
||
| object LavalinkPluginFactory : PluginFactory { | ||
| override fun create(pluginWrapper: PluginWrapper): Plugin = PluginClassWrapper(pluginWrapper) | ||
| } |
138 changes: 138 additions & 0 deletions
138
LavalinkServer/src/main/java/lavalink/server/bootstrap/PluginCompomentClassLoader.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| package lavalink.server.bootstrap | ||
|
|
||
| import org.pf4j.PluginWrapper | ||
| import org.pf4j.util.FileUtils | ||
| import org.slf4j.LoggerFactory | ||
| import java.io.InputStream | ||
| import java.net.URL | ||
| import java.nio.file.Path | ||
| import java.util.Enumeration | ||
| import java.util.stream.Stream | ||
| import kotlin.io.path.isDirectory | ||
| import kotlin.io.path.listDirectoryEntries | ||
|
|
||
| private val LOG = LoggerFactory.getLogger(PluginComponentClassLoader::class.java) | ||
|
|
||
| /** | ||
| * Implementation of [ClassLoader] which is aware of separate plugin class loaders. | ||
| */ | ||
| class PluginComponentClassLoader(pluginLoader: PluginLoader) : ClassLoader() { | ||
|
|
||
| // This list does not necessarily need to be up to date | ||
| // it just acts as a fast path to resolve these names more quickly | ||
| private val systemPackages = listOf( | ||
| "java", | ||
| "javax", | ||
| "lavalink", | ||
| "kotlin", | ||
| "koltinx", | ||
| "jarkarte", | ||
| "git", | ||
| "org/springframework", | ||
| "org/jetbrains/kotlin", | ||
| "org/pf4j", | ||
| "com/sedmelluq", | ||
| "dev/arbjerg", | ||
| "io/prometheus", | ||
| "club/minnced", | ||
| "ch/qos/logback", | ||
| "io/sentry", | ||
| "com/github/oshi", | ||
| "freemarker", | ||
| "com/samskivert", | ||
| "groovy", | ||
| "org/thymeleaf/spring6", | ||
| "org/apache/jasper", | ||
| "org/aspectj", | ||
| "com/fasterxml/jackson/", | ||
| "javax", | ||
DRSchlaubi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "com/hazelcast", | ||
| "com/couchbase", | ||
| "org/infinispan/spring", | ||
| "org/cache2k", | ||
| "com/github/benmanes/caffeine/cache", | ||
| "com/fasterxml/jackson/dataformat/xml", | ||
| "io/r2dbc/spi", | ||
| "reactor", | ||
| "org/eclipse/jetty", | ||
| "org/apache/catalina", | ||
| "org/apache/coyote", | ||
| "freemarker/template", | ||
| "com/samskivert/mustache", | ||
| "org/thymeleaf/spring6", | ||
| "org/apache/jasper/compiler", | ||
| "com/google/gson", | ||
| "META-INF" | ||
| ) | ||
|
|
||
| private val cache = pluginLoader.plugins | ||
| .associateBy { (it.descriptor as LavalinkPluginDescriptor).path.replace('.', '/') } | ||
| .toMutableMap() | ||
|
|
||
| // Due to the nature of the legacy class loading some plugins might produce packages outside their defined paths | ||
| // For this reason we need to build a map of those packages | ||
| private val legacyCache = pluginLoader.plugins | ||
| .asSequence() | ||
| .filter { (it.descriptor as LavalinkPluginDescriptor).manifestVersion == PluginDescriptor.Version.V1 } | ||
| .map { it to findClassesProvidedByPlugin(it.pluginPath) } | ||
| .toList() | ||
|
|
||
| fun findClassesProvidedByPlugin(pluginPath: Path, vararg path: String): List<Path> { | ||
| val fixPath = FileUtils.getPath(pluginPath, "", *path) | ||
| val childPaths = fixPath.listDirectoryEntries() | ||
| .filter { it.isDirectory() } | ||
| return (childPaths + childPaths | ||
| .flatMap { | ||
| findClassesProvidedByPlugin(pluginPath, *path, it.fileName.toString()) | ||
| }) | ||
| // Filter out top-level directories | ||
| .filter { it.toString().contains('/') } | ||
| } | ||
|
|
||
|
|
||
| private fun findLegacyPackage(name: String): PluginWrapper? { | ||
| return legacyCache.firstOrNull { (_, classes) -> | ||
| classes.any { name.startsWith(it.toString()) } | ||
| }?.first ?: run { | ||
| val newPath = name.substringBeforeLast('/') | ||
| if (newPath == name) return null | ||
| findLegacyPackage(newPath) | ||
| } | ||
| } | ||
|
|
||
| private fun findPackage(pack: String): PluginWrapper? { | ||
| return cache.toList().find { (key, _) -> | ||
| pack.startsWith(key) | ||
| }?.second ?: run { | ||
| val newPath = pack.substringBeforeLast('/') | ||
| if (newPath == pack) return null | ||
| findPackage(newPath) | ||
| } | ||
| } | ||
|
|
||
| private fun findClassLoader(name: String?): ClassLoader? { | ||
| if (name == null) return null | ||
| // Sometimes .class files are requested, as paths | ||
| // other times the class name gets requested | ||
| val pack = if (name.endsWith(".class")) { | ||
| name.substringBeforeLast('/') | ||
| } else { | ||
| name.substringBeforeLast('.').replace('.', '/') | ||
| } | ||
| if (systemPackages.any { pack.startsWith(it) }) return javaClass.classLoader | ||
|
|
||
| LOG.debug("Found package of {} to be {}", name, pack) | ||
| val plugin = cache.getOrPut(pack) { findPackage(pack) } ?: findLegacyPackage(name) | ||
| ?: return javaClass.classLoader | ||
|
|
||
| LOG.debug("Found class {} to be provided by plugin {}", name, plugin.pluginId) | ||
|
|
||
| return plugin.pluginClassLoader | ||
| } | ||
|
|
||
| override fun loadClass(name: String?): Class<*>? = findClassLoader(name)?.loadClass(name) | ||
| override fun getResource(name: String?): URL? = findClassLoader(name)?.getResource(name) | ||
| override fun getResources(name: String?): Enumeration<URL?>? = findClassLoader(name)?.getResources(name) | ||
| override fun getResourceAsStream(name: String?): InputStream? = findClassLoader(name)?.getResourceAsStream(name) | ||
| override fun resources(name: String?): Stream<URL?>? = findClassLoader(name)?.resources(name) | ||
| } | ||
62 changes: 62 additions & 0 deletions
62
LavalinkServer/src/main/java/lavalink/server/bootstrap/PluginLoader.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package lavalink.server.bootstrap | ||
|
|
||
| import lavalink.server.info.AppInfo | ||
| import org.pf4j.ClassLoadingStrategy | ||
| import org.pf4j.CompoundPluginDescriptorFinder | ||
| import org.pf4j.CompoundPluginLoader | ||
| import org.pf4j.DefaultPluginLoader | ||
| import org.pf4j.DefaultPluginManager | ||
| import org.pf4j.DevelopmentPluginLoader | ||
| import org.pf4j.PluginLoader as BasePluginLoader | ||
| import org.pf4j.PluginClassLoader | ||
| import org.pf4j.PluginDescriptor | ||
| import org.pf4j.PluginDescriptorFinder | ||
| import org.pf4j.PluginFactory | ||
| import org.springframework.stereotype.Component | ||
| import java.nio.file.Path | ||
| import java.util.jar.Manifest | ||
| import kotlin.io.path.Path | ||
| import kotlin.io.path.extension | ||
|
|
||
| @Component | ||
| class PluginLoader(pluginsConfig: PluginsConfig, private val appInfo: AppInfo) : DefaultPluginManager(Path(pluginsConfig.pluginsDir)) { | ||
|
|
||
| override fun getSystemVersion(): String = appInfo.versionBuild | ||
| override fun createPluginFactory(): PluginFactory = LavalinkPluginFactory | ||
| override fun createPluginLoader(): BasePluginLoader = CompoundPluginLoader() | ||
| .add(DevelopmentPluginLoader(this), this::isDevelopment) | ||
| .add(DefaultPluginLoader(this)) | ||
| .add(LegacyPluginLoader()) | ||
|
|
||
| override fun createPluginDescriptorFinder(): PluginDescriptorFinder? { | ||
| return CompoundPluginDescriptorFinder().apply { | ||
| add(LegacyLavalinkDescriptorFinder) | ||
| add(LavalinkDescriptorFinder) | ||
| } | ||
| } | ||
|
|
||
| private inner class LegacyPluginLoader : BasePluginLoader { | ||
| override fun isApplicable(pluginPath: Path): Boolean = pluginPath.extension == "jar" | ||
| override fun loadPlugin(pluginPath: Path, pluginDescriptor: PluginDescriptor): ClassLoader? { | ||
| val pluginClassLoader = PluginClassLoader( | ||
| this@PluginLoader, pluginDescriptor, javaClass.getClassLoader(), | ||
| ClassLoadingStrategy.APD | ||
| ) | ||
| pluginClassLoader.addFile(pluginPath.toFile()) | ||
|
|
||
| return pluginClassLoader | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private inner class LavalinkPluginClassLoader(descriptor: lavalink.server.bootstrap.PluginDescriptor) : | ||
| PluginClassLoader( | ||
| this@PluginLoader, descriptor, javaClass.classLoader, | ||
| if (descriptor.manifestVersion == lavalink.server.bootstrap.PluginDescriptor.Version.V1) ClassLoadingStrategy.APD else ClassLoadingStrategy.PDA | ||
| ) { | ||
|
|
||
| init { | ||
| definePackage(descriptor.path, Manifest(), null) | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.