Simple resource locator for Java 8+
You can pull rezolver from the central maven repository, just add these to your pom.xml file:
<dependency>
<groupId>com.github.pnavais</groupId>
<artifactId>rezolver</artifactId>
<version>1.0.5</version>
</dependency>
Rezolver will try to do its best to resolve the correct URL of any arbitrary resource specified using a string URL that can be either relative or absolute containing optionally a full valid schema.
Rezolver.fetch("/home/pnavais/images/image.png"); // --> Resolve from file system
Rezolver.fetch("file:/home/pnavais/images/image.png"); // --> Same
Rezolver.fetch("classpath:META-INF/images/image.png"); // --> Resolve from classpath resource
// Resolve a resource URL using a relative path
ResourceInfo resInfo = Rezolver.fetch("images/inner-image.png"); // --> Search resource locally or in classpath:META-INF/ if not found
resInfo.isResolved(); // --> True
resInfo.getSearchPath(); // --> images/inner-image.png
resInfo.getURL(); // --> file:///res/in/classpath/META-INF/images/inner-image.png
// Get URL of resource directly
URL resURL = Rezolver.lookup("image.png");
In order to retrieve the resolved URL of a given resource, Rezolver will use a default chain of loaders performing the following steps :
- Use the local loader to check that the specified resource location string refers to a file in the local file system.
- Use the classpath loader to check if the specified resource location string refers to a path relative to the current application classpath (META-INF will be used as fallback folder in the classpath).
- Use a remote loader to check if the specified resource location string refers to a valid URL
Use the ResourceBuilder to customize the loaders resolution chain :
// A custom chain looking first locally and later in the classpath in case of failure (META-INF/resources is used as fallback folder)
Rezolver r = Rezolver.builder()
.add(new LocalLoader())
.add(FallbackLoader.of(new ClasspathLoader(), "META-INF/resources")))
.build();
r.resolve("inner-resource.conf").getURL(); // --> Will retrieve file:///res/in/classpath/META-INF/resources/inner-resource.conf