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

com.seeq.utilities.configuration.AutoUpdatingConfiguration.kt Maven / Gradle / Ivy

The newest version!
package com.seeq.utilities.configuration

import com.seeq.utilities.FileWatcher
import java.io.Closeable
import java.nio.file.Paths
import java.time.Duration

/**
 * Wrapper around [SettableConfiguration] that keeps it up to date whenever the on-disk version of the configuration is
 * updated.
 */
open class AutoUpdatingConfiguration : SettableConfiguration(), Closeable {
    private lateinit var fileWatchers: List

    /**
     * Read the current configuration option values from disk and start file watchers to ensure the state is kept up
     * to date.
     */
    fun initialize(): AutoUpdatingConfiguration {
        this.initializeFromDisk()
        this.fileWatchers = listOf(
            Pair(Paths.get(this[Configuration.Folders.Data]).resolve("configuration"), "seeq_config.json"),
            Pair(Paths.get(this[Configuration.Folders.Global]), "global_config.json"),
        )
            .map {
                FileWatcher(
                    it.first, it.second,
                    { initializeConfiguration(this) },
                    Duration.ofSeconds(1),
                )
            }
        this.fileWatchers.forEach { it.start() }

        return this
    }

    /** Stop all file watchers */
    override fun close() = this.fileWatchers.forEach { it.stop() }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy