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

com.malinskiy.marathon.ShutdownHook.kt Maven / Gradle / Ivy

There is a newer version: 0.9.1
Show newest version
package com.malinskiy.marathon

import com.malinskiy.marathon.execution.Configuration

class ShutdownHook(
    configuration: Configuration,
    private val runtime: Runtime = Runtime.getRuntime(),
    val block: () -> Unit
) : Thread() {
    private val debug = configuration.debug

    override fun run() {
        block()
    }

    fun install() {
        return when (debug) {
            true -> try {
                runtime.addShutdownHook(this)
            } catch (e: IllegalStateException) {
            } catch (e: SecurityException) {
            }
            else -> Unit
        }
    }

    fun uninstall(): Boolean {
        return when (debug) {
            true -> try {
                runtime.removeShutdownHook(this)
                true
            } catch (e: IllegalStateException) {
                false
            } catch (e: SecurityException) {
                false
            }
            else -> true
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy