KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > fetchmail > FetchMail


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.fetchmail;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Properties JavaDoc;
28
29 import javax.mail.MessagingException JavaDoc;
30 import javax.mail.Session JavaDoc;
31
32 import org.apache.avalon.cornerstone.services.scheduler.Target;
33 import org.apache.avalon.framework.configuration.Configurable;
34 import org.apache.avalon.framework.configuration.Configuration;
35 import org.apache.avalon.framework.configuration.ConfigurationException;
36 import org.apache.avalon.framework.logger.AbstractLogEnabled;
37 import org.apache.avalon.framework.service.ServiceException;
38 import org.apache.avalon.framework.service.ServiceManager;
39 import org.apache.james.services.MailServer;
40 import org.apache.james.services.UsersRepository;
41 import org.apache.james.services.UsersStore;
42
43 /**
44  * <p>Class <code>FetchMail</code> is an Avalon task that is periodically
45  * triggered to fetch mail from a JavaMail Message Store.</p>
46  *
47  * <p>The lifecycle of an instance of <code>FetchMail</code> is managed by
48  * Avalon. The <code>configure(Configuration)</code> method is invoked to parse
49  * and validate Configuration properties. The targetTriggered(String) method is
50  * invoked to execute the task.</p>
51  *
52  * <p>When triggered, a sorted list of Message Store Accounts to be processed is
53  * built. Each Message Store Account is processed by delegating to
54  * <code>StoreProcessor</code>.</p>
55  *
56  * <p>There are two kinds of Message Store Accounts, static and dynamic. Static
57  * accounts are expliciltly declared in the Configuration. Dynamic accounts are
58  * built each time the task is executed, one per each user defined to James,
59  * using the James user name with a configurable prefix and suffix to define
60  * the host user identity and recipient identity for each Account. Dynamic
61  * accounts allow <code>FetchMail</code> to fetch mail for all James users
62  * without modifying the Configuration parameters or restarting the Avalon
63  * server.</p>
64  *
65  * <p>To fully understand the operations supported by this task, read the Class
66  * documention for each Class in the delegation chain starting with this
67  * class' delegate, <code>StoreProcessor</code>. </p>
68  *
69  * <p>Creation Date: 24-May-03</p>
70  *
71  */

