ca.cbc.sportwire.servlet.data
Class DBCacheBean

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--ca.cbc.sportwire.util.PeriodicData
              |
              +--ca.cbc.sportwire.servlet.data.DBCacheBean
All Implemented Interfaces:
DBCacheProperties, GlobalCacheProperties, java.lang.Runnable, SportPageProperties, Stoppable

public class DBCacheBean
extends PeriodicData
implements SportPageProperties, DBCacheProperties, GlobalCacheProperties

DBCacheBean is a general purpose base class for JSP page beans that do a period update from a database, then offer the result set read-only to a large number of concurrent sessions. Data results are refreshed according to a periodicity, and then cached in the global MRUCacheMap so that queries are removed from memory if no one actually accesses them, but are kept in reasonable sync with the database while they are popular.

NOTE: This class creates a JdbcConnection db pool object and stashes it in the application-scoped ExtendedProperties to use for future calls.

Created: Fri Nov 23 13:29:20 2001

Version:
$Id: DBCacheBean.java,v 1.13 2002/02/19 06:24:18 garym Exp $
Author:
Gary Lawrence Murphy

Field Summary
private  TTLCacheMap cache
           
(package private) static org.apache.log4j.Category cat
          Set up a reporting category in Log4J
private  java.util.ArrayList columns
           
private  JdbcConnections connectPool
           
private  java.lang.String query
           
private  java.util.ArrayList results
           
 
Fields inherited from class ca.cbc.sportwire.util.PeriodicData
interval, running
 
Fields inherited from class java.lang.Thread
contextClassLoader, daemon, eetop, group, inheritableThreadLocals, inheritedAccessControlContext, MAX_PRIORITY, MIN_PRIORITY, name, NORM_PRIORITY, priority, single_step, stillborn, stopThreadPermission, target, threadInitNumber, threadLocals, threadQ, userDaemon
 
Fields inherited from interface ca.cbc.sportwire.servlet.data.SportPageProperties
BEAN_SUFFIX, BEANFACTORY_VAR, CONTEXT_VAR, DATE_VAR, DEFAULT_EXTS, DEFAULT_INDEXES, DEFAULT_PAGE_SUFFIX, DEFAULT_SAX_DRIVER_CLASS, DEFAULT_TIMEZONE, DEFAULT_TOPIC, DOM_VAR, ERROR_TEMPLATE_VAR, EXCEPTION_VAR, EXTENSIONS_VAR, FALSE, GLOBAL_VAR, HOME_PROPERTY, INCLUDE_EXTS_PROPERTY, INCLUDE_VAR, INDEX_INDICATOR_VAR, INDEX_TEMPLATE_VAR, LAYOUT_TEMPLATE_VAR, MACRO_LIB_PROPERTY, MISSING_TEMPLATE_VAR, OUTPUT_VAR, PAGE_EXPIRE, PAGE_TEMPLATE_VAR, PANIC_404_PAGE, PANIC_404_PROPERTY, PANIC_500_PAGE, PANIC_500_PROPERTY, PARAM_PARSER_VAR, PATH_VAR, REQUEST_VAR, RESPONSE_VAR, SAX_CLASS_PROPERTY, SERVLET_DEBUG, SERVLET_LOGCONF_PROPERTY, SQL_SUFFIX, TEMPLATE_PATHS_PROPERTY, TIMEZONE_PROPERTY, TRUE, URI_VAR, VELOCITY_LOGFILE_DEFAULT, VELOCITY_LOGFILE_PROPERTY, XML_EXTS, XML_EXTS_PROPERTY, XML_PATHS_PROPERTY, XML_SUFFIX, XSL_EXTS, XSL_EXTS_PROPERTY, XSL_PATHS_PROPERTY
 
Fields inherited from interface ca.cbc.sportwire.servlet.data.DBCacheProperties
DB_CACHE_DEFAULT_PERIOD, DB_REFRESH_PROPERTY, DBCACHE_POOL_PROPERTY, DBCACHE_TEXT_FILTER_DEFAULT, DBCACHE_TEXT_FILTER_PROPERTY
 
Fields inherited from interface ca.cbc.sportwire.servlet.data.GlobalCacheProperties
CACHE_DEFAULT_PERIOD, CACHE_DEFAULT_TIMEOUT, CACHE_PERIOD, CACHE_TIMEOUT, MRU_PROPERTY, MRU_VAR, TTL_PROPERTY, TTL_VAR
 
