@@ -2,6 +2,7 @@ package org.utbot.common
2
2
3
3
import java.io.FileInputStream
4
4
import java.io.IOException
5
+ import java.io.InputStream
5
6
import java.util.*
6
7
import kotlin.Comparator
7
8
import mu.KLogger
@@ -17,6 +18,8 @@ interface SettingsContainer {
17
18
converter : (String ) -> T
18
19
): PropertyDelegateProvider <Any ?, ReadWriteProperty <Any ?, T >>
19
20
21
+ fun getInputStream () : InputStream ? = null
22
+
20
23
// Returns true iff some properties have non-default values
21
24
fun isCustomized () = false
22
25
}
@@ -28,10 +31,10 @@ interface SettingsContainerFactory {
28
31
defaultSettingsPath : String? = null) : SettingsContainer
29
32
}
30
33
31
- class PropertiesSettingsContainer (
34
+ internal open class PropertiesSettingsContainer (
32
35
private val logger : KLogger ,
33
- defaultKeyForSettingsPath : String ,
34
- defaultSettingsPath : String? = null ): SettingsContainer {
36
+ val defaultKeyForSettingsPath : String ,
37
+ val defaultSettingsPath : String? = null ): SettingsContainer {
35
38
companion object : SettingsContainerFactory {
36
39
override fun createSettingsContainer (
37
40
logger : KLogger ,
@@ -41,19 +44,21 @@ class PropertiesSettingsContainer(
41
44
}
42
45
43
46
private val properties = Properties ().also { props ->
44
- val settingsPath = System .getProperty(defaultKeyForSettingsPath) ? : defaultSettingsPath
45
- val settingsPathFile = settingsPath?.toPath()?.toFile()
46
- if (settingsPathFile?.exists() == true ) {
47
- try {
48
- FileInputStream (settingsPathFile).use { reader ->
49
- props.load(reader)
50
- }
51
- } catch (e: IOException ) {
52
- logger.info(e) { e.message }
47
+ try {
48
+ getInputStream()?.use {
49
+ props.load(it)
53
50
}
51
+ } catch (e: IOException ) {
52
+ logger.info(e) { e.message }
54
53
}
55
54
}
56
55
56
+ override fun getInputStream () : InputStream ? {
57
+ val settingsPath = System .getProperty(defaultKeyForSettingsPath) ? : defaultSettingsPath
58
+ val settingsPathFile = settingsPath?.toPath()?.toFile()
59
+ return if (settingsPathFile?.exists() == true ) FileInputStream (settingsPathFile) else null
60
+ }
61
+
57
62
private val settingsValues: MutableMap <KProperty <* >, Any? > = mutableMapOf ()
58
63
private var customized: Boolean = false
59
64
@@ -156,7 +161,12 @@ abstract class AbstractSettings(
156
161
return container.settingFor(defaultValue, null , converter)
157
162
}
158
163
159
- protected fun getBooleanProperty (defaultValue : Boolean ) = getProperty(defaultValue, converter = String ::toBoolean)
164
+ protected fun getBooleanProperty (defaultValue : Boolean ) = getProperty(defaultValue, converter = {
165
+ // Invalid values shouldn't be parsed as "false"
166
+ if (it.equals(" true" , true )) true
167
+ else if (it.equals(" false" , true )) false
168
+ else defaultValue
169
+ })
160
170
protected fun getIntProperty (defaultValue : Int ) = getProperty(defaultValue, converter = String ::toInt)
161
171
protected fun getIntProperty (defaultValue : Int , minValue : Int , maxValue : Int ) = getProperty(defaultValue, Triple (minValue, maxValue, Comparator (Integer ::compare)), String ::toInt)
162
172
protected fun getLongProperty (defaultValue : Long ) = getProperty(defaultValue, converter = String ::toLong)
0 commit comments