Interface ResourceFactory

All Known Subinterfaces:
ResourceFactory.Closeable, ResourceFactory.LifeCycle
All Known Implementing Classes:
MountedPathResourceFactory, PathResourceFactory, URLResourceFactory

public interface ResourceFactory

ResourceFactory is the source of new Resource instances.

Some Resource objects have an internal allocation / release model, that the ResourceFactory is responsible for. Once a ResourceFactory is stopped, the Resource objects created from that ResourceFactory are released.

A ResourceFactory.LifeCycle tied to a Jetty Container

     ResourceFactory.LifeCycle resourceFactory = ResourceFactory.of(container);
     Resource resource = resourceFactory.newResource(ref);
 

The use of of(Container) results in a ResourceFactory.LifeCycle that is tied to a specific Jetty Container such as a Server, ServletContextHandler, or WebAppContext. This will free the Resource instances created by the ResourceFactory once the container that manages it is stopped.

A ResourceFactory.Closeable that exists within a try-with-resources call

     try (ResourceFactory.Closeable resourceFactory = ResourceFactory.closeable()) {
         Resource resource = resourceFactory.newResource(ref);
     }
 

The use of closeable() results in a ResourceFactory that only exists for the duration of the try-with-resources code block, once this try-with-resources is closed, all Resource objects associated with that ResourceFactory are freed as well.

A ResourceFactory that lives at the JVM level

     ResourceFactory resourceFactory = ResourceFactory.root();
     Resource resource = resourceFactory.newResource(ref);
 

The use of root() results in a ResourceFactory that exists for the life of the JVM, and the resources allocated via this ResourceFactory will not be freed until the JVM exits.

Supported URI Schemes

By default, the following schemes are supported by Jetty.

file
The standard Java file:/path/to/dir/ syntax
jar
The standard Java jar:file:/path/to/file.jar!/ syntax
jrt
The standard Java jrt:module-name syntax

Special Note: An effort is made to discover any new schemes that might be present at JVM startup (eg: graalvm and resource: scheme). At startup Jetty will access an internal Jetty resource (found in the jetty-util jar) and seeing what scheme it is using to access it, and will register it with a call to registerResourceFactory(String, ResourceFactory).

Supporting more Schemes

You can register a new URI scheme to a ResourceFactory implementation using the registerResourceFactory(String, ResourceFactory) method, which will cause all new uses of ResourceFactory to use this newly registered scheme.

     URLResourceFactory urlResourceFactory = new URLResourceFactory();
     urlResourceFactory.setConnectTimeout(1000);
     ResourceFactory.registerResourceFactory("https", urlResourceFactory);

     URI web = URI.create("https://jetty.org/");
     Resource resource = ResourceFactory.root().newResource(web);