diff --git a/src/java/com/samskivert/servlet/IndiscriminateSiteIdentifier.java b/src/java/com/samskivert/servlet/IndiscriminateSiteIdentifier.java
index 8dfdf9a7..c7647be2 100644
--- a/src/java/com/samskivert/servlet/IndiscriminateSiteIdentifier.java
+++ b/src/java/com/samskivert/servlet/IndiscriminateSiteIdentifier.java
@@ -26,23 +26,28 @@ import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
/**
- * Used by default and for systems that have no need to discriminate
- * between different sites in their web applications.
+ * Used by default and for systems that have no need to discriminate between different sites in
+ * their web applications.
*/
public class IndiscriminateSiteIdentifier implements SiteIdentifier
{
/**
- * Always returns {@link #DEFAULT_SITE_ID} regardless of the
- * information in the request.
+ * Always returns {@link #DEFAULT_SITE_ID} regardless of the information in the request.
*/
public int identifySite (HttpServletRequest req)
{
- return DEFAULT_SITE_ID;
+ // check whether our site id was overridden
+ Integer override = (Integer)req.getAttribute(SITE_ID_OVERRIDE_KEY);
+ if (override != null) {
+ return override;
+ } else {
+ return DEFAULT_SITE_ID;
+ }
}
/**
- * Always returns {@link #DEFAULT_SITE_STRING} regardless of the value
- * of the supplied identifer.
+ * Always returns {@link #DEFAULT_SITE_STRING} regardless of the value of the supplied
+ * identifer.
*/
public String getSiteString (int siteId)
{
@@ -50,8 +55,7 @@ public class IndiscriminateSiteIdentifier implements SiteIdentifier
}
/**
- * Always returns {@link #DEFAULT_SITE_ID} regardless of the value of
- * the supplied string.
+ * Always returns {@link #DEFAULT_SITE_ID} regardless of the value of the supplied string.
*/
public int getSiteId (String siteString)
{
diff --git a/src/java/com/samskivert/servlet/JDBCTableSiteIdentifier.java b/src/java/com/samskivert/servlet/JDBCTableSiteIdentifier.java
index bf82a319..b68f8580 100644
--- a/src/java/com/samskivert/servlet/JDBCTableSiteIdentifier.java
+++ b/src/java/com/samskivert/servlet/JDBCTableSiteIdentifier.java
@@ -46,35 +46,31 @@ import com.samskivert.util.HashIntMap;
import com.samskivert.Log;
/**
- * Accomplishes the process of site identification based on a mapping from
- * domains (e.g. samskivert.com) to site identifiers that is maintained in
- * a database table, accessible via JDBC (hence the name).
+ * Accomplishes the process of site identification based on a mapping from domains (e.g.
+ * samskivert.com) to site identifiers that is maintained in a database table, accessible via JDBC
+ * (hence the name).
*
- *
There are two tables, one that maps domains to site identifiers and
- * another that maps site identifiers to site strings. These are both
- * loaded at construct time and refreshed periodically in the course of
- * normal operation.
+ *
There are two tables, one that maps domains to site identifiers and another that maps site
+ * identifiers to site strings. These are both loaded at construct time and refreshed periodically
+ * in the course of normal operation.
*
- *
Note that any of the calls to identify, lookup or enumerate site
- * information can result in the sites table being refreshed from the
- * database which will take relatively much longer than the simple
- * hashtable lookup that the operations normally require. However, this
- * happens only once every 15 minutes and the circumstances in which the
- * site identifier are normally used can generally accomodate the extra
- * 100 milliseconds or so that it is likely to take to reload the (tiny)
- * sites and domains tables from the database.
+ *
Note that any of the calls to identify, lookup or enumerate site information can result in
+ * the sites table being refreshed from the database which will take relatively much longer than
+ * the simple hashtable lookup that the operations normally require. However, this happens only
+ * once every 15 minutes and the circumstances in which the site identifier are normally used can
+ * generally accomodate the extra 100 milliseconds or so that it is likely to take to reload the
+ * (tiny) sites and domains tables from the database.
*/
public class JDBCTableSiteIdentifier implements SiteIdentifier
{
- /** The database identifier used to obtain a connection from our
- * connection provider. The value is sitedb which you'll
- * probably need to know to provide the proper configuration to your
- * connection provider. */
+ /** The database identifier used to obtain a connection from our connection provider. The value
+ * is sitedb which you'll probably need to know to provide the proper
+ * configuration to your connection provider. */
public static final String SITE_IDENTIFIER_IDENT = "sitedb";
/**
- * Constructs a JDBC table site identifier with the supplied
- * connection provider from which to obtain its database connection.
+ * Constructs a JDBC table site identifier with the supplied connection provider from which to
+ * obtain its database connection.
*
* @see #SITE_IDENTIFIER_IDENT
*/
@@ -89,6 +85,12 @@ public class JDBCTableSiteIdentifier implements SiteIdentifier
// documentation inherited
public int identifySite (HttpServletRequest req)
{
+ // check whether our site id was overridden
+ Integer override = (Integer)req.getAttribute(SITE_ID_OVERRIDE_KEY);
+ if (override != null) {
+ return override;
+ }
+
checkReloadSites();
String serverName = req.getServerName();
@@ -131,7 +133,6 @@ public class JDBCTableSiteIdentifier implements SiteIdentifier
/**
* Insert a new site into the site table and into this mapping.
*/
- @SuppressWarnings("unchecked")
public Site insertNewSite (String siteString)
throws PersistenceException
{
@@ -144,11 +145,11 @@ public class JDBCTableSiteIdentifier implements SiteIdentifier
site.siteString = siteString;
_repo.insertNewSite(site);
- // add it to our two mapping tables, taking care to avoid causing
- // enumerateSites() to choke
- HashMap newStrings = (HashMap)
- _sitesByString.clone();
- HashIntMap newIds = (HashIntMap)_sitesById.clone();
+ // add it to our two mapping tables, taking care to avoid causing enumerateSites() to choke
+ @SuppressWarnings("unchecked") HashMap newStrings =
+ (HashMap)_sitesByString.clone();
+ @SuppressWarnings("unchecked") HashIntMap newIds =
+ (HashIntMap)_sitesById.clone();
newIds.put(site.siteId, site);
newStrings.put(site.siteString, site);
_sitesByString = newStrings;
@@ -158,8 +159,7 @@ public class JDBCTableSiteIdentifier implements SiteIdentifier
}
/**
- * Checks to see if we should reload our sites information from the
- * sites table.
+ * Checks to see if we should reload our sites information from the sites table.
*/
protected void checkReloadSites ()
{
@@ -221,15 +221,13 @@ public class JDBCTableSiteIdentifier implements SiteIdentifier
rs = stmt.executeQuery(query);
ArrayList mappings = new ArrayList();
while (rs.next()) {
- mappings.add(new SiteMapping(rs.getInt(2),
- rs.getString(1)));
+ mappings.add(new SiteMapping(rs.getInt(2), rs.getString(1)));
}
// sort the mappings in order of specificity
Collections.sort(mappings);
_mappings = mappings;
-// Log.info("Loaded site mappings " +
-// StringUtil.toString(_mappings) + ".");
+// Log.info("Loaded site mappings " + StringUtil.toString(_mappings) + ".");
// nothing to return
return null;
@@ -251,12 +249,10 @@ public class JDBCTableSiteIdentifier implements SiteIdentifier
{
PreparedStatement stmt = null;
try {
- stmt = conn.prepareStatement(
- "insert into sites (siteString) VALUES (?)");
+ stmt = conn.prepareStatement("insert into sites (siteString) VALUES (?)");
stmt.setString(1, site.siteString);
if (1 != stmt.executeUpdate()) {
- throw new PersistenceException(
- "Not inserted " + site);
+ throw new PersistenceException("Not inserted " + site);
}
site.siteId = liaison.lastInsertedId(conn);
@@ -291,8 +287,7 @@ public class JDBCTableSiteIdentifier implements SiteIdentifier
}
/**
- * Site mappings sort from most specific (www.yahoo.com) to least
- * specific (yahoo.com).
+ * Site mappings sort from most specific (www.yahoo.com) to least specific (yahoo.com).
*/
public int compareTo (SiteMapping other)
{
@@ -308,23 +303,18 @@ public class JDBCTableSiteIdentifier implements SiteIdentifier
protected String _rdomain;
}
- /** The repository through which we load up site identifier
- * information. */
+ /** The repository through which we load up site identifier information. */
protected SiteIdentifierRepository _repo;
- /** The list of domain to site identifier mappings ordered from most
- * specific domain to least specific. */
- protected volatile ArrayList _mappings =
- new ArrayList();
+ /** The list of domain to site identifier mappings ordered from most specific domain to least
+ * specific. */
+ protected volatile ArrayList _mappings = new ArrayList();
- /** The mapping from integer site identifiers to string site
- * identifiers. */
+ /** The mapping from integer site identifiers to string site identifiers. */
protected volatile HashIntMap _sitesById = new HashIntMap();
- /** The mapping from string site identifiers to integer site
- * identifiers. */
- protected volatile HashMap _sitesByString =
- new HashMap();
+ /** The mapping from string site identifiers to integer site identifiers. */
+ protected volatile HashMap _sitesByString = new HashMap();
/** Used to periodically reload our site data. */
protected long _lastReload;
diff --git a/src/java/com/samskivert/servlet/SiteIdentifier.java b/src/java/com/samskivert/servlet/SiteIdentifier.java
index 543aaab3..ff6022d8 100644
--- a/src/java/com/samskivert/servlet/SiteIdentifier.java
+++ b/src/java/com/samskivert/servlet/SiteIdentifier.java
@@ -24,57 +24,56 @@ import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
/**
- * Responsible for determining the unique site identifier based on
- * information available in the HTTP request. Site identifiers are
- * integers ranging from 1 to {@link Integer#MAX_VALUE}. Because the site
- * identifier implementation is likely to have access to the site
- * classification metadata, this interface is also used to map integer
- * site identifiers to string site identifiers.
+ * Responsible for determining the unique site identifier based on information available in the
+ * HTTP request. Site identifiers are integers ranging from 1 to {@link Integer#MAX_VALUE}. Because
+ * the site identifier implementation is likely to have access to the site classification metadata,
+ * this interface is also used to map integer site identifiers to string site identifiers.
*/
public interface SiteIdentifier
{
- /** The default site identifier, to be used when a site cannot be
- * identified or for site identifiers that don't wish to distinguish
- * between sites. */
+ /** The default site identifier, to be used when a site cannot be identified or for site
+ * identifiers that don't wish to distinguish between sites. */
public static final int DEFAULT_SITE_ID = -1;
/** The string identifier for the default site. */
public static final String DEFAULT_SITE_STRING = "default";
+ /** Use this to override the site identification process by calling {@link
+ * HttpServletRequest#setAttribute} with this key and an Integer value indicating the site id
+ * to be used, and any call to {@link #identifySite} on that request will return the overridden
+ * site. */
+ public static final String SITE_ID_OVERRIDE_KEY = "SiteIdentifierOverride";
+
/**
- * Returns the unique identifier for the site on which this request
- * originated. That may be divined by looking at the server name, or
- * perhaps a request parameter, or part of the path info. The
- * mechanism (or mechanisms) are up to the implementation.
+ * Returns the unique identifier for the site on which this request originated. That may be
+ * divined by looking at the server name, or perhaps a request parameter, or part of the path
+ * info. The mechanism (or mechanisms) are up to the implementation. Note: the implementation
+ * must honor the {@link #SITE_ID_OVERRIDE_KEY} request attribute.
*
- * @param req the http servlet request the site for which we are
- * trying to identify.
+ * @param req the http servlet request the site for which we are trying to identify.
*
- * @return the unique site identifier requestsed or {@link
- * #DEFAULT_SITE_ID} if the site could not be identified. No site
- * should ever have a site id value of 0.
+ * @return the unique site identifier requestsed or {@link #DEFAULT_SITE_ID} if the site could
+ * not be identified. No site should ever have a site id value of 0.
*/
public int identifySite (HttpServletRequest req);
/**
- * Returns a string representation of the site identifier. The
- * SiteIdentifier in use can map the site ids to strings however it
- * likes as long as it consistently maps the same identifier to the
- * same string and vice versa. Presumably these strings would be human
- * readable and meaningful.
+ * Returns a string representation of the site identifier. The SiteIdentifier in use can map
+ * the site ids to strings however it likes as long as it consistently maps the same identifier
+ * to the same string and vice versa. Presumably these strings would be human readable and
+ * meaningful.
*
- * @param siteId the unique integer identifier for the site that we
- * wish to be identified by a string.
+ * @param siteId the unique integer identifier for the site that we wish to be identified by a
+ * string.
*
* @return the string identifier for this site.
*/
public String getSiteString (int siteId);
/**
- * Returns the site identifier for the site associated with the
- * supplied site string. The SiteIdentifier in use can map the site
- * ids to strings however it likes as long as it consistently maps the
- * same string to the same identifier and vice versa.
+ * Returns the site identifier for the site associated with the supplied site string. The
+ * SiteIdentifier in use can map the site ids to strings however it likes as long as it
+ * consistently maps the same string to the same identifier and vice versa.
*
* @param siteString the string to be converted into a site identifer.
*
@@ -83,8 +82,7 @@ public interface SiteIdentifier
public int getSiteId (String siteString);
/**
- * Returns an enumerator over all {@link Site} mappings known to this
- * SiteIdentifier.
+ * Returns an enumerator over all {@link Site} mappings known to this SiteIdentifier.
*/
public Iterator enumerateSites ();
}