vincenzo    2003/09/08 09:39:42

  Modified:    src/java/org/apache/james/mailrepository Tag: branch_2_1_fcs
                        JDBCMailRepository.java
               src/java/org/apache/james/util Tag: branch_2_1_fcs
                        JDBCUtil.java
  Log:
  Now during initialization will check if everything is OK in the database definitions 
for JDBC repositories support of mail attributes and behave accordingly.
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.30.4.11 +104 -11   
james-server/src/java/org/apache/james/mailrepository/JDBCMailRepository.java
  
  Index: JDBCMailRepository.java
  ===================================================================
  RCS file: 
/home/cvs/james-server/src/java/org/apache/james/mailrepository/JDBCMailRepository.java,v
  retrieving revision 1.30.4.10
  retrieving revision 1.30.4.11
  diff -u -r1.30.4.10 -r1.30.4.11
  --- JDBCMailRepository.java   28 Aug 2003 16:32:02 -0000      1.30.4.10
  +++ JDBCMailRepository.java   8 Sep 2003 16:39:42 -0000       1.30.4.11
  @@ -195,6 +195,11 @@
        * The JDBCUtil helper class
        */
       protected JDBCUtil theJDBCUtil;
  +    
  +    /**
  +     * "Support for Mail Attributes under JDBC repositories is ready" indicator.
  +     */
  +    protected boolean jdbcMailAttributesReady = false;
   
       /**
        * @see 
org.apache.avalon.framework.context.Contextualizable#contextualize(Context)
  @@ -416,12 +421,102 @@
                       getLogger().info(logBuffer.toString());
                   }
               }
  +            
  +            checkJdbcAttributesSupport(dbMetaData);
   
           } finally {
               theJDBCUtil.closeJDBCStatement(createStatement);
               theJDBCUtil.closeJDBCConnection(conn);
           }
       }
  +    
  +    /** Checks whether support for JDBC Mail atributes is activated for this 
repository
  +     * and if everything is consistent.
  +     * Looks for both the "updateMessageAttributesSQL" and 
"retrieveMessageAttributesSQL"
  +     * statements in sqlResources and for a table column named "message_attributes".
  +     *
  +     * @param dbMetaData the database metadata to be used to look up the column
  +     * @throws SQLException if a fatal situation is met
  +     */
  +    protected void checkJdbcAttributesSupport(DatabaseMetaData dbMetaData) throws 
