org.abstracthorizon.mercury.maildir
Class MaildirFolderData

java.lang.Object
  extended by javax.mail.Folder
      extended by org.abstracthorizon.mercury.maildir.MaildirFolderData
Direct Known Subclasses:
UIDMaildirFolderData

public class MaildirFolderData
extends javax.mail.Folder

Folder data actually represents the model of the maildir folder. This class handles all model operations - all operations that are low level and deal with files and directories.

This folder data serves as central repository for messages over a directory. Each directory will have only one folder data for store. That is maintained in store itself

Messages that are present in folder data are later wrapped inside of folder. Pair of (message, file) is kept in folder data's cache. Cache for each directory is maintained as long as there are opened folders over that directory. After that time it is left to be collected throught weak reference. If folder over folder data is opened before reference is garbage collected it is reused.

Author:
Daniel Sendula

Nested Class Summary
protected static class MaildirFolderData.Data
          Folders data class.
 
Field Summary
protected  File base
          Directory folder data is for
protected  String cachedFullName
          Cached folder's full name
protected  String cachedName
          Cached folder's name
protected  Reference<MaildirFolderData.Data> closedRef
          Weak reference to data when there are no open folders
protected  File cur
          Cur subdirectory
protected  MaildirFolderData.Data data
          Folder's data
protected  int delay
          Amount of time between two accesses.
protected  int delayFactor
          Delay factor - amount of time needed for reading directory vs delay.
protected  long lastAccess
          Last time folder data was accessed or 0 if no open folders
static String NO_SUBFOLDERS_FILENAME
          This constant defines a filename of a zero length flag file that denotes no subfolders are suppose to be created for this folder
protected  File nw
          New subdirectory
protected  int openCount
          Count of open folders.
protected  WeakHashMap<javax.mail.Folder,Object> openedFolders
          List of open folders
protected  javax.mail.Flags permanentFlags
          Permanent flags cache
protected  boolean rootFolder
          Flag to denote is this root folder or not
static javax.mail.Flags rootPermanentFlags
          Permanent flags for root are user defined "\\Noselect"
protected  MaildirStore store
          Maildir store
protected  File tmp
          Tmp subdirectory
protected  int type
          Type of the folder.
 
Fields inherited from class javax.mail.Folder
HOLDS_FOLDERS, HOLDS_MESSAGES, mode, READ_ONLY, READ_WRITE
 
Constructor Summary
MaildirFolderData(MaildirStore store, File file)
          Constructor
 
Method Summary
protected  void addMessages(MaildirFolder folder, List<MaildirMessage> messages)
          This method adds messages to the folder.
protected  void appendMessages(MaildirFolder folder, javax.mail.Message[] messages)
          Appends messages to the folder.
 void appendMessages(javax.mail.Message[] messages)
          Appends messages to the maildir data.
protected  void checkDirs()
          This method checks if new, cur and tmp directories exist.
 void close(boolean arg0)
          This method is only to satisfy Folder interface.
protected  void close(MaildirFolder folder)
          This method is called with folder that is closing.
 boolean create(int type)
          Creates folder.
protected  MaildirMessage createExistingMaildirMessage(File file, int num)
          This method creates new maildir message object for existing file in folder data's directory.
protected  List<MessageBase> createFolderMessages()
          This method creates collection structure for storing messages in the folder.
protected  MaildirMessage createNewMaildirMessage(javax.mail.internet.MimeMessage message, int num)
          This method creates new maildir message for folder data (not folder).
 boolean delete(boolean recursive)
          Deletes folder.
protected  boolean deleteAll(File file)
          Utility method that deletes all subdirectories and files from given directory.
 boolean exists()
          Returns true if folder exists.
 javax.mail.Message[] expunge()
          This method is only to satisfy Folder interface.
protected  List<? extends javax.mail.Message> expunge(MaildirFolder folder, boolean explicit)
          Expunges messages for given folder.
protected  boolean expungeMessage(MaildirMessage message)
          Expunges one message
protected  File getCurDir()
          Returns folder's cur directory
 javax.mail.Folder getFolder(String arg0)
          This method is only to satisfy Folder interface.
 File getFolderFile()
          Returns folder's directory
