Class ModuleResource

All Implemented Interfaces:
AsyncResource, Resource, AutoCloseable

public class ModuleResource extends AbstractAsyncResource

A Resource implementation that identifies resources by a URI of the form module://[MODULE_NAME]/path/to/resource or module:/path/to/resource and looks up data from a module.

When no module name is specified in the URI, the jdk.module.main module is considered.

When the application runs without modules, the unnamed module is used whatever the module specified in the URI.

A typical usage is:


 ModuleResource resource = new ModuleResource(URI.create("module://module/path/to/resource"));
 ...
 
Since:
1.0
Author:
Jeremy Kuhn
See Also:
  • Field Details

  • Constructor Details

    • ModuleResource

      public ModuleResource(URI uri) throws IllegalArgumentException

      Creates a module resource with the specified URI.

      Parameters:
      uri - the resource URI
      Throws:
      IllegalArgumentException - if the specified URI does not designate a module resource
    • ModuleResource

      public ModuleResource(URI uri, Class<?> clazz) throws IllegalArgumentException

      Creates a module resource with the specified URI that looks up data from the module layer that contains the module of which the specified class is a member.

      Parameters:
      uri - the resource URI
      clazz - a class
      Throws:
      IllegalArgumentException - if the specified URI does not designate a module resource
      See Also:
    • ModuleResource

      public ModuleResource(URI uri, Class<?> clazz, MediaTypeService mediaTypeService) throws IllegalArgumentException

      Creates a module resource with the specified URI and media type service that looks up data from the module layer that contains the module of which the specified class is a member.

      Parameters:
      uri - the resource URI
      clazz - a class
      mediaTypeService - a media type service
      Throws:
      IllegalArgumentException - if the specified URI does not designate a module resource
    • ModuleResource

      public ModuleResource(URI uri, ModuleLayer moduleLayer) throws IllegalArgumentException

      Creates a module resource with the specified URI that looks up data from the specified module layer.

      Parameters:
      uri - the resource URI
      moduleLayer - a module layer
      Throws:
      IllegalArgumentException - if the specified URI does not designate a module resource
    • ModuleResource

      public ModuleResource(URI uri, MediaTypeService mediaTypeService) throws IllegalArgumentException

      Creates a module resource with the specified URI and media type service.

      Parameters:
      uri - the resource URI
      mediaTypeService - the media type service
      Throws:
      IllegalArgumentException - if the specified URI does not designate a module resource
    • ModuleResource

      public ModuleResource(URI uri, ModuleLayer moduleLayer, MediaTypeService mediaTypeService) throws IllegalArgumentException

      Creates a module resource with the specified URI and media type service that looks up data from the specified module layer.

      Parameters:
      uri - the resource URI
      moduleLayer - a module layer
      mediaTypeService - the media type service
      Throws:
      IllegalArgumentException - if the specified URI does not designate a module resource
  • Method Details

    • checkUri

      public static URI checkUri(URI uri) throws IllegalArgumentException

      Checks that the specified URI is a module resource URI.

      Parameters:
      uri - the uri to check
      Returns:
      the uri if it is a module resource URI
      Throws:
      IllegalArgumentException - if the specified URI does not designate a module resource
    • getURI

      public URI getURI()
      Description copied from interface: Resource

      Returns the resource URI.

      Returns:
      the resource URI
    • getFilename

      public String getFilename() throws ResourceException
      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
    • isFile

      public Optional<Boolean> isFile() throws ResourceException
      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
    • exists

      public Optional<Boolean> exists() throws ResourceException
      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
    • lastModified

      public Optional<FileTime> lastModified() throws ResourceException
      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
    • size

      public Optional<Long> size() throws ResourceException
      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
    • openReadableByteChannel

      public Optional<ReadableByteChannel> openReadableByteChannel() throws ResourceException
      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 resource
      createParents - 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
    • delete

      public boolean delete() throws ResourceException
      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

      public ModuleResource resolve(Path path) throws ResourceException
      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()