SQLException {
  +        String attributesColumnName = "message_attributes";
  +        boolean hasUpdateMessageAttributesSQL = false;
  +        boolean hasRetrieveMessageAttributesSQL = false;
  +        
  +        boolean hasMessageAttributesColumn = theJDBCUtil.columnExists(dbMetaData, 
tableName, attributesColumnName);
  +        
  +        StringBuffer logBuffer = new StringBuffer(64)
  +                                    .append("JdbcMailRepository '"
  +                                            + repositoryName
  +                                            + ", table '"
  +                                            + tableName
  +                                            + "': ");
  +        
  +        //Determine whether attributes are used and available for storing
  +        //Do we have updateMessageAttributesSQL?
  +        String updateMessageAttrSql =
  +            sqlQueries.getSqlString("updateMessageAttributesSQL", false);
  +        if (updateMessageAttrSql!=null) {
  +            hasUpdateMessageAttributesSQL = true;
  +        }
  +        
  +        //Determine whether attributes are used and retrieve them
  +        //Do we have retrieveAttributesSQL?
  +        String retrieveMessageAttrSql =
  +            sqlQueries.getSqlString("retrieveMessageAttributesSQL", false);
  +        if (retrieveMessageAttrSql!=null) {
  +            hasRetrieveMessageAttributesSQL = true;
  +        }
  +        
  +        if (hasUpdateMessageAttributesSQL && !hasRetrieveMessageAttributesSQL) {
  +            logBuffer.append("JDBC Mail Attributes support was activated for update 
but not for retrieval"
  +                             + "(found 'updateMessageAttributesSQL' but not 
'retrieveMessageAttributesSQL'"
  +                             + "in table '"
  +                             + tableName
  +                             + "').");
  +            getLogger().fatalError(logBuffer.toString());
  +            throw new SQLException(logBuffer.toString());
  +        }
  +        if (!hasUpdateMessageAttributesSQL && hasRetrieveMessageAttributesSQL) {
  +            logBuffer.append("JDBC Mail Attributes support was activated for 
retrieval but not for update"
  +                             + "(found 'retrieveMessageAttributesSQL' but not 
'updateMessageAttributesSQL'"
  +                             + "in table '"
  +                             + tableName
  +                             + "'.");
  +            getLogger().fatalError(logBuffer.toString());
  +            throw new SQLException(logBuffer.toString());
  +        }
  +        if (!hasMessageAttributesColumn
  +            && (hasUpdateMessageAttributesSQL || hasRetrieveMessageAttributesSQL)
  +            ) {
  +                logBuffer.append("JDBC Mail Attributes support was activated but 
column '"
  +                                 + attributesColumnName
  +                                 + "' is missing in table '"
  +                                 + tableName
  +                                 + "'.");
  +                getLogger().fatalError(logBuffer.toString());
  +                throw new SQLException(logBuffer.toString());
  +        }
  +        if (hasUpdateMessageAttributesSQL && hasRetrieveMessageAttributesSQL) {
  +            jdbcMailAttributesReady = true;
  +            if (getLogger().isInfoEnabled()) {
  +                logBuffer.append("JDBC Mail Attributes support ready.");
  +                getLogger().info(logBuffer.toString());
  +            }
  +        } else {
  +            jdbcMailAttributesReady = false;
  +            logBuffer.append("JDBC Mail Attributes support not activated. "
  +                             + "Missing both 'updateMessageAttributesSQL' "
  +                             + "and 'retrieveMessageAttributesSQL' "
  +                             + "statements for table '"
  +                             + tableName
  +                             + "' in sqlResources.xml. "
  +                             + "Will not persist in the repository '"
  +                             + repositoryName
  +                             + "'.");
  +            getLogger().warn(logBuffer.toString());
  +        }
  +    }
   
       /**
        * Releases a lock on a message identified by a key
  @@ -540,11 +635,10 @@
                       theJDBCUtil.closeJDBCStatement(localUpdateMessage);
                   }
   
  -                //Determine whether attribues are used and available for storing
  -                //Do we have updateMessageAttributesSQL?
  -                String updateMessageAttrSql =
  -                    sqlQueries.getSqlString("updateMessageAttributesSQL", false);
  -                if (updateMessageAttrSql!=null && mc.hasAttributes()) {
  +                //Determine whether attributes are used and available for storing
  +                if (jdbcMailAttributesReady && mc.hasAttributes()) {
  +                    String updateMessageAttrSql =
  +                        sqlQueries.getSqlString("updateMessageAttributesSQL", 
false);
                       PreparedStatement updateMessageAttr = null;
                       try {
                           updateMessageAttr =
  @@ -756,13 +850,12 @@
                   }
                   return null;
               }
  -            //Determine whether attribues are used and retrieve them
  -            //Do we have retrieveAttributesSQL?
  -            String retrieveMessageAttrSql =
  -                sqlQueries.getSqlString("retrieveMessageAttributesSQL", false);
  +            //Determine whether attributes are used and retrieve them
               PreparedStatement retrieveMessageAttr = null;
               HashMap attributes = null;
  -            if (retrieveMessageAttrSql!=null) {
  +            if (jdbcMailAttributesReady) {
  +                String retrieveMessageAttrSql =
  +                    sqlQueries.getSqlString("retrieveMessageAttributesSQL", false);
                   ResultSet rsMessageAttr = null;
                   try {
                       retrieveMessageAttr =
  
  
  
  No                   revision
  No                   revision
  1.2.4.3   +45 -3     james-server/src/java/org/apache/james/util/JDBCUtil.java
  
  Index: JDBCUtil.java
  ===================================================================
  RCS file: /home/cvs/james-server/src/java/org/apache/james/util/JDBCUtil.java,v
  retrieving revision 1.2.4.2
  retrieving revision 1.2.4.3
  diff -u -r1.2.4.2 -r1.2.4.3
  --- JDBCUtil.java     8 Mar 2003 21:54:11 -0000       1.2.4.2
  +++ JDBCUtil.java     8 Sep 2003 16:39:42 -0000       1.2.4.3
  @@ -68,9 +68,7 @@
    * take advantage of different logging capabilities/interfaces in
    * different parts of the code.</p>
    *
  - * @author Noel Bergman <[EMAIL PROTECTED]>
  - * @author Peter M. Goldstein <[EMAIL PROTECTED]>
  - *
  + * @version CVS $Revision$ $Date$
    */
   abstract public class JDBCUtil
   {
  @@ -110,6 +108,50 @@
       public boolean tableExistsCaseSensitive(DatabaseMetaData dbMetaData, String 
tableName)
           throws SQLException {
           ResultSet rsTables = dbMetaData.getTables(null, null, tableName, null);
  +        try {
  +            boolean found = rsTables.next();
  +            return found;
  +        } finally {
  +            closeJDBCResultSet(rsTables);
  +        }
  +    }
  +
  +    /**
  +     * Checks database metadata to see if a column exists in a table
  +     * Try UPPER, lower, and MixedCase, both on the table name and the column name, 
to see if the column is there.
  +     *
  +     * @param dbMetaData the database metadata to be used to look up this column
  +     * @param tableName the table name
  +     * @param columnName the column name
  +     *
  +     * @throws SQLException if an exception is encountered while accessing the 
database
  +     */
  +    public boolean columnExists(DatabaseMetaData dbMetaData, String tableName, 
String columnName)
  +        throws SQLException {
  +        return ( columnExistsCaseSensitive(dbMetaData, tableName, columnName) ||
  +                 columnExistsCaseSensitive(dbMetaData, tableName, 
columnName.toUpperCase(Locale.US)) ||
  +                 columnExistsCaseSensitive(dbMetaData, tableName, 
columnName.toLowerCase(Locale.US)) ||
  +                 columnExistsCaseSensitive(dbMetaData, 
tableName.toUpperCase(Locale.US), columnName) ||
  +                 columnExistsCaseSensitive(dbMetaData, 
tableName.toUpperCase(Locale.US), columnName.toUpperCase(Locale.US)) ||
  +                 columnExistsCaseSensitive(dbMetaData, 
tableName.toUpperCase(Locale.US), columnName.toLowerCase(Locale.US)) ||
  +                 columnExistsCaseSensitive(dbMetaData, 
tableName.toLowerCase(Locale.US), columnName) ||
  +                 columnExistsCaseSensitive(dbMetaData, 
tableName.toLowerCase(Locale.US), columnName.toUpperCase(Locale.US)) ||
  +                 columnExistsCaseSensitive(dbMetaData, 
tableName.toLowerCase(Locale.US), columnName.toLowerCase(Locale.US)) );
  +    }
  +
  +    /**
  +     * Checks database metadata to see if a column exists in a table.  This method
  +     * is sensitive to the case of both the provided table name and column name.
  +     *
  +     * @param dbMetaData the database metadata to be used to look up this column
  +     * @param tableName the case sensitive table name
  +     * @param columnName the case sensitive column name
  +     *
  +     * @throws SQLException if an exception is encountered while accessing the 
database
  +     */
  +    public boolean columnExistsCaseSensitive(DatabaseMetaData dbMetaData, String 
tableName, String columnName)
  +        throws SQLException {
  +        ResultSet rsTables = dbMetaData.getColumns(null, null, tableName, 
columnName);
           try {
               boolean found = rsTables.next();
               return found;
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to