Skip to content

Commit

Permalink
feat: enhance plugin API
Browse files Browse the repository at this point in the history
  • Loading branch information
duruer committed Apr 2, 2024
1 parent 6723627 commit 3941f10
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
11 changes: 10 additions & 1 deletion Pano/src/main/kotlin/com/panomc/platform/PluginFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions Pano/src/main/kotlin/com/panomc/platform/PluginManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@ 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

@Component
class PluginManager(importPaths: List<Path> = 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
Expand Down
4 changes: 0 additions & 4 deletions Pano/src/main/kotlin/com/panomc/platform/api/EventListener.kt

This file was deleted.

23 changes: 10 additions & 13 deletions Pano/src/main/kotlin/com/panomc/platform/api/PanoPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Class<*>>()
private val registeredBeans = mutableListOf<Any>()

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)
}
Expand All @@ -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)
}
Expand All @@ -84,7 +81,7 @@ abstract class PanoPlugin : Plugin() {
val copyOfRegisteredBeans = registeredBeans.toList()

copyOfRegisteredBeans.forEach {
unRegister(it)
unRegisterGlobal(it)
}

pluginEventManager.unregisterPlugin(this)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.panomc.platform.api.event

interface EventListener {
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.panomc.platform.api
package com.panomc.platform.api.event

interface PanoEventListener : EventListener
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.panomc.platform.api
package com.panomc.platform.api.event

interface PluginEventListener : EventListener {
}

0 comments on commit 3941f10

Please sign in to comment.