KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > matchers > AbstractStorageQuota


1 /***********************************************************************
2  * Copyright (c) 2003-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.transport.matchers;
19
20 import java.util.Iterator JavaDoc;
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 JavaDoc;
37
38 /**
39  * <P>Abstract matcher checking whether a recipient has exceeded a maximum allowed
40  * <I>storage</I> quota for messages standing in his inbox.</P>
41  * <P>"Storage quota" at this level is still an abstraction whose specific interpretation
42  * will be done by subclasses (e.g. could be specific for each user or common to all of them).</P>
43  *
44  * @version CVS $Revision: 1.1.2.4 $ $Date: 2004/02/26 20:37:00 $
45  * @since 2.2.0
46  */

47 abstract public class AbstractStorageQuota extends AbstractQuotaMatcher {
48
49     private MailServer mailServer;
50
51     /** The store containing the local user repository. */
52     private UsersStore usersStore;
53
54     /** The user repository for this mail server. Contains all the users with inboxes
55      * on this server.
56      */

57     private UsersRepository localusers;
58
59     /**
60      * Standard matcher initialization.
61      * Overriding classes must do a <CODE>super.init()</CODE>.
62      */

63     public void init() throws MessagingException JavaDoc {
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     /**
80      * Checks the recipient.
81      * Does a <CODE>super.isRecipientChecked</CODE> and checks that the recipient
82      * is a known user in the local server.
83      * If a subclass overrides this method it should "and" <CODE>super.isRecipientChecked</CODE>
84      * to its check.
85      *
86      * @param recipient the recipient to check
87      */

88     protected boolean isRecipientChecked(MailAddress recipient) throws MessagingException JavaDoc {
89         MailetContext mailetContext = getMailetContext();
90         return super.isRecipientChecked(recipient) && (mailetContext.isLocalServer(recipient.getHost()) && mailetContext.isLocalUser(recipient.getUser()));
91     }
92
93     /**
94      * Gets the storage used in the recipient's inbox.
95      *
96      * @param recipient the recipient to check
97      */

98     protected long getUsed(MailAddress recipient, Mail _) throws MessagingException JavaDoc {
99         long size = 0;
100         MailRepository userInbox = mailServer.getUserInbox(getPrimaryName(recipient.getUser()));
101         for (Iterator JavaDoc it = userInbox.list(); it.hasNext(); ) {
102             String JavaDoc key = (String JavaDoc) it.next();
103             MailImpl mc = userInbox.retrieve(key);
104             // Retrieve can return null if the mail is no longer in the store.
105
if (mc != null) try {
106                 size += mc.getMessageSize();
107             } catch (Throwable JavaDoc e) {
108                 // MailRepository.retrieve() does NOT lock the message.
109
// It could be deleted while we're looping.
110
log("Exception in getting message size: " + e.getMessage());
111             }
112         }
113         return size;
114     }
115
116     /**
117      * Gets the main name of a local customer, handling aliases.
118      *
119      * @param originalUsername the user name to look for; it can be already the primary name or an alias
120      * @return the primary name, or originalUsername unchanged if not found
121      */

122     protected String JavaDoc getPrimaryName(String JavaDoc originalUsername) {
123         String JavaDoc 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 JavaDoc e) {
132             username = originalUsername;
133         }
134         return username;
135     }
136     
137 }
138
Popular Tags