15
15
package org.modelix.model.server
16
16
17
17
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
22
18
import io.ktor.server.application.*
23
19
import io.ktor.server.engine.*
24
20
import io.ktor.server.html.*
@@ -27,148 +23,36 @@ import io.ktor.server.plugins.forwardedheaders.*
27
23
import io.ktor.server.response.*
28
24
import io.ktor.server.routing.*
29
25
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
31
34
import org.apache.commons.io.FileUtils
32
35
import org.apache.ignite.Ignition
33
36
import org.modelix.authorization.KeycloakUtils
34
37
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
35
45
import org.slf4j.LoggerFactory
36
46
import java.io.File
37
47
import java.io.FileReader
38
48
import java.io.FileWriter
39
49
import java.io.IOException
40
50
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.*
46
51
import javax.sql.DataSource
47
52
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
-
136
53
object Main {
137
54
private val LOG = LoggerFactory .getLogger(Main ::class .java)
138
55
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
- }
172
56
173
57
@JvmStatic
174
58
fun main (args : Array <String >) {
@@ -225,15 +109,15 @@ object Main {
225
109
val dataSource: DataSource = Ignition .loadSpringBean<DataSource >(
226
110
Main ::class .java.getResource(" ignite.xml" ), " dataSource"
227
111
)
228
- ensureSchemaInitialization (dataSource)
112
+ SqlUtils (dataSource.connection).ensureSchemaInitialization( )
229
113
}
230
114
}
231
115
var i = 0
232
116
while (i < cmdLineArgs.setValues.size) {
233
117
storeClient.put(cmdLineArgs.setValues[i], cmdLineArgs.setValues[i + 1 ])
234
118
i + = 2
235
119
}
236
- val modelServer = KtorModelServer (storeClient)
120
+ val modelServer = KeyValueLikeModelServer (storeClient)
237
121
val localModelClient = LocalModelClient (storeClient)
238
122
val sharedSecretFile = cmdLineArgs.secretFile
239
123
if (sharedSecretFile.exists()) {
@@ -243,7 +127,7 @@ object Main {
243
127
}
244
128
245
129
val historyHandler = HistoryHandler (localModelClient)
246
- val jsonModelServer = JsonModelServer (localModelClient)
130
+ val jsonModelServer = DeprecatedLightModelServer (localModelClient)
247
131
val ktorServer: NettyApplicationEngine = embeddedServer(Netty , port = port) {
248
132
install(Routing )
249
133
installAuthentication(unitTestMode = ! KeycloakUtils .isEnabled())
@@ -297,13 +181,11 @@ object Main {
297
181
ktorServer.stop()
298
182
LOG .info(" Server stopped" )
299
183
} catch (ex: Exception ) {
300
- System .err.println (" Exception: " + ex.message)
301
- ex.printStackTrace()
184
+ LOG .error(" " , ex)
302
185
}
303
186
})
304
187
} catch (ex: Exception ) {
305
- println (" Server failed: " + ex.message)
306
- ex.printStackTrace()
188
+ LOG .error(" " , ex)
307
189
}
308
190
}
309
191
0 commit comments