vincenzo 2003/09/08 09:41:08
Modified: src/java/org/apache/james/mailrepository
JDBCMailRepository.java
src/java/org/apache/james/util 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
1.47 +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.46
retrieving revision 1.47
diff -u -r1.46 -r1.47
--- JDBCMailRepository.java 17 Jul 2003 13:23:46 -0000 1.46
+++ JDBCMailRepository.java 8 Sep 2003 16:41:08 -0000 1.47
@@ -209,6 +209,11 @@
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)
*/
public void contextualize(final Context context)
@@ -429,12 +434,102 @@
}
}
+ 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
*
@@ -552,11 +647,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 =
@@ -768,13 +862,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 =
1.9 +45 -1 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.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- JDBCUtil.java 8 Mar 2003 21:14:12 -0000 1.8
+++ JDBCUtil.java 8 Sep 2003 16:41:08 -0000 1.9
@@ -74,7 +74,7 @@
* take advantage of different logging capabilities/interfaces in
* different parts of the code.</p>
*
- *
+ * @version CVS $Revision$ $Date$
*/
public abstract class JDBCUtil {
@@ -118,6 +118,50 @@
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;
} finally {
closeJDBCResultSet(rsTables);
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]