Interface ResourceFactory
- All Known Subinterfaces:
ResourceFactory.Closeable
,ResourceFactory.LifeCycle
- All Known Implementing Classes:
MountedPathResourceFactory
,PathResourceFactory
,URLResourceFactory
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);
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
static interface
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic ResourceFactory.Closeable
A ResourceFactory that can close it's opened resources using the Java standardAutoCloseable
techniques.static Resource
Make a directory Resource containing a collection of other directoryResource
sstatic Resource
Make a directory Resource containing a collection of directoryResource
sstatic boolean
isSupported
(String str) Test to see if provided string is supported.static boolean
isSupported
(URI uri) Test to see if provided uri is supported.static ResourceFactory.LifeCycle
A ResourceFactory that implements the Jetty LifeCycle.default Resource
newClassLoaderResource
(String resource) Construct a Resource from a search of ClassLoaders.default Resource
newClassLoaderResource
(String resource, boolean searchSystemClassLoader) Construct a Resource from a search of ClassLoaders.default Resource
newClassPathResource
(String resource) Deprecated, for removal: This API element is subject to removal in a future version.default Resource
newJarFileResource
(URI uri) Construct aResource
from afile:
based URI that is mountable (eg: a jar file).default Resource
newMemoryResource
(URL url) Load a URL into a memory resource.default Resource
newResource
(String resource) Construct a resource from a string.newResource
(URI uri) Construct a resource from a uri.default Resource
newResource
(URL url) Construct aResource
from a provided URL.default Resource
newResource
(Path path) Construct a Resource from provided path.default Resource
newResource
(List<URI> uris) Construct a possible combinedResource
from a list of URIs.default Resource
newSystemResource
(String resource) Deprecated, for removal: This API element is subject to removal in a future version.usenewClassLoaderResource(String)
ornewClassLoaderResource(String, boolean)
instead, will be removed in Jetty 12.1.0static ResourceFactory
A new ResourceFactory tied to a Jetty ComponentContainer
, to allow its allocated resources to be cleaned up during the normal component lifecycle behavior.static ResourceFactory
Deprecated, for removal: This API element is subject to removal in a future version.static void
registerResourceFactory
(String scheme, ResourceFactory resourceFactory) Register a new ResourceFactory that can handle the specific scheme for the Resource API.static ResourceFactory
root()
The JVM wide (root) ResourceFactory.Split a string of references by provided delims into a List ofResource
.static ResourceFactory
unregisterResourceFactory
(String scheme) Unregister a scheme that is supported by the Resource API.
-
Field Details
-
LOG
static final org.slf4j.Logger LOG
-
-
Method Details
-
combine
Make a directory Resource containing a collection of other directory
Resource
s- Parameters:
resources
- multiple directoryResource
s to combine as a single resource. Order is significant.- Returns:
- A
CombinedResource
for multiple resources; or a singleResource
if only 1 is passed; or null if none are passed. The returnedResource
will always returntrue
fromResource.isDirectory()
- Throws:
IllegalArgumentException
- if a non-directory resource is passed.
-
combine
Make a directory Resource containing a collection of directory
Resource
s- Parameters:
resources
- multiple directoryResource
s to combine as a single resource. Order is significant.- Returns:
- A
CombinedResource
for multiple resources; or a singleResource
if only 1 is passed; or null if none are passed. The returnedResource
will always returntrue
fromResource.isDirectory()
- Throws:
IllegalArgumentException
- if a non-directory resource is passed.
-
newResource
Construct a resource from a uri.- Parameters:
uri
- A URI.- Returns:
- A Resource object.
-
newSystemResource
Deprecated, for removal: This API element is subject to removal in a future version.usenewClassLoaderResource(String)
ornewClassLoaderResource(String, boolean)
instead, will be removed in Jetty 12.1.0Construct a Resource from a string reference into classloaders.
- Parameters:
resource
- Resource as string representation- Returns:
- The new Resource
- Throws:
IllegalArgumentException
- if string is blank- See Also:
-
newClassLoaderResource
Construct a Resource from a search of ClassLoaders.
Search order is:
java.lang.Thread.currentThread().getContextClassLoader().getResource(String)
ResourceFactory.class.getClassLoader().getResource(String)
- (optional)
java.lang.ClassLoader.getSystemResource(String)
See
ClassLoader.getResource(String)
for rules on resource name parameter.If a provided resource name starts with a
/
(example:/org/example/ClassName.class
) then the non-slash version is also tried against the same ClassLoader (example:org/example/ClassName.class
).- Parameters:
resource
- the resource name to find in a classloadersearchSystemClassLoader
- true to searchClassLoader.getSystemResource(String)
, false to skip- Returns:
- The new Resource, which may be a
CombinedResource
if multiple directory resources are found. - Throws:
IllegalArgumentException
- if resource name or resulting URL from ClassLoader is invalid.
-
newClassLoaderResource
Construct a Resource from a search of ClassLoaders.
Convenience method
.newClassLoaderResource(resource, true)
- Parameters:
resource
- string representation of resource to find in a classloader- Returns:
- The new Resource
- Throws:
IllegalArgumentException
- if string is blank- See Also:
-
newClassPathResource
Deprecated, for removal: This API element is subject to removal in a future version.usenewClassLoaderResource(String, boolean)
instead, will be removed in Jetty 12.1.0Construct a Resource from a search of ClassLoaders.
Convenience method
.newClassLoaderResource(resource, false)
- Parameters:
resource
- the relative name of the resource- Returns:
- Resource
- Throws:
IllegalArgumentException
- if string is blank- See Also:
-
newMemoryResource
Load a URL into a memory resource.
A Memory Resource is created from a the contents of
URL.openStream()
and kept in memory from that point forward. Never accessing the URL again to refresh it's contents.- Parameters:
url
- the URL to load into memory- Returns:
- Resource, or null if url points to a location that does not exist
- Throws:
IllegalArgumentException
- if URL is null- See Also:
-
newResource
Construct a resource from a string.- Parameters:
resource
- A URL or filename.- Returns:
- A Resource object, or null if the string points to a location that does not exist
- Throws:
IllegalArgumentException
- if resource is invalid
-
newResource
Construct a Resource from provided path.- Parameters:
path
- the path- Returns:
- the Resource for the provided path, or null if the path does not exist
- Throws:
IllegalArgumentException
- if path is null
-
newResource
Construct a possible combinedResource
from a list of URIs.- Parameters:
uris
- the URIs- Returns:
- the Resource for the provided URIs, or null if all of the provided URIs do not exist
- Throws:
IllegalArgumentException
- if list of URIs is empty or null
-
newResource
Construct aResource
from a provided URL.- Parameters:
url
- the URL- Returns:
- the Resource for the provided URL, or null if the url points to a location that does not exist
- Throws:
IllegalArgumentException
- if url is null
-
newJarFileResource
Construct aResource
from afile:
based URI that is mountable (eg: a jar file).- Parameters:
uri
- the URI- Returns:
- the Resource, mounted as a
FileSystem
, or null if the uri points to a location that does not exist. - Throws:
IllegalArgumentException
- if provided URI is not "file" scheme.
-
split
Split a string of references, that may be split with ',
', or ';
', or '|
' into a List ofResource
.Each part of the input string could be path references (unix or windows style), string URI references, or even glob references (eg:
/path/to/libs/*
).If the result of processing the input segment is a java archive, then its resulting URI will be a mountable URI as
jar:file:...!/
- Parameters:
str
- the input string of references- Returns:
- list of resources
-
split
Split a string of references by provided delims into a List ofResource
.Each part of the input string could be path references (unix or windows style), string URI references, or even glob references (eg:
/path/to/libs/*
). Note: that if you use the:
character in your delims, then URI references will be impossible.If the result of processing the input segment is a java archive, then its resulting URI will be a mountable URI as
jar:file:...!/
- Parameters:
str
- the input string of references- Returns:
- list of resources
-
isSupported
Test to see if provided string is supported.- Parameters:
str
- the string to test- Returns:
- true if it is supported
-
isSupported
Test to see if provided uri is supported.- Parameters:
uri
- the uri to test- Returns:
- true if it is supported
-
registerResourceFactory
Register a new ResourceFactory that can handle the specific scheme for the Resource API.This allows
- Parameters:
scheme
- the scheme to support (eg: `ftp`, `http`, `resource`, etc)resourceFactory
- the ResourceFactory to be responsible for the registered scheme.- Throws:
IllegalArgumentException
- if scheme is blank- See Also:
-
unregisterResourceFactory
Unregister a scheme that is supported by the Resource API.- Parameters:
scheme
- the scheme to unregister- Returns:
- the existing
ResourceFactory
that was registered to that scheme. - Throws:
IllegalArgumentException
- if scheme is blank- See Also:
-
root
The JVM wide (root) ResourceFactory.Resources allocated this way are not released until the JVM is closed.
If you have a ResourceFactory need that needs to clean up it's resources at runtime, use
closeable()
orlifecycle()
instead.- Returns:
- the JVM wide ResourceFactory.
- See Also:
-
closeable
A ResourceFactory that can close it's opened resources using the Java standardAutoCloseable
techniques.- Returns:
- a ResourceFactory that can be closed in a try-with-resources code block
-
lifecycle
A ResourceFactory that implements the Jetty LifeCycle.This style of ResourceFactory can be attached to the normal Jetty LifeCycle to clean up it's allocated resources.
- Returns:
- the ResourceFactory that implements
LifeCycle
-
of
Deprecated, for removal: This API element is subject to removal in a future version.A new ResourceFactory from a provided Resource, to basenewResource(URI)
andnewResource(String)
calls against.- Parameters:
baseResource
- the resource to base this ResourceFactory from- Returns:
- the ResourceFactory that builds from the Resource
-
of
A new ResourceFactory tied to a Jetty ComponentContainer
, to allow its allocated resources to be cleaned up during the normal component lifecycle behavior.This is safe to call repeatedly against the same
Container
, the first call will create a ResourceFactory fromlifecycle()
and add it as managed to theContainer
, subsequent calls will return the same ResourceFactory from the providedContainer
.- Parameters:
container
- the container this ResourceFactory belongs to- Returns:
- the ResourceFactory that belongs to this container
-
newClassLoaderResource(String, boolean)
instead, will be removed in Jetty 12.1.0