protected  List<MaildirMessage> getFolderMessages(MaildirFolder folder)
          Returns folder's messages for given folder.
 String getFullName()
          Returns folder's full name (path and name)
 long getLastAccessed()
          Returns when this folder data is last accessd
 MaildirStore getMaildirStore()
          Returns maildir store
 javax.mail.Message getMessage(int i)
          This method is only to satisfy Folder interface.
 int getMessageCount()
          Returns message count.
 String getName()
          Returns folder's name
protected  File getNewDir()
          Returns folder's new directory
 int getNewMessageCount()
          Returns new message count.
 javax.mail.Folder getParent()
          This method is only to satisfy Folder interface.
 String getParentFolderName()
          Returns folder parent's name
 javax.mail.Flags getPermanentFlags()
          This method is only to satisfy Folder interface.
 char getSeparator()
          Returns "/"
 String getSubFolderName(String name)
          Returns name for given subfolder.
protected  File getTmpDir()
          Returns folder's tmp directory
 int getType()
          Returns folder's type.
 boolean hasNewMessages()
          This method is only to satisfy Folder interface.
 boolean isOpen()
          This method is only to satisfy Folder interface.
protected  boolean isRootFolder()
          Returns true if it is root folder
 javax.mail.Folder[] list(String arg0)
          This method is only to satisfy Folder interface.
 String[] listNames(String pattern)
          Lists subfolder names.
protected  void obtainMessages()
          This method reads directory and creates messages for given folder.
 void open(int arg0)
          This method is only to satisfy Folder interface.
protected  void open(MaildirFolder folder)
          This method is called by folder that is being opened.
protected  void removeMessages(MaildirFolder folder, Collection<? extends javax.mail.internet.MimeMessage> messages)
          This method removes folder messages.
 boolean renameTo(javax.mail.Folder folder)
          This method is only to satisfy Folder interface.
 MaildirFolderData renameTo(MaildirFolderData folderData)
          Renames folder to given folder data
static void renumerateMessages(int from, List<? extends MessageBase> messages)
          Renumerates given list of maildir message objects
protected  void setFolderFile(File file)
          Sets folder's directory
 
Methods inherited from class javax.mail.Folder
addConnectionListener, addFolderListener, addMessageChangedListener, addMessageCountListener, copyMessages, fetch, finalize, getDeletedMessageCount, getMessages, getMessages, getMessages, getMode, getStore, getUnreadMessageCount, getURLName, isSubscribed, list, listSubscribed, listSubscribed, notifyConnectionListeners, notifyFolderListeners, notifyFolderRenamedListeners, notifyMessageAddedListeners, notifyMessageChangedListeners, notifyMessageRemovedListeners, removeConnectionListener, removeFolderListener, removeMessageChangedListener, removeMessageCountListener, search, search, setFlags, setFlags, setFlags, setSubscribed, toString
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NO_SUBFOLDERS_FILENAME

public static final String NO_SUBFOLDERS_FILENAME
This constant defines a filename of a zero length flag file that denotes no subfolders are suppose to be created for this folder

See Also:
Constant Field Values

rootPermanentFlags

public static final javax.mail.Flags rootPermanentFlags
Permanent flags for root are user defined "\\Noselect"


permanentFlags

protected javax.mail.Flags permanentFlags
Permanent flags cache


store

protected MaildirStore store
Maildir store


base

protected File base
Directory folder data is for


rootFolder

protected boolean rootFolder
Flag to denote is this root folder or not


type

protected int type
Type of the folder. See Folder.HOLDS_FOLDERS and Folder.HOLDS_MESSAGES.


tmp

protected File tmp
Tmp subdirectory


cur

protected File cur
Cur subdirectory


nw

protected File nw
New subdirectory


cachedFullName

protected String cachedFullName
Cached folder's full name


cachedName

protected String cachedName
Cached folder's name


lastAccess

protected long lastAccess
Last time folder data was accessed or 0 if no open folders


delay

protected int delay
Amount of time between two accesses. TODO - make this as an attribute


