Skip to content

Commit ec4b9e0

Browse files
committed
1 parent 3e54025 commit ec4b9e0

12 files changed

Lines changed: 117 additions & 25 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* The MIT License (MIT)
3+
* <p>
4+
* Copyright (c) 2016-2019 Maksim Gramin
5+
* <p>
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
* <p>
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
* <p>
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package com.github.mgramin.sqlboot.model.connection
26+
27+
import com.fasterxml.jackson.annotation.JsonIgnore
28+
import org.slf4j.LoggerFactory
29+
import javax.sql.DataSource
30+
31+
class CheckedConnection(private val origin: DbConnection) : DbConnection {
32+
33+
override fun getProperties() = origin.getProperties()
34+
35+
private val health: String
36+
37+
private val logger = LoggerFactory.getLogger(this::class.java)
38+
39+
@JsonIgnore
40+
override fun getDataSource(): DataSource {
41+
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
42+
}
43+
44+
override fun getName() = origin.getName()
45+
46+
override fun getHealth(): String {
47+
return health
48+
}
49+
50+
// override fun getDataSource() = origin.getDataSource()
51+
52+
override fun paginationQueryTemplate() = origin.paginationQueryTemplate()
53+
54+
init {
55+
health = try {
56+
logger.info("Check connection...")
57+
origin.getDataSource().connection
58+
"Ok"
59+
} catch (e: Exception) {
60+
e.message.toString()
61+
}
62+
}
63+
64+
}

src/main/kotlin/com/github/mgramin/sqlboot/model/connection/DbConnection.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ import javax.sql.DataSource
2828

2929
interface DbConnection {
3030

31-
fun name(): String
31+
fun getName(): String
32+
33+
fun getHealth(): String
34+
3235
fun getDataSource(): DataSource
36+
3337
fun paginationQueryTemplate(): String
3438

39+
fun getProperties(): Map<String, Any>
40+
3541
}

src/main/kotlin/com/github/mgramin/sqlboot/model/connection/DbConnectionList.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ import org.springframework.stereotype.Service
3939
open class DbConnectionList(val connections: List<SimpleDbConnection>) {
4040

4141
fun getConnectionByName(name: String): SimpleDbConnection {
42-
return connections.first { v -> v.name.equals(name, ignoreCase = true) }
42+
return connections.first { v -> v.getName().equals(name, ignoreCase = true) }
4343
}
4444

4545
fun getConnectionsByMask(name: String): List<SimpleDbConnection> {
46-
return connections.filter { v -> v.name!!.matches(name.toRegex()) }
46+
return connections.filter { v -> v.getName().matches(name.toRegex()) }
4747
}
4848
}

src/main/kotlin/com/github/mgramin/sqlboot/model/connection/FakeDbConnection.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,15 @@ package com.github.mgramin.sqlboot.model.connection
2727
import javax.sql.DataSource
2828

