com.samskivert.servlet
Class SiteResourceLoader

java.lang.Object
  extended by com.samskivert.servlet.SiteResourceLoader

public class SiteResourceLoader
extends Object

Web applications may wish to load resources in such a way that the site on which they are running is allowed to override a resource with a site-specific version (a header, footer or navigation template for example). The site resource loader provides this capability by loading resources from a site-specific jar file.

The site resource loader must be configured with the path to the site-specific jar files, the names of which are dictated by the string identifiers returned by the SiteIdentifier provided to the resource loader at construct time. For example, if the configuration dictates that site-specific jar files are located in /usr/share/java/webapps/site-data and the site identifier returns samskivert as the site identifier for a particular request, site-specific resources will be loaded from /usr/share/java/webapps/site-data/samskivert.jar.


Nested Class Summary
protected static class SiteResourceLoader.SiteClassLoader
           
static class SiteResourceLoader.SiteResourceBundle
          Encapsulates the information we need to load data from a site resource bundle as well as to determine whether the loaded bundle is up to date.
 
Field Summary
protected  HashIntMap<SiteResourceLoader.SiteResourceBundle> _bundles
          The table of site-specific jar file information.
protected  String _jarPath
          The path to our site-specific jar files.
protected  HashIntMap<ClassLoader> _loaders
          The table of site-specific class loaders.
protected  HashIntMap<Object> _locks
          We synchronize on a per-site basis.
protected  SiteIdentifier _siteIdent
          The site identifier we use to identify requests.
protected static String DEFAULT_SITE_JAR_PATH
          The default path to the site-specific jar files.
protected static String JAR_EXTENSION
          The file extension to be appended to the string site identifier to obtain the file name of the site-specific jar file.
 
Constructor Summary
SiteResourceLoader(SiteIdentifier siteIdent, String siteJarPath)
          Constructs a new resource loader.
 
Method Summary
protected  SiteResourceLoader.SiteResourceBundle getBundle(int siteId)
          Obtains the site-specific jar file for the specified site.
 long getLastModified(int siteId)
          Returns the last modification time of the site-specific jar file for the specified site.
protected  Object getLock(int siteId)
          We synchronize on a per-site basis, but we use a separate lock object for each site so that the process of loading a bundle for the first time does not require blocking access to resources from other sites.
 InputStream getResourceAsStream(javax.servlet.http.HttpServletRequest req, String path)
          Loads the specific resource, from the site-specific jar file if one exists and contains the specified resource.
 InputStream getResourceAsStream(int siteId, String path)
          Loads the specific resource, from the site-specific jar file if one exists and contains the specified resource.
 ClassLoader getSiteClassLoader(int siteId)
          Returns a class loader that loads resources from the site-specific jar file for the specified site.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_siteIdent

protected SiteIdentifier _siteIdent
The site identifier we use to identify requests.


_jarPath

protected String _jarPath
The path to our site-specific jar files.


_locks

protected HashIntMap<Object> _locks
We synchronize on a per-site basis.


_bundles

protected HashIntMap<SiteResourceLoader.SiteResourceBundle> _bundles
The table of site-specific jar file information.


_loaders

protected HashIntMap<ClassLoader> _loaders
The table of site-specific class loaders.


DEFAULT_SITE_JAR_PATH

protected static final String DEFAULT_SITE_JAR_PATH
The default path to the site-specific jar files. This won't be used without logging a complaint first.

See Also:
Constant Field Values

JAR_EXTENSION

protected static final String JAR_EXTENSION
The file extension to be appended to the string site identifier to obtain the file name of the site-specific jar file.

See Also:
Constant Field Values
Constructor Detail

SiteResourceLoader

public SiteResourceLoader(SiteIdentifier siteIdent,
                          String siteJarPath)
Constructs a new resource loader.

Parameters:
siteIdent - the site identifier to be used to identify which site through which a request was made when loading resources.
siteJarPath - the path to the site-specific jar files.
Method Detail

getResourceAsStream

public InputStream getResourceAsStream(javax.servlet.http.HttpServletRequest req,
                                       String path)
                                throws IOException
Loads the specific resource, from the site-specific jar file if one exists and contains the specified resource. If no resource exists with that path, null will be returned.

Parameters:
req - the http request for which we are loading a resource (this will be used to determine for which site the resource will be loaded).
path - the path to the desired resource.
Returns:
an input stream via which the resource can be read or null if no resource could be located with the specified path.
Throws:
IOException - thrown if an I/O error occurs while loading a resource.

getResourceAsStream

public InputStream getResourceAsStream(int siteId,
                                       String path)
                                throws IOException
Loads the specific resource, from the site-specific jar file if one exists and contains the specified resource. If no resource exists with that path, null will be returned.

Parameters:
siteId - the unique identifer for the site for which we are loading the resource.
path - the path to the desired resource.
Returns:
an input stream via which the resource can be read or null if no resource could be located with the specified path.
Throws:
IOException - thrown if an I/O error occurs while loading a resource.

getLastModified

public long getLastModified(int siteId)
                     throws IOException
Returns the last modification time of the site-specific jar file for the specified site.

Throws:
IOException - thrown if an error occurs accessing the site-specific jar file (like it doesn't exist).

getSiteClassLoader

public ClassLoader getSiteClassLoader(int siteId)
                               throws IOException
Returns a class loader that loads resources from the site-specific jar file for the specified site. If no site-specific jar file exists for the specified site, null will be returned.

Throws:
IOException

toString

public String toString()
Overrides:
toString in class Object

getLock

protected Object getLock(int siteId)
We synchronize on a per-site basis, but we use a separate lock object for each site so that the process of loading a bundle for the first time does not require blocking access to resources from other sites.


getBundle

protected SiteResourceLoader.SiteResourceBundle getBundle(int siteId)
                                                   throws IOException
Obtains the site-specific jar file for the specified site. This should only be called when the lock for this site is held.

Throws:
IOException


Copyright © 2000-2008 Michael Bayne. All Rights Reserved.