public static URL getResource(String resourceName,
Class callingClass) {
URL url = Thread.currentThread().getContextClassLoader().getResource(resourceName);
if (url == null) {
url = ClassLoaderUtil.class.getClassLoader().getResource(resourceName);
}
if (url == null) {
ClassLoader cl = callingClass.getClassLoader();
if (cl != null) {
url = cl.getResource(resourceName);
}
}
if ((url == null) && (resourceName != null) && (resourceName.charAt(0) != '/")) {
return getResource('/" + resourceName, callingClass);
}
return url;
}
Load a given resource.
This method will try to load the resource using the following methods (in order):
- From Thread.currentThread().getContextClassLoader()
- From ClassLoaderUtil.class.getClassLoader()
- callingClass.getClassLoader()
|
public static Iterator getResources(String resourceName,
Class callingClass,
boolean aggregate) throws IOException {
AggregateIterator< URL > iterator = new AggregateIterator< URL >();
iterator.addEnumeration(Thread.currentThread().getContextClassLoader().getResources(resourceName));
if (!iterator.hasNext() || aggregate) {
iterator.addEnumeration(ClassLoaderUtil.class.getClassLoader().getResources(resourceName));
}
if (!iterator.hasNext() || aggregate) {
ClassLoader cl = callingClass.getClassLoader();
if (cl != null) {
iterator.addEnumeration(cl.getResources(resourceName));
}
}
if (!iterator.hasNext() && (resourceName != null) && (resourceName.charAt(0) != '/")) {
return getResources('/" + resourceName, callingClass, aggregate);
}
return iterator;
}
Load all resources with a given name, potentially aggregating all results
from the searched classloaders. If no results are found, the resource name
is prepended by '/' and tried again.
This method will try to load the resources using the following methods (in order):
- From Thread.currentThread().getContextClassLoader()
- From ClassLoaderUtil.class.getClassLoader()
- callingClass.getClassLoader()
|