|
1 | 1 | package com.lambda.plugin |
2 | 2 |
|
3 | 3 | import com.lambda.Lambda.LOG |
4 | | -import com.lambda.core.Loadable |
5 | | -import com.lambda.util.FolderRegister.createIfNotExists |
6 | | -import com.lambda.util.FolderRegister.listRecursive |
7 | | -import com.lambda.util.FolderRegister.plugins |
8 | 4 | import java.io.File |
| 5 | +import java.lang.reflect.InvocationTargetException |
| 6 | +import java.lang.reflect.Method |
| 7 | +import java.net.URL |
9 | 8 | import java.util.jar.JarFile |
10 | 9 |
|
11 | | -object PluginRegistry : Loadable { |
12 | | - private fun loadPlugin(file: File) { |
13 | | - if (!file.name.endsWith(".jar")) return |
14 | | - if (file.length() == 0L) return LOG.error("The plugin $file is empty") |
| 10 | +object PluginRegistry { |
| 11 | + private val knotClassLoader: ClassLoader = Thread.currentThread().contextClassLoader |
| 12 | + private val addUrlMethod: Method |
| 13 | + |
| 14 | + private var loadClass: Class<*>? = null |
| 15 | + private var loadMethod: Method? = null |
| 16 | + private var loadInstance: Any? = null // TODO: This won't work with constructors |
| 17 | + |
| 18 | + init { |
| 19 | + try { |
| 20 | + addUrlMethod = knotClassLoader.javaClass.getMethod("addUrlFwd", URL::class.java) |
| 21 | + addUrlMethod.isAccessible = true |
| 22 | + } catch (e: NoSuchMethodException) { |
| 23 | + throw RuntimeException("Failed to get the addURL method from the KnotClassLoader.") |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + @Throws( |
| 28 | + IllegalAccessException::class, |
| 29 | + IllegalArgumentException::class, |
| 30 | + InvocationTargetException::class) |
| 31 | + fun feedJarToKnot(jar: File) { |
| 32 | + addUrlMethod.invoke(knotClassLoader, jar.toURI().toURL()) |
| 33 | + } |
| 34 | + |
| 35 | + private fun preLoadPlugin(file: File): PluginClassLoader? { |
| 36 | + runCatching { feedJarToKnot(file) } |
| 37 | + .onFailure { |
| 38 | + LOG.error("Failed to add the URL of the plugin $file", it) |
| 39 | + return null |
| 40 | + } |
| 41 | + |
| 42 | + LOG.debug("Added the URL of the plugin {} to the Knot class loader", file) |
15 | 43 |
|
16 | 44 | val jar = JarFile(file) |
17 | | - val loader = PluginClassLoader(jar, this::class.java.classLoader) |
| 45 | + val loader = PluginClassLoader(jar, knotClassLoader) |
| 46 | + |
18 | 47 | val mainClass = jar.manifest.mainAttributes.getValue("Main-Class") |
19 | | - ?: return LOG.error("The plugin $jar does not have a main class") |
| 48 | + ?: return null.also { LOG.error("The plugin $jar does not have a main class") } |
20 | 49 |
|
21 | | - val clazz = loader.loadClass(mainClass) |
22 | | - val instance = |
23 | | - clazz.declaredFields.firstOrNull { it.name == "INSTANCE" }?.get(null) ?: clazz.constructors.firstOrNull() |
24 | | - ?.newInstance() |
25 | | - ?: return LOG.error("The plugin $jar does not have an object instance or a public constructor") |
| 50 | + loadClass = loader.loadClass(mainClass) |
26 | 51 |
|
27 | | - val loadMethod = |
28 | | - clazz.methods.find { it.name == "load" } ?: return LOG.warn("The plugin $jar does not have a load method") |
| 52 | + loadInstance = |
| 53 | + loadClass?.declaredFields?.firstOrNull { it.name == "INSTANCE" }?.get(null) |
| 54 | + ?: loadClass?.constructors?.firstOrNull()?.newInstance() |
| 55 | + ?: return null.also { LOG.error("The plugin $jar does not have an object instance or a public constructor") } |
29 | 56 |
|
30 | | - loadMethod.invoke(instance) |
31 | | - loader.close() |
32 | | - } |
| 57 | + loadMethod = |
| 58 | + loadClass?.methods?.find { it.name == "load" } ?: return null |
| 59 | + .also { LOG.warn("The plugin $jar does not have a load method") } |
33 | 60 |
|
34 | | - override fun load(): String { |
35 | | - plugins.createIfNotExists() |
| 61 | + return loader |
| 62 | + } |
36 | 63 |
|
37 | | - val plugins = plugins.listRecursive() |
38 | | - plugins.forEach { file -> |
39 | | - loadPlugin(file) |
| 64 | + private fun loadPlugin(file: File) { |
| 65 | + runCatching { |
| 66 | + loadMethod?.invoke(loadInstance) |
40 | 67 | } |
| 68 | + .onFailure { |
| 69 | + return LOG.error(""" |
| 70 | + A serious error occurred while loading a plugin. |
| 71 | + This is likely a bug in the plugin itself but it could also be a bug in Lambda. |
| 72 | + If you are a developer, please check the plugin's main class and load method. |
| 73 | + If you are a regular user, please report this issue to Lambda team and the plugin developer. |
| 74 | + |
| 75 | + Plugin: $file |
| 76 | + Stacktrace: ${Thread.currentThread().stackTrace.joinToString("\n")} |
| 77 | + """.trimIndent()) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + fun preLoad(path: File): List<PluginClassLoader> { |
| 82 | + val plugins = path.walk().filter { it.isFile && it.extension == "jar" }.toList() |
| 83 | + return plugins.mapNotNull { preLoadPlugin(it) } |
| 84 | + } |
41 | 85 |
|
42 | | - return "Loaded ${plugins.count()} plugins" |
| 86 | + fun load(path: File): String { |
| 87 | + val plugins = path.walk().filter { it.isFile && it.extension == "jar" }.toList() |
| 88 | + plugins.forEach { loadPlugin(it) } |
| 89 | + return "Loaded ${plugins.size} plugins" |
43 | 90 | } |
44 | 91 | } |
0 commit comments