Interface ResourceFactory

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

public interface ResourceFactory
ResourceFactory.
  • Method Details

    • combine

      static Resource combine(List<Resource> resources)

      Make a directory Resource containing a collection of other directory Resources

      Parameters:
      resources - multiple directory Resources to combine as a single resource. Order is significant.
      Returns:
      A CombinedResource for multiple resources; or a single Resource if only 1 is passed; or null if none are passed. The returned Resource will always return true from Resource.isDirectory()
      Throws:
      IllegalArgumentException - if a non-directory resource is passed.
      See Also:
    • combine

      static Resource combine(Resource... resources)

      Make a directory Resource containing a collection of directory Resources

      Parameters:
      resources - multiple directory Resources to combine as a single resource. Order is significant.
      Returns:
      A CombinedResource for multiple resources; or a single Resource if only 1 is passed; or null if none are passed. The returned Resource will always return true from Resource.isDirectory()
      Throws:
      IllegalArgumentException - if a non-directory resource is passed.
      See Also:
    • newResource

      Resource newResource(URI uri)
      Construct a resource from a uri.
      Parameters:
      uri - A URI.
      Returns:
      A Resource object.
    • newSystemResource

      default Resource newSystemResource(String resource)
      Construct a system resource from a string. The resource is tried as classloader resource before being treated as a normal resource.
      Parameters:
      resource - Resource as string representation
      Returns:
      The new Resource
    • newClassPathResource

      default Resource newClassPathResource(String resource)
      Find a classpath resource. The Class.getResource(String) method is used to lookup the resource. If it is not found, then the Loader.getResource(String) method is used.
      Parameters:
      resource - the relative name of the resource
      Returns:
      Resource or null
    • newMemoryResource

      default Resource newMemoryResource(URL url)
      Load a URL into a memory resource.
      Parameters:
      url - the URL to load into memory
      Returns:
      Resource or null
      See Also:
    • newResource

      default Resource newResource(String resource)
      Construct a resource from a string.
      Parameters:
      resource - A URL or filename.
      Returns:
      A Resource object.
    • newResource

      default Resource newResource(Path path)
      Construct a Resource from provided path
      Parameters:
      path - the path
      Returns:
      the Resource for the provided path
    • newResource

      default Resource newResource(List<URI> uris)
      Construct a possible CombinedResource from a list of URIs
      Parameters:
      uris - the URIs
      Returns:
      the Resource for the provided path
    • newResource

      default Resource newResource(URL url)
      Construct a Resource from a provided URL
      Parameters:
      url - the URL
      Returns:
      the Resource for the provided URL
    • newJarFileResource

      default Resource newJarFileResource(URI uri)
      Construct a Resource from a file: based URI that is mountable (eg: a jar file)
      Parameters:
      uri - the URI
      Returns:
      the Resource, mounted as a FileSystem
    • registerResourceFactory

      static void registerResourceFactory(String scheme, ResourceFactory resourceFactory)
      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.
      See Also:
    • unregisterResourceFactory

      static ResourceFactory unregisterResourceFactory(String scheme)
      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.
      See Also:
    • byScheme

      static ResourceFactory byScheme(String scheme)
      Get the ResourceFactory that is registered for the specific scheme.
      
       .byScheme("jar") == ResourceFactory supporting jar
       .byScheme("jar:file://foo.jar!/") == null // full url strings not supported)
       
      Parameters:
      scheme - the scheme to look up
      Returns:
      the ResourceFactory responsible for the scheme, null if no ResourceFactory handles the scheme.
      See Also:
    • getBestByScheme

      static ResourceFactory getBestByScheme(String scheme)
      Get the best ResourceFactory for the provided scheme.

      Unlike byScheme(String), this supports arbitrary Strings, that might start with a supported scheme.

      Parameters:
      scheme - the scheme to look up
      Returns:
      the ResourceFactory that best fits the provided scheme.
      See Also:
    • root

      static ResourceFactory 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() or lifecycle() instead.

      Returns:
      the JVM wide ResourceFactory.
      See Also:
    • closeable

      static ResourceFactory.Closeable closeable()
      A ResourceFactory that can close it's opened resources using the Java standard AutoCloseable techniques.
      Returns:
      a ResourceFactory that can be closed in a try-with-resources code block
    • lifecycle

      static ResourceFactory.LifeCycle 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

      static ResourceFactory of(Resource baseResource)
      A new ResourceFactory from a provided Resource, to base newResource(URI) and newResource(String) calls against.
      Parameters:
      baseResource - the resource to base this ResourceFactory from
      Returns:
      the ResourceFactory that builds from the Resource
    • of

      static ResourceFactory of(Container container)
      A new ResourceFactory tied to a Jetty Component Container, 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 from lifecycle() and add it as managed to the Container, subsequent calls will return the same ResourceFactory from the provided Container.

      Parameters:
      container - the container this ResourceFactory belongs to
      Returns:
      the ResourceFactory that belongs to this container