org.apache.commons.i18n
Class JdbcMessageProvider

java.lang.Object
  extended by org.apache.commons.i18n.JdbcMessageProvider
All Implemented Interfaces:
MessageProvider

public class JdbcMessageProvider
extends java.lang.Object
implements MessageProvider

The JdbcMessageProvider provides messages stored in a database (or other data source) accessible via JDBC. The JdbcMessageProvider only has support for different languages, but if support for country or variant is required one could easily subclass it and override the getLocale method. If getLocale is overridden, the languageColumn parameter (or jdbc.sql.locale.column Map entry) of the constructors may be null, since it will not be used.

Author:
Mattias Jiderhamn

Constructor Summary
JdbcMessageProvider(java.sql.Connection conn, java.lang.String table, java.lang.String idColumn, java.lang.String languageColumn)
          Create new JDBC MessageProvider using the provided connection.
JdbcMessageProvider(javax.sql.DataSource ds, java.lang.String table, java.lang.String idColumn, java.lang.String languageColumn)
          Create new JDBC MessageProvider using a connection from the provided DataSource.
JdbcMessageProvider(java.util.Map properties)
          Create JDBC MessageProvider from properties in a Map, such as a java.util.Properties object.
 
Method Summary
 java.util.Map getEntries(java.lang.String id, java.util.Locale locale)
           
protected  java.util.Locale getLocale(java.sql.ResultSet rs)
          Get Locale for the current record in the ResultSet.
 java.lang.String getText(java.lang.String id, java.lang.String entry, java.util.Locale locale)
           
protected  java.lang.String[] getValueColumns(java.sql.ResultSet rs)
          Get a String of all the column names, except the ID column and the language column.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JdbcMessageProvider

public JdbcMessageProvider(java.sql.Connection conn,
                           java.lang.String table,
                           java.lang.String idColumn,
                           java.lang.String languageColumn)
                    throws java.sql.SQLException
Create new JDBC MessageProvider using the provided connection.

Parameters:
conn - The connection to use for initialization.
table - The name of the table holding the messages
idColumn - The name of the column holding the message ID
languageColumn - The name of the column containing the ISO-639 language code.
Throws:
java.sql.SQLException - If there is an error getting data from the table

JdbcMessageProvider

public JdbcMessageProvider(javax.sql.DataSource ds,
                           java.lang.String table,
                           java.lang.String idColumn,
                           java.lang.String languageColumn)
                    throws java.sql.SQLException
Create new JDBC MessageProvider using a connection from the provided DataSource. Will get a connection from the DataSource, initialize and then return the connection.

Parameters:
ds - The connection to use for initialization.
table - The name of the table holding the messages
idColumn - The name of the column holding the message ID
languageColumn - The name of the column containing the ISO-639 language code.
Throws:
java.sql.SQLException - If there is an error getting data from the table

JdbcMessageProvider

public JdbcMessageProvider(java.util.Map properties)
                    throws java.lang.ClassNotFoundException,
                           java.sql.SQLException
Create JDBC MessageProvider from properties in a Map, such as a java.util.Properties object. The following are the properties in use, which are the same as for JDBCResources of Apache Commons Resources jdbc.connect.driver = org.gjt.mm.mysql.Driver jdbc.connect.url = jdbc:mysql://localhost/resources jdbc.connect.login = resourcesTest jdbc.connect.password = resourcesTest jdbc.sql.table = resources jdbc.sql.locale.column = locale jdbc.sql.key.column = msgKey

Throws:
java.lang.ClassNotFoundException
java.sql.SQLException
Method Detail

getValueColumns

protected java.lang.String[] getValueColumns(java.sql.ResultSet rs)
                                      throws java.sql.SQLException
Get a String of all the column names, except the ID column and the language column.

Parameters:
rs - A ResultSet ready for reading meta data.
Returns:
A String array with the text value column names.
Throws:
java.sql.SQLException - If an SQL error occurs.

getLocale

protected java.util.Locale getLocale(java.sql.ResultSet rs)
                              throws java.sql.SQLException
Get Locale for the current record in the ResultSet. May be overridden by subclasses to allow for proprietary interpretation of language data. The default implementation assumes the column with the name provided as languageColumn for the constructor contains the ISO-639 code.

Returns:
The Locale of the current ResultSet record.
Throws:
java.sql.SQLException

getText

public java.lang.String getText(java.lang.String id,
                                java.lang.String entry,
                                java.util.Locale locale)
Specified by:
getText in interface MessageProvider
Parameters:
id - unique id that specifies a particular message
entry - specifies a particular entry in the specified message
locale - the locale for which this text should be provided
Returns:
returns the localized message entry matching the given message id, entry key and locale. If no match is found for the given locale, the parent locale is used, and finally the default. If the id is found but the entry is not, null is returned.

getEntries

public java.util.Map getEntries(java.lang.String id,
                                java.util.Locale locale)
Specified by:
getEntries in interface MessageProvider
Parameters:
id - unique id that specifies a particular message
locale - the locale for which to return the entries
Returns:
returns a map <entry(String) -> localized text(String) of message entries matching the given message id and locale


Copyright 2004-2008 The Apache Software Foundation. All Rights Reserved.