2929
class FakeDbConnection : DbConnection {
30+
override fun getProperties(): Map<String, Any> {
31+
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
32+
}
33+
34+
override fun getHealth(): String {
35+
return "Ok"
36+
}
3037

31-
override fun name(): String {
38+
override fun getName(): String {
3239
return "Simple fake connection"
3340
}
3441

src/main/kotlin/com/github/mgramin/sqlboot/model/connection/SimpleDbConnection.kt

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,17 @@ open class SimpleDbConnection : DbConnection {
3838

3939
override fun paginationQueryTemplate() = this.paginationQueryTemplate!!
4040

41-
override fun name(): String {
41+
override fun getName(): String {
4242
return name!!
4343
}
4444

45-
var name: String? = null
45+
fun setName(name: String) {
46+
this.name = name
47+
}
48+
49+
private var name: String? = null
50+
51+
4652
@JsonIgnore // TODO fix json serialization for Resource class
4753
var baseFolder: Resource? = null
4854
var url: String? = null
@@ -57,15 +63,7 @@ open class SimpleDbConnection : DbConnection {
5763

5864
private var dataSource: DataSource? = null
5965

60-
val health: String
61-
get() {
62-
return try {
63-
getDataSource().connection
64-
"OK"
65-
} catch (e: Exception) {
66-
e.message.toString()
67-
}
68-
}
66+
override fun getHealth() = "UKNOWN"
6967

7068
constructor()
7169
constructor(name: String? = null, baseFolder: Resource? = null, url: String? = null,
@@ -81,7 +79,7 @@ open class SimpleDbConnection : DbConnection {
8179
this.paginationQueryTemplate = paginationQueryTemplate
8280
}
8381

84-
fun getProperties(): Map<String, Any> {
82+
override fun getProperties(): Map<String, Any> {
8583
return JSONObject(properties).toMap()
8684
}
8785

src/main/kotlin/com/github/mgramin/sqlboot/model/resourcetype/impl/sql/SqlResourceType.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class SqlResourceType(
6969
return@map createQuery(uri, connection).execute(hashMapOf("uri" to uri))
7070
.map<Map<String, Any>?> {
7171
val mutableMap = it.toMutableMap()
72-
mutableMap["database"] = connection.name()
72+
mutableMap["database"] = connection.getName()
7373
mutableMap
7474
}
7575
}

src/main/kotlin/com/github/mgramin/sqlboot/model/resourcetype/wrappers/header/DbNameWrapper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class DbNameWrapper(private val origin: ResourceType,
6565

6666
override fun headers(): Map<String, Any> {
6767
val headers = it.headers().toMutableMap()
68-
headers["database"] = dbConnection.name()
68+
headers["database"] = dbConnection.getName()
6969
return headers
7070
}
7171

src/main/kotlin/com/github/mgramin/sqlboot/rest/controllers/ApiController.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ import org.springframework.http.MediaType
5656
import org.springframework.http.MediaType.APPLICATION_JSON_VALUE
5757
import org.springframework.http.ResponseEntity
5858
import org.springframework.web.bind.annotation.CrossOrigin
59-
import org.springframework.web.bind.annotation.GetMapping
6059
import org.springframework.web.bind.annotation.PathVariable
6160
import org.springframework.web.bind.annotation.RequestMapping
6261
import org.springframework.web.bind.annotation.RequestMethod.GET
@@ -65,13 +64,11 @@ import org.springframework.web.bind.annotation.RequestParam
6564
import org.springframework.web.bind.annotation.ResponseBody
6665
import org.springframework.web.bind.annotation.RestController
6766
import reactor.core.publisher.Flux
68-
import java.time.Duration
6967
import java.util.ArrayList
7068
import java.util.Arrays.asList
7169
import java.util.stream.Collectors.joining
7270
import java.util.stream.Collectors.toList
7371
import javax.servlet.http.HttpServletRequest
74-
import kotlin.random.Random
7572

7673
/**
7774
* @author Maksim Gramin (mgramin@gmail.com)

src/main/kotlin/com/github/mgramin/sqlboot/rest/controllers/DbConnectionsController.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,22 @@
2424

2525
package com.github.mgramin.sqlboot.rest.controllers
2626

27+
import com.github.mgramin.sqlboot.model.connection.CheckedConnection
28+
import com.github.mgramin.sqlboot.model.connection.DbConnection
2729
import com.github.mgramin.sqlboot.model.connection.DbConnectionList
2830
import com.github.mgramin.sqlboot.model.connection.SimpleDbConnection
2931
import org.springframework.beans.factory.annotation.Autowired
3032
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
3133
import org.springframework.context.annotation.ComponentScan
34+
import org.springframework.http.MediaType
3235
import org.springframework.web.bind.annotation.CrossOrigin
36+
import org.springframework.web.bind.annotation.GetMapping
3337
import org.springframework.web.bind.annotation.RequestMapping
38+
import org.springframework.web.bind.annotation.ResponseBody
3439
import org.springframework.web.bind.annotation.RestController
40+
import reactor.core.publisher.Flux
41+
import reactor.core.publisher.toFlux
42+
import reactor.core.scheduler.Schedulers
3543

3644
/**
3745
* @author Maksim Gramin (mgramin@gmail.com)
@@ -52,4 +60,16 @@ class DbConnectionsController @Autowired constructor(private val dbConnectionLis
5260
@RequestMapping(value = ["/connections"])
5361
get() = dbConnectionList.connections*/
5462

63+
@GetMapping(value = ["/connections/health"], produces = [MediaType.TEXT_EVENT_STREAM_VALUE])
64+
@ResponseBody
65+
internal fun health(): Flux<DbConnection> {
66+
return dbConnectionList
67+
.connections
68+
.toFlux()
69+
.parallel()
70+
.runOn(Schedulers.elastic())
71+
.map { CheckedConnection(it) as DbConnection }
72+
.sequential()
73+
}
74+
5575
}

src/test/kotlin/com/github/mgramin/sqlboot/model/resourcetype/impl/composite/FsResourceTypesTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class FsResourceTypesTest {
4848
private val db = SimpleDbConnection()
4949

5050
init {
51-
db.name = "unit_test_db"
51+
db.setName("unit_test_db")
5252
db.baseFolder = FileSystemResource("conf/h2/database")
5353
db.url = "jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'classpath:schema.sql';"
5454
db.paginationQueryTemplate = "${'$'}{query} offset ${'$'}{uri.pageSize()*(uri.pageNumber()-1)} limit ${'$'}{uri.pageSize()}"

0 commit comments

Comments
 (0)