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

io.getunleash.polling.CacheDirectoryProvider.kt Maven / Gradle / Ivy

package io.getunleash.polling

import java.io.File
import java.nio.file.Files

class CacheDirectoryProvider {

    @Suppress("NewApi")
    fun getCacheDirectory(): File = try {
        Files.createTempDirectory("unleash_toggles").toFile()
    } catch (e: NoClassDefFoundError) {
        val file = getCacheDirectoryFile()
        createDirectoryIfNotExists(file)
        file
    }

    private fun getCacheDirectoryFile() = File("unleash_toggles")

    private fun createDirectoryIfNotExists(file: File) {
        if (!file.exists())
            createDirectoryAndAddShutdownHook(file)
    }

    private fun createDirectoryAndAddShutdownHook(file: File) {
        file.mkdirs()
        Runtime.getRuntime().addShutdownHook(getShutdownHook(file))
    }

    private fun getShutdownHook(file: File): Thread {
        return DeleteFileShutdownHook(file)
    }

    private class DeleteFileShutdownHook(file: File) : Thread(Runnable {
        file.deleteRecursively()
    })
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy