FileDocCategorySizeDatePackage
AbstractStorageQuota.javaAPI DocApache James 2.3.15858Fri Jan 12 12:56:30 GMT 2007org.apache.james.transport.matchers

AbstractStorageQuota

public abstract class AbstractStorageQuota extends AbstractQuotaMatcher

Experimental: Abstract matcher checking whether a recipient has exceeded a maximum allowed storage quota for messages standing in his inbox.

"Storage quota" at this level is still an abstraction whose specific interpretation will be done by subclasses (e.g. could be specific for each user or common to all of them).

This matcher need to calculate the mailbox size everytime it is called. This can slow down things if there are many mails in the mailbox. Some users also report big problems with the matcher if a JDBC based mailrepository is used.

version
CVS $Revision: 494058 $ $Date: 2007-01-08 14:18:22 +0100 (Mo, 08 Jan 2007) $
since
2.2.0

Fields Summary
private org.apache.james.services.MailServer
mailServer
private org.apache.james.services.UsersRepository
localusers
The user repository for this mail server. Contains all the users with inboxes on this server.
Constructors Summary
Methods Summary
protected java.lang.StringgetPrimaryName(java.lang.String originalUsername)
Gets the main name of a local customer, handling aliases.

param
originalUsername the user name to look for; it can be already the primary name or an alias
return
the primary name, or originalUsername unchanged if not found

        String username;
        try {
            username = localusers.getRealName(originalUsername);
            JamesUser user = (JamesUser) localusers.getUserByName(username);
            if (user.getAliasing()) {
                username = user.getAlias();
            }
        }
        catch (Exception e) {
            username = originalUsername;
        }
        return username;
    
protected longgetUsed(org.apache.mailet.MailAddress recipient, org.apache.mailet.Mail _)
Gets the storage used in the recipient's inbox.

param
recipient the recipient to check

        long size = 0;
        MailRepository userInbox = mailServer.getUserInbox(getPrimaryName(recipient.getUser()));
        for (Iterator it = userInbox.list(); it.hasNext(); ) {
            String key = (String) it.next();
            Mail mc = userInbox.retrieve(key);
            // Retrieve can return null if the mail is no longer in the store.
            if (mc != null) try {
                size += mc.getMessageSize();
            } catch (Throwable e) {
                // MailRepository.retrieve() does NOT lock the message.
                // It could be deleted while we're looping.
                log("Exception in getting message size: " + e.getMessage());
            }
        }
        return size;
    
public voidinit()
Standard matcher initialization. Overriding classes must do a super.init().

        super.init();
        ServiceManager compMgr = (ServiceManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER);
        try {
            mailServer = (MailServer) compMgr.lookup(MailServer.ROLE);
        } catch (ServiceException e) {
            log("Exception in getting the MailServer: " + e.getMessage() + e.getKey());
        }
        try {
            localusers = (UsersRepository) compMgr.lookup(UsersRepository.ROLE);
        } catch (ServiceException e) {
            log("Exception in getting the UsersStore: " + e.getMessage() + e.getKey());
        }
    
protected booleanisRecipientChecked(org.apache.mailet.MailAddress recipient)
Checks the recipient. Does a super.isRecipientChecked and checks that the recipient is a known user in the local server. If a subclass overrides this method it should "and" super.isRecipientChecked to its check.

param
recipient the recipient to check

        MailetContext mailetContext = getMailetContext();
        return super.isRecipientChecked(recipient) && (mailetContext.isLocalServer(recipient.getHost()) && mailetContext.isLocalUser(recipient.getUser()));