org.sadun.util
Class DatabaseResourceBundle

java.lang.Object
  extended byjava.util.ResourceBundle
      extended byorg.sadun.util.DatabaseResourceBundle

public class DatabaseResourceBundle
extends java.util.ResourceBundle

A resource bundle which reads resources from a database.

The default resource table is defined by the constant RESOURCETABLE_DEFAULT_NAME.

A resource table must have the fields (KEY_NAME: VARCHAR, LOCALE: VARCHAR, VALUE: VARCHAR) which are queried when looking up for a key.

Different resource tables may be employed for different keys, by using the setResourceTable() methods, which associate a regular expression to a table name, and make so that a key which matches the regular expression queries the given name.

For example,

 
  setResourceTable("STEP1DIALOG_*", "STEP1_DIALOG_RESOURCES");
 
will associate the table STEP1_DIALOG_RESOURCES to any key matching the STEP1DIALOG_* regular expression.

The bundle typically pre-loads and caches the keys (via its autoAdjustCache, preLoad and cacheSize propreties) to avoid accessing the database for each key lookup.

Author:
Cristiano Sadun

Field Summary
static int DEFAULT_CACHE_SIZE
          Define the default key cache size to 100
static java.lang.String RESOURCETABLE_DEFAULT_NAME
          Define the default name of the default resource table to "APPLICATION_RESOURCES".
 
Fields inherited from class java.util.ResourceBundle
parent
 
Constructor Summary
DatabaseResourceBundle(javax.sql.DataSource ds)
          Create a database resource bundle which will look for resource using the given RESOURCETABLE_DEFAULT_NAME (see setResourceTable()) found at the given jdbc URL, using a cache of the DEFAULT_CACHE_SIZE size.
DatabaseResourceBundle(javax.sql.DataSource ds, int cacheSize)
          Create a database resource bundle which will look for resource using the given RESOURCETABLE_DEFAULT_NAME (see setResourceTable()) found at the given jdbc URL, using a cache of given size.
DatabaseResourceBundle(javax.sql.DataSource ds, java.lang.String defaultResourceTable)
          Create a database resource bundle which will look for resource using the given default table name (see setResourceTable()) found at the given jdbc URL, using a cache of the DEFAULT_CACHE_SIZE size.
DatabaseResourceBundle(javax.sql.DataSource ds, java.lang.String defaultResourceTable, int cacheSize)
          Create a database resource bundle which will look for resource using the given default table name (see setResourceTable()) found at the given jdbc URL, using a cache of the given size.
DatabaseResourceBundle(java.lang.String jdbcUrl)
          Create a database resource bundle which will look for resource using the given RESOURCETABLE_DEFAULT_NAME (see setResourceTable()) found at the given jdbc URL, using a cache of the DEFAULT_CACHE_SIZE size.
DatabaseResourceBundle(java.lang.String jdbcUrl, int cacheSize)
          Create a database resource bundle which will look for resource using the given RESOURCETABLE_DEFAULT_NAME (see setResourceTable()) found at the given jdbc URL, using a cache of given size.
DatabaseResourceBundle(java.lang.String jdbcUrl, java.lang.String defaultResourceTable)
          Create a database resource bundle which will look for resource using the given default table name (see setResourceTable()) found at the given jdbc URL, using a cache of the DEFAULT_CACHE_SIZE size.
DatabaseResourceBundle(java.lang.String jdbcUrl, java.lang.String defaultResourceTable, int cacheSize)
          Create a database resource bundle which will look for resource using the given default table name (see setResourceTable()) found at the given jdbc URL, using a cache of the given size.
 
Method Summary
 void clearCache()
          Clear the cache.
 int getCacheSize()
          Retur the current size of the cache.
 java.lang.String getJdbcUrl()
          Return the JDBC url of the database containing the resources.
 java.util.Enumeration getKeys()
          Connect to all the resource tables and finds out the available keys.
