Class JRResourcesUtil

java.lang.Object
net.sf.jasperreports.engine.util.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 Details

    • createURL

      public static URL createURL(String spec, URLStreamHandlerFactory urlHandlerFactory)
      Tries to parse a String as an URL.
      Parameters:
      spec - the String to parse
      urlHandlerFactory - an URL stream handler factory to use
      Returns:
      an URL if the parsing is successful
      See Also:
    • 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(RepositoryContext context, String location)
    • resolveFile

      public static File resolveFile(RepositoryContext context, String location, Function<String,File> rootLocator)
    • defaultLocateFile

      protected static File defaultLocateFile(String location)
    • locateFile

      protected static File locateFile(RepositoryResourceContext resourceContext, String location, Function<String,File> rootLocator)
    • locateContextDirectory

      protected static File locateContextDirectory(RepositoryResourceContext resourceContext, Function<String,File> rootLocator)
    • getClassLoader

      public static ClassLoader getClassLoader(ClassLoader clsLoader)
      Returns a class loader.

      The first not null value from the following is returned:

      • the value of the parameter
      • the thread local class loader
      • the global class loader
      Parameters:
      clsLoader - a class loader that will be returned if not null
      Returns:
      a class loader.
      See Also:
    • getGlobalClassLoader

      public static ClassLoader getGlobalClassLoader()
      Returns the global class loader.
      Returns:
      the global class loader.
      See Also:
    • getThreadClassLoader

      public static ClassLoader getThreadClassLoader()
      Returns the thread local class loader.
      Returns:
      the thread local class loader.
      See Also:
    • setThreadClassLoader

      public static void setThreadClassLoader(ClassLoader classLoader)
      Sets the thread local class loader.
      Parameters:
      classLoader - a class loader
      See Also:
    • 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:
    • 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(JasperReportsContext jasperReportsContext, 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(RepositoryContext repositoryContext, String baseName, Locale locale)
    • 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: