All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.dankito.utils.info.SystemProperties.kt Maven / Gradle / Ivy

There is a newer version: 1.0.20
Show newest version
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)
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy