Source Code Cross Referenced for AbstractQuotaMatcher.java in  » Web-Mail » james-2.3.1 » org » apache » james » transport » matchers » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Mail » james 2.3.1 » org.apache.james.transport.matchers 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /****************************************************************
002:         * Licensed to the Apache Software Foundation (ASF) under one   *
003:         * or more contributor license agreements.  See the NOTICE file *
004:         * distributed with this work for additional information        *
005:         * regarding copyright ownership.  The ASF licenses this file   *
006:         * to you under the Apache License, Version 2.0 (the            *
007:         * "License"); you may not use this file except in compliance   *
008:         * with the License.  You may obtain a copy of the License at   *
009:         *                                                              *
010:         *   http://www.apache.org/licenses/LICENSE-2.0                 *
011:         *                                                              *
012:         * Unless required by applicable law or agreed to in writing,   *
013:         * software distributed under the License is distributed on an  *
014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015:         * KIND, either express or implied.  See the License for the    *
016:         * specific language governing permissions and limitations      *
017:         * under the License.                                           *
018:         ****************************************************************/package org.apache.james.transport.matchers;
019:
020:        import java.util.Iterator;
021:        import java.util.Collection;
022:        import java.util.ArrayList;
023:        import javax.mail.MessagingException;
024:        import org.apache.mailet.GenericMatcher;
025:        import org.apache.mailet.MailAddress;
026:        import org.apache.mailet.Mail;
027:
028:        /**
029:         * <P>Abstract matcher checking whether a recipient has exceeded a maximum allowed quota.</P>
030:         * <P>"Quota" at this level is an abstraction whose specific interpretation
031:         * will be done by subclasses.</P> 
032:         * <P>Although extending GenericMatcher, its logic is recipient oriented.</P>
033:         *
034:         * @version CVS $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $
035:         * @since 2.2.0
036:         */
037:        abstract public class AbstractQuotaMatcher extends GenericMatcher {
038:
039:            /**
040:             * Standard matcher entrypoint.
041:             * First of all, checks the sender using {@link #isSenderChecked}.
042:             * Then, for each recipient checks it using {@link #isRecipientChecked} and
043:             * {@link #isOverQuota}.
044:             *
045:             * @throws MessagingException if either <CODE>isSenderChecked</CODE> or isRecipientChecked throw an exception
046:             */
047:            public final Collection match(Mail mail) throws MessagingException {
048:                Collection matching = null;
049:                if (isSenderChecked(mail.getSender())) {
050:                    matching = new ArrayList();
051:                    for (Iterator i = mail.getRecipients().iterator(); i
052:                            .hasNext();) {
053:                        MailAddress recipient = (MailAddress) i.next();
054:                        if (isRecipientChecked(recipient)
055:                                && isOverQuota(recipient, mail)) {
056:                            matching.add(recipient);
057:                        }
058:                    }
059:                }
060:                return matching;
061:            }
062:
063:            /**
064:             * Does the quota check.
065:             * Checks if {@link #getQuota} < {@link #getUsed} for a recipient.
066:             * Catches any throwable returning false, and so should any override do.
067:             *
068:             * @param address the recipient addresss to check
069:             * @param mail the mail involved in the check
070:             * @return true if over quota
071:             */
072:            protected boolean isOverQuota(MailAddress address, Mail mail) {
073:                String user = address.getUser();
074:                try {
075:                    boolean over = getQuota(address, mail) < getUsed(address,
076:                            mail);
077:                    if (over)
078:                        log(address + " is over quota.");
079:                    return over;
080:                } catch (Throwable e) {
081:                    log("Exception checking quota for: " + address, e);
082:                    return false;
083:                }
084:            }
085:
086:            /** 
087:             * Checks the sender.
088:             * The default behaviour is to check that the sender <I>is not</I> null nor the local postmaster.
089:             * If a subclass overrides this method it should "and" <CODE>super.isSenderChecked</CODE>
090:             * to its check.
091:             *
092:             * @param sender the sender to check
093:             */
094:            protected boolean isSenderChecked(MailAddress sender)
095:                    throws MessagingException {
096:                return !(sender == null || getMailetContext().getPostmaster()
097:                        .equals(sender));
098:            }
099:
100:            /** 
101:             * Checks the recipient.
102:             * The default behaviour is to check that the recipient <I>is not</I> the local postmaster.
103:             * If a subclass overrides this method it should "and" <CODE>super.isRecipientChecked</CODE>
104:             * to its check.
105:             *
106:             * @param recipient the recipient to check
107:             */
108:            protected boolean isRecipientChecked(MailAddress recipient)
109:                    throws MessagingException {
110:                return !(getMailetContext().getPostmaster().equals(recipient));
111:            }
112:
113:            /** 
114:             * Gets the quota to check against.
115:             *
116:             * @param address the address holding the quota if applicable
117:             * @param mail the mail involved if needed
118:             */
119:            abstract protected long getQuota(MailAddress address, Mail mail)
120:                    throws MessagingException;
121:
122:            /**
123:             * Gets the used amount to check against the quota.
124:             *
125:             * @param address the address involved
126:             * @param mail the mail involved if needed
127:             */
128:            abstract protected long getUsed(MailAddress address, Mail mail)
129:                    throws MessagingException;
130:
131:            /**
132:             * Utility method that parses an amount string.
133:             * You can use 'k' and 'm' as optional postfixes to the amount (both upper and lowercase).
134:             * In other words, "1m" is the same as writing "1024k", which is the same as
135:             * "1048576".
136:             *
137:             * @param amount the amount string to parse
138:             */
139:            protected long parseQuota(String amount) throws MessagingException {
140:                long quota;
141:                try {
142:                    if (amount.endsWith("k")) {
143:                        amount = amount.substring(0, amount.length() - 1);
144:                        quota = Long.parseLong(amount) * 1024;
145:                    } else if (amount.endsWith("m")) {
146:                        amount = amount.substring(0, amount.length() - 1);
147:                        quota = Long.parseLong(amount) * 1024 * 1024;
148:                    } else {
149:                        quota = Long.parseLong(amount);
150:                    }
151:                    return quota;
152:                } catch (Exception e) {
153:                    throw new MessagingException("Exception parsing quota", e);
154:                }
155:            }
156:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.