1 17 18 package org.apache.james.transport.matchers; 19 20 import java.util.Iterator ; 21 import org.apache.avalon.framework.component.ComponentException; 22 import org.apache.avalon.framework.component.ComponentManager; 23 24 import org.apache.james.Constants; 25 import org.apache.james.core.MailImpl; 26 import org.apache.james.services.MailServer; 27 import org.apache.james.services.MailRepository; 28 import org.apache.james.services.UsersStore; 29 import org.apache.james.services.UsersRepository; 30 import org.apache.james.services.JamesUser; 31 32 import org.apache.mailet.Mail; 33 import org.apache.mailet.MailAddress; 34 import org.apache.mailet.MailetContext; 35 36 import javax.mail.MessagingException ; 37 38 47 abstract public class AbstractStorageQuota extends AbstractQuotaMatcher { 48 49 private MailServer mailServer; 50 51 52 private UsersStore usersStore; 53 54 57 private UsersRepository localusers; 58 59 63 public void init() throws MessagingException { 64 super.init(); 65 ComponentManager compMgr = (ComponentManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER); 66 try { 67 mailServer = (MailServer) compMgr.lookup(MailServer.ROLE); 68 } catch (ComponentException e) { 69 log("Exception in getting the MailServer: " + e.getMessage() + e.getRole()); 70 } 71 try { 72 usersStore = (UsersStore)compMgr.lookup(UsersStore.ROLE); 73 } catch (ComponentException e) { 74 log("Exception in getting the UsersStore: " + e.getMessage() + e.getRole()); 75 } 76 localusers = (UsersRepository)usersStore.getRepository("LocalUsers"); 77 } 78 79 88 protected boolean isRecipientChecked(MailAddress recipient) throws MessagingException { 89 MailetContext mailetContext = getMailetContext(); 90 return super.isRecipientChecked(recipient) && (mailetContext.isLocalServer(recipient.getHost()) && mailetContext.isLocalUser(recipient.getUser())); 91 } 92 93 98 protected long getUsed(MailAddress recipient, Mail _) throws MessagingException { 99 long size = 0; 100 MailRepository userInbox = mailServer.getUserInbox(getPrimaryName(recipient.getUser())); 101 for (Iterator it = userInbox.list(); it.hasNext(); ) { 102 String key = (String ) it.next(); 103 MailImpl mc = userInbox.retrieve(key); 104 if (mc != null) try { 106 size += mc.getMessageSize(); 107 } catch (Throwable e) { 108 log("Exception in getting message size: " + e.getMessage()); 111 } 112 } 113 return size; 114 } 115 116 122 protected String getPrimaryName(String originalUsername) { 123 String username; 124 try { 125 username = localusers.getRealName(originalUsername); 126 JamesUser user = (JamesUser) localusers.getUserByName(username); 127 if (user.getAliasing()) { 128 username = user.getAlias(); 129 } 130 } 131 catch (Exception e) { 132 username = originalUsername; 133 } 134 return username; 135 } 136 137 } 138 | Popular Tags |