Skip to content

Commit

Permalink
feat: add bean instances for easy access on dependency injection usage
Browse files Browse the repository at this point in the history
  • Loading branch information
duruer committed Mar 23, 2024
1 parent a4744bc commit 8e2ae68
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/main/kotlin/co/statu/parsek/PluginFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,38 @@ class PluginFactory : DefaultPluginFactory() {
}

override fun createInstance(pluginClass: Class<*>, pluginWrapper: PluginWrapper): Plugin? {
val pluginBeanContext by lazy {
val pluginBeanContext = AnnotationConfigApplicationContext()

pluginBeanContext.parent = Main.applicationContext
pluginBeanContext.classLoader = pluginClass.classLoader
pluginBeanContext.scan(pluginClass.`package`.name)
pluginBeanContext.refresh()

pluginBeanContext
}

try {
val constructor = pluginClass.getConstructor()

val plugin = constructor.newInstance() as ParsekPlugin

pluginEventManager.initializePlugin(plugin, pluginBeanContext)

plugin.pluginId = pluginWrapper.pluginId
plugin.vertx = vertx
plugin.pluginEventManager = pluginEventManager
plugin.environmentType = Main.ENVIRONMENT
plugin.releaseStage = Main.STAGE
plugin.pluginBeanContext = pluginBeanContext
plugin.applicationContext = Main.applicationContext

val pluginBeanContext by lazy {
val pluginBeanContext = AnnotationConfigApplicationContext()

pluginBeanContext.parent = Main.applicationContext
pluginBeanContext.classLoader = pluginClass.classLoader
pluginBeanContext.scan(pluginClass.`package`.name)

pluginBeanContext.beanFactory.registerSingleton(logger.javaClass.name, 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)

runBlocking {
plugin.onLoad()
}
Expand Down

0 comments on commit 8e2ae68

Please sign in to comment.