📄 avalonmailrepository.java
字号:
* @return true if successfully obtained the lock, false otherwise */ public 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()); }// synchronized (this) {// notifyAll();// } return true; } else { return false; } } /** * Stores a message in this repository. Shouldn't this return the key * under which it is stored? * * @param mc the mail message to store */ public void store(MailImpl mc) throws MessagingException { try { String key = mc.getName(); //Remember whether this key was locked boolean wasLocked = lock.isLocked(key); if (!wasLocked) { //If it wasn't locked, we want a lock during the store lock.lock(key); } try { if (!keys.contains(key)) { keys.add(key); } boolean saveStream = true; if (mc.getMessage() instanceof MimeMessageWrapper) { MimeMessageWrapper wrapper = (MimeMessageWrapper) mc.getMessage(); if (DEEP_DEBUG) { System.out.println("Retrieving from: " + wrapper.getSourceId()); StringBuffer debugBuffer = new StringBuffer(64) .append("Saving to: ") .append(destination) .append("/") .append(mc.getName()); System.out.println(debugBuffer.toString()); System.out.println("Modified: " + wrapper.isModified()); } StringBuffer destinationBuffer = new StringBuffer(128) .append(destination) .append("/") .append(mc.getName()); if (wrapper.getSourceId().equals(destinationBuffer.toString()) && !wrapper.isModified()) { //We're trying to save to the same place, and it's not modified... we shouldn't save. //More importantly, if we try to save, we will create a 0-byte file since we're //retrying to retrieve from a file we'll be overwriting. saveStream = false; } } if (saveStream) { OutputStream out = null; try { out = sr.put(key); mc.writeMessageTo(out); } finally { if (out != null) out.close(); } } //Always save the header information or.put(key, mc); } finally { if (!wasLocked) { //If it wasn't locked, we need to now unlock lock.unlock(key); } } if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) { StringBuffer logBuffer = new StringBuffer(64) .append("Mail ") .append(key) .append(" stored."); getLogger().debug(logBuffer.toString()); } synchronized (this) {// notifyAll(); notify(); } } catch (Exception e) { getLogger().error("Exception storing mail: " + e); throw new MessagingException("Exception caught while storing Message Container: " + e); } } /** * Retrieves a message given a key. At the moment, keys can be obtained * from list() in superinterface Store.Repository * * @param key the key of the message to retrieve * @return the mail corresponding to this key, null if none exists */ public MailImpl retrieve(String key) throws MessagingException { if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) { getLogger().debug("Retrieving mail: " + key); } try { MailImpl mc = null; try { mc = (MailImpl) or.get(key); } catch (RuntimeException re) { StringBuffer exceptionBuffer = new StringBuffer(128) .append("Exception retrieving mail: ") .append(re.toString()) .append(", so we're deleting it... good riddance!"); getLogger().debug(exceptionBuffer.toString()); remove(key); return null; } MimeMessageAvalonSource source = new MimeMessageAvalonSource(sr, destination, key); mc.setMessage(new MimeMessageWrapper(source)); return mc; } catch (Exception me) { getLogger().error("Exception retrieving mail: " + me); throw new MessagingException("Exception while retrieving mail: " + me.getMessage()); } } /** * 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 key. * * @param key the key of the message to be removed from the repository */ public void remove(String key) throws MessagingException { if (lock(key)) { try { keys.remove(key); sr.remove(key); or.remove(key); } finally { unlock(key); } } else { StringBuffer exceptionBuffer = new StringBuffer(64) .append("Cannot lock ") .append(key) .append(" to remove it"); throw new MessagingException(exceptionBuffer.toString()); } } /** * List string keys of messages in repository. * * @return an <code>Iterator</code> over the list of keys in the repository * */ public Iterator list() { // Fix ConcurrentModificationException by cloning // the keyset before getting an iterator final ArrayList clone; synchronized(keys) { clone = new ArrayList(keys); } if (fifo) Collections.sort(clone); // Keys is a HashSet; impose FIFO for apps that need it return clone.iterator(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -