ServiceLoaderLite: Support paths with spaces (KT-28527)

This commit is contained in:
Yan Zhulanow
2018-12-04 17:03:29 +09:00
parent e8a8318ead
commit 9a98a4525b
2 changed files with 17 additions and 3 deletions
@@ -11,7 +11,10 @@ import java.lang.Character.isJavaIdentifierPart
import java.lang.Character.isJavaIdentifierStart
import java.lang.IllegalArgumentException
import java.lang.RuntimeException
import java.lang.UnsupportedOperationException
import java.net.URLClassLoader
import java.nio.file.FileSystemNotFoundException
import java.nio.file.Paths
import java.util.zip.ZipFile
/**
@@ -32,9 +35,13 @@ object ServiceLoaderLite {
*/
fun <Service> loadImplementations(service: Class<out Service>, classLoader: URLClassLoader): List<Service> {
val files = classLoader.urLs.map { url ->
if (url.protocol.toLowerCase() != "file") throw IllegalArgumentException("Only local files are supported, got $url")
val path = url.path.takeIf { it.isNotEmpty() } ?: throw IllegalArgumentException("Path is empty for $url")
File(path)
try {
Paths.get(url.toURI()).toFile()
} catch (e: FileSystemNotFoundException) {
throw IllegalArgumentException("Only local URLs are supported, got ${url.protocol}")
} catch (e: UnsupportedOperationException) {
throw IllegalArgumentException("Only local URLs are supported, got ${url.protocol}")
}
}
val implementations = mutableListOf<Service>()
@@ -31,6 +31,13 @@ class ServiceLoaderLiteTestWithClassLoader : AbstractServiceLoaderLiteTest() {
}
}
fun testDirWithSpaces() {
classLoaderTest("test dir", impls<Intf>(NestedComponent::class), clazz<NestedComponent>()) { classLoader ->
val impls = ServiceLoaderLite.loadImplementations<Intf>(classLoader)
assertTrue(impls.single() is NestedComponent)
}
}
fun testNestedComponent() {
classLoaderTest("test", impls<Intf>(NestedComponent::class), clazz<NestedComponent>()) { classLoader ->
val impls = ServiceLoaderLite.loadImplementations<Intf>(classLoader)