public class ResourceLoader extends Object
URLClassLoader
to load these resources, because the
original ContextClassLoader won't be able to load the resources in this jar file.
theJarFile.jar: ==> the ====> package ======> resource.xmlWe could use the following code to retrieve the
resource.xml
resource:
URL url = theJarFile.toURI().toURL(); URL[] urls = new URL[1] urls[0] = url; ClassLoader parentLoader = Thread.currentThread().getContextClassLoader(); URLClassLoader loader = new URLClassLoader(urls, parentLoader); ResourceLoader loader = new ResourceLoader("the/package", loader); URL resourceURL = loader.getURL("resource.xml");
Modifier and Type | Field and Description |
---|---|
protected ClassLoader |
loader
The class loader used to retrieve the resources.
|
protected String |
pack
The package path.
|
protected URL |
resourceURL |
Constructor and Description |
---|
ResourceLoader(File file)
Creates a new resource loader, fetching resources in the
file File, which can be a directory. |
ResourceLoader(String pack)
Creates a new resource loader, retrieving resources in a package, using the current ContextClassLoader.
|
ResourceLoader(String pack,
Class clazz)
Creates a new resource loader, retrieving resources in the
pack package, using the ClassLoader associated with a class. |
ResourceLoader(String pack,
ClassLoader loader)
Creates a new resource loader, retrieving resources in the
pack package, using a specified ClassLoader. |
ResourceLoader(String pack,
Object o)
Creates a new resource loader, retrieving resources in the
pack package, using the ClassLoader associated with an Object. |
ResourceLoader(URL url)
Creates a new resource loader, fetching resources in the
url URL (which can be a directory). |
Modifier and Type | Method and Description |
---|---|
static ResourceLoader |
createFromContext(String pack)
CreateResourceLoader to use the ClassLoader or the caller class.
|
static ResourceLoader |
createFromContext(String pack,
boolean useInternalAPI)
CreateResourceLoader to use the ClassLoader or the caller class.
|
ClassLoader |
getDefaultClassLoader()
Return the default ClassLoader used by this Class.
|
String |
getPackage()
Return the ResourceLoader used package.
|
PropertyResourceBundle |
getPropertyResourceBundle()
Return the PropertyResourceBundle of the selected absolute URL.
|
PropertyResourceBundle |
getPropertyResourceBundle(String bundle)
Return the PropertyResourceBundle looking for the
bundle in the resource package or URL. |
InputStream |
getResourceAsStream(String name)
Return an InputStream for a resource, using the default ClassLoader.
|
InputStream |
getResourceAsStream(String name,
Class clazz)
Return an InputStream for a resource, using the ClassLoader associated with the Class which is passed in the arguments.
|
InputStream |
getResourceAsStream(String name,
Object o)
Return an InputStream for a resource, using the ClassLoader associated with the Object which is passed in the arguments.
|
static InputStream |
getResourceAsStream(String pack,
String name)
Return an InputStream for a resource, using the current ContextClassLoader.
|
static InputStream |
getResourceAsStream(String pack,
String name,
Class clazz)
Return an InputStream for a resource, using the ClassLoader associated with the Class which is passed in the arguments.
|
static InputStream |
getResourceAsStream(String pack,
String name,
ClassLoader loader)
Return an InputStream for a resource, using the a specified ClassLoader.
|
static InputStream |
getResourceAsStream(String pack,
String name,
Object o)
Return an InputStream for a resource, using the ClassLoader associated with the Object which is passed in the arguments.
|
URL |
getResourceURL()
Return the resource URL.
|
URL |
getURL(String path)
Return the URL with a specified path, using the package provided at the class creation, and the default ClassLoader.
|
URL |
getURL(String path,
Class clazz)
Return the URL with a specified path, using the package provided at the class creation, and the ClassLoader associated with the Class
which is passed in the arguments.
|
URL |
getURL(String path,
ClassLoader loader)
Return the URL with a specified path, using a specified ClassLoader.
|
URL |
getURL(String path,
Object o)
Return the URL with a specified path, using the package provided at the class creation, and the ClassLoader associated with the Object
which is passed in the arguments.
|
static URL |
getURL(String pack,
String name)
Return an URL for a resource, using the current ContextClassLoader.
|
static URL |
getURL(String pack,
String name,
ClassLoader loader)
Return an URL for a resource, using a specified ClassLoader.
|
protected URL resourceURL
protected String pack
protected ClassLoader loader
public ResourceLoader(String pack)
ResourceLoader loader = new ResourceLoader("svglab/resources");It is useful to retrieve bundled resources.
pack
- the packagepublic ResourceLoader(String pack, ClassLoader loader)
pack
package, using a specified ClassLoader.
Example:
ResourceLoader loader = new ResourceLoader("mypack/resources", myClassLoader);
pack
- the packageloader
- the ClassLoaderpublic ResourceLoader(String pack, Class clazz)
pack
package, using the ClassLoader associated with a class.
Example:
ResourceLoader loader = new ResourceLoader("mypack/resources", myClassLoader);
pack
- the packageclazz
- the class used to get the Class loaderpublic ResourceLoader(String pack, Object o)
pack
package, using the ClassLoader associated with an Object.
Example:
ResourceLoader loader = new ResourceLoader("mypack/resources", myClassLoader);
pack
- the packageo
- the Object used to get the Class loaderpublic ResourceLoader(File file)
file
File, which can be a directory. It is useful to retrieve
use properties resources.file
- the Filepublic ResourceLoader(URL url)
url
URL (which can be a directory).url
- the URLpublic static ResourceLoader createFromContext(String pack)
createFromContext(java.lang.String, boolean)
with
false for the second argumentpack
- the packagepublic static ResourceLoader createFromContext(String pack, boolean useInternalAPI)
pack
- the packageuseInternalAPI
- true if the sun.reflect.Reflection
will be usedpublic URL getResourceURL()
public String getPackage()
public ClassLoader getDefaultClassLoader()
public PropertyResourceBundle getPropertyResourceBundle()
public PropertyResourceBundle getPropertyResourceBundle(String bundle)
bundle
in the resource package or URL. This method will use the default
ClassLoader.bundle
- the bundlepublic URL getURL(String path)
getPackage()
).
There are two cases for returning the URL:
path
- the pathpublic URL getURL(String path, ClassLoader loader)
getPackage()
). There are two cases for returning the URL:
loader
- the ClassLoaderpath
- the pathpublic URL getURL(String path, Class clazz)
getPackage()
). There
are two cases for returning the URL:
clazz
- the Class used to get the ClassLoaderpath
- the pathpublic URL getURL(String path, Object o)
getPackage()
). There
are two cases for returning the URL:
o
- the Object used to get the ClassLoaderpath
- the pathpublic static URL getURL(String pack, String name)
getPackage()
).pack
- the packagename
- the resource namepublic static URL getURL(String pack, String name, ClassLoader loader)
getPackage()
).pack
- the packagename
- the resource nameloader
- the ClassLoaderpublic InputStream getResourceAsStream(String name)
getPackage()
).name
- the resource namepublic InputStream getResourceAsStream(String name, Class clazz)
getPackage()
).name
- the resource nameclazz
- the Class used to get the ClassLoaderpublic InputStream getResourceAsStream(String name, Object o)
getPackage()
).name
- the resource nameo
- the Object used to get the ClassLoaderpublic static InputStream getResourceAsStream(String pack, String name)
pack
- the packagename
- the resource namepublic static InputStream getResourceAsStream(String pack, String name, ClassLoader loader)
pack
- the packagename
- the resource nameloader
- the ClassLoaderpublic static InputStream getResourceAsStream(String pack, String name, Class clazz)
pack
- the packagename
- the resource nameclazz
- the Class used to get the ClassLoaderpublic static InputStream getResourceAsStream(String pack, String name, Object o)
pack
- the packagename
- the resource nameo
- the Object used to get the ClassLoader