Skip to content

Commit 937657b

Browse files
author
Oleksandr Dzhychko
committed
chore(model-server): pass properties instead of a file to create IgniteStoreClient
Passing properties instead of files simplifies testing.
1 parent 76862bf commit 937657b

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ import org.modelix.model.server.store.writeDump
7575
import org.slf4j.LoggerFactory
7676
import org.springframework.util.ResourceUtils
7777
import java.io.File
78+
import java.io.FileReader
7879
import java.io.IOException
7980
import java.nio.charset.StandardCharsets
8081
import java.time.Duration
82+
import java.util.Properties
8183
import javax.sql.DataSource
8284

8385
object Main {
@@ -143,9 +145,11 @@ object Main {
143145
)
144146
}
145147
} else if (cmdLineArgs.localPersistence) {
146-
storeClient = IgniteStoreClient(cmdLineArgs.jdbcConfFile, inmemory = true)
148+
val jdbcProperties = cmdLineArgs.jdbcConfFile?.let(::readJdbcProperties)
149+
storeClient = IgniteStoreClient(jdbcProperties, inmemory = true)
147150
} else {
148-
storeClient = IgniteStoreClient(cmdLineArgs.jdbcConfFile)
151+
val jdbcProperties = cmdLineArgs.jdbcConfFile?.let(::readJdbcProperties)
152+
storeClient = IgniteStoreClient(jdbcProperties)
149153
if (cmdLineArgs.schemaInit) {
150154
val dataSource: DataSource = Ignition.loadSpringBean<DataSource>(
151155
Main::class.java.getResource("ignite.xml"),
@@ -251,6 +255,16 @@ object Main {
251255
}
252256
}
253257

258+
private fun readJdbcProperties(jdbcConfFile: File): Properties {
259+
val properties = Properties()
260+
try {
261+
properties.load(FileReader(jdbcConfFile))
262+
} catch (e: IOException) {
263+
throw IllegalStateException("Could not load the JDBC configuration from ${jdbcConfFile.absolutePath}.", e)
264+
}
265+
return properties
266+
}
267+
254268
/**
255269
* Installs the status pages extension with a configuration suitable for generating application/problem+json
256270
* responses as defined in the API specification.

model-server/src/main/kotlin/org/modelix/model/server/store/IgniteStoreClient.kt

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ import org.modelix.model.IGenericKeyListener
2626
import org.modelix.model.lazy.RepositoryId
2727
import org.modelix.model.persistent.HashUtil
2828
import org.modelix.model.server.SqlUtils
29-
import java.io.File
30-
import java.io.FileReader
31-
import java.io.IOException
3229
import java.sql.SQLException
3330
import java.util.*
3431
import javax.cache.Cache
@@ -40,7 +37,9 @@ private val LOG = KotlinLogging.logger { }
4037
* Store client implementation with an ignite cache.
4138
* If [inmemory] is true, the data is not persisted in a database.
4239
*/
43-
class IgniteStoreClient(jdbcConfFile: File? = null, private val inmemory: Boolean = false) : IsolatingStore, AutoCloseable {
40+
class IgniteStoreClient(jdbcProperties: Properties? = null, private val inmemory: Boolean = false) :
41+
IsolatingStore,
42+
AutoCloseable {
4443

4544
companion object {
4645
private const val ENTRY_CHANGED_TOPIC = "entryChanged"
@@ -68,28 +67,14 @@ class IgniteStoreClient(jdbcConfFile: File? = null, private val inmemory: Boolea
6867
* from ignite.xml is used
6968
*/
7069
init {
71-
if (jdbcConfFile != null) {
70+
if (jdbcProperties != null) {
7271
// Given that systemPropertiesMode is set to 2 (SYSTEM_PROPERTIES_MODE_OVERRIDE) in
7372
// ignite.xml, we can override the properties through system properties
74-
try {
75-
val properties = Properties()
76-
properties.load(FileReader(jdbcConfFile))
77-
for (pn in properties.stringPropertyNames()) {
78-
if (pn.startsWith("jdbc.")) {
79-
System.setProperty(pn, properties.getProperty(pn))
80-
} else {
81-
throw RuntimeException(
82-
"Properties not related to jdbc are not permitted. Check file " +
83-
jdbcConfFile.absolutePath,
84-
)
85-
}
73+
for (propertyName in jdbcProperties.stringPropertyNames()) {
74+
require(propertyName.startsWith("jdbc.")) {
75+
"Property `$propertyName` is invalid. Only properties starting with `jdbc.` are permitted."
8676
}
87-
} catch (e: IOException) {
88-
throw RuntimeException(
89-
"We are unable to load the JDBC configuration from " +
90-
jdbcConfFile.absolutePath,
91-
e,
92-
)
77+
System.setProperty(propertyName, jdbcProperties.getProperty(propertyName))
9378
}
9479
}
9580
if (!inmemory) updateDatabaseSchema()

0 commit comments

Comments
 (0)