From 6af4d5e3b0d268f9353798686684155816a66a2f Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Sat, 16 Feb 2019 00:48:09 +0100 Subject: Refactor service manager for Spring Boot. --- .../pl/treksoft/kvision/remote/KVController.kt | 6 ++- .../pl/treksoft/kvision/remote/KVServiceManager.kt | 46 +++++++++------- .../pl/treksoft/kvision/remote/SpringContext.kt | 63 ---------------------- 3 files changed, 31 insertions(+), 84 deletions(-) delete mode 100644 kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/SpringContext.kt (limited to 'kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision') diff --git a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVController.kt b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVController.kt index 86789705..da47c750 100644 --- a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVController.kt +++ b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVController.kt @@ -22,6 +22,7 @@ package pl.treksoft.kvision.remote import org.springframework.beans.factory.annotation.Autowired +import org.springframework.context.ApplicationContext import org.springframework.stereotype.Controller import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestMethod @@ -34,6 +35,9 @@ open class KVController { @Autowired lateinit var kvServer: KVServer + @Autowired + lateinit var applicationContext: ApplicationContext + @RequestMapping( "/kv/**", method = [RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE, RequestMethod.OPTIONS] @@ -51,7 +55,7 @@ open class KVController { } }.firstOrNull() if (route != null) { - route.invoke(req, res) + route.invoke(req, res, applicationContext) } else { res.status = HttpServletResponse.SC_NOT_FOUND } diff --git a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index 4705b0b1..838abd67 100644 --- a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -28,6 +28,7 @@ import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import org.slf4j.Logger import org.slf4j.LoggerFactory +import org.springframework.context.ApplicationContext import pl.treksoft.kvision.types.KV_JSON_DATE_FORMAT import java.text.SimpleDateFormat import javax.servlet.http.HttpServletRequest @@ -44,11 +45,16 @@ actual open class KVServiceManager actual constructor(val serviceClass: val LOG: Logger = LoggerFactory.getLogger(KVServiceManager::class.java.name) } - val getRequests: MutableMap Unit> = mutableMapOf() - val postRequests: MutableMap Unit> = mutableMapOf() - val putRequests: MutableMap Unit> = mutableMapOf() - val deleteRequests: MutableMap Unit> = mutableMapOf() - val optionsRequests: MutableMap Unit> = mutableMapOf() + val getRequests: MutableMap Unit> = + mutableMapOf() + val postRequests: MutableMap Unit> = + mutableMapOf() + val putRequests: MutableMap Unit> = + mutableMapOf() + val deleteRequests: MutableMap Unit> = + mutableMapOf() + val optionsRequests: MutableMap Unit> = + mutableMapOf() val mapper = jacksonObjectMapper().apply { dateFormat = SimpleDateFormat(KV_JSON_DATE_FORMAT) @@ -67,8 +73,8 @@ actual open class KVServiceManager actual constructor(val serviceClass: method: HttpMethod, route: String? ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" - addRoute(method, "/kv/$routeDef") { req, res -> - val service = SpringContext.getBean(serviceClass.java) + addRoute(method, "/kv/$routeDef") { req, res, ctx -> + val service = ctx.getBean(serviceClass.java) val jsonRpcRequest = if (method == HttpMethod.GET) { JsonRpcRequest(req.getParameter("id")?.toInt() ?: 0, "", listOf()) } else { @@ -114,8 +120,8 @@ actual open class KVServiceManager actual constructor(val serviceClass: if (method == HttpMethod.GET) throw UnsupportedOperationException("GET method is only supported for methods without parameters") val routeDef = route ?: "route${this::class.simpleName}${counter++}" - addRoute(method, "/kv/$routeDef") { req, res -> - val service = SpringContext.getBean(serviceClass.java) + addRoute(method, "/kv/$routeDef") { req, res, ctx -> + val service = ctx.getBean(serviceClass.java) val jsonRpcRequest = mapper.readValue(req.inputStream, JsonRpcRequest::class.java) if (jsonRpcRequest.params.size == 1) { val param = getParameter(jsonRpcRequest.params[0]) @@ -169,8 +175,8 @@ actual open class KVServiceManager actual constructor(val serviceClass: if (method == HttpMethod.GET) throw UnsupportedOperationException("GET method is only supported for methods without parameters") val routeDef = route ?: "route${this::class.simpleName}${counter++}" - addRoute(method, "/kv/$routeDef") { req, res -> - val service = SpringContext.getBean(serviceClass.java) + addRoute(method, "/kv/$routeDef") { req, res, ctx -> + val service = ctx.getBean(serviceClass.java) val jsonRpcRequest = mapper.readValue(req.inputStream, JsonRpcRequest::class.java) if (jsonRpcRequest.params.size == 2) { val param1 = getParameter(jsonRpcRequest.params[0]) @@ -225,8 +231,8 @@ actual open class KVServiceManager actual constructor(val serviceClass: if (method == HttpMethod.GET) throw UnsupportedOperationException("GET method is only supported for methods without parameters") val routeDef = route ?: "route${this::class.simpleName}${counter++}" - addRoute(method, "/kv/$routeDef") { req, res -> - val service = SpringContext.getBean(serviceClass.java) + addRoute(method, "/kv/$routeDef") { req, res, ctx -> + val service = ctx.getBean(serviceClass.java) val jsonRpcRequest = mapper.readValue(req.inputStream, JsonRpcRequest::class.java) @Suppress("MagicNumber") if (jsonRpcRequest.params.size == 3) { @@ -283,8 +289,8 @@ actual open class KVServiceManager actual constructor(val serviceClass: if (method == HttpMethod.GET) throw UnsupportedOperationException("GET method is only supported for methods without parameters") val routeDef = route ?: "route${this::class.simpleName}${counter++}" - addRoute(method, "/kv/$routeDef") { req, res -> - val service = SpringContext.getBean(serviceClass.java) + addRoute(method, "/kv/$routeDef") { req, res, ctx -> + val service = ctx.getBean(serviceClass.java) val jsonRpcRequest = mapper.readValue(req.inputStream, JsonRpcRequest::class.java) @Suppress("MagicNumber") if (jsonRpcRequest.params.size == 4) { @@ -343,8 +349,8 @@ actual open class KVServiceManager actual constructor(val serviceClass: if (method == HttpMethod.GET) throw UnsupportedOperationException("GET method is only supported for methods without parameters") val routeDef = route ?: "route${this::class.simpleName}${counter++}" - addRoute(method, "/kv/$routeDef") { req, res -> - val service = SpringContext.getBean(serviceClass.java) + addRoute(method, "/kv/$routeDef") { req, res, ctx -> + val service = ctx.getBean(serviceClass.java) val jsonRpcRequest = mapper.readValue(req.inputStream, JsonRpcRequest::class.java) @Suppress("MagicNumber") if (jsonRpcRequest.params.size == 5) { @@ -398,8 +404,8 @@ actual open class KVServiceManager actual constructor(val serviceClass: function: T.(String?, String?) -> List ) { val routeDef = "route${this::class.simpleName}${counter++}" - addRoute(HttpMethod.POST, "/kv/$routeDef") { req, res -> - val service = SpringContext.getBean(serviceClass.java) + addRoute(HttpMethod.POST, "/kv/$routeDef") { req, res, ctx -> + val service = ctx.getBean(serviceClass.java) val jsonRpcRequest = mapper.readValue(req.inputStream, JsonRpcRequest::class.java) if (jsonRpcRequest.params.size == 2) { val param1 = getParameter(jsonRpcRequest.params[0]) @@ -441,7 +447,7 @@ actual open class KVServiceManager actual constructor(val serviceClass: fun addRoute( method: HttpMethod, path: String, - handler: (HttpServletRequest, HttpServletResponse) -> Unit + handler: (HttpServletRequest, HttpServletResponse, ApplicationContext) -> Unit ) { when (method) { HttpMethod.GET -> getRequests[path] = handler diff --git a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/SpringContext.kt b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/SpringContext.kt deleted file mode 100644 index 6655141c..00000000 --- a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/SpringContext.kt +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2017-present Robert Jaros - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package pl.treksoft.kvision.remote - -import org.springframework.beans.BeansException -import org.springframework.context.ApplicationContext -import org.springframework.context.ApplicationContextAware -import org.springframework.stereotype.Component - -@Component -class SpringContext : ApplicationContextAware { - - @Throws(BeansException::class) - override fun setApplicationContext(applicationContext: ApplicationContext) { - CONTEXT = applicationContext - } - - companion object { - - private var CONTEXT: ApplicationContext? = null - - /** - * Get a Spring bean by type. - */ - fun getBean(beanClass: Class): T { - return CONTEXT!!.getBean(beanClass) - } - - /** - * Get a Spring bean by name. - */ - fun getBean(beanName: String): Any { - return CONTEXT!!.getBean(beanName) - } - - /** - * Get a Spring bean by name and type. - */ - fun getBean(beanName: String, beanClass: Class): T { - return CONTEXT!!.getBean(beanName, beanClass) - } - } - -} -- cgit