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

yakworks.message.MsgServiceRegistry.kt Maven / Gradle / Ivy

There is a newer version: 3.14
Show newest version
package yakworks.message

import yakworks.message.spi.MsgService
import yakworks.message.spi.MsgServiceProvider
import java.util.*

/**
 * Looks up MsgServiceProvider
 */
object MsgServiceRegistry {
    private var INSTANCE: MsgService? = null
    private var isLoaded = false

    // recomended to use the serviceLoader but can use this to set the static on app startup
    @JvmStatic
    var service: MsgService?
        get() {
            if (INSTANCE == null && !isLoaded) {
                INSTANCE = loadMsgService()
                isLoaded = true
            }
            return INSTANCE
        }
        set(value) {
            INSTANCE = value
        }

    fun loadMsgService(): MsgService? {
        val msp = loadService(MsgServiceProvider::class.java)
        return msp?.get()
    }

    /**
     * Generic implementation of a service lookup, can be refactored out into helper later
     */
    fun  loadService(serviceType: Class?): SERVICE? {
        val services: Iterator =
            ServiceLoader.load(serviceType, MsgServiceRegistry::class.java.classLoader).iterator()
        val result = if (services.hasNext()) services.next() else null
        //if only one should be expected then this can show error
        if (result == null) {
            System.err.print("No MsgService Found")
        } else if (services.hasNext()) {
            System.err.printf(
                "Found multiple implementations for the service provider %s. Only one should be setup, Using the first one",
                serviceType
            )
        }
        return result
    }

    // another examlpe but with a default that can be passed in.
    operator fun  get(serviceType: Class?, defaultValue: SERVICE): SERVICE {
        val services: Iterator =
            ServiceLoader.load(serviceType, MsgServiceRegistry::class.java.classLoader).iterator()
        var result = if (services.hasNext()) services.next() else defaultValue
        //if only one should be expected then this can show error
        if (services.hasNext()) {
            result = defaultValue
            //System.err.println(
            //    String.format(
            //        "Found multiple implementations for the service provider %s. Using the default: %s",
            //        serviceType, result.javaClass
            //    )
            //)
        }
        return result
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy