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