From 3941f10b6bc378ab39aeec515c8b170cb4964c27 Mon Sep 17 00:00:00 2001 From: kahverengi Date: Tue, 2 Apr 2024 23:35:50 +0300 Subject: [PATCH] feat: enhance plugin API --- .../com/panomc/platform/PluginEventManager.kt | 6 ++--- .../com/panomc/platform/PluginFactory.kt | 11 ++++++++- .../com/panomc/platform/PluginManager.kt | 14 +++++++++++ .../com/panomc/platform/api/EventListener.kt | 4 ---- .../com/panomc/platform/api/PanoPlugin.kt | 23 ++++++++----------- .../platform/api/event/EventListener.kt | 4 ++++ .../api/{ => event}/PanoEventListener.kt | 2 +- .../api/{ => event}/PluginEventListener.kt | 2 +- 8 files changed, 43 insertions(+), 23 deletions(-) delete mode 100644 Pano/src/main/kotlin/com/panomc/platform/api/EventListener.kt create mode 100644 Pano/src/main/kotlin/com/panomc/platform/api/event/EventListener.kt rename Pano/src/main/kotlin/com/panomc/platform/api/{ => event}/PanoEventListener.kt (53%) rename Pano/src/main/kotlin/com/panomc/platform/api/{ => event}/PluginEventListener.kt (56%) diff --git a/Pano/src/main/kotlin/com/panomc/platform/PluginEventManager.kt b/Pano/src/main/kotlin/com/panomc/platform/PluginEventManager.kt index 9ce83d1e..413e930e 100644 --- a/Pano/src/main/kotlin/com/panomc/platform/PluginEventManager.kt +++ b/Pano/src/main/kotlin/com/panomc/platform/PluginEventManager.kt @@ -1,9 +1,9 @@ package com.panomc.platform -import com.panomc.platform.api.EventListener -import com.panomc.platform.api.PanoEventListener import com.panomc.platform.api.PanoPlugin -import com.panomc.platform.api.PluginEventListener +import com.panomc.platform.api.event.EventListener +import com.panomc.platform.api.event.PanoEventListener +import com.panomc.platform.api.event.PluginEventListener import org.springframework.context.annotation.AnnotationConfigApplicationContext class PluginEventManager { diff --git a/Pano/src/main/kotlin/com/panomc/platform/PluginFactory.kt b/Pano/src/main/kotlin/com/panomc/platform/PluginFactory.kt index 2213d06f..d48eec56 100644 --- a/Pano/src/main/kotlin/com/panomc/platform/PluginFactory.kt +++ b/Pano/src/main/kotlin/com/panomc/platform/PluginFactory.kt @@ -29,18 +29,27 @@ class PluginFactory(private val pluginEventManager: PluginEventManager, private plugin.pluginUiManager = pluginUiManager plugin.environmentType = Main.ENVIRONMENT plugin.releaseStage = Main.STAGE + plugin.pluginGlobalBeanContext = PluginManager.pluginGlobalBeanContext plugin.applicationContext = Main.applicationContext val pluginBeanContext by lazy { val pluginBeanContext = AnnotationConfigApplicationContext() - pluginBeanContext.parent = Main.applicationContext + pluginBeanContext.setAllowBeanDefinitionOverriding(true) + + pluginBeanContext.parent = PluginManager.pluginGlobalBeanContext pluginBeanContext.classLoader = pluginClass.classLoader pluginBeanContext.scan(pluginClass.`package`.name) + + pluginBeanContext.beanFactory.registerSingleton(plugin.logger.javaClass.name, plugin.logger) + pluginBeanContext.beanFactory.registerSingleton(pluginEventManager.javaClass.name, pluginEventManager) + pluginBeanContext.beanFactory.registerSingleton(plugin.javaClass.name, plugin) + pluginBeanContext.refresh() pluginBeanContext } + plugin.pluginBeanContext = pluginBeanContext pluginEventManager.initializePlugin(plugin, pluginBeanContext) diff --git a/Pano/src/main/kotlin/com/panomc/platform/PluginManager.kt b/Pano/src/main/kotlin/com/panomc/platform/PluginManager.kt index b5039653..a5695f7e 100644 --- a/Pano/src/main/kotlin/com/panomc/platform/PluginManager.kt +++ b/Pano/src/main/kotlin/com/panomc/platform/PluginManager.kt @@ -3,6 +3,7 @@ package com.panomc.platform import com.panomc.platform.SpringConfig.Companion.pluginEventManager import com.panomc.platform.SpringConfig.Companion.pluginUiManager import org.pf4j.* +import org.springframework.context.annotation.AnnotationConfigApplicationContext import org.springframework.stereotype.Component import java.nio.file.Path import java.nio.file.Paths @@ -10,6 +11,19 @@ import java.nio.file.Paths @Component class PluginManager(importPaths: List = listOf(Paths.get(System.getProperty("pf4j.pluginsDir", "./plugins")))) : DefaultPluginManager(importPaths) { + companion object { + internal val pluginGlobalBeanContext by lazy { + val pluginGlobalBeanContext = AnnotationConfigApplicationContext() + + pluginGlobalBeanContext.setAllowBeanDefinitionOverriding(true) + + pluginGlobalBeanContext.beanFactory.registerSingleton(SpringConfig.vertx.javaClass.name, SpringConfig.vertx) + + pluginGlobalBeanContext.refresh() + + pluginGlobalBeanContext + } + } override fun createPluginDescriptorFinder(): CompoundPluginDescriptorFinder { return CompoundPluginDescriptorFinder() // Demo is using the Manifest file diff --git a/Pano/src/main/kotlin/com/panomc/platform/api/EventListener.kt b/Pano/src/main/kotlin/com/panomc/platform/api/EventListener.kt deleted file mode 100644 index b6c2bf59..00000000 --- a/Pano/src/main/kotlin/com/panomc/platform/api/EventListener.kt +++ /dev/null @@ -1,4 +0,0 @@ -package com.panomc.platform.api - -interface EventListener { -} \ No newline at end of file diff --git a/Pano/src/main/kotlin/com/panomc/platform/api/PanoPlugin.kt b/Pano/src/main/kotlin/com/panomc/platform/api/PanoPlugin.kt index a3fbdca2..63e01f3b 100644 --- a/Pano/src/main/kotlin/com/panomc/platform/api/PanoPlugin.kt +++ b/Pano/src/main/kotlin/com/panomc/platform/api/PanoPlugin.kt @@ -4,6 +4,7 @@ import com.panomc.platform.Main import com.panomc.platform.PluginEventManager import com.panomc.platform.PluginUiManager import com.panomc.platform.ReleaseStage +import com.panomc.platform.api.event.PluginEventListener import io.vertx.core.Vertx import kotlinx.coroutines.runBlocking import org.pf4j.Plugin @@ -27,19 +28,21 @@ abstract class PanoPlugin : Plugin() { internal set lateinit var pluginBeanContext: AnnotationConfigApplicationContext internal set + lateinit var pluginGlobalBeanContext: AnnotationConfigApplicationContext + internal set internal lateinit var applicationContext: AnnotationConfigApplicationContext val logger: Logger = LoggerFactory.getLogger(this::class.java) - private val registeredBeans = mutableListOf>() + private val registeredBeans = mutableListOf() - fun register(bean: Class<*>) { + fun registerSingletonGlobal(bean: Any) { if (registeredBeans.contains(bean)) { return } - applicationContext.register(bean) + pluginGlobalBeanContext.beanFactory.registerSingleton(bean.javaClass.name, bean) registeredBeans.add(bean) } @@ -48,20 +51,14 @@ abstract class PanoPlugin : Plugin() { pluginEventManager.register(this, eventListener) } - fun unRegister(bean: Class<*>) { + fun unRegisterGlobal(bean: Any) { if (!registeredBeans.contains(bean)) { return } - val registry = applicationContext.beanFactory as BeanDefinitionRegistry - val beanNames = registry.beanDefinitionNames + val registry = pluginGlobalBeanContext.beanFactory as BeanDefinitionRegistry - for (beanName in beanNames) { - if (registry.getBeanDefinition(beanName).beanClassName == bean.name) { - registry.removeBeanDefinition(beanName) - return // Stop after removing the first bean definition of the given class - } - } + registry.removeBeanDefinition(bean.javaClass.name) registeredBeans.remove(bean) } @@ -84,7 +81,7 @@ abstract class PanoPlugin : Plugin() { val copyOfRegisteredBeans = registeredBeans.toList() copyOfRegisteredBeans.forEach { - unRegister(it) + unRegisterGlobal(it) } pluginEventManager.unregisterPlugin(this) diff --git a/Pano/src/main/kotlin/com/panomc/platform/api/event/EventListener.kt b/Pano/src/main/kotlin/com/panomc/platform/api/event/EventListener.kt new file mode 100644 index 00000000..2168a56b --- /dev/null +++ b/Pano/src/main/kotlin/com/panomc/platform/api/event/EventListener.kt @@ -0,0 +1,4 @@ +package com.panomc.platform.api.event + +interface EventListener { +} \ No newline at end of file diff --git a/Pano/src/main/kotlin/com/panomc/platform/api/PanoEventListener.kt b/Pano/src/main/kotlin/com/panomc/platform/api/event/PanoEventListener.kt similarity index 53% rename from Pano/src/main/kotlin/com/panomc/platform/api/PanoEventListener.kt rename to Pano/src/main/kotlin/com/panomc/platform/api/event/PanoEventListener.kt index 10b30f70..7e95766f 100644 --- a/Pano/src/main/kotlin/com/panomc/platform/api/PanoEventListener.kt +++ b/Pano/src/main/kotlin/com/panomc/platform/api/event/PanoEventListener.kt @@ -1,3 +1,3 @@ -package com.panomc.platform.api +package com.panomc.platform.api.event interface PanoEventListener : EventListener \ No newline at end of file diff --git a/Pano/src/main/kotlin/com/panomc/platform/api/PluginEventListener.kt b/Pano/src/main/kotlin/com/panomc/platform/api/event/PluginEventListener.kt similarity index 56% rename from Pano/src/main/kotlin/com/panomc/platform/api/PluginEventListener.kt rename to Pano/src/main/kotlin/com/panomc/platform/api/event/PluginEventListener.kt index 0a572531..ba0db416 100644 --- a/Pano/src/main/kotlin/com/panomc/platform/api/PluginEventListener.kt +++ b/Pano/src/main/kotlin/com/panomc/platform/api/event/PluginEventListener.kt @@ -1,4 +1,4 @@ -package com.panomc.platform.api +package com.panomc.platform.api.event interface PluginEventListener : EventListener { } \ No newline at end of file