Constructor Summary
DBCacheBean()
          Creates a new DBCacheBean instance with no values set; could be used this way within JSP; the refreshing doesn't start until you set the query.
DBCacheBean(org.apache.commons.collections.ExtendedProperties sc)
          constructor used withing Velocity to pre-build a query object using the default periodicities.
DBCacheBean(org.apache.commons.collections.ExtendedProperties sc, java.lang.String query)
          DBCacheBean constructor to set up the connection from the server properties and also set the query to start the fetch.
DBCacheBean(org.apache.commons.collections.ExtendedProperties sc, java.lang.String query, int reloadPeriod)
          DBCacheBean deluxe constructor constructor sets the query, and sets the reload-period.
 
Method Summary
protected  TTLCacheMap getCache()
          Get the cache map.
 java.util.ArrayList getColumns()
          Get the list of column labels as an ArrayList in the same order as the result set.
protected  JdbcConnections getConnectPool()
          Get the connectPool; this object caches its XALAN connectPool used to get connections to the database.
static DBCacheBean getInstance(org.apache.velocity.context.Context context)
          getInstance attempts to fetch the matching instance via the Velocity Context $globals link to the TTL cache.
static DBCacheBean getInstance(org.apache.velocity.context.Context context, java.lang.String sql)
          getInstance gets the app config and also sets the query in motion.
static DBCacheBean getInstance(org.apache.commons.collections.ExtendedProperties sc)
          getInstance:
static DBCacheBean getInstance(org.apache.commons.collections.ExtendedProperties sc, java.lang.String sql)
          getInstance is the same as the version using the Velocity page config, but gets the app config directly.
 java.lang.String getQuery()
          Get the SQL query; since updates are done periodically, this must handle time-sensitive reports on the update schedule.
protected static int getReloadPeriod(org.apache.commons.collections.ExtendedProperties sc)
          getReloadPeriod computes the refresh period from the properties, or sets it to the default DB_CACHE_DEFAULT_PERIOD
 java.util.ArrayList getResults()
          getResults will wait for an initial ResultSet and then return the cached list of columns and a list of value lists (rows of columns of Object elements) -- if you need the column labels, they are in the getColumns().
protected  void refresh()
          refresh is the PeriodicData method called by the timer thread.
protected  void setCache(TTLCacheMap v)
          Set the cache.
protected  void setColumns(java.util.ArrayList v)
          Set the columns list.
protected  void setConnectPool(org.apache.commons.collections.ExtendedProperties sc)
          Retrieve the connectPool from the global properties, or create one and stash it if none exists.
protected  void setQuery(java.lang.String v)
          Set the SQL query and begin the updates.
 void setResults()
          Set the value of cached results by doing a database lookup using the statement; notes that a ResultSet falls out of scope with the Statement that created it so we make a copy to be used by the cache.
 
Methods inherited from class ca.cbc.sportwire.util.PeriodicData
bump, isRunning, run, setInterval, setRunning
 
Methods inherited from class java.lang.Thread
, activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, exit, getContextClassLoader, getName, getPriority, getThreadGroup, init, interrupt, interrupt0, interrupted, isAlive, isDaemon, isInterrupted, isInterrupted, join, join, join, nextThreadNum, registerNatives, resume, resume0, setContextClassLoader, setDaemon, setName, setPriority, setPriority0, sleep, sleep, start, stop, stop, stop0, suspend, suspend0, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

cat

static org.apache.log4j.Category cat
Set up a reporting category in Log4J

connectPool

private JdbcConnections connectPool

cache

private TTLCacheMap cache

query

private java.lang.String query

columns

private java.util.ArrayList columns

results

private java.util.ArrayList results
Constructor Detail

DBCacheBean

public DBCacheBean()
Creates a new DBCacheBean instance with no values set; could be used this way within JSP; the refreshing doesn't start until you set the query.

DBCacheBean

public DBCacheBean(org.apache.commons.collections.ExtendedProperties sc)
constructor used withing Velocity to pre-build a query object using the default periodicities.
Parameters:
sc - the ExtendedProperties app config

DBCacheBean

public DBCacheBean(org.apache.commons.collections.ExtendedProperties sc,
                   java.lang.String query)
