📄 jdbcmailrepository.java
字号:
sr = (StreamRepository) store.select(streamConfiguration); if (getLogger().isDebugEnabled()) { getLogger().debug("Got filestore for JdbcMailRepository: " + filestore); } } lock = new Lock(); if (getLogger().isDebugEnabled()) { StringBuffer logBuffer = new StringBuffer(128) .append(this.getClass().getName()) .append(" created according to ") .append(destination); getLogger().debug(logBuffer.toString()); } } catch (Exception e) { final String message = "Failed to retrieve Store component:" + e.getMessage(); getLogger().error(message, e); throw new ConfigurationException(message, e); } } /** * Initialises the JDBC repository. * 1) Tests the connection to the database. * 2) Loads SQL strings from the SQL definition file, * choosing the appropriate SQL for this connection, * and performing paramter substitution, * 3) Initialises the database with the required tables, if necessary. * * @throws Exception if an error occurs */ public void initialize() throws Exception { StringBuffer logBuffer = null; if (getLogger().isDebugEnabled()) { getLogger().debug(this.getClass().getName() + ".initialize()"); } theJDBCUtil = new JDBCUtil() { protected void delegatedLog(String logString) { JDBCMailRepository.this.getLogger().warn("JDBCMailRepository: " + logString); } }; // Get the data-source required. datasource = (DataSourceComponent)datasources.select(datasourceName); // Test the connection to the database, by getting the DatabaseMetaData. Connection conn = datasource.getConnection(); PreparedStatement createStatement = null; try { // Initialise the sql strings. File sqlFile = null; try { sqlFile = AvalonContextUtilities.getFile(context, sqlFileName); sqlFileName = null; } catch (Exception e) { getLogger().fatalError(e.getMessage(), e); throw e; } String resourceName = "org.apache.james.mailrepository.JDBCMailRepository"; if (getLogger().isDebugEnabled()) { logBuffer = new StringBuffer(128) .append("Reading SQL resources from file: ") .append(sqlFile.getAbsolutePath()) .append(", section ") .append(this.getClass().getName()) .append("."); getLogger().debug(logBuffer.toString()); } // Build the statement parameters Map sqlParameters = new HashMap(); if (tableName != null) { sqlParameters.put("table", tableName); } if (repositoryName != null) { sqlParameters.put("repository", repositoryName); } sqlQueries = new SqlResources(); sqlQueries.init(sqlFile, this.getClass().getName(), conn, sqlParameters); // Check if the required table exists. If not, create it. DatabaseMetaData dbMetaData = conn.getMetaData(); // Need to ask in the case that identifiers are stored, ask the DatabaseMetaInfo. // Try UPPER, lower, and MixedCase, to see if the table is there. if (!(theJDBCUtil.tableExists(dbMetaData, tableName))) { // Users table doesn't exist - create it. createStatement = conn.prepareStatement(sqlQueries.getSqlString("createTable", true)); createStatement.execute(); if (getLogger().isInfoEnabled()) { logBuffer = new StringBuffer(64) .append("JdbcMailRepository: Created table '") .append(tableName) .append("'."); 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 * * @param key the key of the message to be unlocked * * @return true if successfully released the lock, false otherwise */ public synchronized boolean unlock(String key) { if (lock.unlock(key)) { if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) { StringBuffer debugBuffer = new StringBuffer(256) .append("Unlocked ") .append(key) .append(" for ") .append(Thread.currentThread().getName()) .append(" @ ") .append(new java.util.Date(System.currentTimeMillis())); getLogger().debug(debugBuffer.toString()); }// notifyAll(); return true; } else { return false; } } /** * Obtains a lock on a message identified by a key * * @param key the key of the message to be locked * * @return true if successfully obtained the lock, false otherwise */ public synchronized boolean lock(String key) { if (lock.lock(key)) { if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) { StringBuffer debugBuffer = new StringBuffer(256) .append("Locked ") .append(key) .append(" for ") .append(Thread.currentThread().getName()) .append(" @ ") .append(new java.util.Date(System.currentTimeMillis())); getLogger().debug(debugBuffer.toString()); } return true; } else { return false; } } /** * Store this message to the database. Optionally stores the message * body to the filesystem and only writes the headers to the database. */ public void store(MailImpl mc) throws MessagingException { Connection conn = null; try { conn = datasource.getConnection(); //Need to determine whether need to insert this record, or update it. //Begin a transaction conn.setAutoCommit(false);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -