Interface ResourceAccessor

All Superinterfaces:
AutoCloseable
All Known Implementing Classes:
AbstractPathResourceAccessor, AbstractResourceAccessor, AntResourceAccessor, ClassLoaderResourceAccessor, CompositeResourceAccessor, DirectoryResourceAccessor, FileSystemResourceAccessor, MavenResourceAccessor, MockResourceAccessor, OSGiResourceAccessor, SearchPathResourceAccessor, SpringResourceAccessor, ZipResourceAccessor

public interface ResourceAccessor extends AutoCloseable
ResourceAccessors abstract file access so they can be read in a variety of environments. Implementations may look for local files in known locations, read from the network, or do whatever else they need to find files. Think of ResourceAccessors as a ClassLoader but for finding and reading files, not finding and loading classes.
  • Method Details

    • openStreams

      @Deprecated default InputStreamList openStreams(String relativeTo, String streamPath) throws IOException
      Return the streams for each resource mapped by the given path. The path is often a URL but does not have to be. Should accept both / and \ chars for file paths to be platform-independent. If path points to a compressed resource, return a stream of the uncompressed contents of the file. Returns InputStreamList since multiple resources can map to the same path, such as "META-INF/MAINFEST.MF". Remember to close streams when finished with them.
      Parameters:
      relativeTo - Location that streamPath should be found relative to. If null, streamPath is an absolute path
      Returns:
      Empty list if the resource does not exist.
      Throws:
      IOException - if there is an error reading an existing path.
    • openStream

      @Deprecated default InputStream openStream(String relativeTo, String streamPath) throws IOException
      Returns a single stream matching the given path. See openStreams(String, String) for details about path options. Implementations should respect GlobalConfiguration.DUPLICATE_FILE_MODE
      Parameters:
      relativeTo - Location that streamPath should be found relative to. If null, streamPath is an absolute path
      Returns:
      null if the resource does not exist
      Throws:
      IOException - if multiple paths matched the stream
      IOException - if there is an error reading an existing path
    • list

      @Deprecated default SortedSet<String> list(String relativeTo, String path, boolean recursive, boolean includeFiles, boolean includeDirectories) throws IOException
      Deprecated.
      Returns the path to all resources contained in the given path. The passed path is not included in the returned set. Returned strings should use "/" for file path separators, regardless of the OS and should accept both / and \ chars for file paths to be platform-independent. Returned set is sorted, normally alphabetically but subclasses can use different comparators. The values returned should be able to be passed into openStreams(String, String) and return the contents. Returned paths should normally be root-relative and therefore not be an absolute path, unless there is a good reason to be absolute.

      Default implementation calls search(String, boolean) and collects the paths from the resources. Because the new method no longer supports listing directories, it will silently ignore the includeDirectories argument UNLESS includeFiles is false. In that case, it will throw an exception.

      Parameters:
      relativeTo - Location that streamPath should be found relative to. If null, path is an absolute path
      path - The path to lookup resources in.
      recursive - Set to true and will return paths to contents in sub directories as well.
      includeFiles - Set to true and will return paths to files.
      includeDirectories - Set to true and will return paths to directories.
      Returns:
      empty set if nothing was found
      Throws:
      IOException - if there is an error reading an existing root.
    • search

      default List<Resource> search(String path, ResourceAccessor.SearchOptions searchOptions) throws IOException
      Returns the path to all resources contained in the given path that match the searchOptions criteria. Multiple resources may be returned with the same path, but only if they are actually unique files. Order is important to pay attention to, they should be returned in a user-expected manner based on this resource accessor.

      Should return an empty list if:
      • Path does not exist or maxDepth less or equals than zero
      Should throw an exception if:
      • Path is null
      • Path is not a "directory"
      • Path exists but cannot be read from
      Parameters:
      path - The path to lookup resources in.
      searchOptions - A set of criteria for how resources should be found/filtered
      Returns:
      empty set if nothing was found
      Throws:
      IOException - if there is an error searching the system.
    • search

      List<Resource> search(String path, boolean recursive) throws IOException
      Returns the path to all resources contained in the given path. Multiple resources may be returned with the same path, but only if they are actually unique files. Order is important to pay attention to, they should be returned in a user-expected manner based on this resource accessor.

      Should return an empty list if:
      • Path does not exist
      Should throw an exception if:
      • Path is null
      • Path is not a "directory"
      • Path exists but cannot be read from
      Parameters:
      path - The path to lookup resources in.
      recursive - Set to true and will return paths to contents in subdirectories as well.
      Returns:
      empty set if nothing was found
      Throws:
      IOException - if there is an error searching the system.
    • getAll

      List<Resource> getAll(String path) throws IOException
      Returns all Resources at the given path. For many resource accessors (such as a file system), only one resource can exist at a given spot, but some accessors (such as CompositeResourceAccessor or ClassLoaderResourceAccessor) can have multiple resources for a single path.

      If the resourceAccessor returns multiple values, the returned List should be considered sorted for that resource accessor. For example, ClassLoaderResourceAccessor returns them in order based on the configured classloader. Order is important to pay attention to, because users may set GlobalConfiguration.DUPLICATE_FILE_MODE to pick the "best" file which is defined as "the first file from this function".

      Returns:
      null if no resources match the path
      Throws:
      IOException - if there is an unexpected error determining what is at the path
    • getExisting

      default Resource getExisting(String path) throws IOException
      Convenience version of get(String) which throws an exception if the file does not exist.
      Throws:
      FileNotFoundException - if the file does not exist
      IOException
    • get

      default Resource get(String path) throws IOException
      Finds a single specific Resource. If multiple files match the given path, handle based on the GlobalConfiguration.DUPLICATE_FILE_MODE setting. Default implementation calls getAll(String).
      Returns:
      a Resource even if the path does not exist
      Throws:
      IOException
    • describeLocations

      List<String> describeLocations()
      Returns a description of the places this classloader will look for paths. Used in error messages and other troubleshooting cases.