org.jasig.portal.utils
Class ResourceLoader

java.lang.Object
  extended by org.jasig.portal.utils.ResourceLoader

public class ResourceLoader
extends Object

This utility provides methods for accessing resources. The methods generally use the classpath to find the resource if the requested URL isn't already specified as a fully-qualified URL string.

The methods of this class sort of replace the old UtiltiesBean.fixURI() method.

Since:
uPortal 2.0
Version:
$Revision: 19776 $
Author:
Ken Weiner, kweiner@unicon.net

Constructor Summary
ResourceLoader()
           
 
Method Summary
static Document getResourceAsDocument(Class<?> requestingClass, String resource)
          Get the contents of a URL as an XML Document, first trying to read the Document with validation turned on, and falling back to reading it with validation turned off.
static Document getResourceAsDocument(Class<?> requestingClass, String resource, boolean validate)
          Get the contents of a URL as an XML Document
static Properties getResourceAsProperties(Class<?> requestingClass, String resource)
          Get the contents of a URL as a java.util.Properties object
static InputSource getResourceAsSAXInputSource(Class<?> requestingClass, String resource)
          Returns the requested resource as a SAX input source.
static InputStream getResourceAsStream(Class<?> requestingClass, String resource)
          Returns the requested resource as a stream.
static String getResourceAsString(Class<?> requestingClass, String resource)
          Get the contents of a URL as a String
static URL getResourceAsURL(Class<?> requestingClass, String resource)
          Finds a resource with a given name.
static String getResourceAsURLString(Class<?> requestingClass, String resource)
          Returns the requested resource as a URL string.
static long getResourceLastModified(Class<?> requestingClass, String resource)
           
 Map<Tuple<Class<?>,String>,URL> getResourceUrlCache()
           
 Map<Tuple<Class<?>,String>,ResourceMissingException> getResourceUrlNotFoundCache()
           
 void setResourceUrlCache(Map<Tuple<Class<?>,String>,URL> resourceUrlCache)
           
 void setResourceUrlNotFoundCache(Map<Tuple<Class<?>,String>,ResourceMissingException> resourceUrlNotFoundCache)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ResourceLoader

public ResourceLoader()
Method Detail

getResourceUrlCache

public Map<Tuple<Class<?>,String>,URL> getResourceUrlCache()
Returns:
the resourceUrlCache

setResourceUrlCache

public void setResourceUrlCache(Map<Tuple<Class<?>,String>,URL> resourceUrlCache)
Parameters:
resourceUrlCache - the resourceUrlCache to set

getResourceUrlNotFoundCache

public Map<Tuple<Class<?>,String>,ResourceMissingException> getResourceUrlNotFoundCache()
Returns:
the resourceUrlNotFoundCache

setResourceUrlNotFoundCache

public void setResourceUrlNotFoundCache(Map<Tuple<Class<?>,String>,ResourceMissingException> resourceUrlNotFoundCache)
Parameters:
resourceUrlNotFoundCache - the resourceUrlNotFoundCache to set

getResourceAsURL

public static URL getResourceAsURL(Class<?> requestingClass,
                                   String resource)
                            throws ResourceMissingException
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.

Parameters:
requestingClass - the java.lang.Class object of the class that is attempting to load the resource
resource - a String describing the full or partial URL of the resource to load
Returns:
a URL identifying the requested resource
Throws:
ResourceMissingException

getResourceAsURLString

public static String getResourceAsURLString(Class<?> requestingClass,
                                            String resource)
                                     throws ResourceMissingException
Returns the requested resource as a URL string.

Parameters:
requestingClass - the java.lang.Class object of the class that is attempting to load the resource
resource - a String describing the full or partial URL of the resource to load
Returns:
the requested resource as a URL string
Throws:
ResourceMissingException

getResourceLastModified

public static long getResourceLastModified(Class<?> requestingClass,
                                           String resource)

getResourceAsStream

public static InputStream getResourceAsStream(Class<?> requestingClass,
                                              String resource)
                                       throws ResourceMissingException,
                                              IOException
Returns the requested resource as a stream.

Parameters:
requestingClass - the java.lang.Class object of the class that is attempting to load the resource
resource - a String describing the full or partial URL of the resource to load
Returns:
the requested resource as a stream
Throws:
ResourceMissingException
IOException

getResourceAsSAXInputSource

public static InputSource getResourceAsSAXInputSource(Class<?> requestingClass,
                                                      String resource)
                                               throws ResourceMissingException,
                                                      IOException
Returns the requested resource as a SAX input source.

Parameters:
requestingClass - the java.lang.Class object of the class that is attempting to load the resource
resource - a String describing the full or partial URL of the resource to load
Returns:
the requested resource as a SAX input source
Throws:
ResourceMissingException
IOException

getResourceAsDocument

public static Document getResourceAsDocument(Class<?> requestingClass,
                                             String resource,
                                             boolean validate)
                                      throws ResourceMissingException,
                                             IOException,
                                             ParserConfigurationException,
                                             SAXException
Get the contents of a URL as an XML Document

Parameters:
requestingClass - the java.lang.Class object of the class that is attempting to load the resource
resource - a String describing the full or partial URL of the resource whose contents to load
validate - boolean. True if the document builder factory should validate, false otherwise.
Returns:
the actual contents of the resource as an XML Document
Throws:
ResourceMissingException
IOException
ParserConfigurationException
SAXException

getResourceAsDocument

public static Document getResourceAsDocument(Class<?> requestingClass,
                                             String resource)
                                      throws ResourceMissingException,
                                             IOException,
                                             ParserConfigurationException,
                                             SAXException
Get the contents of a URL as an XML Document, first trying to read the Document with validation turned on, and falling back to reading it with validation turned off.

Parameters:
requestingClass - the java.lang.Class object of the class that is attempting to load the resource
resource - a String describing the full or partial URL of the resource whose contents to load
Returns:
the actual contents of the resource as an XML Document
Throws:
ResourceMissingException
IOException
ParserConfigurationException
SAXException

getResourceAsProperties

public static Properties getResourceAsProperties(Class<?> requestingClass,
                                                 String resource)
                                          throws ResourceMissingException,
                                                 IOException
Get the contents of a URL as a java.util.Properties object

Parameters:
requestingClass - the java.lang.Class object of the class that is attempting to load the resource
resource - a String describing the full or partial URL of the resource whose contents to load
Returns:
the actual contents of the resource as a Properties object
Throws:
ResourceMissingException
IOException

getResourceAsString

public static String getResourceAsString(Class<?> requestingClass,
                                         String resource)
                                  throws ResourceMissingException,
                                         IOException
Get the contents of a URL as a String

Parameters:
requestingClass - the java.lang.Class object of the class that is attempting to load the resource
resource - a String describing the full or partial URL of the resource whose contents to load
Returns:
the actual contents of the resource as a String
Throws:
ResourceMissingException
IOException


Copyright © 2010 Jasig. All Rights Reserved.