Added ability to retrieve last modified time of the site-specific jar

file.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@427 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2001-11-06 04:48:54 +00:00
parent d9622d84de
commit 4d10799b9e
@@ -1,5 +1,5 @@
// //
// $Id: SiteResourceLoader.java,v 1.2 2001/11/05 18:08:01 mdb Exp $ // $Id: SiteResourceLoader.java,v 1.3 2001/11/06 04:48:54 mdb Exp $
// //
// samskivert library - useful routines for java programs // samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne // Copyright (C) 2001 Michael Bayne
@@ -133,6 +133,33 @@ public class SiteResourceLoader
*/ */
public InputStream getResourceAsStream (int siteId, String path) public InputStream getResourceAsStream (int siteId, String path)
throws IOException throws IOException
{
return getResourceAsStream(siteId, path, true);
}
/**
* Loads the specific resource, from the site-specific jar file if one
* exists and contains the specified resource, or via the servlet
* context (if <code>fallbackToServletContext</code> is true). If no
* resource exists with that path in either location, null will be
* returned.
*
* @param siteId the unique identifer for the site for which we are
* loading the resource.
* @param path the path to the desired resource.
* @param fallbackToServletContext if true, the servlet context will
* be searched for the resource if there is no site-specific
* resource. If false, it will not.
*
* @return an input stream via which the resource can be read or null
* if no resource could be located with the specified path.
*
* @exception IOException thrown if an I/O error occurs while loading
* a resource.
*/
public InputStream getResourceAsStream (int siteId, String path,
boolean fallbackToServletContext)
throws IOException
{ {
InputStream stream = null; InputStream stream = null;
@@ -146,7 +173,7 @@ public class SiteResourceLoader
// if we didn't find a site-specific resource (or didn't look for // if we didn't find a site-specific resource (or didn't look for
// one because we're loading for the default site), we need to // one because we're loading for the default site), we need to
// attempt to load the resource from the servlet context // attempt to load the resource from the servlet context
if (stream == null) { if (stream == null && fallbackToServletContext) {
stream = _context.getResourceAsStream(path); stream = _context.getResourceAsStream(path);
} }
@@ -154,6 +181,23 @@ public class SiteResourceLoader
return stream; return stream;
} }
/**
* Returns the last modification time of the site-specific jar file
* for the specified site.
*
* @exception IOException thrown if an error occurs accessing the
* site-specific jar file (like it doesn't exist).
*/
public long getLastModified (int siteId)
throws IOException
{
// synchronize on the lock to ensure that only one thread per site
// is concurrently executing
synchronized (getLock(siteId)) {
return getBundle(siteId).getLastModified();
}
}
/** /**
* Fetches the resource with the specified path from the site-specific * Fetches the resource with the specified path from the site-specific
* jar file associated with the specified site. * jar file associated with the specified site.
@@ -161,15 +205,34 @@ public class SiteResourceLoader
protected InputStream getSiteResourceAsStream (int siteId, String path) protected InputStream getSiteResourceAsStream (int siteId, String path)
throws IOException throws IOException
{ {
Object lock = null;
// Log.info("Loading site resource [siteId=" + siteId + // Log.info("Loading site resource [siteId=" + siteId +
// ", path=" + path + "]."); // ", path=" + path + "].");
// we synchronize on a per-site basis, but we use a separate lock // synchronize on the lock to ensure that only one thread per site
// object for each site so that the process of loading a bundle // is concurrently executing
// for the first time does not require blocking access to synchronized (getLock(siteId)) {
// resources from other sites SiteResourceBundle bundle = getBundle(siteId);
// make sure the path has no leading slash
if (path.startsWith("/")) {
path = path.substring(1);
}
// obtain our resource from the bundle
return bundle.getResourceAsStream(path);
}
}
/**
* 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.
*/
protected Object getLock (int siteId)
{
Object lock = null;
synchronized (_locks) { synchronized (_locks) {
lock = _locks.get(siteId); lock = _locks.get(siteId);
@@ -179,9 +242,16 @@ public class SiteResourceLoader
} }
} }
// now synchronize on the lock to ensure that only one thread per return lock;
// site is concurrently executing }
synchronized (lock) {
/**
* Obtains the site-specific jar file for the specified site. This
* should only be called when the lock for this site is held.
*/
protected SiteResourceBundle getBundle (int siteId)
throws IOException
{
// look up the site resource bundle for this site // look up the site resource bundle for this site
SiteResourceBundle bundle = (SiteResourceBundle) SiteResourceBundle bundle = (SiteResourceBundle)
_bundles.get(siteId); _bundles.get(siteId);
@@ -199,14 +269,7 @@ public class SiteResourceLoader
_bundles.put(siteId, bundle); _bundles.put(siteId, bundle);
} }
// make sure the path has no leading slash return bundle;
if (path.startsWith("/")) {
path = path.substring(1);
}
// obtain our resource from the bundle
return bundle.getResourceAsStream(path);
}
} }
/** /**
@@ -223,10 +286,6 @@ public class SiteResourceLoader
/** A handle on the site-specific jar file. */ /** A handle on the site-specific jar file. */
public File file; public File file;
/** The last modified time of the jar file at the time that we
* opened it for reading. */
public long lastModified;
/** /**
* Constructs a new site resource bundle. The associated jar file * Constructs a new site resource bundle. The associated jar file
* will be opened the first time a resource is read. * will be opened the first time a resource is read.
@@ -256,6 +315,18 @@ public class SiteResourceLoader
return entry == null ? null : jarFile.getInputStream(entry); return entry == null ? null : jarFile.getInputStream(entry);
} }
/**
* Returns the last modified time of the underlying jar file.
*/
public long getLastModified ()
throws IOException
{
// open or reopen our underlying jar file as necessary
refreshJarFile();
return _lastModified;
}
/** /**
* Reopens our site-specific jar file if it has been modified * Reopens our site-specific jar file if it has been modified
* since it was last opened. * since it was last opened.
@@ -265,9 +336,9 @@ public class SiteResourceLoader
{ {
// determine whether or not we need to create a new jarfile // determine whether or not we need to create a new jarfile
// instance // instance
if (file.lastModified() > lastModified) { if (file.lastModified() > _lastModified) {
// make a note of the last modified time // make a note of the last modified time
lastModified = file.lastModified(); _lastModified = file.lastModified();
// close our old jar file if we've got one // close our old jar file if we've got one
if (jarFile != null) { if (jarFile != null) {
@@ -281,6 +352,10 @@ public class SiteResourceLoader
jarFile = new JarFile(file); jarFile = new JarFile(file);
} }
} }
/** The last modified time of the jar file at the time that we
* opened it for reading. */
protected long _lastModified;
} }
/** The site identifier we use to identify requests. */ /** The site identifier we use to identify requests. */