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

jvmBaseMain.Services.kt Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2019-2023 Mamoe Technologies and contributors.
 *
 * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
 * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
 *
 * https://github.com/mamoe/mirai/blob/dev/LICENSE
 */

package net.mamoe.mirai.utils

import java.util.*
import kotlin.reflect.KClass
import kotlin.reflect.full.createInstance

private enum class LoaderType {
    JDK,
    BOTH,
    FALLBACK,
}

private val loaderType = when (systemProp("mirai.service.loader", "both")) {
    "jdk" -> LoaderType.JDK
    "both" -> LoaderType.BOTH
    "fallback" -> LoaderType.FALLBACK
    else -> throw IllegalStateException("cannot find a service loader, mirai.service.loader must be both, jdk or fallback (default by both)")
}

@Suppress("UNCHECKED_CAST")
public actual fun  loadService(clazz: KClass, fallbackImplementation: String?): T {
    val fallbackService by lazy {
        Services.firstImplementationOrNull(Services.qualifiedNameOrFail(clazz)) as T?
    }

    val jdkService by lazy {
        ServiceLoader.load(clazz.java).firstOrNull()?.let { return@lazy it }

        ServiceLoader.load(clazz.java, clazz.java.classLoader).firstOrNull()
    }

    var suppressed: Throwable? = null

    val services by lazy {
        when (loaderType) {
            LoaderType.JDK -> jdkService
            LoaderType.BOTH -> jdkService ?: fallbackService
            LoaderType.FALLBACK -> fallbackService
        }?.let { return@lazy it }

        if (fallbackImplementation != null) {
            runCatching {
                findCreateInstance(fallbackImplementation)
            }.onFailure { suppressed = it }.getOrNull()
        } else null
    }

    return Services.getOverrideOrNull(clazz) ?: services
    ?: throw NoSuchElementException("Could not find an implementation for service class ${clazz.qualifiedName}").apply {
        if (suppressed != null) addSuppressed(suppressed!!)
    }
}

private fun  findCreateInstance(fallbackImplementation: String): T {
    return Class.forName(fallbackImplementation).cast>().kotlin.run { objectInstance ?: createInstance() }
}

public actual fun  loadServiceOrNull(clazz: KClass, fallbackImplementation: String?): T? {
    return runCatching { loadService(clazz, fallbackImplementation) }.getOrNull()
}

@Suppress("UNCHECKED_CAST")
public actual fun  loadServices(clazz: KClass): Sequence {
    fun fallBackServicesSeq(): Sequence {
        return Services.implementations(Services.qualifiedNameOrFail(clazz)).orEmpty()
            .map { it.value as T }
    }

    fun jdkServices(): Sequence = sequence {
        val current = ServiceLoader.load(clazz.java).iterator()
        if (current.hasNext()) {
            yieldAll(current)
        } else {
            yieldAll(ServiceLoader.load(clazz.java, clazz.java.classLoader))
        }
    }

    fun bothServices(): Sequence = sequence {
        Services.getOverrideOrNull(clazz)?.let { yield(it) }

        var jdkServices = ServiceLoader.load(clazz.java).toList()
        if (jdkServices.isEmpty()) {
            jdkServices = ServiceLoader.load(clazz.java, clazz.java.classLoader).toList()
        }
        yieldAll(jdkServices)

        Services.implementationsDirectly(Services.qualifiedNameOrFail(clazz)).asSequence()
            .filter { impl ->
                // Drop duplicated
                jdkServices.none { it.javaClass.name == impl.implementationClass }
            }
            .forEach { yield(it.instance.value as T) }
    }




    return when (loaderType) {
        LoaderType.JDK -> jdkServices()
        LoaderType.BOTH -> bothServices()
        LoaderType.FALLBACK -> fallBackServicesSeq()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy