Package net.sf.jasperreports.engine.util
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 Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static URL
createURL(String spec, URLStreamHandlerFactory urlHandlerFactory)
Tries to parse aString
as an URL.protected static File
defaultLocateFile(String location)
static URL
findClassLoaderResource(String location, ClassLoader clsLoader)
Attempts to find a resource using a class loader.static URL
findClassLoaderResource(String location, ClassLoader clsLoader, Class<?> clazz)
Deprecated.Replaced byfindClassLoaderResource(String, ClassLoader)
.static ClassLoader
getClassLoader(ClassLoader clsLoader)
Returns a class loader.static ClassLoader
getGlobalClassLoader()
Returns the global class loader.static ClassLoader
getThreadClassLoader()
Returns the thread local class loader.static URLStreamHandler
getURLHandler(String spec, URLStreamHandlerFactory urlHandlerFact)
Returns an URL stream handler for an URL specified as aString
.static ResourceBundle
loadResourceBundle(String baseName, Locale locale)
Loads a resource bundle for a given base name and locale.static ResourceBundle
loadResourceBundle(String baseName, Locale locale, ClassLoader clsLoader)
Loads a resource bundle for a given base name and locale.static ResourceBundle
loadResourceBundle(JasperReportsContext jasperReportsContext, String baseName, Locale locale)
Loads a resource bundle for a given base name and locale.static ResourceBundle
loadResourceBundle(RepositoryContext repositoryContext, String baseName, Locale locale)
protected static File
locateContextDirectory(RepositoryResourceContext resourceContext, Function<String,File> rootLocator)
protected static File
locateFile(RepositoryResourceContext resourceContext, String location, Function<String,File> rootLocator)
static void
resetClassLoader()
Resets the the thread local class loader to its previous value.static File
resolveFile(RepositoryContext context, String location)
static File
resolveFile(RepositoryContext context, String location, Function<String,File> rootLocator)
static void
setGlobalClassLoader(ClassLoader classLoader)
Sets a global class loader to be used for resource resolution.static void
setThreadClassLoader(ClassLoader classLoader)
Sets the thread local class loader.
-
-
-
Method Detail
-
createURL
public static URL createURL(String spec, URLStreamHandlerFactory urlHandlerFactory)
Tries to parse aString
as an URL.- Parameters:
spec
- theString
to parseurlHandlerFactory
- an URL stream handler factory to use- Returns:
- an URL if the parsing is successful
- See Also:
getURLHandler(String, URLStreamHandlerFactory)
-
getURLHandler
public static URLStreamHandler getURLHandler(String spec, URLStreamHandlerFactory urlHandlerFact)
Returns an URL stream handler for an URL specified as aString
.- Parameters:
spec
- theString
to 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
public static File resolveFile(RepositoryContext context, String location)
-
resolveFile
public static File resolveFile(RepositoryContext context, String location, Function<String,File> rootLocator)
-
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:
setGlobalClassLoader(ClassLoader)
,setThreadClassLoader(ClassLoader)
-
getGlobalClassLoader
public static ClassLoader getGlobalClassLoader()
Returns the global class loader.- Returns:
- the global class loader.
- See Also:
setGlobalClassLoader(ClassLoader)
-
getThreadClassLoader
public static ClassLoader getThreadClassLoader()
Returns the thread local class loader.- Returns:
- the thread local class loader.
- See Also:
setThreadClassLoader(ClassLoader)
-
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)
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
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 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
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 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:
ResourceBundle.getBundle(String, Locale, ClassLoader)
- the class loader returned by
-
-