-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: The major of plugin API has been changed! Be cautious. We will implement full documentation soon for v2.
- Loading branch information
Showing
15 changed files
with
185 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,45 @@ | ||
package co.statu.parsek | ||
|
||
import co.statu.parsek.api.ParsekEvent | ||
import co.statu.parsek.api.ParsekPlugin | ||
import co.statu.parsek.api.PluginEvent | ||
import co.statu.parsek.api.event.EventListener | ||
import co.statu.parsek.api.event.ParsekEventListener | ||
import co.statu.parsek.api.event.PluginEventListener | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext | ||
|
||
class PluginEventManager { | ||
@PublishedApi | ||
internal val pluginEventListeners = mutableMapOf<ParsekPlugin, MutableList<PluginEvent>>() | ||
|
||
companion object { | ||
internal val parsekEventListeners = mutableMapOf<ParsekPlugin, MutableList<ParsekEvent>>() | ||
private val eventListeners = mutableMapOf<ParsekPlugin, MutableList<EventListener>>() | ||
|
||
internal inline fun <reified T : ParsekEvent> getEventHandlers() = | ||
parsekEventListeners.flatMap { it.value }.filterIsInstance<T>() | ||
} | ||
fun getEventListeners() = eventListeners.toMap() | ||
|
||
private fun initializePluginIfNot(plugin: ParsekPlugin) { | ||
if (pluginEventListeners[plugin] == null) { | ||
pluginEventListeners[plugin] = mutableListOf() | ||
} | ||
|
||
if (parsekEventListeners[plugin] == null) { | ||
parsekEventListeners[plugin] = mutableListOf() | ||
} | ||
} | ||
internal inline fun <reified T : ParsekEventListener> getParsekEventListeners() = | ||
eventListeners.flatMap { it.value }.filterIsInstance<T>() | ||
|
||
fun register(plugin: ParsekPlugin, pluginEvent: PluginEvent) { | ||
initializePluginIfNot(plugin) | ||
|
||
pluginEventListeners[plugin]!!.add(pluginEvent) | ||
inline fun <reified T : PluginEventListener> getEventListeners() = | ||
getEventListeners().flatMap { it.value }.filter { it !is ParsekEventListener }.filterIsInstance<T>() | ||
} | ||
|
||
fun register(plugin: ParsekPlugin, parsekEvent: ParsekEvent) { | ||
initializePluginIfNot(plugin) | ||
|
||
parsekEventListeners[plugin]!!.add(parsekEvent) | ||
internal fun initializePlugin(plugin: ParsekPlugin, pluginBeanContext: AnnotationConfigApplicationContext) { | ||
if (eventListeners[plugin] == null) { | ||
eventListeners[plugin] = pluginBeanContext | ||
.getBeansWithAnnotation(co.statu.parsek.api.annotation.EventListener::class.java) | ||
.map { it.value as EventListener } | ||
.toMutableList() | ||
} | ||
} | ||
|
||
fun unregister(plugin: ParsekPlugin, pluginEvent: PluginEvent) { | ||
pluginEventListeners[plugin]?.remove(pluginEvent) | ||
internal fun unregisterPlugin(plugin: ParsekPlugin) { | ||
eventListeners.remove(plugin) | ||
} | ||
|
||
fun unregister(plugin: ParsekPlugin, parsekEvent: ParsekEvent) { | ||
parsekEventListeners[plugin]?.remove(parsekEvent) | ||
fun register(plugin: ParsekPlugin, eventListener: EventListener) { | ||
if (eventListeners[plugin]!!.none { it::class == eventListener::class }) { | ||
eventListeners[plugin]!!.add(eventListener) | ||
} | ||
} | ||
|
||
inline fun <reified T : PluginEvent> getEventHandlers() = | ||
pluginEventListeners.flatMap { it.value }.filterIsInstance<T>() | ||
fun unRegister(plugin: ParsekPlugin, eventListener: EventListener) { | ||
eventListeners[plugin]?.remove(eventListener) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,99 @@ | ||
package co.statu.parsek.api | ||
|
||
import co.statu.parsek.Main | ||
import co.statu.parsek.PluginEventManager | ||
import co.statu.parsek.ReleaseStage | ||
import co.statu.parsek.api.event.PluginEventListener | ||
import io.vertx.core.Vertx | ||
import kotlinx.coroutines.runBlocking | ||
import org.pf4j.Plugin | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext | ||
|
||
abstract class ParsekPlugin(val context: PluginContext) : Plugin() | ||
abstract class ParsekPlugin : Plugin() { | ||
lateinit var pluginId: String | ||
internal set | ||
lateinit var vertx: Vertx | ||
internal set | ||
lateinit var pluginEventManager: PluginEventManager | ||
internal set | ||
lateinit var environmentType: Main.Companion.EnvironmentType | ||
internal set | ||
lateinit var releaseStage: ReleaseStage | ||
internal set | ||
lateinit var pluginBeanContext: AnnotationConfigApplicationContext | ||
internal set | ||
|
||
internal lateinit var applicationContext: AnnotationConfigApplicationContext | ||
|
||
val logger: Logger = LoggerFactory.getLogger(this::class.java) | ||
|
||
private val registeredBeans = mutableListOf<Class<*>>() | ||
|
||
fun register(bean: Class<*>) { | ||
if (registeredBeans.contains(bean)) { | ||
return | ||
} | ||
|
||
applicationContext.register(bean) | ||
|
||
registeredBeans.add(bean) | ||
} | ||
|
||
fun register(eventListener: PluginEventListener) { | ||
pluginEventManager.register(this, eventListener) | ||
} | ||
|
||
fun unRegister(bean: Class<*>) { | ||
if (!registeredBeans.contains(bean)) { | ||
return | ||
} | ||
|
||
val registry = applicationContext.beanFactory as BeanDefinitionRegistry | ||
val beanNames = registry.beanDefinitionNames | ||
|
||
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 | ||
} | ||
} | ||
|
||
registeredBeans.remove(bean) | ||
} | ||
|
||
fun unRegister(eventListener: PluginEventListener) { | ||
pluginEventManager.unRegister(this, eventListener) | ||
} | ||
|
||
@Deprecated("Use onEnable method.") | ||
override fun start() { | ||
runBlocking { | ||
onEnable() | ||
} | ||
} | ||
|
||
@Deprecated("Use onDisable method.") | ||
override fun stop() { | ||
pluginBeanContext.close() | ||
|
||
val copyOfRegisteredBeans = registeredBeans.toList() | ||
|
||
copyOfRegisteredBeans.forEach { | ||
unRegister(it) | ||
} | ||
|
||
pluginEventManager.unregisterPlugin(this) | ||
|
||
runBlocking { | ||
onDisable() | ||
} | ||
} | ||
|
||
open suspend fun onLoad() {} | ||
|
||
open suspend fun onEnable() {} | ||
open suspend fun onDisable() {} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/co/statu/parsek/api/annotation/EventListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package co.statu.parsek.api.annotation | ||
|
||
import org.springframework.stereotype.Component | ||
|
||
@Target(AnnotationTarget.ANNOTATION_CLASS, AnnotationTarget.CLASS) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
@Component | ||
annotation class EventListener( | ||
val value: String = "" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/co/statu/parsek/api/event/CoreEventListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package co.statu.parsek.api.event | ||
|
||
import co.statu.parsek.config.ConfigManager | ||
|
||
interface CoreEventListener : ParsekEventListener { | ||
suspend fun onConfigManagerReady(configManager: ConfigManager) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package co.statu.parsek.api.event | ||
|
||
interface EventListener { | ||
} |
12 changes: 1 addition & 11 deletions
12
src/main/kotlin/co/statu/parsek/api/event/ParsekEventListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,3 @@ | ||
package co.statu.parsek.api.event | ||
|
||
import co.statu.parsek.api.ParsekEvent | ||
import co.statu.parsek.config.ConfigManager | ||
|
||
/** | ||
* ParsekEventListener is an extension point for listening Parsek related events | ||
* such as when config manager has been initialized. | ||
*/ | ||
interface ParsekEventListener : ParsekEvent { | ||
|
||
suspend fun onConfigManagerReady(configManager: ConfigManager) | ||
} | ||
interface ParsekEventListener : EventListener |
4 changes: 4 additions & 0 deletions
4
src/main/kotlin/co/statu/parsek/api/event/PluginEventListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package co.statu.parsek.api.event | ||
|
||
interface PluginEventListener : EventListener { | ||
} |
4 changes: 2 additions & 2 deletions
4
src/main/kotlin/co/statu/parsek/api/event/RouterEventListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
package co.statu.parsek.api.event | ||
|
||
import co.statu.parsek.api.ParsekEvent | ||
import co.statu.parsek.model.Route | ||
|
||
interface RouterEventListener : ParsekEvent { | ||
interface RouterEventListener : ParsekEventListener { | ||
|
||
fun onInitRouteList(routes: MutableList<Route>) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters