diff options
Diffstat (limited to 'kvision-modules/kvision-server-jooby')
3 files changed, 36 insertions, 9 deletions
diff --git a/kvision-modules/kvision-server-jooby/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVModules.kt b/kvision-modules/kvision-server-jooby/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVModules.kt index 2245e92b..a85752eb 100644 --- a/kvision-modules/kvision-server-jooby/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVModules.kt +++ b/kvision-modules/kvision-server-jooby/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVModules.kt @@ -22,14 +22,18 @@ package pl.treksoft.kvision.remote import com.google.inject.AbstractModule +import com.google.inject.Injector import com.google.inject.Module import com.typesafe.config.Config import io.jooby.AssetSource +import io.jooby.Context import io.jooby.Environment import io.jooby.Kooby import io.jooby.di.GuiceModule import io.jooby.json.JacksonModule +const val KV_INJECTOR_KEY = "pl.treksoft.kvision.injector.key" + /** * Initialization function for Jooby server. */ @@ -38,6 +42,10 @@ fun Kooby.kvisionInit(vararg modules: Module) { assets("/*", AssetSource.create(javaClass.classLoader, "assets")) install(GuiceModule(MainModule(this), *modules)) install(JacksonModule()) + before { ctx -> + val injector = ctx.require(Injector::class.java).createChildInjector(ContextModule(ctx)) + ctx.attribute(KV_INJECTOR_KEY, injector) + } } internal class MainModule(private val kooby: Kooby) : AbstractModule() { @@ -47,3 +55,9 @@ internal class MainModule(private val kooby: Kooby) : AbstractModule() { bind(Config::class.java).toInstance(kooby.config) } } + +class ContextModule(private val ctx: Context) : AbstractModule() { + override fun configure() { + bind(Context::class.java).toInstance(ctx) + } +} diff --git a/kvision-modules/kvision-server-jooby/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-jooby/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index ea37c477..69040e51 100644 --- a/kvision-modules/kvision-server-jooby/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-jooby/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -24,6 +24,7 @@ package pl.treksoft.kvision.remote import com.fasterxml.jackson.databind.module.SimpleModule import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue +import com.google.inject.Injector import io.jooby.Context import io.jooby.CoroutineRouter import io.jooby.HandlerContext @@ -91,6 +92,7 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: /** * @suppress internal function */ + @Suppress("DEPRECATION") fun initializeService(service: T, ctx: Context) { if (service is WithContext) { service.ctx = ctx @@ -118,7 +120,8 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: } else { ctx.body<JsonRpcRequest>() } - val service = ctx.require(serviceClass.java) + val injector = ctx.attribute<Injector>(KV_INJECTOR_KEY) + val service = injector.getInstance(serviceClass.java) initializeService(service, ctx) try { val result = function.invoke(service) @@ -154,7 +157,8 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: val jsonRpcRequest = ctx.body<JsonRpcRequest>() if (jsonRpcRequest.params.size == 1) { val param = getParameter<PAR>(jsonRpcRequest.params[0]) - val service = ctx.require(serviceClass.java) + val injector = ctx.attribute<Injector>(KV_INJECTOR_KEY) + val service = injector.getInstance(serviceClass.java) initializeService(service, ctx) try { val result = function.invoke(service, param) @@ -194,7 +198,8 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: if (jsonRpcRequest.params.size == 2) { val param1 = getParameter<PAR1>(jsonRpcRequest.params[0]) val param2 = getParameter<PAR2>(jsonRpcRequest.params[1]) - val service = ctx.require(serviceClass.java) + val injector = ctx.attribute<Injector>(KV_INJECTOR_KEY) + val service = injector.getInstance(serviceClass.java) initializeService(service, ctx) try { val result = function.invoke(service, param1, param2) @@ -236,7 +241,8 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: val param1 = getParameter<PAR1>(jsonRpcRequest.params[0]) val param2 = getParameter<PAR2>(jsonRpcRequest.params[1]) val param3 = getParameter<PAR3>(jsonRpcRequest.params[2]) - val service = ctx.require(serviceClass.java) + val injector = ctx.attribute<Injector>(KV_INJECTOR_KEY) + val service = injector.getInstance(serviceClass.java) initializeService(service, ctx) try { val result = function.invoke(service, param1, param2, param3) @@ -279,7 +285,8 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: val param2 = getParameter<PAR2>(jsonRpcRequest.params[1]) val param3 = getParameter<PAR3>(jsonRpcRequest.params[2]) val param4 = getParameter<PAR4>(jsonRpcRequest.params[3]) - val service = ctx.require(serviceClass.java) + val injector = ctx.attribute<Injector>(KV_INJECTOR_KEY) + val service = injector.getInstance(serviceClass.java) initializeService(service, ctx) try { val result = function.invoke(service, param1, param2, param3, param4) @@ -324,7 +331,8 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: val param3 = getParameter<PAR3>(jsonRpcRequest.params[2]) val param4 = getParameter<PAR4>(jsonRpcRequest.params[3]) val param5 = getParameter<PAR5>(jsonRpcRequest.params[4]) - val service = ctx.require(serviceClass.java) + val injector = ctx.attribute<Injector>(KV_INJECTOR_KEY) + val service = injector.getInstance(serviceClass.java) initializeService(service, ctx) try { val result = function.invoke(service, param1, param2, param3, param4, param5) @@ -370,7 +378,8 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: val param4 = getParameter<PAR4>(jsonRpcRequest.params[3]) val param5 = getParameter<PAR5>(jsonRpcRequest.params[4]) val param6 = getParameter<PAR6>(jsonRpcRequest.params[5]) - val service = ctx.require(serviceClass.java) + val injector = ctx.attribute<Injector>(KV_INJECTOR_KEY) + val service = injector.getInstance(serviceClass.java) initializeService(service, ctx) try { val result = function.invoke(service, param1, param2, param3, param4, param5, param6) @@ -403,7 +412,8 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) { val routeDef = "route${this::class.simpleName}${counter++}" webSocketRequests["/kvws/$routeDef"] = { ctx, configurer -> - val service = ctx.require(serviceClass.java) + val injector = ctx.require(Injector::class.java).createChildInjector(ContextModule(ctx)) + val service = injector.getInstance(serviceClass.java) initializeService(service, ctx) val incoming = Channel<String>() val outgoing = Channel<String>() @@ -487,7 +497,8 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: @Suppress("MagicNumber") val param5 = getParameter<String?>(jsonRpcRequest.params[4]) - val service = ctx.require(serviceClass.java) + val injector = ctx.attribute<Injector>(KV_INJECTOR_KEY) + val service = injector.getInstance(serviceClass.java) initializeService(service, ctx) try { val result = function.invoke(service, param1, param2, param3, param4, param5) diff --git a/kvision-modules/kvision-server-jooby/src/jvmMain/kotlin/pl/treksoft/kvision/remote/SessionInterfaces.kt b/kvision-modules/kvision-server-jooby/src/jvmMain/kotlin/pl/treksoft/kvision/remote/SessionInterfaces.kt index 06955a07..6f79ebec 100644 --- a/kvision-modules/kvision-server-jooby/src/jvmMain/kotlin/pl/treksoft/kvision/remote/SessionInterfaces.kt +++ b/kvision-modules/kvision-server-jooby/src/jvmMain/kotlin/pl/treksoft/kvision/remote/SessionInterfaces.kt @@ -24,10 +24,12 @@ package pl.treksoft.kvision.remote import io.jooby.Context import io.jooby.Session +@Deprecated("Use dependency injection instead.") interface WithContext { var ctx: Context } +@Deprecated("Use dependency injection instead.") interface WithSession { var session: Session } |