View Javadoc

1   /************************************************************************
2    * Copyright (c) 2003-2006 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.fetchmail;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import javax.mail.Session;
24  import javax.mail.internet.ParseException;
25  
26  import org.apache.avalon.framework.configuration.ConfigurationException;
27  import org.apache.mailet.MailAddress;
28  
29  /***
30   * <p>Class <code>Account</code> encapsulates the account details required to
31   * fetch mail from a message store.</p>
32   * 
33   * <p>Instances are <code>Comparable</code> based on their sequence number.</p>
34   * 
35   * <p>Creation Date: 05-Jun-03</p>
36   */
37  class Account implements Comparable
38  {
39      /***
40       * The user password for this account
41       */
42      private String fieldPassword;     
43  
44      /***
45       * The user to send the fetched mail to
46       */
47      private MailAddress fieldRecipient;  
48  
49      /***
50       * The user name for this account
51       */
52      private String fieldUser;
53  
54      /***
55       * The ParsedConfiguration
56       */    
57      private ParsedConfiguration fieldParsedConfiguration;
58      
59      /***
60       * List of MessageIDs for which processing has been deferred
61       * because the intended recipient could not be found.
62       */
63      private List fieldDeferredRecipientNotFoundMessageIDs;    
64      
65      /***
66       * The sequence number for this account
67       */
68      private int fieldSequenceNumber;
69      
70      /***
71       * Ignore the recipient deduced from the header and use 'fieldRecipient'
72       */
73      private boolean fieldIgnoreRecipientHeader;     
74  
75      /***
76       * The JavaMail Session for this Account.
77       */ 
78      private Session fieldSession;
79  
80      /***
81       * A custom header to be used as the recipient address
82       */
83      private String customRecipientHeader;
84  
85      /***
86       * Constructor for Account.
87       */
88      private Account()
89      {
90          super();
91      }
92      
93      /***
94       * Constructor for Account.
95       * 
96       * @param sequenceNumber
97       * @param parsedConfiguration
98       * @param user
99       * @param password
100      * @param recipient
101      * @param ignoreRecipientHeader
102      * @param session
103      * @throws ConfigurationException
104      */
105     
106     public Account(
107         int sequenceNumber,
108         ParsedConfiguration parsedConfiguration,
109         String user,
110         String password,
111         String recipient,
112         boolean ignoreRecipientHeader,
113         String customRecipientHeader,
114         Session session)
115         throws ConfigurationException
116     {
117         this();
118         setSequenceNumber(sequenceNumber);
119         setParsedConfiguration(parsedConfiguration);
120         setUser(user);
121         setPassword(password);
122         setRecipient(recipient);
123         setIgnoreRecipientHeader(ignoreRecipientHeader);
124         setCustomRecipientHeader(customRecipientHeader);
125         setSession(session);
126     }   
127 
128     /***
129      * Returns the custom recipient header.
130      * @return String
131      */
132     public String getCustomRecipientHeader() {
133         return this.customRecipientHeader;
134     }
135 
136     /***
137      * Returns the password.
138      * @return String
139      */
140     public String getPassword()
141     {
142         return fieldPassword;
143     }
144 
145     /***
146      * Returns the recipient.
147      * @return MailAddress
148      */
149     public MailAddress getRecipient()
150     {
151         return fieldRecipient;
152     }
153 
154     /***
155      * Returns the user.
156      * @return String
157      */
158     public String getUser()
159     {
160         return fieldUser;
161     }
162 
163     /***
164      * Sets the custom recipient header.
165      * @param customRecipientHeader The header to be used
166      */
167     public void setCustomRecipientHeader(String customRecipientHeader) {
168         this.customRecipientHeader = customRecipientHeader;
169     }
170 
171     /***
172      * Sets the password.
173      * @param password The password to set
174      */
175     protected void setPassword(String password)
176     {
177         fieldPassword = password;
178     }
179 
180     /***
181      * Sets the recipient.
182      * @param recipient The recipient to set
183      */
184     protected void setRecipient(MailAddress recipient)
185     {
186         fieldRecipient = recipient;
187     }
188     
189     /***
190      * Sets the recipient.
191      * @param recipient The recipient to set
192      */
193     protected void setRecipient(String recipient) throws ConfigurationException
194     {
195         if (null == recipient)
196         {
197             fieldRecipient = null;
198             return;
199         }
200             
201         try
202         {
203             setRecipient(new MailAddress(recipient));
204         }
205         catch (ParseException pe)
206         {
207             throw new ConfigurationException(
208                 "Invalid recipient address specified: " + recipient);
209         }
210     }   
211 
212 
213     
214 
215     /***
216      * Sets the user.
217      * @param user The user to set
218      */
219     protected void setUser(String user)
220     {
221         fieldUser = user;
222     }
223 
224     /***
225      * Sets the ignoreRecipientHeader.
226      * @param ignoreRecipientHeader The ignoreRecipientHeader to set
227      */
228     protected void setIgnoreRecipientHeader(boolean ignoreRecipientHeader)
229     {
230         fieldIgnoreRecipientHeader = ignoreRecipientHeader;
231     }
232 
233     /***
234      * Returns the ignoreRecipientHeader.
235      * @return boolean
236      */
237     public boolean isIgnoreRecipientHeader()
238     {
239         return fieldIgnoreRecipientHeader;
240     }
241 
242     /***
243      * Returns the sequenceNumber.
244      * @return int
245      */
246     public int getSequenceNumber()
247     {
248         return fieldSequenceNumber;
249     }
250 
251     /***
252      * Sets the sequenceNumber.
253      * @param sequenceNumber The sequenceNumber to set
254      */
255     protected void setSequenceNumber(int sequenceNumber)
256     {
257         fieldSequenceNumber = sequenceNumber;
258     }
259 
260     /***
261      * Compares this object with the specified object for order.  Returns a
262      * negative integer, zero, or a positive integer if this object is less
263      * than, equal to, or greater than the specified object.
264      * 
265      * @see java.lang.Comparable#compareTo(Object)
266      */
267     public int compareTo(Object o)
268     {
269         return getSequenceNumber() - ((Account) o).getSequenceNumber();
270     }
271 
272     /***
273      * Returns the deferredRecipientNotFoundMessageIDs. lazily initialised.
274      * @return List
275      */
276     public List getDeferredRecipientNotFoundMessageIDs()
277     {
278         List messageIDs = null;
279         if (null
280             == (messageIDs = getDeferredRecipientNotFoundMessageIDsBasic()))
281         {
282             updateDeferredRecipientNotFoundMessageIDs();
283             return getDeferredRecipientNotFoundMessageIDs();
284         }
285         return messageIDs;
286     }
287 
288     /***
289      * Returns the deferredRecipientNotFoundMessageIDs.
290      * @return List
291      */
292     private List getDeferredRecipientNotFoundMessageIDsBasic()
293     {
294         return fieldDeferredRecipientNotFoundMessageIDs;
295     }
296     
297     /***
298      * Returns a new List of deferredRecipientNotFoundMessageIDs.
299      * @return List
300      */
301     protected List computeDeferredRecipientNotFoundMessageIDs()
302     {
303         return new ArrayList(16);
304     }    
305     
306     /***
307      * Updates the deferredRecipientNotFoundMessageIDs.
308      */
309     protected void updateDeferredRecipientNotFoundMessageIDs()
310     {
311         setDeferredRecipientNotFoundMessageIDs(computeDeferredRecipientNotFoundMessageIDs());
312     }    
313 
314     /***
315      * Sets the defferedRecipientNotFoundMessageIDs.
316      * @param defferedRecipientNotFoundMessageIDs The defferedRecipientNotFoundMessageIDs to set
317      */
318     protected void setDeferredRecipientNotFoundMessageIDs(List defferedRecipientNotFoundMessageIDs)
319     {
320         fieldDeferredRecipientNotFoundMessageIDs =
321             defferedRecipientNotFoundMessageIDs;
322     }
323 
324     /***
325      * Returns the parsedConfiguration.
326      * @return ParsedConfiguration
327      */
328     public ParsedConfiguration getParsedConfiguration()
329     {
330         return fieldParsedConfiguration;
331     }
332 
333     /***
334      * Sets the parsedConfiguration.
335      * @param parsedConfiguration The parsedConfiguration to set
336      */
337     protected void setParsedConfiguration(ParsedConfiguration parsedConfiguration)
338     {
339         fieldParsedConfiguration = parsedConfiguration;
340     }
341 
342     /***
343      * Returns the session.
344      * @return Session
345      */
346     public Session getSession()
347     {
348         return fieldSession;
349     }
350 
351     /***
352      * Sets the session.
353      * @param session The session to set
354      */
355     protected void setSession(Session session)
356     {
357         fieldSession = session;
358     }
359 
360 }