net.dankito.utils.info.SystemProperties.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-utils Show documentation
Show all versions of java-utils Show documentation
Some basic utils needed in many projects
package net.dankito.utils.info
import org.slf4j.Logger
open class SystemProperties {
companion object {
const val UserNamePropertyName = "user.name"
const val UserCountryPropertyName = "user.country"
const val UserLanguagePropertyName = "user.language"
const val UserHomeDirectoryPropertyName = "user.home"
const val OsNamePropertyName = "os.name"
const val OsVersionPropertyName = "os.version"
const val CpuArchitecturePropertyName = "os.arch"
const val JavaVersionPropertyName = "java.specification.version"
const val JavaRuntimeVersionPropertyName = "java.runtime.version"
const val StartupDirectoryPropertyName = "user.dir"
const val StartupClassPathPropertyName = "java.class.path"
}
open val propertyNames: Set = System.getProperties().stringPropertyNames()
open val propertyValues: Map = propertyNames.associateWith { System.getProperty(it) }
open val userName: String = getProperty(UserNamePropertyName, "")
open val userCountry: String = getProperty(UserCountryPropertyName, "")
open val userLanguage: String = getProperty(UserLanguagePropertyName, "")
open val userHomeDirectory: String = getProperty(UserHomeDirectoryPropertyName, "")
open val osName: String = getProperty(OsNamePropertyName, "")
open val osVersion: String = getProperty(OsVersionPropertyName, "")
open val cpuArchitecture: String = getProperty(CpuArchitecturePropertyName, "")
open val javaVersion: String = getProperty(JavaVersionPropertyName, "")
open val javaRuntimeVersion: String = getProperty(JavaRuntimeVersionPropertyName, "")
open val startupDirectory: String = getProperty(StartupDirectoryPropertyName, "")
open val startupClassPath: String = getProperty(StartupClassPathPropertyName, "")
open fun getProperty(name: String): String? {
return propertyValues[name]
}
open fun getProperty(name: String, defaultValue: String): String {
return getProperty(name) ?: defaultValue
}
open fun printAllProperties() {
doForEachProperty { name, value ->
println("$name: $value")
}
}
open fun logAllProperties(log: Logger) {
doForEachProperty { name, value ->
log.info("$name: $value")
}
}
open fun doForEachProperty(property: (name: String, value: String) -> Unit) {
propertyValues.forEach { entry ->
property(entry.key, entry.value)
}
}
}