KickJava   Java API By Example, From Geeks To Geeks.

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


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.avalon.framework.component.ComponentException;
21 import org.apache.avalon.framework.component.ComponentManager;
22 import org.apache.avalon.framework.configuration.DefaultConfiguration;
23 import org.apache.james.Constants;
24 import org.apache.james.core.MailImpl;
25 import org.apache.james.services.MailRepository;
26 import org.apache.james.services.MailStore;
27 import org.apache.james.util.RFC2822Headers;
28 import org.apache.mailet.GenericMailet;
29 import org.apache.mailet.MailAddress;
30 import org.apache.mailet.Mail;
31 import javax.mail.internet.InternetAddress JavaDoc;
32 import javax.mail.MessagingException JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 /**
36  * Re-spools Mail found in the specified Repository.
37  *
38  * <mailet match="RecipientIs=respool@localhost" class="FromRepository">
39  * &lt;repositoryPath&gt; <i>repository path</i> &lt;/repositoryPath&gt;
40  * &lt;processor&gt; <i>target processor</i> &lt;/repositoryPath&gt;
41  * &lt;delete&t; [true|<b>false</b>] &lt;/delete&gt;
42  * &lt;/mailet&gt;
43  *
44  * @version This is $Revision: 1.1.2.4 $
45  */

46 public class FromRepository extends GenericMailet {
47
48     /**
49      * The repository from where this mailet spools mail.
50      */

51     private MailRepository repository;
52
53     /**
54      * Whether this mailet should delete messages after being spooled
55      */

56     private boolean delete = false;
57
58     /**
59      * The path to the repository
60      */

61     private String JavaDoc repositoryPath;
62
63     /**
64      * The processor that will handle the re-spooled message(s)
65      */

66     private String JavaDoc processor;
67
68     /**
69      * Initialize the mailet, loading configuration information.
70      */

71     public void init() {
72         repositoryPath = getInitParameter("repositoryPath");
73         processor = (getInitParameter("processor") == null) ? Mail.DEFAULT : getInitParameter("processor");
74
75         try {
76             delete = (getInitParameter("delete") == null) ? false : new Boolean JavaDoc(getInitParameter("delete")).booleanValue();
77         } catch (Exception JavaDoc e) {
78             // Ignore exception, default to false
79
}
80
81         ComponentManager compMgr = (ComponentManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER);
82         try {
83             MailStore mailstore = (MailStore) compMgr.lookup("org.apache.james.services.MailStore");
84             DefaultConfiguration mailConf
85                 = new DefaultConfiguration("repository", "generated:ToRepository");
86             mailConf.setAttribute("destinationURL", repositoryPath);
87             mailConf.setAttribute("type", "MAIL");
88             repository = (MailRepository) mailstore.select(mailConf);
89         } catch (ComponentException cnfe) {
90             log("Failed to retrieve Store component:" + cnfe.getMessage());
91         } catch (Exception JavaDoc e) {
92             log("Failed to retrieve Store component:" + e.getMessage());
93         }
94     }
95
96     /**
97      * Spool mail from a particular repository.
98      *
99      * @param triggering e-mail (eventually parameterize via the
100      * trigger message)
101      */

102     public void service(Mail trigger) throws MessagingException JavaDoc {
103         trigger.setState(Mail.GHOST);
104         java.util.Collection JavaDoc processed = new java.util.ArrayList JavaDoc();
105         Iterator JavaDoc list = repository.list();
106         while (list.hasNext()) {
107             String JavaDoc key = (String JavaDoc) list.next();
108             try {
109                 MailImpl mail = repository.retrieve(key);
110                 if (mail != null && mail.getRecipients() != null) {
111                     log((new StringBuffer JavaDoc(160).append("Spooling mail ").append(mail.getName()).append(" from ").append(repositoryPath)).toString());
112
113                     /*
114                     log("Return-Path: " + mail.getMessage().getHeader(RFC2822Headers.RETURN_PATH, ", "));
115                     log("Sender: " + mail.getSender());
116                     log("To: " + mail.getMessage().getHeader(RFC2822Headers.TO, ", "));
117                     log("Recipients: ");
118                     for (Iterator i = mail.getRecipients().iterator(); i.hasNext(); ) {
119                         log(" " + ((MailAddress)i.next()).toString());
120                     };
121                     */

122
123                     mail.setAttribute("FromRepository", Boolean.TRUE);
124                     mail.setState(processor);
125                     getMailetContext().sendMail(mail);
126                     if (delete) processed.add(key);
127                 }
128             } catch (MessagingException JavaDoc e) {
129                 log((new StringBuffer JavaDoc(160).append("Unable to re-spool mail ").append(key).append(" from ").append(repositoryPath)).toString(), e);
130             }
131         }
132         if (delete) repository.remove(processed);
133     }
134
135     /**
136      * Return a string describing this mailet.
137      *
138      * @return a string describing this mailet
139      */

140     public String JavaDoc getMailetInfo() {
141         return "FromRepository Mailet";
142     }
143 }
144
Popular Tags