You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by df...@apache.org on 2007/04/30 12:54:04 UTC

svn commit: r533692 - /jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/JdbcMessageProvider.java

Author: dflorey
Date: Mon Apr 30 03:54:03 2007
New Revision: 533692

URL: http://svn.apache.org/viewvc?view=rev&rev=533692
Log:
Applied patch

Modified:
    jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/JdbcMessageProvider.java

Modified: jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/JdbcMessageProvider.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/JdbcMessageProvider.java?view=diff&rev=533692&r1=533691&r2=533692
==============================================================================
--- jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/JdbcMessageProvider.java (original)
+++ jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/JdbcMessageProvider.java Mon Apr 30 03:54:03 2007
@@ -3,8 +3,14 @@
 import javax.sql.DataSource;
 import java.util.*;
 import java.sql.*;
+import java.text.MessageFormat;
 
 /**
+ * The <code>JdbcMessageProvider</code> provides messages stored in a database (or other data source)
+ * accessible via JDBC. The <code>JdbcMessageProvider</code> only has support for different languages,
+ * but if support for country or variant is required one could easily subclass it and override the
+ * <code>getLocale</code> method. If <code>getLocale</code> is overridden, the languageColumn parameter
+ * (or <code>jdbc.sql.locale.column<code> Map entry) of the constructors may be null, since it will not be used.
  * @author Mattias Jiderhamn
  */
 public class JdbcMessageProvider implements MessageProvider {
@@ -18,6 +24,14 @@
 
     private String languageColumn;
 
+    /**
+     * Create new JDBC <code>MessageProvider</code> using the provided connection.
+     * @param conn The connection to use for initialization.
+     * @param table The name of the table holding the messages
+     * @param idColumn The name of the column holding the message ID
+     * @param languageColumn The name of the column containing the ISO-639 language code.
+     * @throws SQLException If there is an error getting data from the table
+     */
     public JdbcMessageProvider(Connection conn, String table, String idColumn, String languageColumn)
             throws SQLException {
         this.idColumn = idColumn;
@@ -25,6 +39,15 @@
         init(conn, table);
     }
 
+    /**
+     * Create new JDBC <code>MessageProvider</code> using a connection from the provided <code>DataSource</code>. Will
+     * get a connection from the <code>DataSource</code>, initialize and then return the connection.
+     * @param ds The connection to use for initialization.
+     * @param table The name of the table holding the messages
+     * @param idColumn The name of the column holding the message ID
+     * @param languageColumn The name of the column containing the ISO-639 language code.
+     * @throws SQLException If there is an error getting data from the table
+     */
     public JdbcMessageProvider(DataSource ds, String table, String idColumn, String languageColumn)
             throws SQLException {
         this.idColumn = idColumn;
@@ -41,9 +64,9 @@
     }
 
     /**
-     * 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 Jakarta Commons Resources
+     * Create JDBC <code>MessageProvider</code> from properties in a Map, such
+     * as a <code>java.util.Properties</code> object. The following are the properties in use, which
+     * are the same as for <code>JDBCResources</code> of Jakarta Commons Resources
      * jdbc.connect.driver               = org.gjt.mm.mysql.Driver
      * jdbc.connect.url                  = jdbc:mysql://localhost/resources
      * jdbc.connect.login                = resourcesTest
@@ -91,7 +114,9 @@
                 Locale locale = getLocale(rs);
                 Map entries = new HashMap();
                 for(int i = 0; i < valueColumns.length; i++) {
-                    entries.put(valueColumns[i], rs.getString(valueColumns[i]));
+                    String entry = rs.getString(valueColumns[i]);
+                    if(entry != null)
+                        entries.put(valueColumns[i], entry);
                 }
                 Map localeMap = (Map)locales.get(locale);
                 if(localeMap == null) { // If first record for this Locale
@@ -105,7 +130,7 @@
             if(stmt != null)
                 stmt.close();
             if(rs != null)
-              rs.close();
+                rs.close();
         }
     }
 
@@ -122,7 +147,7 @@
         int count = metadata.getColumnCount();
         for(int i = 0; i < count; i++) {
             String columnName = metadata.getColumnName(i+1); // (Count from 1)
-            if(! idColumn.equals(columnName) && ! languageColumn.equals(columnName) )
+            if(! columnName.equals(idColumn) && ! columnName.equals(languageColumn) )
                 output.add(columnName);
         }
         return (String[])output.toArray(new String[0]);
@@ -145,7 +170,7 @@
 
     public String getText(String id, String entry, Locale locale) {
         // TODO: Add Logging
-        Map entries = getEntries(id, locale);
+        Map entries = findEntries(id, locale);
         if(entries != null) {
             // TODO: Consider whether we need to recurse up if entries does not contain requested entry
             return (String)entries.get(entry);
@@ -155,10 +180,22 @@
     }
 
     public Map getEntries(String id, Locale locale) {
-        Map entries = findEntriesRecursively(id,locale);
-        if(entries == null) // If not found by using specified locale, try to use default
-            entries = findEntriesRecursively(id,Locale.getDefault());
+        Map entries = findEntries(id,locale);
+        if(entries == null) { // If not found by using specified or default locale
+            throw new MessageNotFoundException(MessageFormat.format(
+                    I18nUtils.INTERNAL_MESSAGES.getString(I18nUtils.NO_MESSAGE_ENTRIES_FOUND),
+                    new String[] { id }));
+        }
         return entries;
+    }
+
+    private Map findEntries(String id, Locale locale) {
+        Map entries = findEntriesRecursively(id,locale);
+        if(entries == null) { // If not found by using specified locale, try to use default
+            return findEntriesRecursively(id,Locale.getDefault());
+        }
+        else
+            return entries;
     }
 
     /**



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org