Package net.sf.jasperreports.engine.util
Class JRResourcesUtil
java.lang.Object
net.sf.jasperreports.engine.util.JRResourcesUtil
Provides methods for resource resolution via class loaders or URL stream handlers.
- Author:
- Lucian Chirita (lucianc@users.sourceforge.net)
-
Method Summary
Modifier and TypeMethodDescriptionstatic URLcreateURL(String spec, URLStreamHandlerFactory urlHandlerFactory) Tries to parse aStringas an URL.protected static FiledefaultLocateFile(String location) static URLfindClassLoaderResource(String location, ClassLoader clsLoader) Attempts to find a resource using a class loader.static URLfindClassLoaderResource(String location, ClassLoader clsLoader, Class<?> clazz) Deprecated.static ClassLoadergetClassLoader(ClassLoader clsLoader) Returns a class loader.static ClassLoaderReturns the global class loader.static ClassLoaderReturns the thread local class loader.static URLStreamHandlergetURLHandler(String spec, URLStreamHandlerFactory urlHandlerFact) Returns an URL stream handler for an URL specified as aString.static ResourceBundleloadResourceBundle(String baseName, Locale locale) Loads a resource bundle for a given base name and locale.static ResourceBundleloadResourceBundle(String baseName, Locale locale, ClassLoader clsLoader) Loads a resource bundle for a given base name and locale.static ResourceBundleloadResourceBundle(JasperReportsContext jasperReportsContext, String baseName, Locale locale) Loads a resource bundle for a given base name and locale.static ResourceBundleloadResourceBundle(RepositoryContext repositoryContext, String baseName, Locale locale) protected static FilelocateContextDirectory(RepositoryResourceContext resourceContext, Function<String, File> rootLocator) protected static FilelocateFile(RepositoryResourceContext resourceContext, String location, Function<String, File> rootLocator) static voidResets the the thread local class loader to its previous value.static FileresolveFile(RepositoryContext context, String location) static FileresolveFile(RepositoryContext context, String location, Function<String, File> rootLocator) static voidsetGlobalClassLoader(ClassLoader classLoader) Sets a global class loader to be used for resource resolution.static voidsetThreadClassLoader(ClassLoader classLoader) Sets the thread local class loader.
-
Method Details
-
createURL
Tries to parse aStringas an URL.- Parameters:
spec- theStringto parseurlHandlerFactory- an URL stream handler factory to use- Returns:
- an URL if the parsing is successful
- See Also:
-
getURLHandler
Returns an URL stream handler for an URL specified as aString.- Parameters:
spec- theStringto parse as an URLurlHandlerFact- an URL stream handler factory- Returns:
- an URL stream handler if one was found for the protocol of the URL
-
resolveFile
-
resolveFile
-
defaultLocateFile
-
locateFile
-
locateContextDirectory
protected static File locateContextDirectory(RepositoryResourceContext resourceContext, Function<String, File> rootLocator) -
getClassLoader
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
Returns the global class loader.- Returns:
- the global class loader.
- See Also:
-
getThreadClassLoader
Returns the thread local class loader.- Returns:
- the thread local class loader.
- See Also:
-
setThreadClassLoader
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
Sets a global class loader to be used for resource resolution.- Parameters:
classLoader- the class loader- See Also:
-
findClassLoaderResource
Deprecated.Replaced byfindClassLoaderResource(String, ClassLoader).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
clazz.getClassLoader()clazz.getResource()
- Parameters:
location- the resource nameclsLoader- a class loaderclazz- a class- Returns:
- the resource URL if found
- the class loader returned by
-
findClassLoaderResource
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 nameclsLoader- a class loader- Returns:
- the resource URL if found
- the class loader returned by
-
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 namelocale- 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
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 namelocale- 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):
- the class loader returned by
getClassLoader(ClassLoader) - the context class loader
JRClassLoader.class.getClassLoader()
- Parameters:
baseName- the base namelocale- the localeclsLoader-- Returns:
- the resource bundle for the given base name and locale
- See Also:
- the class loader returned by
-
findClassLoaderResource(String, ClassLoader).