KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > workflowtool > function > email > AddressProvider


1 /**
2  *
3  */

4 package org.infoglue.cms.applications.workflowtool.function.email;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.Collection JavaDoc;
8
9 import javax.mail.internet.InternetAddress JavaDoc;
10
11 import org.infoglue.cms.applications.workflowtool.function.InfoglueFunction;
12
13 import com.opensymphony.workflow.WorkflowException;
14
15 /**
16  *
17  */

18 public abstract class AddressProvider extends InfoglueFunction
19 {
20     /**
21      *
22      */

23     private static final String JavaDoc REQUIRED_ARGUMENT = "required";
24     
25     /**
26      * The addresses.
27      */

28     private Collection JavaDoc addresses; // type: <InternetAddress>
29

30     /**
31      * The illegal addresses.
32      */

33     private Collection JavaDoc illegalAddresses; // type: <String>
34

35     /**
36      * Indicates if empty addresses should be silently discarded.
37      */

38     private boolean required;
39     
40     /**
41      * Default constructor.
42      */

43     public AddressProvider()
44     {
45         super();
46     }
47
48     /**
49      * Add all recipients. Note that empty email-addresses will be discarded
50      * if the <code>required</code> attribute is <code>false</code>.
51      */

52     protected abstract void populate() throws WorkflowException;
53
54     /**
55      *
56      */

57     protected final void execute() throws WorkflowException
58     {
59         populate();
60         setParameter(EmailFunction.TO_PARAMETER, addresses);
61     }
62     
63     /**
64      *
65      */

66     protected final void addRecipient(final String JavaDoc email)
67     {
68         final boolean isEmpty = (email == null || email.trim().length() == 0);
69         
70         if(!isEmpty)
71         {
72             try
73             {
74                 addresses.add(new InternetAddress JavaDoc(email.trim()));
75             }
76             catch(Exception JavaDoc e)
77             {
78                 illegalAddresses.add(email);
79             }
80         }
81         else if(required)
82         {
83             illegalAddresses.add("");
84         }
85     }
86     
87     /**
88      * Method used for initializing the function; will be called before <code>execute</code> is called.
89      * <p><strong>Note</strong>! You must call <code>super.initialize()</code> first.</p>
90      *
91      * @throws WorkflowException if an error occurs during the initialization.
92      */

93     protected void initialize() throws WorkflowException
94     {
95         super.initialize();
96         this.required = getArgument(REQUIRED_ARGUMENT, "true").equalsIgnoreCase("true");
97         this.addresses = (Collection JavaDoc) getParameter(EmailFunction.TO_PARAMETER, new ArrayList JavaDoc());
98         this.illegalAddresses = (Collection JavaDoc) getParameter(EmailFunction.ILLEGAL_ADDRESSES_PARAMETER, new ArrayList JavaDoc());
99     }
100 }
101
Popular Tags