@@ -2,6 +2,7 @@ package org.utbot.common
22
33import java.io.FileInputStream
44import java.io.IOException
5+ import java.io.InputStream
56import java.util.*
67import kotlin.Comparator
78import mu.KLogger
@@ -17,6 +18,8 @@ interface SettingsContainer {
1718 converter : (String ) -> T
1819 ): PropertyDelegateProvider <Any ?, ReadWriteProperty <Any ?, T >>
1920
21+ fun getInputStream () : InputStream ? = null
22+
2023 // Returns true iff some properties have non-default values
2124 fun isCustomized () = false
2225}
@@ -28,10 +31,10 @@ interface SettingsContainerFactory {
2831 defaultSettingsPath : String? = null) : SettingsContainer
2932}
3033
31- class PropertiesSettingsContainer (
34+ internal open class PropertiesSettingsContainer (
3235 private val logger : KLogger ,
33- defaultKeyForSettingsPath : String ,
34- defaultSettingsPath : String? = null ): SettingsContainer {
36+ val defaultKeyForSettingsPath : String ,
37+ val defaultSettingsPath : String? = null ): SettingsContainer {
3538 companion object : SettingsContainerFactory {
3639 override fun createSettingsContainer (
3740 logger : KLogger ,
@@ -41,19 +44,21 @@ class PropertiesSettingsContainer(
4144 }
4245
4346 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)
5350 }
51+ } catch (e: IOException ) {
52+ logger.info(e) { e.message }
5453 }
5554 }
5655
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+
5762 private val settingsValues: MutableMap <KProperty <* >, Any? > = mutableMapOf ()
5863 private var customized: Boolean = false
5964
@@ -156,7 +161,12 @@ abstract class AbstractSettings(
156161 return container.settingFor(defaultValue, null , converter)
157162 }
158163
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+ })
160170 protected fun getIntProperty (defaultValue : Int ) = getProperty(defaultValue, converter = String ::toInt)
161171 protected fun getIntProperty (defaultValue : Int , minValue : Int , maxValue : Int ) = getProperty(defaultValue, Triple (minValue, maxValue, Comparator (Integer ::compare)), String ::toInt)
162172 protected fun getLongProperty (defaultValue : Long ) = getProperty(defaultValue, converter = String ::toLong)
0 commit comments