Skip to content
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

Implement new plugin system using PF4J #1111

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions LavalinkServer/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies {
implementation(projects.pluginApi) {
exclude(group = "org.springframework.boot", module = "spring-boot-starter-tomcat")
}
implementation(libs.pf4j)

implementation(libs.bundles.metrics)
implementation(libs.bundles.spring) {
Expand Down
9 changes: 6 additions & 3 deletions LavalinkServer/src/main/java/lavalink/server/Launcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
package lavalink.server

import com.sedmelluq.discord.lavaplayer.tools.PlayerLibrary
import lavalink.server.bootstrap.LavalinkPluginDescriptor
import lavalink.server.bootstrap.PluginComponentClassLoader
import lavalink.server.bootstrap.PluginManager
import lavalink.server.info.AppInfo
import lavalink.server.info.GitRepoState
Expand Down Expand Up @@ -126,7 +128,7 @@ object Launcher {
launchMain(parent, args)
}

private fun launchPluginBootstrap() = SpringApplication(PluginManager::class.java).run {
private fun launchPluginBootstrap() = SpringApplication(AppInfo::class.java, PluginManager::class.java).run {
setBannerMode(Banner.Mode.OFF)
webApplicationType = WebApplicationType.NONE
run()
Expand All @@ -135,15 +137,16 @@ object Launcher {
private fun launchMain(parent: ConfigurableApplicationContext, args: Array<String>) {
val pluginManager = parent.getBean(PluginManager::class.java)
val properties = Properties()
properties["componentScan"] = pluginManager.pluginManifests.map { it.path }
properties["componentScan"] = pluginManager.loader.plugins
.map { (it.descriptor as LavalinkPluginDescriptor).path }
.toMutableList().apply { add("lavalink.server") }

SpringApplicationBuilder()
.sources(LavalinkApplication::class.java)
.properties(properties)
.web(WebApplicationType.SERVLET)
.bannerMode(Banner.Mode.OFF)
.resourceLoader(DefaultResourceLoader(pluginManager.classLoader))
.resourceLoader(DefaultResourceLoader(PluginComponentClassLoader(pluginManager.loader)))
.listeners(
ApplicationListener { event: Any ->
when (event) {
Expand Down
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)
}
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",
DRSchlaubi marked this conversation as resolved.
Show resolved Hide resolved
"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.
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)
}
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)
}
}
}
Loading