Class ResourceUtils

  • All Implemented Interfaces:
    Loggable

    public final class ResourceUtils
    extends LoggedObject
    Utility class that provides a couple of methods for extracting files stored as resource in a JAR.
    • Constructor Detail

      • ResourceUtils

        public ResourceUtils()
    • Method Detail

      • getResourceLoader

        public static ClassLoader getResourceLoader()
        Returns:
        Class loader used for the getResourceAsStream calls.
      • setResourceLoader

        public static void setResourceLoader​(ClassLoader resourceLoader)
        Parameters:
        resourceLoader - Class loader used for the getResourceAsStream calls.
      • copyResource

        public void copyResource​(String resourceName,
                                 File destFile)
                          throws IOException
        Copies a container resource from the JAR into the specified file.
        Parameters:
        resourceName - The name of the resource
        destFile - The file to which the contents of the resource should be copied
        Throws:
        IOException - If an I/O error occurs while copying the resource
      • copyResource

        public void copyResource​(String resourceName,
                                 String destFile,
                                 FileHandler handler)
                          throws IOException
        Copies a container resource from the JAR into the specified file using the specified file handler.
        Parameters:
        resourceName - The name of the resource
        destFile - The file to which the contents of the resource should be copied
        handler - The file handler to use
        Throws:
        IOException - If an I/O error occurs while copying the resource
      • copyResource

        public void copyResource​(String resourceName,
                                 File destFile,
                                 org.apache.tools.ant.types.FilterChain filterChain,
                                 String encoding)
                          throws IOException
        Copies a container resource from the JAR into the specified file, thereby applying the specified filters.
        Parameters:
        resourceName - The name of the resource, relative to the org.codehaus.cargo.container.internal.util package
        destFile - The file to which the contents of the resource should be copied
        filterChain - The ordered list of filter readers that should be applied while copying
        encoding - The encoding that should be used when copying the resource. Use null for system default encoding
        Throws:
        IOException - If an I/O error occurs while copying the resource
      • copyResource

        public void copyResource​(String resourceName,
                                 String destFile,
                                 FileHandler handler,
                                 org.apache.tools.ant.types.FilterChain filterChain,
                                 String encoding)
                          throws IOException
        Copies a container resource from the JAR into the specified file, using the specified file handler thereby applying the specified filters.
        Parameters:
        resourceName - The name of the resource, relative to the org.codehaus.cargo.container.internal.util package
        destFile - The file to which the contents of the resource should be copied
        handler - The file handler to be used for file copy
        filterChain - The ordered list of filter readers that should be applied while copying
        encoding - The encoding that should be used when copying the resource. Use null for system default encoding
        Throws:
        IOException - If an I/O error occurs while copying the resource
      • getResourceLocation

        public File getResourceLocation​(Class where,
                                        String resourceName)
        Search for the given resource and return the directory or archive that contains it.

        Doesn't work for archives in JDK 1.1 as the URL returned by getResource doesn't contain the name of the archive.

        Parameters:
        where - Class where to look for the resource (its class loader and parent class loaders are used recursively for the lookup).
        resourceName - The name of the resource
        Returns:
        The directory or archive containing the specified resource
      • readResource

        public String readResource​(String resourceName,
                                   org.apache.tools.ant.types.FilterChain filterChain,
                                   String encoding)
                            throws IOException
        Reads a container resource from the JAR, applies the specified filters and returns content as String.
        Parameters:
        resourceName - The name of the resource, relative to the org.codehaus.cargo.container.internal.util package
        filterChain - The ordered list of filter readers that should be applied while reading
        encoding - The encoding that should be used when reading the resource. Use null for system default encoding
        Returns:
        Content of resource as String.
        Throws:
        IOException - If an I/O error occurs while reading the resource