Class JRResourcesUtil


  • public final class JRResourcesUtil
    extends Object
    Provides methods for resource resolution via class loaders or URL stream handlers.
    Author:
    Lucian Chirita (lucianc@users.sourceforge.net)
    • Method Detail

      • getURLHandler

        public static URLStreamHandler getURLHandler​(String spec,
                                                     URLStreamHandlerFactory urlHandlerFact)
        Returns an URL stream handler for an URL specified as a String.
        Parameters:
        spec - the String to parse as an URL
        urlHandlerFact - an URL stream handler factory
        Returns:
        an URL stream handler if one was found for the protocol of the URL
      • resolveFile

        public static File resolveFile​(String location,
                                       FileResolver fileRes)
        Deprecated.
        To be removed.
        Attempts to find a file using a file resolver.
        Parameters:
        location - file name
        fileRes - a file resolver
        Returns:
        the file, if found
      • defaultLocateFile

        protected static File defaultLocateFile​(String location)
      • setThreadClassLoader

        public static void setThreadClassLoader​(ClassLoader classLoader)
        Sets the thread local class loader.
        Parameters:
        classLoader - a class loader
        See Also:
        getClassLoader(ClassLoader)
      • resetClassLoader

        public static void resetClassLoader()
        Resets the the thread local class loader to its previous value.
      • setGlobalClassLoader

        public static void setGlobalClassLoader​(ClassLoader classLoader)
        Sets a global class loader to be used for resource resolution.
        Parameters:
        classLoader - the class loader
        See Also:
        getClassLoader(ClassLoader)
      • findClassLoaderResource

        public static URL findClassLoaderResource​(String location,
                                                  ClassLoader clsLoader,
                                                  Class<?> clazz)
        Attempts to find a resource using a class loader.

        The following sources are tried:

        Parameters:
        location - the resource name
        clsLoader - a class loader
        clazz - a class
        Returns:
        the resource URL if found
      • findClassLoaderResource

        public static URL findClassLoaderResource​(String location,
                                                  ClassLoader clsLoader)
        Attempts to find a resource using a class loader.

        The following sources are tried:

        • the class loader returned by getClassLoader(ClassLoader)
        • the context class loader
        • JRLoader.class.getClassLoader()
        • JRLoader.class.getResource()
        Parameters:
        location - the resource name
        clsLoader - a class loader
        Returns:
        the resource URL if found
      • loadResourceBundle

        public static ResourceBundle loadResourceBundle​(String baseName,
                                                        Locale locale)
        Loads a resource bundle for a given base name and locale.

        This methods calls loadResourceBundle(String, Locale, ClassLoader) with a null classloader.

        Parameters:
        baseName - the base name
        locale - the locale
        Returns:
        the resource bundle for the given base name and locale
      • loadResourceBundle

        public static ResourceBundle loadResourceBundle​(String baseName,
                                                        Locale locale,
                                                        ClassLoader clsLoader)
        Loads a resource bundle for a given base name and locale.

        The method attempts to load the resource bundle using the following classloaders (and stops at the first successful attempt):

        Parameters:
        baseName - the base name
        locale - the locale
        clsLoader -
        Returns:
        the resource bundle for the given base name and locale
        See Also:
        ResourceBundle.getBundle(String, Locale, ClassLoader)