Added a version of getResourceAsStream() that takes the site identifier

for when the site has already been identified.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@422 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2001-11-05 18:08:01 +00:00
parent dad303a022
commit 3148bf5c76
2 changed files with 37 additions and 29 deletions
@@ -1,5 +1,5 @@
//
// $Id: SiteResourceLoader.java,v 1.1 2001/11/05 09:12:31 mdb Exp $
// $Id: SiteResourceLoader.java,v 1.2 2001/11/05 18:08:01 mdb Exp $
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne
@@ -112,10 +112,29 @@ public class SiteResourceLoader
HttpServletRequest req, String path)
throws IOException
{
InputStream stream = null;
return getResourceAsStream(_siteIdent.identifySite(req), path);
}
// figure out which site in which we're interested
int siteId = _siteIdent.identifySite(req);
/**
* Loads the specific resource, from the site-specific jar file if one
* exists and contains the specified resource, or via the servlet
* context. 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.
*
* @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)
throws IOException
{
InputStream stream = null;
// if we identified a non-default site, we first look for a
// site-specific resource
@@ -1,5 +1,5 @@
//
// $Id: SiteResourceLoaderTest.java,v 1.1 2001/11/05 09:12:31 mdb Exp $
// $Id: SiteResourceLoaderTest.java,v 1.2 2001/11/05 18:08:01 mdb Exp $
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne
@@ -59,27 +59,24 @@ public class SiteResourceLoaderTest extends TestCase
SiteResourceLoader loader = new SiteResourceLoader(ident, context);
try {
testResourceLoader(loader, "defaultout.txt");
ident.setSiteId(SITE1_ID);
testResourceLoader(loader, "site1out.txt");
ident.setSiteId(SITE2_ID);
testResourceLoader(loader, "site2out.txt");
testResourceLoader(SiteIdentifier.DEFAULT_SITE_ID,
loader, "defaultout.txt");
testResourceLoader(SITE1_ID, loader, "site1out.txt");
testResourceLoader(SITE2_ID, loader, "site2out.txt");
} catch (IOException ioe) {
fail("Error testing resource loader: " + ioe);
}
}
protected void testResourceLoader (SiteResourceLoader loader,
String compareFile)
protected void testResourceLoader (
int siteId, SiteResourceLoader loader, String compareFile)
throws IOException
{
StringBuffer gen = new StringBuffer();
appendResource(gen, loader, "/header.txt");
appendResource(gen, loader, "/body.txt");
appendResource(gen, loader, "/footer.txt");
appendResource(siteId, gen, loader, "/header.txt");
appendResource(siteId, gen, loader, "/body.txt");
appendResource(siteId, gen, loader, "/footer.txt");
StringBuffer cmp = new StringBuffer();
compareFile = "servlet/srl/" + compareFile;
@@ -96,11 +93,11 @@ public class SiteResourceLoaderTest extends TestCase
cmp.toString(), gen.toString());
}
protected void appendResource (
StringBuffer buffer, SiteResourceLoader loader, String path)
protected void appendResource (int siteId, StringBuffer buffer,
SiteResourceLoader loader, String path)
throws IOException
{
InputStream rin = loader.getResourceAsStream(null, path);
InputStream rin = loader.getResourceAsStream(siteId, path);
if (rin == null) {
throw new IOException("Unable to load " + path);
}
@@ -165,27 +162,19 @@ public class SiteResourceLoaderTest extends TestCase
public static class TestSiteIdentifier implements SiteIdentifier
{
public void setSiteId (int siteId)
{
_siteId = siteId;
}
public int identifySite (HttpServletRequest req)
{
return _siteId;
return DEFAULT_SITE_ID;
}
public String getSiteString (int siteId)
{
// we are required to map site ids to strings, however
switch (siteId) {
case SITE1_ID: return "site1";
case SITE2_ID: return "site2";
default: return DEFAULT_SITE_STRING;
}
}
protected int _siteId = SiteIdentifier.DEFAULT_SITE_ID;
}
protected static final int SITE1_ID = 1;