📄 jdbcmailrepository.java
字号:
try { retrieveMessageAttr = conn.prepareStatement(retrieveMessageAttrSql); retrieveMessageAttr.setString(1, key); retrieveMessageAttr.setString(2, repositoryName); rsMessageAttr = retrieveMessageAttr.executeQuery(); if (rsMessageAttr.next()) { try { byte[] serialized_attr = null; String getAttributesOption = sqlQueries.getDbOption("getAttributes"); if (getAttributesOption != null && getAttributesOption.equalsIgnoreCase("useBlob")) { Blob b = rsMessageAttr.getBlob(1); serialized_attr = b.getBytes(1, (int)b.length()); } else { serialized_attr = rsMessageAttr.getBytes(1); } // this check is for better backwards compatibility if (serialized_attr != null) { ByteArrayInputStream bais = new ByteArrayInputStream(serialized_attr); ObjectInputStream ois = new ObjectInputStream(bais); attributes = (HashMap)ois.readObject(); ois.close(); } } catch (IOException ioe) { if (getLogger().isDebugEnabled()) { StringBuffer debugBuffer = new StringBuffer(64) .append("Exception reading attributes ") .append(key) .append(" in ") .append(repositoryName); getLogger().debug(debugBuffer.toString(), ioe); } } } else { if (getLogger().isDebugEnabled()) { StringBuffer debugBuffer = new StringBuffer(64) .append("Did not find a record (attributes) ") .append(key) .append(" in ") .append(repositoryName); getLogger().debug(debugBuffer.toString()); } } } catch (SQLException sqle) { StringBuffer errorBuffer = new StringBuffer(256) .append("Error retrieving message") .append(sqle.getMessage()) .append(sqle.getErrorCode()) .append(sqle.getSQLState()) .append(sqle.getNextException()); getLogger().error(errorBuffer.toString()); } finally { theJDBCUtil.closeJDBCResultSet(rsMessageAttr); theJDBCUtil.closeJDBCStatement(retrieveMessageAttr); } } MailImpl mc = new MailImpl(); mc.setAttributesRaw (attributes); mc.setName(key); mc.setState(rsMessage.getString(1)); mc.setErrorMessage(rsMessage.getString(2)); String sender = rsMessage.getString(3); if (sender == null) { mc.setSender(null); } else { mc.setSender(new MailAddress(sender)); } StringTokenizer st = new StringTokenizer(rsMessage.getString(4), "\r\n", false); Set recipients = new HashSet(); while (st.hasMoreTokens()) { recipients.add(new MailAddress(st.nextToken())); } mc.setRecipients(recipients); mc.setRemoteHost(rsMessage.getString(5)); mc.setRemoteAddr(rsMessage.getString(6)); mc.setLastUpdated(rsMessage.getTimestamp(7)); MimeMessageJDBCSource source = new MimeMessageJDBCSource(this, key, sr); MimeMessageWrapper message = new MimeMessageWrapper(source); mc.setMessage(message); return mc; } catch (SQLException sqle) { StringBuffer errorBuffer = new StringBuffer(256) .append("Error retrieving message") .append(sqle.getMessage()) .append(sqle.getErrorCode()) .append(sqle.getSQLState()) .append(sqle.getNextException()); getLogger().error(errorBuffer.toString()); throw new MessagingException("Exception while retrieving mail: " + sqle.getMessage()); } catch (Exception me) { throw new MessagingException("Exception while retrieving mail: " + me.getMessage()); } finally { theJDBCUtil.closeJDBCResultSet(rsMessage); theJDBCUtil.closeJDBCStatement(retrieveMessage); theJDBCUtil.closeJDBCConnection(conn); } } /** * Removes a specified message * * @param mail the message to be removed from the repository */ public void remove(MailImpl mail) throws MessagingException { remove(mail.getName()); } /** * Removes a Collection of mails from the repository * @param mails The Collection of <code>MailImpl</code>'s to delete * @throws MessagingException * @since 2.2.0 */ public void remove(Collection mails) throws MessagingException { Iterator delList = mails.iterator(); while (delList.hasNext()) { remove((MailImpl)delList.next()); } } /** * Removes a message identified by a key. * * @param key the key of the message to be removed from the repository */ public void remove(String key) throws MessagingException { //System.err.println("removing " + key); if (lock(key)) { Connection conn = null; PreparedStatement removeMessage = null; try { conn = datasource.getConnection(); removeMessage = conn.prepareStatement(sqlQueries.getSqlString("removeMessageSQL", true)); removeMessage.setString(1, key); removeMessage.setString(2, repositoryName); removeMessage.execute(); if (sr != null) { sr.remove(key); } } catch (Exception me) { throw new MessagingException("Exception while removing mail: " + me.getMessage()); } finally { theJDBCUtil.closeJDBCStatement(removeMessage); theJDBCUtil.closeJDBCConnection(conn); unlock(key); } } } /** * Gets a list of message keys stored in this repository. * * @return an Iterator of the message keys */ public Iterator list() throws MessagingException { //System.err.println("listing messages"); Connection conn = null; PreparedStatement listMessages = null; ResultSet rsListMessages = null; try { conn = datasource.getConnection(); listMessages = conn.prepareStatement(sqlQueries.getSqlString("listMessagesSQL", true)); listMessages.setString(1, repositoryName); rsListMessages = listMessages.executeQuery(); List messageList = new ArrayList(); while (rsListMessages.next() && !Thread.currentThread().isInterrupted()) { messageList.add(rsListMessages.getString(1)); } return messageList.iterator(); } catch (Exception me) { throw new MessagingException("Exception while listing mail: " + me.getMessage()); } finally { theJDBCUtil.closeJDBCResultSet(rsListMessages); theJDBCUtil.closeJDBCStatement(listMessages); theJDBCUtil.closeJDBCConnection(conn); } } /** * Gets the SQL connection to be used by this JDBCMailRepository * * @return the connection * @throws SQLException if there is an issue with getting the connection */ protected Connection getConnection() throws SQLException { return datasource.getConnection(); } /** * @see java.lang.Object#equals(Object) */ public boolean equals(Object obj) { if (!(obj instanceof JDBCMailRepository)) { return false; } // TODO: Figure out whether other instance variables should be part of // the equals equation JDBCMailRepository repository = (JDBCMailRepository)obj; return ((repository.tableName == tableName) || ((repository.tableName != null) && repository.tableName.equals(tableName))) && ((repository.repositoryName == repositoryName) || ((repository.repositoryName != null) && repository.repositoryName.equals(repositoryName))); } /** * Provide a hash code that is consistent with equals for this class * * @return the hash code */ public int hashCode() { int result = 17; if (tableName != null) { result = 37 * tableName.hashCode(); } if (repositoryName != null) { result = 37 * repositoryName.hashCode(); } return result; } /** * This method calculates number of parameters in a prepared statement SQL String. * It does so by counting the number of '?' in the string * @param sqlstring to return parameter count for * @return number of parameters **/ private int getNumberOfParameters (String sqlstring) { //it is alas a java 1.4 feature to be able to call //getParameterMetaData which could provide us with the parameterCount char[] chars = sqlstring.toCharArray(); int count = 0; for (int i = 0; i < chars.length; i++) { count += chars[i]=='?' ? 1 : 0; } return count; } /** * Closes output streams used to update message * * @param headerStream the stream containing header information - potentially the same * as the body stream * @param bodyStream the stream containing body information */ private void closeOutputStreams(OutputStream headerStream, OutputStream bodyStream) { try { // If the header stream is not the same as the body stream, // close the header stream here. if ((headerStream != null) && (headerStream != bodyStream)) { headerStream.close(); } } catch (IOException ioe) { getLogger().debug("JDBCMailRepository: Unexpected exception while closing output stream."); } try { if (bodyStream != null) { bodyStream.close(); } } catch (IOException ioe) { getLogger().debug("JDBCMailRepository: Unexpected exception while closing output stream."); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -