Class ResourceUtils


  • public abstract class ResourceUtils
    extends Object
    Utility methods for resolving resource locations to files in the file system.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static String URL_PROTOCOL_VFSZIP
      URL protocol for an entry from a JBoss jar file: "vfszip"
    • Constructor Summary

      Constructors 
      Constructor Description
      ResourceUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static String extractJarEntryPath​(URL jarUrl)
      Extract the JAR entry from the given URL
      static URL extractJarFileURL​(URL jarUrl)
      Extract the URL for the actual jar file from the given URL (which may point to a resource in a jar file or to a jar file itself).
      static File getFile​(URI resourceUri)  
      static File getFile​(URL resourceUrl)
      Resolve the given resource URL to a java.io.File, i.e.
      static File getFile​(URL resourceUrl, String description)
      Resolve the given resource URL to a java.io.File, i.e.
      static File getFile​(URL resourceUrl, org.zeroturnaround.javarebel.RebelServletContext sc)
      Resolve the given resource URL to a java.io.File, i.e.
      static File[] getFiles​(URL[] resourceUrls)  
      static boolean isFileURL​(URL url)
      Determine whether the given URL points to a resource in the file system.
      static boolean isJarURL​(URL url)
      Determine whether the given URL points to a resource in a jar file, that is, has protocol "jar", "zip", "wsjar" or "code-source".
      static boolean isNestedJarEntry​(String entry)
      Determine if the jar entry is in a nested jar
      static boolean isUrl​(String s)
      Determines if the parameter is a URL with a protocol or not
      static boolean isVirtualJarURL​(URL url)  
      static URI toURI​(String location)
      Create a URI instance for the given location String, replacing spaces with "%20" quotes first.
      static URI toURI​(URL url)
      Create a URI instance for the given URL, replacing spaces with "%20" quotes first.
    • Field Detail

      • URL_PROTOCOL_VFSZIP

        public static final String URL_PROTOCOL_VFSZIP
        URL protocol for an entry from a JBoss jar file: "vfszip"
        See Also:
        Constant Field Values
    • Constructor Detail

      • ResourceUtils

        public ResourceUtils()
    • Method Detail

      • isUrl

        public static boolean isUrl​(String s)
        Determines if the parameter is a URL with a protocol or not
      • getFile

        public static File getFile​(URL resourceUrl,
                                   org.zeroturnaround.javarebel.RebelServletContext sc)
        Resolve the given resource URL to a java.io.File, i.e. to a file in the file system.
        Parameters:
        resourceUrl - the resource URL to resolve
        Returns:
        a corresponding File object or null
      • getFile

        public static File getFile​(URL resourceUrl)
        Resolve the given resource URL to a java.io.File, i.e. to a file in the file system.
        Parameters:
        resourceUrl - the resource URL to resolve
        Returns:
        a corresponding File object
      • getFile

        public static File getFile​(URI resourceUri)
      • getFiles

        public static File[] getFiles​(URL[] resourceUrls)
      • getFile

        public static File getFile​(URL resourceUrl,
                                   String description)
        Resolve the given resource URL to a java.io.File, i.e. to a file in the file system.
        Parameters:
        resourceUrl - the resource URL to resolve
        description - a description of the original resource that the URL was created for (for example, a class path location)
        Returns:
        a corresponding File object
      • isFileURL

        public static boolean isFileURL​(URL url)
        Determine whether the given URL points to a resource in the file system.
        Parameters:
        url - the URL to check. May be null.
      • isJarURL

        public static boolean isJarURL​(URL url)
        Determine whether the given URL points to a resource in a jar file, that is, has protocol "jar", "zip", "wsjar" or "code-source".

        "zip" and "wsjar" are used by BEA WebLogic Server and IBM WebSphere, respectively, but can be treated like jar files. The same applies to "code-source" URLs on Oracle OC4J, provided that the path contains a jar separator.

        Parameters:
        url - the URL to check. May be null.
        Returns:
        whether the URL has been identified as a JAR URL
      • isVirtualJarURL

        public static boolean isVirtualJarURL​(URL url)
      • isNestedJarEntry

        public static boolean isNestedJarEntry​(String entry)
        Determine if the jar entry is in a nested jar
        Parameters:
        entry - Then name of the entry in the jar file
        Returns:
        if the entry is in a nested jar
      • extractJarFileURL

        public static URL extractJarFileURL​(URL jarUrl)
        Extract the URL for the actual jar file from the given URL (which may point to a resource in a jar file or to a jar file itself).
        Parameters:
        jarUrl - the original URL
        Returns:
        the URL for the actual jar file
      • extractJarEntryPath

        public static String extractJarEntryPath​(URL jarUrl)
        Extract the JAR entry from the given URL
        Parameters:
        jarUrl - the original URL
        Returns:
        the JAR entry
      • toURI

        public static URI toURI​(URL url)
                         throws URISyntaxException
        Create a URI instance for the given URL, replacing spaces with "%20" quotes first.

        Furthermore, this method works on JDK 1.4 as well, in contrast to the URL.toURI() method.

        Parameters:
        url - the URL to convert into a URI instance
        Returns:
        the URI instance
        Throws:
        URISyntaxException - if the URL wasn't a valid URI
        See Also:
        URL.toURI()
      • toURI

        public static URI toURI​(String location)
                         throws URISyntaxException
        Create a URI instance for the given location String, replacing spaces with "%20" quotes first.
        Parameters:
        location - the location String to convert into a URI instance
        Returns:
        the URI instance
        Throws:
        URISyntaxException - if the location wasn't a valid URI