delayFactor

protected int delayFactor
Delay factor - amount of time needed for reading directory vs delay. TODO - make this as an attribute


openedFolders

protected WeakHashMap<javax.mail.Folder,Object> openedFolders
List of open folders


data

protected MaildirFolderData.Data data
Folder's data


closedRef

protected Reference<MaildirFolderData.Data> closedRef
Weak reference to data when there are no open folders


openCount

protected int openCount
Count of open folders. When count reaches zero, storage may remove this folder data

Constructor Detail

MaildirFolderData

public MaildirFolderData(MaildirStore store,
                         File file)
Constructor

Parameters:
store - store
file - directory
Method Detail

getMaildirStore

public MaildirStore getMaildirStore()
Returns maildir store

Returns:
maildir store

getFolderFile

public File getFolderFile()
Returns folder's directory

Returns:
folder's directory

setFolderFile

protected void setFolderFile(File file)
Sets folder's directory

Parameters:
file - folder's directory

getLastAccessed

public long getLastAccessed()
Returns when this folder data is last accessd

Returns:
when this folder data is last accessd

isRootFolder

protected boolean isRootFolder()
Returns true if it is root folder

Returns:
true if it is root folder

getNewDir

protected File getNewDir()
Returns folder's new directory

Returns:
folder's new directory

getCurDir

protected File getCurDir()
Returns folder's cur directory

Returns:
folder's cur directory

getTmpDir

protected File getTmpDir()
Returns folder's tmp directory

Returns:
folder's tmp directory

getName

public String getName()
Returns folder's name

Specified by:
getName in class javax.mail.Folder
Returns:
folder's name

getFullName

public String getFullName()
Returns folder's full name (path and name)

Specified by:
getFullName in class javax.mail.Folder
Returns:
folder's full name

getParentFolderName

public String getParentFolderName()
Returns folder parent's name

Returns:
folder parent's name

exists

public boolean exists()
               throws javax.mail.MessagingException
Returns true if folder exists. This method checks if folder's directory exist.

Specified by:
exists in class javax.mail.Folder
Returns:
true if folder exists
Throws:
javax.mail.MessagingException

listNames

public String[] listNames(String pattern)
                   throws javax.mail.MessagingException
Lists subfolder names.

Parameters:
pattern - pattern
Returns:
list of subfolder names
Throws:
javax.mail.MessagingException

getSeparator

public char getSeparator()
Returns "/"

Specified by:
getSeparator in class javax.mail.Folder
Returns:
"/"

getSubFolderName

public String getSubFolderName(String name)
Returns name for given subfolder.

Parameters:
name - name of subfolder
Returns:
subfolder's full name

create

public boolean create(int type)
               throws javax.mail.MessagingException
Creates folder.

Specified by:
create in class javax.mail.Folder
Parameters:
type - See Folder.HOLDS_FOLDERS and Folder.HOLDS_MESSAGES.
Returns:
true if subfolder is successfully created.
Throws:
javax.mail.MessagingException - in case of a problem while creating folder.

delete

public boolean delete(boolean recursive)
               throws javax.mail.MessagingException
Deletes folder.

Specified by:
delete in class javax.mail.Folder
Parameters:
recursive - if true deletes all subfolders as well.
Returns:
true if operation was successful.
Throws:
javax.mail.MessagingException - in case of an error while deleting folder

deleteAll

protected boolean deleteAll(File file)
Utility method that deletes all subdirectories and files from given directory.

Parameters:
file - directory to be deleted
Returns:
true if operation was successful.

getType

public int getType()
            throws javax.mail.MessagingException
Returns folder's type. It checks for flag file to determine are subfolders are allowed. In this implementation messages are always allowed.

Specified by:
getType in class javax.mail.Folder
Returns:
folder's type. See Folder.HOLDS_FOLDERS and Folder.HOLDS_MESSAGES.
Throws:
javax.mail.MessagingException - never.

renameTo

public MaildirFolderData renameTo(MaildirFolderData folderData)
                           throws javax.mail.MessagingException
Renames folder to given folder data

Parameters:
folderData - folder data
Returns:
true if folder was successfully renamed
Throws:
javax.mail.MessagingException - thrown in case when there is a problem renaming folder

checkDirs

protected void checkDirs()
                  throws javax.mail.MessagingException
This method checks if new, cur and tmp directories exist. If not it tries to create them.

Throws:
javax.mail.MessagingException - in case subdirectories cannot be created.

getMessageCount

public int getMessageCount()
                    throws javax.mail.MessagingException
Returns message count. This implementation checks files on the disk - new and cur directories.

Specified by:
getMessageCount in class javax.mail.Folder
Returns:
message count
Throws:
javax.mail.MessagingException - never

getNewMessageCount

public int getNewMessageCount()
                       throws javax.mail.MessagingException
Returns new message count. This implementation checks files on the disk - only in new directory.

Overrides:
getNewMessageCount in class javax.mail.Folder
Returns:
new message count
Throws:
javax.mail.MessagingException - never

open

protected void open(MaildirFolder folder)
             throws javax.mail.MessagingException
This method is called by folder that is being opened. This implementation reads all files from the new and cur directories and wraps them in appropriate message message objects (see createExistingMaildirMessage(File, int)).

Parameters:
folder - folder that asked opening
Throws:
javax.mail.MessagingException - thrown if an error is encountered while creating messages
javax.mail.MessagingException - thrown if an error is encountered while creating messages

close

protected void close(MaildirFolder folder)
This method is called with folder that is closing.

Parameters:
folder - folder that is closed

appendMessages

protected void appendMessages(MaildirFolder folder,
                              javax.mail.Message[] messages)
                       throws javax.mail.MessagingException
Appends messages to the folder.

Parameters:
folder - folder that initiated appending messages
messages - array of messages
Throws:
javax.mail.MessagingException - thrown while creating new message.

appendMessages

public void appendMessages(javax.mail.Message[] messages)
                    throws javax.mail.MessagingException
Appends messages to the maildir data. This method doesn't append messages to any specific folder - but all opened.

Specified by:
appendMessages in class javax.mail.Folder
Parameters:
messages - messages to be appended
Throws:
javax.mail.MessagingException

expunge

protected List<? extends javax.mail.Message> expunge(MaildirFolder folder,
                                                     boolean explicit)
                                              throws javax.mail.MessagingException
Expunges messages for given folder.

Parameters:
folder - folder
explicit - should folder be notified of messages
Returns:
list of messages that were expunged
Throws:
javax.mail.MessagingException - thrown in javax.mail.Message.getFlags() method

expunge

public javax.mail.Message[] expunge()
                             throws javax.mail.MessagingException
This method is only to satisfy Folder interface. Not to be used.

Specified by:
expunge in class javax.mail.Folder
Returns:
null
Throws:
javax.mail.MessagingException

getFolderMessages

protected List<MaildirMessage> getFolderMessages(MaildirFolder folder)
Returns folder's messages for given folder. This implementation unwraps messages from the folder.

Parameters:
folder -
Returns:
list of messages

obtainMessages

protected void obtainMessages()
                       throws javax.mail.MessagingException
This method reads directory and creates messages for given folder.

Throws:
javax.mail.MessagingException

addMessages

protected void addMessages(MaildirFolder folder,
                           List<MaildirMessage> messages)
                    throws javax.mail.MessagingException
This method adds messages to the folder.

Parameters:
folder - folder where messages should be added without being notified. All others will be notified. If null supplied then all will be notified.
messages - messages to be added
Throws:
javax.mail.MessagingException

removeMessages

protected void removeMessages(MaildirFolder folder,
                              Collection<? extends javax.mail.internet.MimeMessage> messages)
                       throws javax.mail.MessagingException
This method removes folder messages. Called only for implicite removal.

Parameters:
folder - folder that initiated call. That folder will be excluded from removal and method folder.removeMessages must be called separately
messages - messages to be removed
Throws:
javax.mail.MessagingException

createFolderMessages

protected List<MessageBase> createFolderMessages()
This method creates collection structure for storing messages in the folder. This implementation returns ArrayList

Returns:
list for messages to be stored in.

expungeMessage

protected boolean expungeMessage(MaildirMessage message)
Expunges one message

Parameters:
message - message to be expunged
Returns:
true if expunge succed

renumerateMessages

public static void renumerateMessages(int from,
                                      List<? extends MessageBase> messages)
Renumerates given list of maildir message objects

Parameters:
from - first number for renumeration to start with
messages - maildir message objects

createNewMaildirMessage

protected MaildirMessage createNewMaildirMessage(javax.mail.internet.MimeMessage message,
                                                 int num)
                                          throws IOException,
                                                 javax.mail.MessagingException
This method creates new maildir message for folder data (not folder). It is expected for this method to create appropriate file in proper directory based on flags in supplied message (cur or new).

Parameters:
message - message whose content will be copied to new message
num - message number
Returns:
new maildir message this folder will keep for its folder data
Throws:
IOException
javax.mail.MessagingException

createExistingMaildirMessage

protected MaildirMessage createExistingMaildirMessage(File file,
                                                      int num)
                                               throws IOException,
                                                      javax.mail.MessagingException
This method creates new maildir message object for existing file in folder data's directory.

Parameters:
file - file message object is going to be created
num - message number
Returns:
new maildir message this folder will keep for its folder data
Throws:
IOException
javax.mail.MessagingException

getParent

public javax.mail.Folder getParent()
                            throws javax.mail.MessagingException
This method is only to satisfy Folder interface. Not to be used.

Specified by:
getParent in class javax.mail.Folder
Returns:
null
Throws:
javax.mail.MessagingException

list

public javax.mail.Folder[] list(String arg0)
                         throws javax.mail.MessagingException
This method is only to satisfy Folder interface. Not to be used.

Specified by:
list in class javax.mail.Folder
Parameters:
arg0 -
Returns:
null
Throws:
javax.mail.MessagingException

hasNewMessages

public boolean hasNewMessages()
                       throws javax.mail.MessagingException
This method is only to satisfy Folder interface. Not to be used.

Specified by:
hasNewMessages in class javax.mail.Folder
Returns:
getMessageCount() > 0
Throws:
javax.mail.MessagingException

getFolder

public javax.mail.Folder getFolder(String arg0)
                            throws javax.mail.MessagingException
This method is only to satisfy Folder interface. Not to be used.

Specified by:
getFolder in class javax.mail.Folder
Parameters:
arg0 -
Returns:
null
Throws:
javax.mail.MessagingException

renameTo

public boolean renameTo(javax.mail.Folder folder)
                 throws javax.mail.MessagingException
This method is only to satisfy Folder interface. Not to be used.

Specified by:
renameTo in class javax.mail.Folder
Parameters:
folder -
Returns:
result of renameTo(MaildirFolderData) method.
Throws:
javax.mail.MessagingException

open

public void open(int arg0)
          throws javax.mail.MessagingException
This method is only to satisfy Folder interface. Not to be used.

Specified by:
open in class javax.mail.Folder
Parameters:
arg0 -
Throws:
javax.mail.MessagingException

close

public void close(boolean arg0)
           throws javax.mail.MessagingException
This method is only to satisfy Folder interface. Not to be used.

Specified by:
close in class javax.mail.Folder
Parameters:
arg0 -
Throws:
javax.mail.MessagingException

isOpen

public boolean isOpen()
This method is only to satisfy Folder interface. Not to be used.

Specified by:
isOpen in class javax.mail.Folder
Returns:
openCount > 0

getPermanentFlags

public javax.mail.Flags getPermanentFlags()
This method is only to satisfy Folder interface. Not to be used.

Specified by:
getPermanentFlags in class javax.mail.Folder
Returns:
rootPermanentFlags if root folder or permanentFlags otherwise

getMessage

public javax.mail.Message getMessage(int i)
                              throws javax.mail.MessagingException
This method is only to satisfy Folder interface. Not to be used.

Specified by:
getMessage in class javax.mail.Folder
Parameters:
i - index
Returns:
returns message with given index
Throws:
javax.mail.MessagingException


Copyright © 2005-2009 Abstract Horizon. All Rights Reserved.