FileDocCategorySizeDatePackage
InMemorySpoolRepository.javaAPI DocApache James 2.3.115631Fri Jan 12 12:56:36 GMT 2007org.apache.james.test.mock.james

InMemorySpoolRepository

public class InMemorySpoolRepository extends Object implements org.apache.james.services.SpoolRepository
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

Fields Summary
protected static final boolean
DEEP_DEBUG
Whether 'deep debugging' is turned on.
private org.apache.james.util.Lock
lock
private org.apache.james.test.mock.avalon.MockLogger
logger
private Hashtable
spool
Constructors Summary
public InMemorySpoolRepository()

        spool = new Hashtable();
        lock = new Lock();
    
Methods Summary
public synchronized org.apache.mailet.Mailaccept()

Returns an arbitrarily selected mail deposited in this Repository. Usage: SpoolManager calls accept() to see if there are any unprocessed mails in the spool repository.

Synchronized to ensure thread safe access to the underlying spool.

return
the mail

        if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) {
            getLogger().debug("Method accept() called");
        }
        return accept(new SpoolRepository.AcceptFilter () {
            public boolean accept (String _, String __, long ___, String ____) {
                return true;
            }

            public long getWaitTime () {
                return 0;
            }
        });
    
public synchronized org.apache.mailet.Mailaccept(long delay)

Returns an arbitrarily selected mail deposited in this Repository that is either ready immediately for delivery, or is younger than it's last_updated plus the number of failed attempts times the delay time. Usage: RemoteDeliverySpool calls accept() with some delay and should block until an unprocessed mail is available.

Synchronized to ensure thread safe access to the underlying spool.

return
the mail

        if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) {
            getLogger().debug("Method accept(delay) called");
        }
        return accept(new SpoolRepository.AcceptFilter () {
            long youngest = 0;
                
                public boolean accept (String key, String state, long lastUpdated, String errorMessage) {
                    if (state.equals(Mail.ERROR)) {
                        //Test the time...
                        long timeToProcess = delay + lastUpdated;
                
                        if (System.currentTimeMillis() > timeToProcess) {
                            //We're ready to process this again
                            return true;
                        } else {
                            //We're not ready to process this.
                            if (youngest == 0 || youngest > timeToProcess) {
                                //Mark this as the next most likely possible mail to process
                                youngest = timeToProcess;
                            }
                            return false;
                        }
                    } else {
                        //This mail is good to go... return the key
                        return true;
                    }
                }
        
                public long getWaitTime () {
                    if (youngest == 0) {
                        return 0;
                    } else {
                        long duration = youngest - System.currentTimeMillis();
                        youngest = 0; //get ready for next round
                        return duration <= 0 ? 1 : duration;
                    }
                }
            });
    
public synchronized org.apache.mailet.Mailaccept(SpoolRepository.AcceptFilter filter)
Returns an arbitrarily select mail deposited in this Repository for which the supplied filter's accept method returns true. Usage: RemoteDeliverySpool calls accept(filter) with some a filter which determines based on number of retries if the mail is ready for processing. If no message is ready the method will block until one is, the amount of time to block is determined by calling the filters getWaitTime method.

Synchronized to ensure thread safe access to the underlying spool.

return
the mail

        if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) {
            getLogger().debug("Method accept(Filter) called");
        }
        while (!Thread.currentThread().isInterrupted()) try {
            for (Iterator it = list(); it.hasNext(); ) {
                String s = it.next().toString();
                if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) {
                    StringBuffer logBuffer =
                        new StringBuffer(64)
                                .append("Found item ")
                                .append(s)
                                .append(" in spool.");
                    getLogger().debug(logBuffer.toString());
                }
                if (lock(s)) {
                    if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) {
                        getLogger().debug("accept(Filter) has locked: " + s);
                    }
                    try {
                        Mail mail = retrieve(s);
                        // Retrieve can return null if the mail is no longer on the spool
                        // (i.e. another thread has gotten to it first).
                        // In this case we simply continue to the next key
                        if (mail == null || !filter.accept (mail.getName(),
                                                            mail.getState(),
                                                            mail.getLastUpdated().getTime(),
                                                            mail.getErrorMessage())) {
                            unlock(s);
                            continue;
                        }
                        return mail;
                    } catch (javax.mail.MessagingException e) {
                        unlock(s);
                        getLogger().error("Exception during retrieve -- skipping item " + s, e);
                    }
                }
            }

            //We did not find any... let's wait for a certain amount of time
            wait (filter.getWaitTime());
        } catch (InterruptedException ex) {
            throw ex;
        } catch (ConcurrentModificationException cme) {
            // Should never get here now that list methods clones keyset for iterator
            getLogger().error("CME in spooler - please report to http://james.apache.org", cme);
        }
        throw new InterruptedException();
    
public voidclear()

        spool.clear();
    
private org.apache.james.test.mock.avalon.MockLoggergetLogger()


       
        if (logger == null) {
            logger = new MockLogger();
        }
        return logger;
    
public java.util.Iteratorlist()
List string keys of messages in repository.

return
an Iterator over the list of keys in the repository

        // Fix ConcurrentModificationException by cloning 
        // the keyset before getting an iterator
        final ArrayList clone;
        synchronized(spool) {
            clone = new ArrayList(spool.keySet());
        }
        return clone.iterator();
    
public booleanlock(java.lang.String key)
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

        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;
        }
    
public voidremove(org.apache.mailet.Mail mail)
Removes a specified message

param
mail the message to be removed from the repository

        remove(mail.getName());
    
public voidremove(java.util.Collection mails)
Removes a Collection of mails from the repository

param
mails The Collection of MailImpl's to delete
throws
MessagingException
since
2.2.0

        Iterator delList = mails.iterator();
        while (delList.hasNext()) {
            remove((Mail)delList.next());
        }
    
public voidremove(java.lang.String key)
Removes a message identified by key.

param
key the key of the message to be removed from the repository

        if (lock(key)) {
            try {
                if (spool != null) spool.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());
        }
    
public org.apache.mailet.Mailretrieve(java.lang.String key)
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

        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());
        }
    
public intsize()

        return spool.size();
    
public voidstore(org.apache.mailet.Mail mc)
Stores a message in this repository. Shouldn't this return the key under which it is stored?

param
mc the mail message to store

        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);
        }
    
public booleanunlock(java.lang.String key)
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

        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;
        }