org.apache.james.mailrepository.javamail
Class HashJavamailStoreMailRepository

java.lang.Object
  extended by org.apache.avalon.framework.logger.AbstractLogEnabled
      extended by org.apache.james.mailrepository.javamail.AbstractJavamailStoreMailRepository
          extended by org.apache.james.mailrepository.javamail.HashJavamailStoreMailRepository
All Implemented Interfaces:
org.apache.avalon.framework.activity.Initializable, org.apache.avalon.framework.configuration.Configurable, org.apache.avalon.framework.logger.LogEnabled, org.apache.avalon.framework.service.Serviceable, FolderAdapterFactory, StoreGateKeeperAware, MailRepository

public class HashJavamailStoreMailRepository
extends AbstractJavamailStoreMailRepository

MailRepository implementation to store mail in a Javamail store.
should work with every JavamailStore implementation that has deterministic message content. (checksum save). This implementation should be considered as EXPERIMENTAL. TODO examine for thread-safety


Nested Class Summary
static class HashJavamailStoreMailRepository.HasherOutputStream
           
protected  class HashJavamailStoreMailRepository.KeyToMsgMap
           
protected static class HashJavamailStoreMailRepository.MsgObj
          used to internal represent a message
 
Field Summary
protected  HashJavamailStoreMailRepository.KeyToMsgMap keyToMsgMap
          tridirectional map of messages key, hash and number saved in internaly used class MsgObj
 
Fields inherited from class org.apache.james.mailrepository.javamail.AbstractJavamailStoreMailRepository
cacheMessages, DEEP_DEBUG, log
 
Fields inherited from interface org.apache.james.services.MailRepository
MAIL, ROLE
 
Constructor Summary
HashJavamailStoreMailRepository()
           
 
Method Summary
protected static java.lang.Object calcMessageHash(javax.mail.internet.MimeMessage mm)
          currently uses Arrays.hashCode to build an Integer.
 FolderInterface createAdapter(javax.mail.Folder folder)
          just uses a FolderAdapter to wrap around folder
protected static java.lang.String generateKey(java.lang.String hash)
          builds a key for unknow messages
protected  HashJavamailStoreMailRepository.KeyToMsgMap getKeyToMsgMap()
          lazy loads KeyToMsgMap
protected  int getMessageCount()
           
protected  javax.mail.internet.MimeMessage getMessageFromInbox(java.lang.String key)
          Fetches a message from inbox.
 java.util.Iterator list()
          calls rehash and uses stored keys in KeyToMsgMap
protected  javax.mail.internet.MimeMessage rehash(java.lang.String filterkey)
          Calls getMessages(); on Folder and rehashes messages an renews message numbers calls retainAllListedAndAddedByKeys on KeyToMsgMap to remove keys not found in Folder
 void remove(java.lang.String key)
          Removes a message identified by key.
 org.apache.mailet.Mail retrieve(java.lang.String key)
          uses getMessageFromInbox, returns null if not found
 void store(org.apache.mailet.Mail mc)
          Stores a message by Javamails appendMessages method.
 
Methods inherited from class org.apache.james.mailrepository.javamail.AbstractJavamailStoreMailRepository
configure, enableLogging, getFolderGateKeeper, getLock, getRandom, getStore, initialize, lock, remove, remove, service, unlock
 
Methods inherited from class org.apache.avalon.framework.logger.AbstractLogEnabled
getLogger, setupLogger, setupLogger, setupLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

keyToMsgMap

protected HashJavamailStoreMailRepository.KeyToMsgMap keyToMsgMap
tridirectional map of messages key, hash and number saved in internaly used class MsgObj

Constructor Detail

HashJavamailStoreMailRepository

public HashJavamailStoreMailRepository()
Method Detail

getMessageCount

protected int getMessageCount()
                       throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

store

public void store(org.apache.mailet.Mail mc)
           throws javax.mail.MessagingException
Stores a message by Javamails appendMessages method. Tries to guess resulting messagenumber and saves result in keyToMsgMap. If Folder supports getMessageCount on closed folder, this could be quite efficient

Parameters:
mc - the mail message to store
Throws:
javax.mail.MessagingException
See Also:
MailRepository.store(Mail)

list

public java.util.Iterator list()
                        throws javax.mail.MessagingException
calls rehash and uses stored keys in KeyToMsgMap

Returns:
an Iterator over the list of keys in the repository
Throws:
javax.mail.MessagingException
See Also:
MailRepository.list()

retrieve

public org.apache.mailet.Mail retrieve(java.lang.String key)
                                throws javax.mail.MessagingException
uses getMessageFromInbox, returns null if not found

Parameters:
key - the key of the message to retrieve
Returns:
the mail corresponding to this key, null if none exists
Throws:
javax.mail.MessagingException
See Also:
MailRepository.retrieve(String)

remove

public void remove(java.lang.String key)
            throws javax.mail.MessagingException
Removes a message identified by key. uses getMessageFromInbox and does a setFlag(Flags.Flag.DELETED, true); on message. removes message from KeyToMsgMap. Messagenumbers are recalculated for next guesses.

Parameters:
key - the key of the message to be removed from the repository
Throws:
javax.mail.MessagingException
See Also:
MailRepository.remove(String)

rehash

protected javax.mail.internet.MimeMessage rehash(java.lang.String filterkey)
                                          throws javax.mail.MessagingException
Calls getMessages(); on Folder and rehashes messages an renews message numbers calls retainAllListedAndAddedByKeys on KeyToMsgMap to remove keys not found in Folder

Parameters:
filterkey - key of message that should be returned, can be null
Returns:
a message if found by filterkey
Throws:
javax.mail.MessagingException

getMessageFromInbox

protected javax.mail.internet.MimeMessage getMessageFromInbox(java.lang.String key)
                                                       throws javax.mail.MessagingException
Fetches a message from inbox. Fast fails if key is unknown in KeyToMsgMap. Tries to get message at last known position, if that was not successfull calls rehash

Parameters:
key - message key
Returns:
message if found, otherwise null
Throws:
javax.mail.MessagingException

getKeyToMsgMap

protected HashJavamailStoreMailRepository.KeyToMsgMap getKeyToMsgMap()
lazy loads KeyToMsgMap

Returns:
keyToMsgMap the KeyToMsgMap

calcMessageHash

protected static java.lang.Object calcMessageHash(javax.mail.internet.MimeMessage mm)
                                           throws javax.mail.MessagingException
currently uses Arrays.hashCode to build an Integer. Resulting Method should provide a good hashCode and a correct equals method

Parameters:
mm - message to hash
Returns:
an Object provides and correct equals method.
Throws:
javax.mail.MessagingException

generateKey

protected static java.lang.String generateKey(java.lang.String hash)
builds a key for unknow messages

Parameters:
hash - Hash to be included in key
Returns:
contains "james-hashed:", the hash, the time and a random long

createAdapter

public FolderInterface createAdapter(javax.mail.Folder folder)
just uses a FolderAdapter to wrap around folder

Parameters:
folder - JavaMail folder


Copyright ? 2002-2009 The Apache Software Foundation. All Rights Reserved.