DBCacheBean constructor to set up the connection from the server properties and also set the query to start the fetch.
Parameters:
sc - the ExtendedProperties app config
query - a String SQL statement.

DBCacheBean

public DBCacheBean(org.apache.commons.collections.ExtendedProperties sc,
                   java.lang.String query,
                   int reloadPeriod)
DBCacheBean deluxe constructor constructor sets the query, and sets the reload-period.
Parameters:
sc - an ExtendedProperties app config
query - a String SQL statement
reloadPeriod - an int seconds value
Method Detail

getConnectPool

protected JdbcConnections getConnectPool()
Get the connectPool; this object caches its XALAN connectPool used to get connections to the database.
Returns:
JdbcConnections value of connectPool.

setConnectPool

protected void setConnectPool(org.apache.commons.collections.ExtendedProperties sc)
Retrieve the connectPool from the global properties, or create one and stash it if none exists.
Parameters:
sc - the ExtendedProperties app config

getCache

protected TTLCacheMap getCache()
Get the cache map.
Returns:
TTLCacheMap cache. Could be a MRUCacheMap.

setCache

protected void setCache(TTLCacheMap v)
Set the cache.
Parameters:
v - Value to assign to cache.

getQuery

public java.lang.String getQuery()
Get the SQL query; since updates are done periodically, this must handle time-sensitive reports on the update schedule.
Returns:
String value SQL query.

setQuery

protected void setQuery(java.lang.String v)
Set the SQL query and begin the updates.
Parameters:
v - String value of the SQL query.

getReloadPeriod

protected static int getReloadPeriod(org.apache.commons.collections.ExtendedProperties sc)
getReloadPeriod computes the refresh period from the properties, or sets it to the default DB_CACHE_DEFAULT_PERIOD
Parameters:
sc - an ExtendedProperties value
Returns:
an int value

getInstance

public static DBCacheBean getInstance(org.apache.velocity.context.Context context)
getInstance attempts to fetch the matching instance via the Velocity Context $globals link to the TTL cache.
Parameters:
context - the Velocity page Context
Returns:
a DBCacheBean value

getInstance

public static DBCacheBean getInstance(org.apache.velocity.context.Context context,
                                      java.lang.String sql)
getInstance gets the app config and also sets the query in motion.
Parameters:
context - a Velocity page Context
sql - a String SQL statement
Returns:
a DBCacheBean value

getInstance

public static DBCacheBean getInstance(org.apache.commons.collections.ExtendedProperties sc)
getInstance:
Parameters:
sc - an ExtendedProperties value
Returns:
a DBCacheBean value

getInstance

public static DBCacheBean getInstance(org.apache.commons.collections.ExtendedProperties sc,
                                      java.lang.String sql)
getInstance is the same as the version using the Velocity page config, but gets the app config directly. This is used in pre-loading SQL beans specified in the sportpage.conf file.
Parameters:
sc - the ExtendedProperties app config
sql - a String SQL statement
Returns:
a DBCacheBean value

refresh

protected void refresh()
refresh is the PeriodicData method called by the timer thread. Here it re-fetches the result set.
Overrides:
refresh in class PeriodicData

getColumns

public java.util.ArrayList getColumns()
Get the list of column labels as an ArrayList in the same order as the result set. ArrayList would let you query this list to find a string if you need to pick and choose (or you can alter your SQL, which is preferable). Be aware that the columns use JDBC ColumnLabel and not the ColumnName ... I'm not certain how you distinguish these on the RDBMS side.
Returns:
list of ColumnName Strings

setColumns

protected void setColumns(java.util.ArrayList v)
Set the columns list.
Parameters:
v - ArrayList value to assign to columns.

getResults

public java.util.ArrayList getResults()
getResults will wait for an initial ResultSet and then return the cached list of columns and a list of value lists (rows of columns of Object elements) -- if you need the column labels, they are in the getColumns(). It is up to the calling program to decide what to do with the Object values.

The data is restructured into this form because a ResultSet is closed when its connection or its statement go out of scope; if we every had a requirement to store more metadata about each column, the Objects in the row lists could be wrapped in an inner class.

Returns:
List of row value Lists

setResults

public void setResults()
Set the value of cached results by doing a database lookup using the statement; notes that a ResultSet falls out of scope with the Statement that created it so we make a copy to be used by the cache.