use of org.apereo.portal.ResourceMissingException in project uPortal by Jasig.
the class ResourceLoader method getResourceAsURL.
/**
* Finds a resource with a given name. This is a convenience method for accessing a resource
* from a channel or from the uPortal framework. If a well-formed URL is passed in, this method
* will use that URL unchanged to find the resource. If the URL is not well-formed, this method
* will look for the desired resource relative to the classpath. If the resource name starts
* with "/", it is unchanged. Otherwise, the package name of the requesting class is prepended
* to the resource name.
*
* @param requestingClass the java.lang.Class object of the class that is attempting to load the
* resource
* @param resource a String describing the full or partial URL of the resource to load
* @return a URL identifying the requested resource
* @throws ResourceMissingException
*/
public static URL getResourceAsURL(Class<?> requestingClass, String resource) throws ResourceMissingException {
final Tuple<Class<?>, String> cacheKey = new Tuple<Class<?>, String>(requestingClass, resource);
//Look for a cached URL
final Map<Tuple<Class<?>, String>, URL> resourceUrlCache = ResourceLoader.resourceUrlCache;
URL resourceURL = resourceUrlCache != null ? resourceUrlCache.get(cacheKey) : null;
if (resourceURL != null) {
return resourceURL;
}
//Look for a failed lookup
final Map<Tuple<Class<?>, String>, ResourceMissingException> resourceUrlNotFoundCache = ResourceLoader.resourceUrlNotFoundCache;
ResourceMissingException exception = resourceUrlNotFoundCache != null ? resourceUrlNotFoundCache.get(cacheKey) : null;
if (exception != null) {
throw new ResourceMissingException(exception);
}
try {
resourceURL = new URL(resource);
} catch (MalformedURLException murle) {
// URL is invalid, now try to load from classpath
resourceURL = requestingClass.getResource(resource);
if (resourceURL == null) {
String resourceRelativeToClasspath = null;
if (resource.startsWith("/"))
resourceRelativeToClasspath = resource;
else
resourceRelativeToClasspath = '/' + requestingClass.getPackage().getName().replace('.', '/') + '/' + resource;
exception = new ResourceMissingException(resource, resourceRelativeToClasspath, "Resource not found in classpath: " + resourceRelativeToClasspath);
if (resourceUrlNotFoundCache != null) {
resourceUrlNotFoundCache.put(cacheKey, exception);
}
throw new ResourceMissingException(exception);
}
}
if (resourceUrlCache != null) {
resourceUrlCache.put(cacheKey, resourceURL);
}
return resourceURL;
}
Aggregations