ResourceLoader

ResourceLoader is a testable API for loading resources from the classpath, from the filesystem, from memory, or from another Backend source.

Resource addresses have a scheme name, a colon, and an absolute filesystem-like path: classpath:/migrations/v1.sql. Schemes identify backends classpath: or memory:. Paths start with a slash and have any number of segments.

Classpath resources use the scheme classpath:. The backend reads data from the src/main/resources of the project's modules and the contents of all library .jar files. Classpath resources are read-only.

Filesystem resources use the scheme filesystem:. The backend reads data from the host machine's local filesystem. It is read-only and does not support list.

Memory resources use the scheme memory:. The backend starts empty and is populated by calls to put.

Other backends are permitted. They should be registered with a MapBinder with the backend scheme like classpath: as the key.

Constructors

Link copied to clipboard
constructor(backends: Map<String, ResourceLoader.Backend>)

Types

Link copied to clipboard
abstract class Backend
Link copied to clipboard
object Companion

Functions

Link copied to clipboard
fun copyTo(root: String, dir: Path)

Copies all resources with root as a prefix to the directory dir.

Link copied to clipboard
fun exists(address: String): Boolean

Returns true if a resource at address exists.

Link copied to clipboard
fun list(address: String): List<String>

Returns the full path of the resources that are immediate children of address.

Link copied to clipboard
fun open(address: String): BufferedSource?

Return a buffered source for address, or null if no such resource exists.

Link copied to clipboard
fun put(address: String, utf8: String)

Writes a resource as UTF-8. Throws if the backend is readonly.

fun put(address: String, data: ByteString)

Writes a resource. Throws if the backend is readonly.

Link copied to clipboard
fun requireUtf8(address: String): String

Like utf8, but throws IllegalStateException if the resource is missing.

Link copied to clipboard
fun unwatch(address: String)
Link copied to clipboard
fun utf8(address: String): String?

Return the contents of address as a string, or null if no such resource exists. Note that this method decodes the resource on every use. It is the caller's responsibility to cache the result if it is to be loaded frequently.

Link copied to clipboard
fun walk(address: String): List<String>
Link copied to clipboard
fun watch(address: String, resourceChangedListener: (address: String) -> Unit)