Skip to content

pnavais/rezolver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Coverage Status License Quality Gate Maven Central

Simple resource locator for Java 8+

Maven Repository

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>

Usage

Resolving the location of a given resource using the default chain of loaders.

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 :

  1. Use the local loader to check that the specified resource location string refers to a file in the local file system.
  2. 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).
  3. Use a remote loader to check if the specified resource location string refers to a valid URL

Creating a custom chain of loaders

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

Icon made by Pixel Buddha from www.flaticon.com is licensed by CC 3.0 BY

About

A simple resource locator for Java 8+

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages