Skip to content

Commit 684cee1

Browse files
committed
refactor: restructured the model server packages
1 parent ee7b33a commit 684cee1

File tree

21 files changed

+203
-263
lines changed

21 files changed

+203
-263
lines changed

light-model-client/src/jvmTest/kotlin/org/modelix/client/light/LightModelClientTest.kt

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,17 @@ import io.ktor.client.request.*
1919
import io.ktor.server.application.*
2020
import io.ktor.server.testing.*
2121
import io.ktor.websocket.*
22-
import kotlinx.coroutines.CoroutineScope
23-
import kotlinx.coroutines.Dispatchers
24-
import kotlinx.coroutines.channels.Channel
25-
import kotlinx.coroutines.channels.ClosedReceiveChannelException
2622
import kotlinx.coroutines.coroutineScope
2723
import kotlinx.coroutines.delay
2824
import kotlinx.coroutines.launch
2925
import kotlinx.coroutines.withTimeout
3026
import org.modelix.authorization.installAuthentication
3127
import org.modelix.model.api.addNewChild
3228
import org.modelix.model.api.getDescendants
33-
import org.modelix.model.server.InMemoryStoreClient
34-
import org.modelix.model.server.JsonModelServer
35-
import org.modelix.model.server.JsonModelServer2
36-
import org.modelix.model.server.LocalModelClient
37-
import org.modelix.model.server.api.MessageFromClient
38-
import org.modelix.model.server.api.MessageFromServer
29+
import org.modelix.model.server.store.InMemoryStoreClient
30+
import org.modelix.model.server.handlers.DeprecatedLightModelServer
31+
import org.modelix.model.server.handlers.LightModelServer
32+
import org.modelix.model.server.store.LocalModelClient
3933
import org.modelix.model.test.RandomModelChangeGenerator
4034
import kotlin.random.Random
4135
import kotlin.test.Ignore
@@ -54,8 +48,8 @@ class LightModelClientTest {
5448
install(io.ktor.server.websocket.WebSockets)
5549
val modelClient = LocalModelClient(InMemoryStoreClient())
5650
localModelClient = modelClient
57-
JsonModelServer(modelClient).init(this)
58-
JsonModelServer2(modelClient).init(this)
51+
DeprecatedLightModelServer(modelClient).init(this)
52+
LightModelServer(modelClient).init(this)
5953
}
6054
val client = createClient {
6155
install(WebSockets)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.modelix.model.server
2+
3+
import com.beust.jcommander.Parameter
4+
import com.beust.jcommander.converters.BooleanConverter
5+
import com.beust.jcommander.converters.IntegerConverter
6+
import com.beust.jcommander.converters.StringConverter
7+
import java.io.File
8+
import java.util.*
9+
10+
internal class CmdLineArgs {
11+
@Parameter(names = ["-secret"], description = "Path to the secretfile", converter = FileConverter::class)
12+
var secretFile = File("/secrets/modelsecret/modelsecret.txt")
13+
14+
@Parameter(
15+
names = ["-jdbcconf"],
16+
description = "Path to the JDBC configuration file",
17+
converter = FileConverter::class
18+
)
19+
var jdbcConfFile: File? = null
20+
21+
@Parameter(names = ["-inmemory"], description = "Use in-memory storage", converter = BooleanConverter::class)
22+
var inmemory = false
23+
24+
@Parameter(names = ["-dumpout"], description = "Dump in memory storage", converter = StringConverter::class)
25+
var dumpOutName: String? = null
26+
27+
@Parameter(names = ["-dumpin"], description = "Read dump in memory storage", converter = StringConverter::class)
28+
var dumpInName: String? = null
29+
30+
@Parameter(names = ["-port"], description = "Set port", converter = IntegerConverter::class)
31+
var port: Int? = null
32+
33+
@Parameter(names = ["-set"], description = "Set values", arity = 2)
34+
var setValues: List<String> = LinkedList<String>()
35+
36+
@Parameter(
37+
names = ["-schemainit"],
38+
description = "Initialize the schema, if necessary",
39+
converter = BooleanConverter::class
40+
)
41+
var schemaInit = false
42+
}

model-server/src/main/kotlin/org/modelix/model/server/Main.kt

Lines changed: 20 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
package org.modelix.model.server
1616

1717
import com.beust.jcommander.JCommander
18-
import com.beust.jcommander.Parameter
19-
import com.beust.jcommander.converters.BooleanConverter
20-
import com.beust.jcommander.converters.IntegerConverter
21-
import com.beust.jcommander.converters.StringConverter
2218
import io.ktor.server.application.*
2319
import io.ktor.server.engine.*
2420
import io.ktor.server.html.*
@@ -27,148 +23,36 @@ import io.ktor.server.plugins.forwardedheaders.*
2723
import io.ktor.server.response.*
2824
import io.ktor.server.routing.*
2925
import io.ktor.server.websocket.*
30-
import kotlinx.html.*
26+
import kotlinx.html.a
27+
import kotlinx.html.body
28+
import kotlinx.html.br
29+
import kotlinx.html.div
30+
import kotlinx.html.head
31+
import kotlinx.html.li
32+
import kotlinx.html.style
33+
import kotlinx.html.ul
3134
import org.apache.commons.io.FileUtils
3235
import org.apache.ignite.Ignition
3336
import org.modelix.authorization.KeycloakUtils
3437
import org.modelix.authorization.installAuthentication
38+
import org.modelix.model.server.handlers.HistoryHandler
39+
import org.modelix.model.server.handlers.DeprecatedLightModelServer
40+
import org.modelix.model.server.handlers.KeyValueLikeModelServer
41+
import org.modelix.model.server.store.IStoreClient
42+
import org.modelix.model.server.store.IgniteStoreClient
43+
import org.modelix.model.server.store.InMemoryStoreClient
44+
import org.modelix.model.server.store.LocalModelClient
3545
import org.slf4j.LoggerFactory
3646
import java.io.File
3747
import java.io.FileReader
3848
import java.io.FileWriter
3949
import java.io.IOException
4050
import java.nio.charset.StandardCharsets
41-
import java.sql.Connection
42-
import java.sql.DatabaseMetaData
43-
import java.sql.ResultSet
44-
import java.sql.SQLException
45-
import java.util.*
4651
import javax.sql.DataSource
4752

48-
internal class CmdLineArgs {
49-
@Parameter(names = ["-secret"], description = "Path to the secretfile", converter = FileConverter::class)
50-
var secretFile = File("/secrets/modelsecret/modelsecret.txt")
51-
52-
@Parameter(
53-
names = ["-jdbcconf"],
54-
description = "Path to the JDBC configuration file",
55-
converter = FileConverter::class
56-
)
57-
var jdbcConfFile: File? = null
58-
59-
@Parameter(names = ["-inmemory"], description = "Use in-memory storage", converter = BooleanConverter::class)
60-
var inmemory = false
61-
62-
@Parameter(names = ["-dumpout"], description = "Dump in memory storage", converter = StringConverter::class)
63-
var dumpOutName: String? = null
64-
65-
@Parameter(names = ["-dumpin"], description = "Read dump in memory storage", converter = StringConverter::class)
66-
var dumpInName: String? = null
67-
68-
@Parameter(names = ["-port"], description = "Set port", converter = IntegerConverter::class)
69-
var port: Int? = null
70-
71-
@Parameter(names = ["-set"], description = "Set values", arity = 2)
72-
var setValues: List<String> = LinkedList<String>()
73-
74-
@Parameter(
75-
names = ["-schemainit"],
76-
description = "Initialize the schema, if necessary",
77-
converter = BooleanConverter::class
78-
)
79-
var schemaInit = false
80-
}
81-
82-
internal class SqlUtils(private val connection: Connection) {
83-
@Throws(SQLException::class)
84-
fun isSchemaExisting(schemaName: String?): Boolean {
85-
val metadata: DatabaseMetaData = connection.metaData
86-
val schemasRS: ResultSet = metadata.getSchemas()
87-
while (schemasRS.next()) {
88-
if (schemasRS.getString("table_schem") == schemaName) {
89-
return true
90-
}
91-
}
92-
return false
93-
}
94-
95-
@Throws(SQLException::class)
96-
fun isTableExisting(schemaName: String?, tableName: String): Boolean {
97-
val metadata: DatabaseMetaData = connection.metaData
98-
val schemasRS: ResultSet = metadata.getTables(null, schemaName, tableName, null)
99-
while (schemasRS.next()) {
100-
if (schemasRS.getString("table_schem") == schemaName && schemasRS.getString("table_name") == tableName) {
101-
return true
102-
}
103-
}
104-
return false
105-
}
106-
107-
@Throws(SQLException::class)
108-
fun ensureTableIsPresent(
109-
schemaName: String?, username: String?, tableName: String, creationSql: String?
110-
) {
111-
if (!isTableExisting(schemaName, tableName)) {
112-
val stmt = connection.createStatement()
113-
stmt.execute(creationSql)
114-
}
115-
val stmt = connection.createStatement()
116-
stmt.execute(
117-
"GRANT ALL ON TABLE $schemaName.$tableName TO $username;"
118-
)
119-
}
120-
121-
@Throws(SQLException::class)
122-
fun ensureSchemaIsPresent(schemaName: String?, username: String?) {
123-
if (!isSchemaExisting(schemaName)) {
124-
val stmt = connection.createStatement()
125-
stmt.execute("CREATE SCHEMA $schemaName;")
126-
}
127-
val stmt = connection.createStatement()
128-
stmt.execute("GRANT ALL ON SCHEMA $schemaName TO $username;")
129-
}
130-
131-
companion object {
132-
private val LOG = LoggerFactory.getLogger(SqlUtils::class.java)
133-
}
134-
}
135-
13653
object Main {
13754
private val LOG = LoggerFactory.getLogger(Main::class.java)
13855
const val DEFAULT_PORT = 28101
139-
private const val DEFAULT_DB_USER_NAME = "modelix"
140-
private const val DEFAULT_SCHEMA_NAME = "modelix"
141-
private fun ensureSchemaInitialization(dataSource: DataSource) {
142-
var userName = System.getProperty("jdbc.user")
143-
if (userName == null) {
144-
userName = DEFAULT_DB_USER_NAME
145-
}
146-
var schemaName = System.getProperty("jdbc.schema")
147-
if (schemaName == null) {
148-
schemaName = DEFAULT_SCHEMA_NAME
149-
}
150-
LOG.info("ensuring schema initialization")
151-
LOG.info(" schema: $schemaName")
152-
LOG.info(" db username: $userName")
153-
try {
154-
val sqlUtils = SqlUtils(dataSource.connection)
155-
sqlUtils.ensureSchemaIsPresent(schemaName, userName)
156-
sqlUtils.ensureTableIsPresent(
157-
schemaName,
158-
userName,
159-
"model",
160-
"""CREATE TABLE $schemaName.model
161-
(
162-
key character varying NOT NULL,
163-
value character varying,
164-
reachable boolean,
165-
CONSTRAINT kv_pkey PRIMARY KEY (key)
166-
);"""
167-
)
168-
} catch (e: SQLException) {
169-
e.printStackTrace()
170-
}
171-
}
17256

17357
@JvmStatic
17458
fun main(args: Array<String>) {
@@ -225,15 +109,15 @@ object Main {
225109
val dataSource: DataSource = Ignition.loadSpringBean<DataSource>(
226110
Main::class.java.getResource("ignite.xml"), "dataSource"
227111
)
228-
ensureSchemaInitialization(dataSource)
112+
SqlUtils(dataSource.connection).ensureSchemaInitialization()
229113
}
230114
}
231115
var i = 0
232116
while (i < cmdLineArgs.setValues.size) {
233117
storeClient.put(cmdLineArgs.setValues[i], cmdLineArgs.setValues[i + 1])
234118
i += 2
235119
}
236-
val modelServer = KtorModelServer(storeClient)
120+
val modelServer = KeyValueLikeModelServer(storeClient)
237121
val localModelClient = LocalModelClient(storeClient)
238122
val sharedSecretFile = cmdLineArgs.secretFile
239123
if (sharedSecretFile.exists()) {
@@ -243,7 +127,7 @@ object Main {
243127
}
244128

245129
val historyHandler = HistoryHandler(localModelClient)
246-
val jsonModelServer = JsonModelServer(localModelClient)
130+
val jsonModelServer = DeprecatedLightModelServer(localModelClient)
247131
val ktorServer: NettyApplicationEngine = embeddedServer(Netty, port = port) {
248132
install(Routing)
249133
installAuthentication(unitTestMode = !KeycloakUtils.isEnabled())
@@ -297,13 +181,11 @@ object Main {
297181
ktorServer.stop()
298182
LOG.info("Server stopped")
299183
} catch (ex: Exception) {
300-
System.err.println("Exception: " + ex.message)
301-
ex.printStackTrace()
184+
LOG.error("", ex)
302185
}
303186
})
304187
} catch (ex: Exception) {
305-
println("Server failed: " + ex.message)
306-
ex.printStackTrace()
188+
LOG.error("", ex)
307189
}
308190
}
309191

model-server/src/main/kotlin/org/modelix/model/server/RedisStoreClient.kt

Lines changed: 0 additions & 83 deletions
This file was deleted.

0 commit comments

Comments
 (0)