org.brains2b.resx
Class ExtResourceBundle

java.lang.Object
  extended by java.util.ResourceBundle
      extended by org.brains2b.resx.ExtResourceBundle

public class ExtResourceBundle
extends java.util.ResourceBundle

ResourceBundle that reads Bundles and domains from a database connection.

This is a partial rewrite of ResourceBundle to implement a type of resourcebundle next to List and Properties to take advantage of a database connection.

It does this by implicitly using properties to create a connection to a database and then lazily retrieving the information into a Hashtable.

For reasons of expedience there will be sought first for a regular ResourceBundle and only then for a database bundle. This to allow for using the faster ListResourceBundle for default Locales and a slower database connection for more uncommen Locales.

The expected table can be any table, but by default is called LOCALIZED_RESOURCE. It has a fields for the bundleName or Domain, Key, Value, Country and Language. You can overwrite getSQL to use your own table.Please make sure that the SQL statement has as first column the key and as second the value. There are no other restrictions that I'm aware of.

Due to the finalizing of the getBundle() methods it was not possible to implement it without creating new static methods getExtBundle() Needless to say the getBundle() methods will not retrieve any bundles from the database.

Version:
0.16 [22-11-2006]
Author:
dennis@brains2b.nl

Field Summary
protected static java.sql.Connection m_con
           
protected  java.lang.String m_domain
           
protected  java.util.Locale m_locale
           
 
Fields inherited from class java.util.ResourceBundle
parent
 
Constructor Summary
ExtResourceBundle(java.lang.String baseName, java.util.Locale locale)
          Constructor for DatabaseBundle.
 
Method Summary
 java.lang.String getBaseName()
          get the base name of this resource bundle
protected static java.util.ResourceBundle getBundleImpl(ExtResourceBundle res)
          Method getBundleImpl.
protected  java.sql.Connection getConnection()
          get the connection to the database from which to collect the resources
protected  java.util.HashMap getContents()
          get the content of this resource bundle as a hashtable
static java.util.ResourceBundle getExtBundle(java.lang.String baseName)
          Method getExtBundle.
static java.util.ResourceBundle getExtBundle(java.lang.String baseName, java.util.Locale locale)
          Method getExtBundle.
 java.util.Enumeration getKeys()
           
 java.util.Locale getLocale()
          get the locale with which this resource bundle was created This implementation is contrary to ResourceBundle.getLocale() which returns the Locale of the resource bundle which was actually used and could return the Locale for the parent
protected  java.util.ResourceBundle getParent()
          get the parent of this resource bundle.
 java.lang.String getSQL()
          Method getSQL.
protected  java.lang.Object handleGetObject(java.lang.String key)
           
protected  void makeConnection()
          Make a connection to the database The connection is default made using either: System.properties sql.driver the full classpath name of the driver sql.url the url to connect to the database sql.user the username with which to connect to the database sql.password the password to connect to the database or a properties.file in the root of the application named connection.properties in which the four above properties are defined.
 
Methods inherited from class java.util.ResourceBundle
getBundle, getBundle, getBundle, getObject, getString, getStringArray, setParent
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_domain

protected java.lang.String m_domain

m_locale

protected java.util.Locale m_locale

m_con

protected static java.sql.Connection m_con
Constructor Detail

ExtResourceBundle

public ExtResourceBundle(java.lang.String baseName,
                         java.util.Locale locale)
Constructor for DatabaseBundle.

Method Detail

getExtBundle

public static java.util.ResourceBundle getExtBundle(java.lang.String baseName)
Method getExtBundle. Replacement for getBundle(String) to get access to Database functions

Parameters:
baseName - String, the Name of the Bundle/Domain
Returns:
ResourceBundle

getExtBundle

public static java.util.ResourceBundle getExtBundle(java.lang.String baseName,
                                                    java.util.Locale locale)
Method getExtBundle. Replacement for getBundle(String,Locale) to get access to Database functions

Parameters:
baseName -
locale -
Returns:
ResourceBundle

getBundleImpl

protected static java.util.ResourceBundle getBundleImpl(ExtResourceBundle res)
Method getBundleImpl. Actual method for getting the Bundle.
    sequence is:
  • try finding a default List or Properties bundle
  • try to find a database bundle

Parameters:
res - ExtResourceBundle
Returns:
ResourceBundle

getParent

protected java.util.ResourceBundle getParent()
get the parent of this resource bundle.

A parent of a resource bundle can be defined as a less Locale specific resource bundle: en is the parent of en_GB and has the empy locale as its owner parent.

Returns:
ResourceBundle, the parent of this resource bundle

handleGetObject

protected java.lang.Object handleGetObject(java.lang.String key)
Specified by:
handleGetObject in class java.util.ResourceBundle
See Also:
ResourceBundle.handleGetObject(String)

getKeys

public java.util.Enumeration getKeys()
Specified by:
getKeys in class java.util.ResourceBundle
See Also:
ResourceBundle.getKeys()

makeConnection

protected void makeConnection()
                       throws java.sql.SQLException
Make a connection to the database

The connection is default made using either:

    System.properties
  • sql.driver the full classpath name of the driver
  • sql.url the url to connect to the database
  • sql.user the username with which to connect to the database
  • sql.password the password to connect to the database

or a properties.file in the root of the application named connection.properties in which the four above properties are defined.

Overwrite if you want to use another way of getting a connection

Throws:
SQLException, - in the event a connection could not be established
java.sql.SQLException

getSQL

public java.lang.String getSQL()
Method getSQL.

Composes the default query for getting values from a LOCALIZED_RESOURCE table. The query should be defined to get all values from a given base/domain, language and country at once

    The table definition is expected to be as Follows:
  • DOMAIN VARCHAR
  • KEY VARCHAR
  • VALUE VARCHAR
  • LANGUAGE VARCHAR
  • COUNTRY VARCHAR

The lengths are not specified, so you can use your own create scripts are however provided for this table in the projects package

Overwrite if you want to use another table

Returns:
String, the SQL-query

getContents

protected java.util.HashMap getContents()
get the content of this resource bundle as a hashtable

Returns:
Hashtable, the content of this resource bundle

getConnection

protected java.sql.Connection getConnection()
                                     throws java.sql.SQLException
get the connection to the database from which to collect the resources

Returns:
Connection
Throws:
java.sql.SQLException, - thrown if the connection to the database fails
java.sql.SQLException
See Also:
makeConnection()

getBaseName

public java.lang.String getBaseName()
get the base name of this resource bundle

Returns:
String, the name of the bundle

getLocale

public java.util.Locale getLocale()
get the locale with which this resource bundle was created

This implementation is contrary to ResourceBundle.getLocale() which returns the Locale of the resource bundle which was actually used and could return the Locale for the parent

Overrides:
getLocale in class java.util.ResourceBundle
See Also:
ResourceBundle.getLocale()