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