|
| 1 | +package kotlinx.coroutines.internal |
| 2 | + |
| 3 | +import java.util.* |
| 4 | +import java.io.* |
| 5 | +import java.net.* |
| 6 | +import java.util.jar.* |
| 7 | +import java.util.zip.* |
| 8 | + |
| 9 | +/** |
| 10 | + * Name of the boolean property that enables using of [FastServiceLoader]. |
| 11 | + */ |
| 12 | +private const val FAST_SERVICE_LOADER_PROPERTY_NAME = "kotlinx.coroutines.verify.service.loader" |
| 13 | + |
| 14 | +/** |
| 15 | + * A simplified version of [ServiceLoader]. |
| 16 | + * FastServiceLoader locates and instantiates all service providers named in configuration |
| 17 | + * files placed in the resource directory <tt>META-INF/services</tt>. |
| 18 | + * |
| 19 | + * The main difference between this class and classic service loader is in skipping |
| 20 | + * verification JARs. A verification requires reading the whole JAR (and it causes problems and ANRs on Android devices) |
| 21 | + * and prevents only trivial checksum issues. See #878. |
| 22 | + * |
| 23 | + * If any error occurs during loading, it fallbacks to [ServiceLoader], mostly to prevent R8 issues. |
| 24 | + */ |
| 25 | + |
| 26 | +internal object FastServiceLoader { |
| 27 | + private const val PREFIX: String = "META-INF/services/" |
| 28 | + |
| 29 | + @JvmField |
| 30 | + internal val FAST_SERVICE_LOADER_ENABLED = systemProp(FAST_SERVICE_LOADER_PROPERTY_NAME, true) |
| 31 | + |
| 32 | + internal fun <S> load(service: Class<S>, loader: ClassLoader): List<S> { |
| 33 | + if (!FAST_SERVICE_LOADER_ENABLED) { |
| 34 | + return ServiceLoader.load(service, loader).toList() |
| 35 | + } |
| 36 | + return try { |
| 37 | + loadProviders(service, loader) |
| 38 | + } catch (e: Throwable) { |
| 39 | + // Fallback to default service loader |
| 40 | + ServiceLoader.load(service, loader).toList() |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + internal fun <S> loadProviders(service: Class<S>, loader: ClassLoader): List<S> { |
| 45 | + val fullServiceName = PREFIX + service.name |
| 46 | + val urls = loader.getResources(fullServiceName).toList() |
| 47 | + val providers = mutableListOf<S>() |
| 48 | + urls.forEach { |
| 49 | + val providerNames = parse(it) |
| 50 | + providers.addAll(providerNames.map { getProviderInstance(it, loader, service) }) |
| 51 | + } |
| 52 | + require(providers.isNotEmpty()) { "No providers were loaded with FastServiceLoader" } |
| 53 | + return providers |
| 54 | + } |
| 55 | + |
| 56 | + private fun <S> getProviderInstance(name: String, loader: ClassLoader, service: Class<S>): S { |
| 57 | + val clazz = Class.forName(name, false, loader) |
| 58 | + require(service.isAssignableFrom(clazz)) { "Expected service of class $service, but found $clazz" } |
| 59 | + return service.cast(clazz.getDeclaredConstructor().newInstance()) |
| 60 | + } |
| 61 | + |
| 62 | + private fun parse(url: URL): List<String> { |
| 63 | + val string = url.toString() |
| 64 | + return if (string.startsWith("jar")) { |
| 65 | + val pathToJar = string.substringAfter("jar:file:").substringBefore('!') |
| 66 | + val entry = string.substringAfter("!/") |
| 67 | + (JarFile(pathToJar, false) as Closeable).use { file -> |
| 68 | + BufferedReader(InputStreamReader((file as JarFile).getInputStream(ZipEntry(entry)),"UTF-8")).use { r -> |
| 69 | + parseFile(r) |
| 70 | + } |
| 71 | + } |
| 72 | + } else emptyList() |
| 73 | + } |
| 74 | + |
| 75 | + private fun parseFile(r: BufferedReader): List<String> { |
| 76 | + val names = mutableSetOf<String>() |
| 77 | + while (true) { |
| 78 | + val line = r.readLine() ?: break |
| 79 | + val serviceName = line.substringBefore("#").trim() |
| 80 | + require(serviceName.all { it == '.' || Character.isJavaIdentifierPart(it) }) { "Illegal service provider class name: $serviceName" } |
| 81 | + if (serviceName.isNotEmpty()) { |
| 82 | + names.add(serviceName) |
| 83 | + } |
| 84 | + } |
| 85 | + return names.toList() |
| 86 | + } |
| 87 | +} |
0 commit comments