-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
ClasspathHandler.kt
32 lines (27 loc) · 947 Bytes
/
ClasspathHandler.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.hexagonkt.core
import com.hexagonkt.core.logging.logger
import java.net.URL
import java.net.URLConnection
import java.net.URLStreamHandler
object ClasspathHandler : URLStreamHandler() {
private val classLoader: ClassLoader by lazy { Thread.currentThread().contextClassLoader }
private val protocolHandlers: Map<String, URLStreamHandler> by lazy {
mapOf("classpath" to this)
}
override fun openConnection(url: URL): URLConnection =
classLoader.getResource(url.path)?.openConnection()
?: throw ResourceNotFoundException("$url cannot be open")
fun registerHandler() {
try {
URL.setURLStreamHandlerFactory {
createURLStreamHandler(it)
}
}
catch (e: Error) {
logger.error(e)
}
}
fun createURLStreamHandler(protocol: String): URLStreamHandler? {
return protocolHandlers[protocol]
}
}