KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > mailets > SetMailAttribute


1 /***********************************************************************
2  * Copyright (c) 2000-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.transport.mailets;
19
20 import org.apache.mailet.GenericMailet;
21 import org.apache.mailet.Mail;
22 import org.apache.mailet.MailetException;
23 import java.io.Serializable JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.mail.MessagingException JavaDoc;
29
30 /**
31  * This mailet sets attributes on the Mail.
32  *
33  * Sample configuration:
34  * <mailet match="All" class="SetMailAttribute">
35  * <name1>value1</name1>
36  * <name2>value2</name2>
37  * </mailet>
38  *
39  * @version CVS $Revision: 1.1.2.2 $ $Date: 2004/03/15 03:54:19 $
40  * @since 2.2.0
41  */

42 public class SetMailAttribute extends GenericMailet {
43
44     private HashMap JavaDoc attributesToSet = new HashMap JavaDoc(2);
45     
46     private Set JavaDoc entries;
47     
48     /**
49      * Return a string describing this mailet.
50      *
51      * @return a string describing this mailet
52      */

53     public String JavaDoc getMailetInfo() {
54         return "Set Mail Attribute Mailet";
55     }
56
57     /**
58      * Initialize the mailet
59      *
60      * @throws MailetException if the processor parameter is missing
61      */

62     public void init() throws MailetException
63     {
64         Iterator JavaDoc iter = getInitParameterNames();
65         while (iter.hasNext()) {
66             String JavaDoc name = iter.next().toString();
67             String JavaDoc value = getInitParameter (name);
68             attributesToSet.put (name,value);
69         }
70         entries = attributesToSet.entrySet();
71     }
72
73     /**
74      * Sets the configured attributes
75      *
76      * @param mail the mail to process
77      *
78      * @throws MessagingException in all cases
79      */

80     public void service(Mail mail) throws MessagingException JavaDoc {
81         if (entries != null) {
82             Iterator JavaDoc iter = entries.iterator();
83             while (iter.hasNext()) {
84                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
85                 mail.setAttribute ((String JavaDoc)entry.getKey(),(Serializable JavaDoc)entry.getValue());
86             }
87         }
88     }
89     
90
91 }
92
Popular Tags