aboutsummaryrefslogtreecommitdiff
path: root/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl
diff options
context:
space:
mode:
authorRobert Jaros <rjaros@finn.pl>2019-02-16 00:48:09 +0100
committerRobert Jaros <rjaros@finn.pl>2019-02-16 00:48:09 +0100
commit6af4d5e3b0d268f9353798686684155816a66a2f (patch)
tree9f1489f14dec8e3c653c808b7c636f82404b3794 /kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl
parent55c3d369ecde5a3567e990104aa171ad8ee80874 (diff)
downloadkvision-6af4d5e3b0d268f9353798686684155816a66a2f.tar.gz
kvision-6af4d5e3b0d268f9353798686684155816a66a2f.tar.bz2
kvision-6af4d5e3b0d268f9353798686684155816a66a2f.zip
Refactor service manager for Spring Boot.
Diffstat (limited to 'kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl')
-rw-r--r--kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVController.kt6
-rw-r--r--kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt46
-rw-r--r--kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/SpringContext.kt63
3 files changed, 31 insertions, 84 deletions
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<T : Any> actual constructor(val serviceClass:
val LOG: Logger = LoggerFactory.getLogger(KVServiceManager::class.java.name)
}
- val getRequests: MutableMap<String, (HttpServletRequest, HttpServletResponse) -> Unit> = mutableMapOf()
- val postRequests: MutableMap<String, (HttpServletRequest, HttpServletResponse) -> Unit> = mutableMapOf()
- val putRequests: MutableMap<String, (HttpServletRequest, HttpServletResponse) -> Unit> = mutableMapOf()
- val deleteRequests: MutableMap<String, (HttpServletRequest, HttpServletResponse) -> Unit> = mutableMapOf()
- val optionsRequests: MutableMap<String, (HttpServletRequest, HttpServletResponse) -> Unit> = mutableMapOf()
+ val getRequests: MutableMap<String, (HttpServletRequest, HttpServletResponse, ApplicationContext) -> Unit> =
+ mutableMapOf()
+ val postRequests: MutableMap<String, (HttpServletRequest, HttpServletResponse, ApplicationContext) -> Unit> =
+ mutableMapOf()
+ val putRequests: MutableMap<String, (HttpServletRequest, HttpServletResponse, ApplicationContext) -> Unit> =
+ mutableMapOf()
+ val deleteRequests: MutableMap<String, (HttpServletRequest, HttpServletResponse, ApplicationContext) -> Unit> =
+ mutableMapOf()
+ val optionsRequests: MutableMap<String, (HttpServletRequest, HttpServletResponse, ApplicationContext) -> Unit> =
+ mutableMapOf()
val mapper = jacksonObjectMapper().apply {
dateFormat = SimpleDateFormat(KV_JSON_DATE_FORMAT)
@@ -67,8 +73,8 @@ actual open class KVServiceManager<T : Any> 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<T : Any> 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<PAR>(jsonRpcRequest.params[0])
@@ -169,8 +175,8 @@ actual open class KVServiceManager<T : Any> 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<PAR1>(jsonRpcRequest.params[0])
@@ -225,8 +231,8 @@ actual open class KVServiceManager<T : Any> 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<T : Any> 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<T : Any> 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<T : Any> actual constructor(val serviceClass:
function: T.(String?, String?) -> List<RemoteSelectOption>
) {
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<String?>(jsonRpcRequest.params[0])
@@ -441,7 +447,7 @@ actual open class KVServiceManager<T : Any> 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 <T> getBean(beanClass: Class<T>): 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 <T> getBean(beanName: String, beanClass: Class<T>): T {
- return CONTEXT!!.getBean(beanName, beanClass)
- }
- }
-
-}