- All Implemented Interfaces:
AsyncResource
,Resource
,AutoCloseable
A Resource
implementation that identifies resources by a URI of the form classpath:/path/to/resource
and looks up data on the classpath.
A typical usage is:
ClasspathResource resource = new ClasspathResource(URI.create("classpath:/path/to/resource"));
...
- Since:
- 1.0
- Author:
- Jeremy Kuhn
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class io.inverno.mod.base.resource.AbstractAsyncResource
AbstractAsyncResource.EndOfFileException
-
Field Summary
Fields inherited from class io.inverno.mod.base.resource.AbstractAsyncResource
DEFAULT_READ_BUFFER_CAPACITY
Fields inherited from class io.inverno.mod.base.resource.AbstractResource
IS_WINDOWS_PATH
-
Constructor Summary
ConstructorDescriptionClasspathResource
(URI uri) Creates a classpath resource with the specified URI.ClasspathResource
(URI uri, MediaTypeService mediaTypeService) Creates a classpath resource with the specified URI and media type service.ClasspathResource
(URI uri, Class<?> clazz) Creates a classpath resource with the specified URI that looks up data from the specified class.ClasspathResource
(URI uri, Class<?> clazz, MediaTypeService mediaTypeService) Creates a classpath resource with the specified URI and media type service that looks up data from the specified class.ClasspathResource
(URI uri, ClassLoader classLoader) Creates a classpath resource with the specified URI that looks up data from the specified class loader.ClasspathResource
(URI uri, ClassLoader classLoader, MediaTypeService mediaTypeService) Creates a classpath resource with the specified URI and media type service that looks up data from the specified class loader. -
Method Summary
Modifier and TypeMethodDescriptionstatic URI
Checks that the specified URI is a classpath resource URI.void
close()
boolean
delete()
Deletes the resource.exists()
Determines whether the resource exists.Returns the resource file name.Returns the resource media type.getURI()
Returns the resource URI.isFile()
Determines whether this resource represents a file.Returns the resource last modified time stamp.Opens a readable byte channel to the resource.openWritableByteChannel
(boolean append, boolean createParents) Opens a writable byte channel to the resource that will append or not content to an existing resource and create or not missing parent directories.read()
Reads the resource in a reactive way.Resolves the specified URI against the resource URI as defined byPath.resolve(Path)
.void
setExecutor
(ExecutorService executor) Sets the executor service to use when reading or writing the resource asynchronously.size()
Returns the resource content size.Writes content to the resource in a reactive way appending or not content to an existing resource and create or not missing parent directories.Methods inherited from class io.inverno.mod.base.resource.AbstractAsyncResource
getExecutor, setReadBufferCapacity
Methods inherited from class io.inverno.mod.base.resource.AbstractResource
getMediaTypeService, pathToSanitizedString, setMediaTypeService
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.inverno.mod.base.resource.Resource
openWritableByteChannel, openWritableByteChannel, resolve, write, write
-
Field Details
-
SCHEME_CLASSPATH
The classpath resource scheme- See Also:
-
-
Constructor Details
-
ClasspathResource
Creates a classpath resource with the specified URI.
- Parameters:
uri
- the resource URI- Throws:
IllegalArgumentException
- if the specified URI does not designate a classpath resource
-
ClasspathResource
Creates a classpath resource with the specified URI that looks up data from the specified class.
- Parameters:
uri
- the resource URIclazz
- a class- Throws:
IllegalArgumentException
- if the specified URI does not designate a classpath resource- See Also:
-
ClasspathResource
Creates a classpath resource with the specified URI that looks up data from the specified class loader.
- Parameters:
uri
- the resource URIclassLoader
- a class loader- Throws:
IllegalArgumentException
- if the specified URI does not designate a classpath resource- See Also:
-
ClasspathResource
public ClasspathResource(URI uri, MediaTypeService mediaTypeService) throws IllegalArgumentException Creates a classpath resource with the specified URI and media type service.
- Parameters:
uri
- the resource URImediaTypeService
- a media type service- Throws:
IllegalArgumentException
- if the specified URI does not designate a classpath resource
-
ClasspathResource
public ClasspathResource(URI uri, Class<?> clazz, MediaTypeService mediaTypeService) throws IllegalArgumentException Creates a classpath resource with the specified URI and media type service that looks up data from the specified class.
- Parameters:
uri
- the resource URIclazz
- a classmediaTypeService
- a media type service- Throws:
IllegalArgumentException
- if the specified URI does not designate a classpath resource
-
ClasspathResource
public ClasspathResource(URI uri, ClassLoader classLoader, MediaTypeService mediaTypeService) throws IllegalArgumentException Creates a classpath resource with the specified URI and media type service that looks up data from the specified class loader.
- Parameters:
uri
- the resource URIclassLoader
- a class loadermediaTypeService
- a media type service- Throws:
IllegalArgumentException
- if the specified URI does not designate a classpath resource
-
-
Method Details
-
checkUri
Checks that the specified URI is a classpath resource URI.
- Parameters:
uri
- the uri to check- Returns:
- the uri if it is a classpath resource URI
- Throws:
IllegalArgumentException
- if the specified URI does not designate a classpath resource
-
setExecutor
Description copied from interface:AsyncResource
Sets the executor service to use when reading or writing the resource asynchronously.
- Specified by:
setExecutor
in interfaceAsyncResource
- Overrides:
setExecutor
in classAbstractAsyncResource
- Parameters:
executor
- the executor service to set
-
getURI
Description copied from interface:Resource
Returns the resource URI.
- Returns:
- the resource URI
-
getFilename
Description copied from interface:Resource
Returns the resource file name.
- Returns:
- the resource file name
- Throws:
ResourceException
- if there was an error resolving the resource file name
-
getMediaType
Description copied from interface:Resource
Returns the resource media type.
- Specified by:
getMediaType
in interfaceResource
- Overrides:
getMediaType
in classAbstractResource
- Returns:
- the resource media type or null if it couldn't be determined
- Throws:
ResourceException
- if there was an error resolving the resource media type
-
exists
Description copied from interface:Resource
Determines whether the resource exists.
- Returns:
- an optional returning true if the resource exists, false otherwise or an empty optional if existence couldn't be determined
- Throws:
ResourceException
- if there was an error determining resource existence
-
isFile
Description copied from interface:Resource
Determines whether this resource represents a file.
A file resource is a resource that can be accessed through a
FileChannel
.- Returns:
- an optional returning true if the resource is a file, false otherwise or an empty optional if it couldn't be determined
- Throws:
ResourceException
- if there was an error determining whether the resource is a file
-
size
Description copied from interface:Resource
Returns the resource content size.
- Returns:
- an optional returning the resource content size or an empty optional if it couldn't be determined
- Throws:
ResourceException
- if there was an error resolving resource content size
-
lastModified
Description copied from interface:Resource
Returns the resource last modified time stamp.
- Returns:
- an optional returning the resource last modified time stamp or an empty optional if it couldn't be determined
- Throws:
ResourceException
- if there was an error resolving resource last modified time stamp
-
openReadableByteChannel
Description copied from interface:Resource
Opens a readable byte channel to the resource.
The caller is responsible for closing the channel to prevent resource leak.
- Returns:
- an optional returning a readable byte channel or an empty optional if the resource is not readable
- Throws:
ResourceException
- if there was an error opening the readable byte channel
-
openWritableByteChannel
public Optional<WritableByteChannel> openWritableByteChannel(boolean append, boolean createParents) throws ResourceException Description copied from interface:Resource
Opens a writable byte channel to the resource that will append or not content to an existing resource and create or not missing parent directories.
The caller is responsible for closing the channel to prevent resource leak.
- Parameters:
append
- true to append content to an existing resourcecreateParents
- true to create missing parent directories- Returns:
- an optional returning a writable byte channel or an empty optional if the resource is not writable
- Throws:
ResourceException
- if there was an error opening the writable byte channel
-
read
Description copied from interface:Resource
Reads the resource in a reactive way.
- Specified by:
read
in interfaceResource
- Overrides:
read
in classAbstractAsyncResource
- Returns:
- a stream of ByteBuf
- Throws:
NotReadableResourceException
- if the resource is not readableResourceException
- if there was an error reading the resource
-
write
public Publisher<Integer> write(Publisher<ByteBuf> data, boolean append, boolean createParents) throws NotWritableResourceException, ResourceException Description copied from interface:Resource
Writes content to the resource in a reactive way appending or not content to an existing resource and create or not missing parent directories.
- Specified by:
write
in interfaceResource
- Overrides:
write
in classAbstractAsyncResource
- Parameters:
data
- the stream of data to writeappend
- true to append content to an existing resourcecreateParents
- true to create missing parent directories- Returns:
- a stream of integer emitting number of bytes written
- Throws:
ResourceException
- if there was an error writing to the resourceNotWritableResourceException
-
delete
Description copied from interface:Resource
Deletes the resource.
- Returns:
- true if the resource had been deleted, false otherwise
- Throws:
ResourceException
- if there was an error deleting to the resource
-
resolve
Description copied from interface:Resource
Resolves the specified URI against the resource URI as defined by
Path.resolve(Path)
.- Parameters:
path
- the path to resolve- Returns:
- a new resource resulting from the resolution of the specified path against the resource
- Throws:
ResourceException
- if there was an error resolving the resource
-
close
public void close()
-