Revamped site handling so that we can eventually migrate all the site

stuff into JDBCTableSiteIdentifier and out of UserRepository.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@1293 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2003-11-13 00:53:00 +00:00
parent 57bda0ba01
commit 135ea9ecc4
5 changed files with 105 additions and 38 deletions
@@ -1,5 +1,5 @@
// //
// $Id: IndiscriminateSiteIdentifier.java,v 1.2 2001/11/06 04:48:08 mdb Exp $ // $Id: IndiscriminateSiteIdentifier.java,v 1.3 2003/11/13 00:53:00 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
@@ -20,6 +20,9 @@
package com.samskivert.servlet; package com.samskivert.servlet;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
/** /**
@@ -45,4 +48,24 @@ public class IndiscriminateSiteIdentifier implements SiteIdentifier
{ {
return DEFAULT_SITE_STRING; return DEFAULT_SITE_STRING;
} }
/**
* Always returns {@link #DEFAULT_SITE_ID} regardless of the value of
* the supplied string.
*/
public int getSiteId (String siteString)
{
return DEFAULT_SITE_ID;
}
// documented inherited from interface
public Iterator enumerateSites ()
{
return _sites.iterator();
}
protected static ArrayList _sites = new ArrayList();
static {
_sites.add(new Site(DEFAULT_SITE_ID, DEFAULT_SITE_STRING));
}
} }
@@ -1,5 +1,5 @@
// //
// $Id: JDBCTableSiteIdentifier.java,v 1.3 2003/07/04 20:34:34 mdb Exp $ // $Id: JDBCTableSiteIdentifier.java,v 1.4 2003/11/13 00:53:00 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
@@ -27,7 +27,11 @@ import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import com.samskivert.io.PersistenceException; import com.samskivert.io.PersistenceException;
import com.samskivert.jdbc.ConnectionProvider; import com.samskivert.jdbc.ConnectionProvider;
@@ -94,8 +98,21 @@ public class JDBCTableSiteIdentifier implements SiteIdentifier
// documentation inherited // documentation inherited
public String getSiteString (int siteId) public String getSiteString (int siteId)
{ {
String stringId = (String)_sites.get(siteId); Site site = (Site)_sitesById.get(siteId);
return (stringId == null) ? DEFAULT_SITE_STRING : stringId; return (site == null) ? DEFAULT_SITE_STRING : site.siteString;
}
// documentation inherited
public int getSiteId (String siteString)
{
Site site = (Site)_sitesByString.get(siteString);
return (site == null) ? DEFAULT_SITE_ID : site.siteId;
}
// documentation inherited from interface
public Iterator enumerateSites ()
{
return _sitesById.values().iterator();
} }
/** /**
@@ -125,10 +142,14 @@ public class JDBCTableSiteIdentifier implements SiteIdentifier
String query = "select siteId, stringId from sites"; String query = "select siteId, stringId from sites";
ResultSet rs = stmt.executeQuery(query); ResultSet rs = stmt.executeQuery(query);
HashIntMap sites = new HashIntMap(); HashIntMap sites = new HashIntMap();
HashMap strings = new HashMap();
while (rs.next()) { while (rs.next()) {
sites.put(rs.getInt(1), rs.getString(2)); Site site = new Site(rs.getInt(1), rs.getString(2));
sites.put(site.siteId, site);
strings.put(site.siteString, site);
} }
_sites = sites; _sitesById = sites;
_sitesByString = strings;
// now load up the domain mappings // now load up the domain mappings
query = "select domain, siteId from domains"; query = "select domain, siteId from domains";
@@ -209,5 +230,9 @@ public class JDBCTableSiteIdentifier implements SiteIdentifier
/** The mapping from integer site identifiers to string site /** The mapping from integer site identifiers to string site
* identifiers. */ * identifiers. */
protected HashIntMap _sites = new HashIntMap(); protected HashIntMap _sitesById = new HashIntMap();
/** The mapping from string site identifiers to integer site
* identifiers. */
protected HashMap _sitesByString = new HashMap();
} }
@@ -1,42 +1,42 @@
// //
// $Id: Site.java,v 1.1 2003/09/19 02:12:55 eric Exp $ // $Id: Site.java,v 1.2 2003/11/13 00:53:00 mdb Exp $
package com.samskivert.servlet.user; package com.samskivert.servlet;
import com.samskivert.util.StringUtil; import com.samskivert.util.StringUtil;
/** /**
* A representation of a row in the sites table. * Represents a site mapping known to a {@link SiteIdentifier}.
*
* @see SiteIdentifier#enumerateSites
*/ */
public class Site public class Site
{ {
/** The sites unqiue identifier. */ /** The site's unqiue identifier. */
public int siteId; public int siteId;
/** The sites human readable identifier (I.e., "Shockwave") */ /** The site's human readable identifier (i.e., "monkeybutter"). */
public String stringId; public String siteString;
/** Construct a Site record with the specified siteId. */ /** Constructs a site record with the specified id and string. */
public Site (int siteId) public Site (int siteId, String siteString)
{ {
this.siteId = siteId; this.siteId = siteId;
} this.siteString = siteString;
/** Construct a Site record with the specified stringId. */
public Site (String stringId)
{
this.stringId = stringId;
} }
/** /**
* Constructs a blank Site record for unserialization * Constructs a blank record for unserialization from the repository.
* from the repository.
*/ */
public Site () public Site ()
{ {
} }
public String toString () { /**
* Generates a string representation of this instance.
*/
public String toString ()
{
return StringUtil.fieldsToString(this); return StringUtil.fieldsToString(this);
} }
} }
@@ -1,5 +1,5 @@
// //
// $Id: SiteIdentifier.java,v 1.2 2001/11/06 04:48:35 mdb Exp $ // $Id: SiteIdentifier.java,v 1.3 2003/11/13 00:53:00 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
@@ -20,6 +20,7 @@
package com.samskivert.servlet; package com.samskivert.servlet;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
/** /**
@@ -56,11 +57,11 @@ public interface SiteIdentifier
public int identifySite (HttpServletRequest req); public int identifySite (HttpServletRequest req);
/** /**
* Returns a string representation of the site identifier. The site * Returns a string representation of the site identifier. The
* identifier in use can map the site ids to strings however it likes * SiteIdentifier in use can map the site ids to strings however it
* as long as it consistently maps the same identifier to the same * likes as long as it consistently maps the same identifier to the
* string. Presumably these strings would be human readable and * same string and vice versa. Presumably these strings would be human
* meaningful. * readable and meaningful.
* *
* @param siteId the unique integer identifier for the site that we * @param siteId the unique integer identifier for the site that we
* wish to be identified by a string. * wish to be identified by a string.
@@ -68,4 +69,22 @@ public interface SiteIdentifier
* @return the string identifier for this site. * @return the string identifier for this site.
*/ */
public String getSiteString (int siteId); 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.
*
* @param siteString the string to be converted into a site identifer.
*
* @return the integer identifier for this site.
*/
public int getSiteId (String siteString);
/**
* Returns an enumerator over all {@link Site} mappings known to this
* SiteIdentifier.
*/
public Iterator enumerateSites ();
} }
@@ -1,5 +1,5 @@
// //
// $Id: UserRepository.java,v 1.34 2003/10/29 19:41:52 eric Exp $ // $Id: UserRepository.java,v 1.35 2003/11/13 00:53:00 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
@@ -43,9 +43,11 @@ import com.samskivert.jdbc.jora.Cursor;
import com.samskivert.jdbc.jora.FieldMask; import com.samskivert.jdbc.jora.FieldMask;
import com.samskivert.jdbc.jora.Session; import com.samskivert.jdbc.jora.Session;
import com.samskivert.jdbc.jora.Table; import com.samskivert.jdbc.jora.Table;
import com.samskivert.servlet.SiteIdentifier;
import com.samskivert.util.HashIntMap; import com.samskivert.util.HashIntMap;
import com.samskivert.servlet.Site;
import com.samskivert.servlet.SiteIdentifier;
/** /**
* Interfaces with the RDBMS in which the user information is stored. The * Interfaces with the RDBMS in which the user information is stored. The
* user repository encapsulates the creating, loading and management of * user repository encapsulates the creating, loading and management of
@@ -117,8 +119,8 @@ public class UserRepository extends JORARepository
* *
* @return The userid of the newly created user. * @return The userid of the newly created user.
*/ */
public int createUser (String username, String password, public int createUser (String username, String password, String realname,
String realname, String email, int siteId) String email, int siteId)
throws InvalidUsernameException, UserExistsException, throws InvalidUsernameException, UserExistsException,
PersistenceException PersistenceException
{ {
@@ -352,7 +354,7 @@ public class UserRepository extends JORARepository
while (itr.hasNext()) { while (itr.hasNext()) {
Site site = (Site)itr.next(); Site site = (Site)itr.next();
_siteIdToSite.put(site.siteId, site); _siteIdToSite.put(site.siteId, site);
_siteNameToSite.put(site.stringId, site); _siteNameToSite.put(site.siteString, site);
} }
return null; return null;
} }
@@ -365,7 +367,6 @@ public class UserRepository extends JORARepository
public int getSiteId (String stringId) public int getSiteId (String stringId)
{ {
Site site = (Site)_siteNameToSite.get(stringId); Site site = (Site)_siteNameToSite.get(stringId);
return (site == null) ? -1 : site.siteId; return (site == null) ? -1 : site.siteId;
} }
@@ -375,8 +376,7 @@ public class UserRepository extends JORARepository
public String getSiteName (int siteId) public String getSiteName (int siteId)
{ {
Site site = (Site)_siteIdToSite.get(siteId); Site site = (Site)_siteIdToSite.get(siteId);
return (site == null) ? null : site.siteString;
return (site == null) ? null : site.stringId;
} }
/** /**