📄 inmemoryspoolrepository.java
字号:
/**************************************************************** * Licensed to the Apache Software Foundation (ASF) under one * * or more contributor license agreements. See the NOTICE file * * distributed with this work for additional information * * regarding copyright ownership. The ASF licenses this file * * to you under the Apache License, Version 2.0 (the * * "License"); you may not use this file except in compliance * * with the License. You may obtain a copy of the License at * * * * http://www.apache.org/licenses/LICENSE-2.0 * * * * Unless required by applicable law or agreed to in writing, * * software distributed under the License is distributed on an * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * * KIND, either express or implied. See the License for the * * specific language governing permissions and limitations * * under the License. * ****************************************************************/package org.apache.james.test.mock.james;import org.apache.james.services.SpoolRepository;import org.apache.james.test.mock.avalon.MockLogger;import org.apache.james.util.Lock;import org.apache.mailet.Mail;import javax.mail.MessagingException;import java.util.ArrayList;import java.util.Collection;import java.util.ConcurrentModificationException;import java.util.Hashtable;import java.util.Iterator;/** * Implementation of a MailRepository on a FileSystem. * * Requires a configuration element in the .conf.xml file of the form: * <repository destinationURL="file://path-to-root-dir-for-repository" * type="MAIL" * model="SYNCHRONOUS"/> * Requires a logger called MailRepository. * * @version 1.0.0, 24/04/1999 */public class InMemorySpoolRepository implements SpoolRepository { /** * Whether 'deep debugging' is turned on. */ protected final static boolean DEEP_DEBUG = true; private Lock lock; private MockLogger logger; private Hashtable spool; private MockLogger getLogger() { if (logger == null) { logger = new MockLogger(); } return logger; } /** * 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 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()); } 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 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(Mail mc) throws MessagingException { try { String key = mc.getName(); //Remember whether this key was locked boolean wasLocked = true; synchronized (this) { wasLocked = lock.isLocked(key); if (!wasLocked) { //If it wasn't locked, we want a lock during the store lock(key); } } try { spool.put(key,mc); } finally { if (!wasLocked) { // If it wasn't locked, we need to unlock now unlock(key); synchronized (this) { notify(); } } } if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) { StringBuffer logBuffer = new StringBuffer(64) .append("Mail ") .append(key) .append(" stored."); getLogger().debug(logBuffer.toString()); } } 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 Mail retrieve(String key) throws MessagingException { if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) { getLogger().debug("Retrieving mail: " + key); } try { Mail mc = null; try { mc = (Mail) spool.get(key); } catch (RuntimeException re){ StringBuffer exceptionBuffer = new StringBuffer(128); if(re.getCause() instanceof Error){ exceptionBuffer.append("Error when retrieving mail, not deleting: ") .append(re.toString()); }else{ exceptionBuffer.append("Exception retrieving mail: ") .append(re.toString()) .append(", so we're deleting it."); remove(key); } getLogger().warn(exceptionBuffer.toString()); return null; } 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(Mail mail) throws MessagingException { remove(mail.getName()); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -