FileDocCategorySizeDatePackage
FolderProcessor.javaAPI DocApache James 2.3.19138Sat Apr 21 10:32:48 BST 2007org.apache.james.fetchmail

FolderProcessor

public class FolderProcessor extends ProcessorAbstract

Class FolderProcessor opens a Folder and iterates over all of the Messages, delegating their processing to MessageProcessor.

If isRecurse(), all subfolders are fetched recursively.

Creation Date: 25-May-03

Fields Summary
private Folder
fieldFolder
The fetched folder
private Boolean
fieldMarkSeenPermanent
Constructors Summary
protected FolderProcessor(Folder folder, Account account)
Constructor for FolderProcessor.

param
folder The folder to be fetched
param
account The account being processed

        super(account);
        setFolder(folder);
    
Methods Summary
protected voidclose()
Method close.

throws
MessagingException

        if (null != getFolder() && getFolder().isOpen())
            getFolder().close(true);
    
protected java.lang.BooleancomputeMarkSeenPermanent()
Answer the result of computing markSeenPermanent.

return
Boolean

        return new Boolean(
            getFolder().getPermanentFlags().contains(Flags.Flag.SEEN));
    
protected javax.mail.FoldergetFolder()
Returns the folder.

return
Folder

        return fieldFolder;
    
protected booleanhandleMarkSeenNotPermanent(javax.mail.internet.MimeMessage aMessage)

Handler for when the folder does not support the SEEN flag. The default behaviour implemented here is to answer the value of the SEEN flag anyway.

Subclasses may choose to override this method and implement their own solutions.

param
aMessage
return
boolean
throws
MessagingException

        return aMessage.isSet(Flags.Flag.SEEN);
    
protected java.lang.BooleanisMarkSeenPermanent()
Returns the isMarkSeenPermanent.

return
Boolean

        Boolean markSeenPermanent = null;
        if (null == (markSeenPermanent = isMarkSeenPermanentBasic()))
        {
            updateMarkSeenPermanent();
            return isMarkSeenPermanent();
        }    
        return markSeenPermanent;
    
private java.lang.BooleanisMarkSeenPermanentBasic()
Returns the markSeenPermanent.

return
Boolean

        return fieldMarkSeenPermanent;
    
protected booleanisSeen(javax.mail.internet.MimeMessage aMessage)
Answer if aMessage has been SEEN.

param
aMessage
return
boolean
throws
MessagingException

        boolean isSeen = false;
        if (isMarkSeenPermanent().booleanValue())
            isSeen = aMessage.isSet(Flags.Flag.SEEN);
        else
            isSeen = handleMarkSeenNotPermanent(aMessage);
        return isSeen;
    
protected voidopen()
Method open.

throws
MessagingException

        int openFlag = Folder.READ_WRITE;
        
        if (isOpenReadOnly())
            openFlag = Folder.READ_ONLY;

        getFolder().open(openFlag);                 
    
public voidprocess()
Method process opens a Folder, fetches the Envelopes for all of its Messages, creates a MessageProcessor and runs it to process each message.

see
org.apache.james.fetchmail.ProcessorAbstract#process()

        int messagesProcessed = 0;
        int messageCount = 0;
        try
        {
            // open the folder            
            try
            {
                open();
            }
            catch (MessagingException ex)
            {
                getLogger().error(
                    getFetchTaskName() + " Failed to open folder!");
                throw ex;
            }

            // Lock the folder while processing each message
            synchronized (getFolder())
            {
                messageCount = getFolder().getMessageCount();
                for (int i = 1; i <= messageCount; i++)
                {
                    MimeMessage message =
                        (MimeMessage) getFolder().getMessage(i);
                    if (isFetchAll() || !isSeen(message))
                    {
                        try
                        {
                            new MessageProcessor(message, getAccount())
                                .process();
                            messagesProcessed++;
                        }
                        // Catch and report an exception but don't rethrow it, 
                        // allowing subsequent messages to be processed.                    
                        catch (Exception ex)
                        {
                            StringBuffer logMessageBuffer =
                                new StringBuffer("Exception processing message ID: ");
                            logMessageBuffer.append(message.getMessageID());
                            getLogger().error(logMessageBuffer.toString(), ex);
                        }
                    }
                }
            }
        }
        catch (MessagingException mex)
        {
            getLogger().error(
                "A MessagingException has terminated fetching messages for this folder",
                mex);
        }
        finally
        {
            // Close the folder
            try
            {
                close();
            }
            catch (MessagingException ex)
            {
                // No-op
            }
            StringBuffer logMessageBuffer = new StringBuffer("Processed ");
            logMessageBuffer.append(messagesProcessed);
            logMessageBuffer.append(" messages of ");
            logMessageBuffer.append(messageCount);
            logMessageBuffer.append(" in folder '");
            logMessageBuffer.append(getFolder().getName());
            logMessageBuffer.append("'");
            getLogger().info(logMessageBuffer.toString());
        }

        // Recurse through sub-folders if required
        try
        {
            if (isRecurse())
                recurse();
        }
        catch (MessagingException mex)
        {
            getLogger().error(
                "A MessagingException has terminated recursing through sub-folders",
                mex);
        }

        return;
    
protected voidrecurse()
Method recurse.

throws
MessagingException

        if ((getFolder().getType() & Folder.HOLDS_FOLDERS)
            == Folder.HOLDS_FOLDERS)
        {
            // folder contains subfolders...
            Folder folders[] = getFolder().list();

            for (int i = 0; i < folders.length; i++)
            {
                new FolderProcessor(folders[i], getAccount()).process();
            }

        }
    
protected voidsetFolder(javax.mail.Folder folder)
Sets the folder.

param
folder The folder to set

        fieldFolder = folder;
    
protected voidsetMarkSeenPermanent(java.lang.Boolean markSeenPermanent)
Sets the markSeenPermanent.

param
markSeenPermanent The isMarkSeenPermanent to set

        fieldMarkSeenPermanent = markSeenPermanent;
    
protected voidupdateMarkSeenPermanent()
Updates the markSeenPermanent.

        setMarkSeenPermanent(computeMarkSeenPermanent());