protected  java.lang.Object handleGetObject(java.lang.String key)
          Finds the table matching the key (or uses the default table) and expects it to have a (KEY: VARCHAR[50], LOCALE: VARCHAR[5], VALUE: VARCHAR[100]) structure.
 boolean isAutoAdjustCache()
          Return the value of the auto-adjust-cache property (see setAutoAdjustCache().
 boolean isPreLoad()
          Return the value of the pre-load property (see setPreLoad()).
static void main(java.lang.String[] args)
           
 void setAutoAdjustCache(boolean autoAdjustCache)
          Set the auto-adjust-cache property.
 void setCacheSize(int cacheSize)
          Set the current size of the cache.
 void setJdbcUrl(java.lang.String jdbcUrl)
          Set the JDBC url of the database containing the resources.
 void setPreLoad(boolean preLoad)
          Set the value of the pre-load property.
 void setResourceTable(java.util.regex.Pattern keyPattern, java.lang.String tableName)
          Set the resource table associated to keys matching the given regular expression.
 void setResourceTable(java.lang.String keyRegExpPattern, java.lang.String tableName)
          Set the resource table associated to keys matching the given regular expression.
 
Methods inherited from class java.util.ResourceBundle
getBundle, getBundle, getBundle, getLocale, getObject, getString, getStringArray, setParent
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RESOURCETABLE_DEFAULT_NAME

public static final java.lang.String RESOURCETABLE_DEFAULT_NAME
Define the default name of the default resource table to "APPLICATION_RESOURCES".

See Also:
Constant Field Values

DEFAULT_CACHE_SIZE

public static final int DEFAULT_CACHE_SIZE
Define the default key cache size to 100

See Also:
Constant Field Values
Constructor Detail

DatabaseResourceBundle

public DatabaseResourceBundle(javax.sql.DataSource ds,
                              java.lang.String defaultResourceTable,
                              int cacheSize)
Create a database resource bundle which will look for resource using the given default table name (see setResourceTable()) found at the given jdbc URL, using a cache of the given size.

The bundle will pre-load the keys.

Parameters:
ds - the data source to connect to. A ConfigurableDataSource can be used to wrap a JDBC URL if necessary
defaultResourceTable - the name of the table where to look, by default, for keys. The table must have a specific layout (see class comment) and is used unless a specific table name is set for a specific key regular expression pattern by setResourceTable().
cacheSize - the size of the in-core key cache

DatabaseResourceBundle

public DatabaseResourceBundle(javax.sql.DataSource ds,
                              java.lang.String defaultResourceTable)
Create a database resource bundle which will look for resource using the given default table name (see setResourceTable()) found at the given jdbc URL, using a cache of the DEFAULT_CACHE_SIZE size.

The bundle will pre-load the keys.

Parameters:
ds - the data source to connect to. A ConfigurableDataSource can be used to wrap a JDBC URL if necessary
defaultResourceTable - the name of the table where to look, by default, for keys. The table must have a specific layout (see class comment) and is used unless a specific table name is set for a specific key regular expression pattern by setResourceTable().

DatabaseResourceBundle

public DatabaseResourceBundle(javax.sql.DataSource ds,
                              int cacheSize)
Create a database resource bundle which will look for resource using the given RESOURCETABLE_DEFAULT_NAME (see setResourceTable()) found at the given jdbc URL, using a cache of given size.

The bundle will pre-load the keys.

Parameters:
ds - the data source to connect to. A ConfigurableDataSource can be used to wrap a JDBC URL if necessary
cacheSize - the size of the in-core key cache

DatabaseResourceBundle

public DatabaseResourceBundle(javax.sql.DataSource ds)
Create a database resource bundle which will look for resource using the given RESOURCETABLE_DEFAULT_NAME (see setResourceTable()) found at the given jdbc URL, using a cache of the DEFAULT_CACHE_SIZE size.

The bundle will pre-load the keys.

Parameters:
ds - the data source to connect to. A ConfigurableDataSource can be used to wrap a JDBC URL if necessary

DatabaseResourceBundle

public DatabaseResourceBundle(java.lang.String jdbcUrl,
                              java.lang.String defaultResourceTable,
                              int cacheSize)
Create a database resource bundle which will look for resource using the given default table name (see setResourceTable()) found at the given jdbc URL, using a cache of the given size.

The bundle will pre-load the keys.

Parameters:
jdbcUrl - the JDBC URL of the Database to connect to
defaultResourceTable - the name of the table where to look, by default, for keys. The table must have a specific layout (see class comment) and is used unless a specific table name is set for a specific key regular expression pattern by setResourceTable().
cacheSize - the size of the in-core key cache

DatabaseResourceBundle

public DatabaseResourceBundle(java.lang.String jdbcUrl,
                              java.lang.String defaultResourceTable)
Create a database resource bundle which will look for resource using the given default table name (see setResourceTable()) found at the given jdbc URL, using a cache of the DEFAULT_CACHE_SIZE size.

The bundle will pre-load the keys.

Parameters:
jdbcUrl - the JDBC URL of the Database to connect to
defaultResourceTable - the name of the table where to look, by default, for keys. The table must have a specific layout (see class comment) and is used unless a specific table name is set for a specific key regular expression pattern by setResourceTable().

DatabaseResourceBundle

public DatabaseResourceBundle(java.lang.String jdbcUrl,
                              int cacheSize)
Create a database resource bundle which will look for resource using the given RESOURCETABLE_DEFAULT_NAME (see setResourceTable()) found at the given jdbc URL, using a cache of given size.

The bundle will pre-load the keys.

Parameters:
jdbcUrl - the JDBC URL of the Database to connect to
cacheSize - the size of the in-core key cache

DatabaseResourceBundle

public DatabaseResourceBundle(java.lang.String jdbcUrl)
Create a database resource bundle which will look for resource using the given RESOURCETABLE_DEFAULT_NAME (see setResourceTable()) found at the given jdbc URL, using a cache of the DEFAULT_CACHE_SIZE size.

The bundle will pre-load the keys.

Parameters:
jdbcUrl - the JDBC URL of the Database to connect to
Method Detail

clearCache

public void clearCache()
Clear the cache. The next invocation will reconnect and re-read to the database.


setResourceTable

public void setResourceTable(java.lang.String keyRegExpPattern,
                             java.lang.String tableName)
Set the resource table associated to keys matching the given regular expression.

When queried with a key, the bundle will match the key with the regular expression, and use the given table rather than the default table if there is a match.

Parameters:
keyRegExpPattern - the regular expression to match
tableName - the table where to lookup resources whose key matches the given regular expression

setResourceTable

public void setResourceTable(java.util.regex.Pattern keyPattern,
                             java.lang.String tableName)
Set the resource table associated to keys matching the given regular expression.

When queried with a key, the bundle will match the key with the regular expression, and use the given table rather than the default table if there is a match.

Parameters:
keyPattern - the regular expression pattern to match
tableName - the table where to lookup resources whose key matches the given regular expression

handleGetObject

protected java.lang.Object handleGetObject(java.lang.String key)
Finds the table matching the key (or uses the default table) and expects it to have a (KEY: VARCHAR[50], LOCALE: VARCHAR[5], VALUE: VARCHAR[100]) structure. (non-Javadoc)

See Also:
ResourceBundle.handleGetObject(java.lang.String)

getKeys

public java.util.Enumeration getKeys()
Connect to all the resource tables and finds out the available keys.

This method can be time-consuming.

See Also:
ResourceBundle.getKeys()

isAutoAdjustCache

public boolean isAutoAdjustCache()
Return the value of the auto-adjust-cache property (see setAutoAdjustCache().

Returns:
the value of the auto-adjust-cache property.

setAutoAdjustCache

public void setAutoAdjustCache(boolean autoAdjustCache)
Set the auto-adjust-cache property. Clear the cache if necessary.

Parameters:
autoAdjustCache - if true, the cache will adjust itself to the size necessary to hold all the keys for one resource table.

getCacheSize

public int getCacheSize()
Retur the current size of the cache.

Returns:
the current size of the cache.

setCacheSize

public void setCacheSize(int cacheSize)
Set the current size of the cache. The auto-adjust-cache property (see {@link #setAutoAdjustCache(boolean) is automatically set to false. Clear the cache if necessary.

Parameters:
cacheSize - the current size of the cache.

getJdbcUrl

public java.lang.String getJdbcUrl()
Return the JDBC url of the database containing the resources.

Returns:
the JDBC url of the database containing the resources.

setJdbcUrl

public void setJdbcUrl(java.lang.String jdbcUrl)
Set the JDBC url of the database containing the resources. Clear the cache if necessary.

Parameters:
jdbcUrl - the JDBC url of the database containing the resources.

isPreLoad

public boolean isPreLoad()
Return the value of the pre-load property (see setPreLoad()).

Returns:
the value of the pre-load property.

setPreLoad

public void setPreLoad(boolean preLoad)
Set the value of the pre-load property.

Parameters:
preLoad - if true, the bundle will pre-load all the key/value pairs on the first opportunity.

main

public static void main(java.lang.String[] args)