72 public class FetchMail extends AbstractLogEnabled implements Configurable, Target
73 {
74     /**
75      * Key fields for DynamicAccounts.
76      */

77     private class DynamicAccountKey
78     {
79         /**
80          * The base user name without prfix or suffix
81          */

82         private String JavaDoc fieldUserName;
83         
84         /**
85          * The sequence number of the parameters used to construct the Account
86          */

87         private int fieldSequenceNumber;
88
89         /**
90          * Constructor for DynamicAccountKey.
91          */

92         private DynamicAccountKey()
93         {
94             super();
95         }
96         
97         /**
98          * Constructor for DynamicAccountKey.
99          */

100         public DynamicAccountKey(String JavaDoc userName, int sequenceNumber)
101         {
102             this();
103             setUserName(userName);
104             setSequenceNumber(sequenceNumber);
105         }
106
107         /**
108          * @see java.lang.Object#equals(Object)
109          */

110         public boolean equals(Object JavaDoc obj)
111         {
112             if (null == obj)
113                 return false;
114             if (!(obj.getClass() == getClass()))
115                 return false;
116             return (
117                 getUserName().equals(((DynamicAccountKey) obj).getUserName())
118                     && getSequenceNumber()
119                         == ((DynamicAccountKey) obj).getSequenceNumber());
120         }
121
122         /**
123          * @see java.lang.Object#hashCode()
124          */

125         public int hashCode()
126         {
127             return getUserName().hashCode() ^ getSequenceNumber();
128         }
129
130         /**
131          * Returns the sequenceNumber.
132          * @return int
133          */

134         public int getSequenceNumber()
135         {
136             return fieldSequenceNumber;
137         }
138
139         /**
140          * Returns the userName.
141          * @return String
142          */

143         public String JavaDoc getUserName()
144         {
145             return fieldUserName;
146         }
147
148         /**
149          * Sets the sequenceNumber.
150          * @param sequenceNumber The sequenceNumber to set
151          */

152         protected void setSequenceNumber(int sequenceNumber)
153         {
154             fieldSequenceNumber = sequenceNumber;
155         }
156
157         /**
158          * Sets the userName.
159          * @param userName The userName to set
160          */

161         protected void setUserName(String JavaDoc userName)
162         {
163             fieldUserName = userName;
164         }
165
166     }
167     /**
168      * Creation Date: 06-Jun-03
169      */

170     private class ParsedDynamicAccountParameters
171     {
172         private String JavaDoc fieldUserPrefix;
173         private String JavaDoc fieldUserSuffix;
174         
175         private String JavaDoc fieldPassword;
176         
177         private int fieldSequenceNumber;
178
179         private boolean fieldIgnoreRecipientHeader;
180         private String JavaDoc fieldRecipientPrefix;
181         private String JavaDoc fieldRecipientSuffix;
182
183         /**
184          * Constructor for ParsedDynamicAccountParameters.
185          */

186         private ParsedDynamicAccountParameters()
187         {
188             super();
189         }
190
191         /**
192          * Constructor for ParsedDynamicAccountParameters.
193          */

194         public ParsedDynamicAccountParameters(
195             int sequenceNumber,
196             Configuration configuration)
197             throws ConfigurationException
198         {
199             this();
200             setSequenceNumber(sequenceNumber);
201             setUserPrefix(configuration.getAttribute("userprefix", ""));
202             setUserSuffix(configuration.getAttribute("usersuffix", ""));
203             setRecipientPrefix(configuration.getAttribute("recipientprefix", ""));
204             setRecipientSuffix(configuration.getAttribute("recipientsuffix", ""));
205             setPassword(configuration.getAttribute("password"));
206             setIgnoreRecipientHeader(
207                 configuration.getAttributeAsBoolean("ignorercpt-header"));
208         }
209
210         /**
211          * Returns the recipientprefix.
212          * @return String
213          */

214         public String JavaDoc getRecipientPrefix()
215         {
216             return fieldRecipientPrefix;
217         }
218
219         /**
220          * Returns the recipientsuffix.
221          * @return String
222          */

223         public String JavaDoc getRecipientSuffix()
224         {
225             return fieldRecipientSuffix;
226         }
227
228         /**
229          * Returns the userprefix.
230          * @return String
231          */

232         public String JavaDoc getUserPrefix()
233         {
234             return fieldUserPrefix;
235         }
236
237         /**
238          * Returns the userSuffix.
239          * @return String
240          */

241         public String JavaDoc getUserSuffix()
242         {
243             return fieldUserSuffix;
244         }
245
246         /**
247          * Sets the recipientprefix.
248          * @param recipientprefix The recipientprefix to set
249          */

250         protected void setRecipientPrefix(String JavaDoc recipientprefix)
251         {
252             fieldRecipientPrefix = recipientprefix;
253         }
254
255         /**
256          * Sets the recipientsuffix.
257          * @param recipientsuffix The recipientsuffix to set
258          */

259         protected void setRecipientSuffix(String JavaDoc recipientsuffix)
260         {
261             fieldRecipientSuffix = recipientsuffix;
262         }
263
264         /**
265          * Sets the userprefix.
266          * @param userprefix The userprefix to set
267          */

268         protected void setUserPrefix(String JavaDoc userprefix)
269         {
270             fieldUserPrefix = userprefix;
271         }
272
273         /**
274          * Sets the userSuffix.
275          * @param userSuffix The userSuffix to set
276          */

277         protected void setUserSuffix(String JavaDoc userSuffix)
278         {
279             fieldUserSuffix = userSuffix;
280         }
281
282         /**
283          * Returns the password.
284          * @return String
285          */

286         public String JavaDoc getPassword()
287         {
288             return fieldPassword;
289         }
290
291         /**
292          * Sets the ignoreRecipientHeader.
293          * @param ignoreRecipientHeader The ignoreRecipientHeader to set
294          */

295         protected void setIgnoreRecipientHeader(boolean ignoreRecipientHeader)
296         {
297             fieldIgnoreRecipientHeader = ignoreRecipientHeader;
298         }
299
300         /**
301          * Sets the password.
302          * @param password The password to set
303          */

304         protected void setPassword(String JavaDoc password)
305         {
306             fieldPassword = password;
307         }
308
309         /**
310          * Returns the ignoreRecipientHeader.
311          * @return boolean
312          */

313         public boolean isIgnoreRecipientHeader()
314         {
315             return fieldIgnoreRecipientHeader;
316         }
317
318         /**
319          * Returns the sequenceNumber.
320          * @return int
321          */

322         public int getSequenceNumber()
323         {
324             return fieldSequenceNumber;
325         }
326
327         /**
328          * Sets the sequenceNumber.
329          * @param sequenceNumber The sequenceNumber to set
330          */

331         protected void setSequenceNumber(int sequenceNumber)
332         {
333             fieldSequenceNumber = sequenceNumber;
334         }
335
336     }
337     /**
338      * @see org.apache.avalon.cornerstone.services.scheduler.Target#targetTriggered(String)
339      */

340     private boolean fieldFetching = false;
341     
342     /**
343      * The Configuration for this task
344      */

345     private ParsedConfiguration fieldConfiguration;
346     
347     /**
348      * A List of ParsedDynamicAccountParameters, one for every <alllocal> entry
349      * in the configuration.
350      */

351     private List JavaDoc fieldParsedDynamicAccountParameters;
352     
353     /**
354      * The Static Accounts for this task.
355      * These are setup when the task is configured.
356      */

357     private List JavaDoc fieldStaticAccounts;
358     
359     /**
360      * The JavaMail Session for this fetch task.
361      */

362
363     private Session JavaDoc fieldSession;
364     
365     /**
366      * The Dynamic Accounts for this task.
367      * These are setup each time the fetchtask is run.
368      */

369     private Map JavaDoc fieldDynamicAccounts;
370     
371    /**
372      * The MailServer service
373      */

374     private MailServer fieldServer;
375     
376    /**
377      * The Local Users repository
378      */

379     private UsersRepository fieldLocalUsers;
380
381     /**
382      * Constructor for POP3mail.
383      */

384     public FetchMail()
385     {
386         super();
387     }
388
389     /**
390      * Method configure parses and validates the Configuration data and creates
391      * a new <code>ParsedConfiguration</code>, an <code>Account</code> for each
392      * configured static account and a <code>ParsedDynamicAccountParameters</code>
393      * for each dynamic account.
394      *
395      * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
396      */

397     public void configure(Configuration configuration)
398         throws ConfigurationException
399     {
400         // Set any Session parameters passed in the Configuration
401
setSessionParameters(configuration);
402
403         // Create the ParsedConfiguration used in the delegation chain
404
ParsedConfiguration parsedConfiguration =
405             new ParsedConfiguration(
406                 configuration,
407                 getLogger(),
408                 getServer(),
409                 getLocalUsers());
410         setConfiguration(parsedConfiguration);
411
412         // Setup the Accounts
413
Configuration[] allAccounts = configuration.getChildren("accounts");
414         if (allAccounts.length < 1)
415             throw new ConfigurationException("Missing <accounts> section.");
416         if (allAccounts.length > 1)
417             throw new ConfigurationException("Too many <accounts> sections, there must be exactly one");
418         Configuration accounts = allAccounts[0];
419
420         // Create an Account for every configured account
421
Configuration[] accountsChildren = accounts.getChildren();
422         if (accountsChildren.length < 1)
423             throw new ConfigurationException("Missing <account> section.");
424
425         for (int i = 0; i < accountsChildren.length; i++)
426         {
427             Configuration accountsChild = accountsChildren[i];
428
429             if (accountsChild.getName() == "alllocal")
430             {
431                 // <allLocal> is dynamic, save the parameters for accounts to
432
// be created when the task is triggered
433
getParsedDynamicAccountParameters().add(
434                     new ParsedDynamicAccountParameters(i, accountsChild));
435                 continue;
436             }
437
438             if (accountsChild.getName() == "account")
439             {
440                 // Create an Account for the named user and
441
// add it to the list of static accounts
442
getStaticAccounts().add(
443                     new Account(
444                         i,
445                         parsedConfiguration,
446                         accountsChild.getAttribute("user"),
447                         accountsChild.getAttribute("password"),
448                         accountsChild.getAttribute("recipient"),
449                         accountsChild.getAttributeAsBoolean(
450                             "ignorercpt-header"),
451                         getSession()));
452                 continue;
453             }
454
455             throw new ConfigurationException(
456                 "Illegal token: <"
457                     + accountsChild.getName()
458                     + "> in <accounts>");
459         }
460     }
461
462     /**
463      * Method target triggered fetches mail for each configured account.
464      *
465      * @see org.apache.avalon.cornerstone.services.scheduler.Target#targetTriggered(String)
466      */

467     public void targetTriggered(String JavaDoc arg0)
468     {
469         // if we are already fetching then just return
470
if (isFetching())
471         {
472             getLogger().info(
473                 "Triggered fetch cancelled. A fetch is already in progress.");
474             return;
475         }
476
477         // Enter Fetching State
478
try
479         {
480             setFetching(true);
481             getLogger().info("Fetcher starting fetches");
482
483             // if debugging, list the JavaMail property key/value pairs
484
// for this Session
485
if (getLogger().isDebugEnabled())
486             {
487                 getLogger().debug("Session properties:");
488                 Properties JavaDoc properties = getSession().getProperties();
489                 Enumeration JavaDoc e = properties.keys();
490                 while (e.hasMoreElements())
491                 {
492                     String JavaDoc key = (String JavaDoc) e.nextElement();
493                     String JavaDoc val = (String JavaDoc) properties.get(key);
494                     if (val.length() > 40)
495                     {
496                         val = val.substring(0, 37) + "...";
497                     }
498                     getLogger().debug(key + "=" + val);
499
500                 }
501             }
502
503             // Update the dynamic accounts,
504
// merge with the static accounts and
505
// sort the accounts so they are in the order
506
// they were entered in config.xml
507
updateDynamicAccounts();
508             ArrayList JavaDoc mergedAccounts =
509                 new ArrayList JavaDoc(
510                     getDynamicAccounts().size() + getStaticAccounts().size());
511             mergedAccounts.addAll(getDynamicAccounts().values());
512             mergedAccounts.addAll(getStaticAccounts());
513             Collections.sort(mergedAccounts);
514
515             StringBuffer JavaDoc logMessage = new StringBuffer JavaDoc(64);
516             logMessage.append("Processing ");
517             logMessage.append(getStaticAccounts().size());
518             logMessage.append(" static accounts and ");
519             logMessage.append(getDynamicAccounts().size());
520             logMessage.append(" dynamic accounts.");
521             getLogger().info(logMessage.toString());
522
523             // Fetch each account
524
Iterator JavaDoc accounts = mergedAccounts.iterator();
525             while (accounts.hasNext())
526             {
527                 try
528                 {
529                     new StoreProcessor((Account) accounts.next()).process();
530                 }
531                 catch (MessagingException JavaDoc ex)
532                 {
533                     getLogger().error(
534                         "A MessagingException has terminated processing of this Account",
535                         ex);
536                 }
537             }
538         }
539         catch (Exception JavaDoc ex)
540         {
541             getLogger().error("An Exception has terminated this fetch.", ex);
542         }
543         finally
544         {
545             getLogger().info("Fetcher completed fetches");
546
547             // Exit Fetching State
548
setFetching(false);
549         }
550     }
551
552     /**
553      * Returns the fetching.
554      * @return boolean
555      */

556     protected boolean isFetching()
557     {
558         return fieldFetching;
559     }
560
561     /**
562      * @see org.apache.avalon.framework.service.Serviceable#service(ServiceManager)
563      */

564     public void service(final ServiceManager manager) throws ServiceException
565     {
566         try
567         {
568             setServer((MailServer) manager.lookup(MailServer.ROLE));
569         }
570         catch (ClassCastException JavaDoc cce)
571         {
572             StringBuffer JavaDoc errorBuffer =
573                 new StringBuffer JavaDoc(128).append("Component ").append(
574                     MailServer.ROLE).append(
575                     "does not implement the required interface.");
576             throw new ServiceException("", errorBuffer.toString());
577         }
578
579         UsersStore usersStore =
580             (UsersStore) manager.lookup("org.apache.james.services.UsersStore");
581         setLocalUsers(usersStore.getRepository("LocalUsers"));
582         if (getLocalUsers() == null)
583             throw new ServiceException(
584                 "",
585                 "The user repository could not be found.");
586     }
587
588
589
590             
591
592
593     /**
594      * Sets the fetching.
595      * @param fetching The fetching to set
596      */

597     protected void setFetching(boolean fetching)
598     {
599         fieldFetching = fetching;
600     }
601
602     /**
603      * Returns the server.
604      * @return MailServer
605      */

606     protected MailServer getServer()
607     {
608         return fieldServer;
609     }
610
611     /**
612      * Returns the configuration.
613      * @return ParsedConfiguration
614      */

615     protected ParsedConfiguration getConfiguration()
616     {
617         return fieldConfiguration;
618     }
619
620     /**
621      * Sets the configuration.
622      * @param configuration The configuration to set
623      */

624     protected void setConfiguration(ParsedConfiguration configuration)
625     {
626         fieldConfiguration = configuration;
627     }
628
629 /**
630  * Sets the server.
631  * @param server The server to set
632  */

633 protected void setServer(MailServer server)
634 {
635     fieldServer = server;
636 }
637
638 /**
639  * Returns the localUsers.
640  * @return UsersRepository
641  */

642 protected UsersRepository getLocalUsers()
643 {
644     return fieldLocalUsers;
645 }
646
647 /**
648  * Sets the localUsers.
649  * @param localUsers The localUsers to set
650  */

651 protected void setLocalUsers(UsersRepository localUsers)
652 {
653     fieldLocalUsers = localUsers;
654 }
655
656     /**
657      * Returns the accounts. Initializes if required.
658      * @return List
659      */

660     protected List JavaDoc getStaticAccounts()
661     {
662         List JavaDoc accounts = null;
663         if (null == (accounts = getStaticAccountsBasic()))
664         {
665             updateStaticAccounts();
666             return getStaticAccounts();
667         }
668         return fieldStaticAccounts;
669     }
670     
671     /**
672      * Returns the staticAccounts.
673      * @return List
674      */

675     private List JavaDoc getStaticAccountsBasic()
676     {
677         return fieldStaticAccounts;
678     }
679
680     /**
681      * Sets the accounts.
682      * @param accounts The accounts to set
683      */

684     protected void setStaticAccounts(List JavaDoc accounts)
685     {
686         fieldStaticAccounts = accounts;
687     }
688     
689     /**
690      * Updates the staticAccounts.
691      */

692     protected void updateStaticAccounts()
693     {
694         setStaticAccounts(computeStaticAccounts());
695     }
696     
697     /**
698      * Updates the ParsedDynamicAccountParameters.
699      */

700     protected void updateParsedDynamicAccountParameters()
701     {
702         setParsedDynamicAccountParameters(computeParsedDynamicAccountParameters());
703     }
704     
705     /**
706      * Updates the dynamicAccounts.
707      */

708     protected void updateDynamicAccounts() throws ConfigurationException
709     {
710         setDynamicAccounts(computeDynamicAccounts());
711     }
712     
713     /**
714      * Computes the staticAccounts.
715      */

716     protected List JavaDoc computeStaticAccounts()
717     {
718         return new ArrayList JavaDoc();
719     }
720     
721     /**
722      * Computes the ParsedDynamicAccountParameters.
723      */

724     protected List JavaDoc computeParsedDynamicAccountParameters()
725     {
726         return new ArrayList JavaDoc();
727     }
728     
729     /**
730      * Computes the dynamicAccounts.
731      */

732     protected Map JavaDoc computeDynamicAccounts() throws ConfigurationException
733     {
734         Map JavaDoc newAccounts =
735             new HashMap JavaDoc(
736                 getLocalUsers().countUsers()
737                     * getParsedDynamicAccountParameters().size());
738         Map JavaDoc oldAccounts = getDynamicAccountsBasic();
739         if (null == oldAccounts)
740             oldAccounts = new HashMap JavaDoc(0);
741
742         Iterator JavaDoc parameterIterator =
743             getParsedDynamicAccountParameters().iterator();
744
745         // Process each ParsedDynamicParameters
746
while (parameterIterator.hasNext())
747         {
748             Map JavaDoc accounts =
749                 computeDynamicAccounts(
750                     oldAccounts,
751                     (ParsedDynamicAccountParameters) parameterIterator.next());
752             // Remove accounts from oldAccounts.
753
// This avoids an average 2*N increase in heapspace used as the
754
// newAccounts are created.
755
Iterator JavaDoc oldAccountsIterator = oldAccounts.keySet().iterator();
756             while (oldAccountsIterator.hasNext())
757             {
758                 if (accounts.containsKey(oldAccountsIterator.next()))
759                     oldAccountsIterator.remove();
760             }
761             // Add this parameter's accounts to newAccounts
762
newAccounts.putAll(accounts);
763         }
764         return newAccounts;
765     }
766     
767     /**
768      * Returns the dynamicAccounts. Initializes if required.
769      * @return Map
770      */

771     protected Map JavaDoc getDynamicAccounts() throws ConfigurationException
772     {
773         Map JavaDoc accounts = null;
774         if (null == (accounts = getDynamicAccountsBasic()))
775         {
776             updateDynamicAccounts();
777             return getDynamicAccounts();
778         }
779         return fieldDynamicAccounts;
780     }
781     
782     /**
783      * Returns the dynamicAccounts.
784      * @return Map
785      */

786     private Map JavaDoc getDynamicAccountsBasic()
787     {
788         return fieldDynamicAccounts;
789     }
790
791     /**
792      * Sets the dynamicAccounts.
793      * @param dynamicAccounts The dynamicAccounts to set
794      */

795     protected void setDynamicAccounts(Map JavaDoc dynamicAccounts)
796     {
797         fieldDynamicAccounts = dynamicAccounts;
798     }
799     
800     /**
801      * Compute the dynamicAccounts for the passed parameters.
802      * Accounts for existing users are copied and accounts for new users are
803      * created.
804      * @param oldAccounts
805      * @param parameters
806      * @return Map - The current Accounts
807      * @throws ConfigurationException
808      */

809     protected Map JavaDoc computeDynamicAccounts(
810         Map JavaDoc oldAccounts,
811         ParsedDynamicAccountParameters parameters)
812         throws ConfigurationException
813     {
814         Map JavaDoc accounts = new HashMap JavaDoc(getLocalUsers().countUsers());
815         Iterator JavaDoc usersIterator = getLocalUsers().list();
816         while (usersIterator.hasNext())
817         {
818             String JavaDoc userName = (String JavaDoc) usersIterator.next();
819             DynamicAccountKey key =
820                 new DynamicAccountKey(userName, parameters.getSequenceNumber());
821             Account account = (Account) oldAccounts.get(key);
822             if (null == account)
823             {
824                 // Create a new DynamicAccount
825
account =
826                     new DynamicAccount(
827                         parameters.getSequenceNumber(),
828                         getConfiguration(),
829                         userName,
830                         parameters.getUserPrefix(),
831                         parameters.getUserSuffix(),
832                         parameters.getPassword(),
833                         parameters.getRecipientPrefix(),
834                         parameters.getRecipientSuffix(),
835                         parameters.isIgnoreRecipientHeader(),
836                         getSession());
837             }
838             accounts.put(key, account);
839         }
840         return accounts;
841     }
842     /**
843      * Resets the dynamicAccounts.
844      */

845     protected void resetDynamicAccounts()
846     {
847         setDynamicAccounts(null);
848     }
849
850     /**
851      * Returns the ParsedDynamicAccountParameters.
852      * @return List
853      */

854     protected List JavaDoc getParsedDynamicAccountParameters()
855     {
856         List JavaDoc accounts = null;
857         if (null == (accounts = getParsedDynamicAccountParametersBasic()))
858         {
859             updateParsedDynamicAccountParameters();
860             return getParsedDynamicAccountParameters();
861         }
862         return fieldParsedDynamicAccountParameters;
863     }
864     
865     /**
866      * Returns the ParsedDynamicAccountParameters.
867      * @return List
868      */

869     private List JavaDoc getParsedDynamicAccountParametersBasic()
870     {
871         return fieldParsedDynamicAccountParameters;
872     }
873
874     /**
875      * Sets the ParsedDynamicAccountParameters.
876      * @param ParsedDynamicAccountParameters The ParsedDynamicAccountParametersto set
877      */

878     protected void setParsedDynamicAccountParameters(List JavaDoc parsedDynamicAccountParameters)
879     {
880         fieldParsedDynamicAccountParameters = parsedDynamicAccountParameters;
881     }
882
883     /**
884      * Returns the session, lazily initialized if required.
885      * @return Session
886      */

887     protected Session JavaDoc getSession()
888     {
889         Session JavaDoc session = null;
890         if (null == (session = getSessionBasic()))
891         {
892             updateSession();
893             return getSession();
894         }
895         return session;
896     }
897     
898     /**
899      * Returns the session.
900      * @return Session
901      */

902     private Session JavaDoc getSessionBasic()
903     {
904         return fieldSession;
905     }
906
907     /**
908      * Answers a new Session.
909      * @return Session
910      */

911     protected Session JavaDoc computeSession()
912     {
913         return Session.getInstance(System.getProperties());
914     }
915     
916     /**
917      * Updates the current Session.
918      */

919     protected void updateSession()
920     {
921         setSession(computeSession());
922     }
923
924     /**
925      * Sets the session.
926      * @param session The session to set
927      */

928     protected void setSession(Session JavaDoc session)
929     {
930         fieldSession = session;
931     }
932     
933     
934     /**
935      * Propogate any Session parameters in the configuration to the Session.
936      * @param configuration The configuration containing the parameters
937      * @throws ConfigurationException
938      */

939     protected void setSessionParameters(Configuration configuration)
940         throws ConfigurationException
941     {
942         Configuration javaMailProperties =
943             configuration.getChild("javaMailProperties", false);
944         if (null != javaMailProperties)
945         {
946             Properties JavaDoc properties = getSession().getProperties();
947             Configuration[] allProperties =
948                 javaMailProperties.getChildren("property");
949             for (int i = 0; i < allProperties.length; i++)
950             {
951                 properties.setProperty(
952                     allProperties[i].getAttribute("name"),
953                     allProperties[i].getAttribute("value"));
954                 if (getLogger().isDebugEnabled())
955                 {
956                     StringBuffer JavaDoc messageBuffer =
957                         new StringBuffer JavaDoc("Set property name: ");
958                     messageBuffer.append(allProperties[i].getAttribute("name"));
959                     messageBuffer.append(" to: ");
960                     messageBuffer.append(
961                         allProperties[i].getAttribute("value"));
962                     getLogger().debug(messageBuffer.toString());
963                 }
964             }
965         }
966     }
967
968 }
969
Popular Tags