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 // samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne // Copyright (C) 2001 Michael Bayne
@@ -112,10 +112,29 @@ public class SiteResourceLoader
HttpServletRequest req, String path) HttpServletRequest req, String path)
throws IOException 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 // if we identified a non-default site, we first look for a
// site-specific resource // 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 // samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne // Copyright (C) 2001 Michael Bayne
@@ -59,27 +59,24 @@ public class SiteResourceLoaderTest extends TestCase
SiteResourceLoader loader = new SiteResourceLoader(ident, context); SiteResourceLoader loader = new SiteResourceLoader(ident, context);
try { try {
testResourceLoader(loader, "defaultout.txt"); testResourceLoader(SiteIdentifier.DEFAULT_SITE_ID,
loader, "defaultout.txt");
ident.setSiteId(SITE1_ID); testResourceLoader(SITE1_ID, loader, "site1out.txt");
testResourceLoader(loader, "site1out.txt"); testResourceLoader(SITE2_ID, loader, "site2out.txt");
ident.setSiteId(SITE2_ID);
testResourceLoader(loader, "site2out.txt");
} catch (IOException ioe) { } catch (IOException ioe) {
fail("Error testing resource loader: " + ioe); fail("Error testing resource loader: " + ioe);
} }
} }
protected void testResourceLoader (SiteResourceLoader loader, protected void testResourceLoader (
String compareFile) int siteId, SiteResourceLoader loader, String compareFile)
throws IOException throws IOException
{ {
StringBuffer gen = new StringBuffer(); StringBuffer gen = new StringBuffer();
appendResource(gen, loader, "/header.txt"); appendResource(siteId, gen, loader, "/header.txt");
appendResource(gen, loader, "/body.txt"); appendResource(siteId, gen, loader, "/body.txt");
appendResource(gen, loader, "/footer.txt"); appendResource(siteId, gen, loader, "/footer.txt");
StringBuffer cmp = new StringBuffer(); StringBuffer cmp = new StringBuffer();
compareFile = "servlet/srl/" + compareFile; compareFile = "servlet/srl/" + compareFile;
@@ -96,11 +93,11 @@ public class SiteResourceLoaderTest extends TestCase
cmp.toString(), gen.toString()); cmp.toString(), gen.toString());
} }
protected void appendResource ( protected void appendResource (int siteId, StringBuffer buffer,
StringBuffer buffer, SiteResourceLoader loader, String path) SiteResourceLoader loader, String path)
throws IOException throws IOException
{ {
InputStream rin = loader.getResourceAsStream(null, path); InputStream rin = loader.getResourceAsStream(siteId, path);
if (rin == null) { if (rin == null) {
throw new IOException("Unable to load " + path); throw new IOException("Unable to load " + path);
} }
@@ -165,27 +162,19 @@ public class SiteResourceLoaderTest extends TestCase
public static class TestSiteIdentifier implements SiteIdentifier public static class TestSiteIdentifier implements SiteIdentifier
{ {
public void setSiteId (int siteId)
{
_siteId = siteId;
}
public int identifySite (HttpServletRequest req) public int identifySite (HttpServletRequest req)
{ {
return _siteId; return DEFAULT_SITE_ID;
} }
public String getSiteString (int siteId) public String getSiteString (int siteId)
{ {
// we are required to map site ids to strings, however
switch (siteId) { switch (siteId) {
case SITE1_ID: return "site1"; case SITE1_ID: return "site1";
case SITE2_ID: return "site2"; case SITE2_ID: return "site2";
default: return DEFAULT_SITE_STRING; default: return DEFAULT_SITE_STRING;
} }
} }
protected int _siteId = SiteIdentifier.DEFAULT_SITE_ID;
} }
protected static final int SITE1_ID = 1; protected static final int SITE1_ID = 1;