KickJava   Java API By Example, From Geeks To Geeks.

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


1 /***********************************************************************
2  * Copyright (c) 1999-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.mailet.GenericMailet;
28 import org.apache.mailet.Mail;
29
30 /**
31  * Stores incoming Mail in the specified Repository.
32  * If the "passThrough" in confs is true the mail will be returned untouched in
33  * the pipe. If false will be destroyed.
34  * @version 1.0.0, 24/04/1999
35  *
36  * @version This is $Revision: 1.8.4.4 $
37  */

38 public class ToRepository extends GenericMailet {
39
40     /**
41      * The repository where this mailet stores mail.
42      */

43     private MailRepository repository;
44
45     /**
46      * Whether this mailet should allow mails to be processed by additional mailets
47      * or mark it as finished.
48      */

49     private boolean passThrough = false;
50
51     /**
52      * The path to the repository
53      */

54     private String JavaDoc repositoryPath;
55
56     /**
57      * Initialize the mailet, loading configuration information.
58      */

59     public void init() {
60         repositoryPath = getInitParameter("repositoryPath");
61         try {
62             passThrough = new Boolean JavaDoc(getInitParameter("passThrough")).booleanValue();
63         } catch (Exception JavaDoc e) {
64             // Ignore exception, default to false
65
}
66
67         ComponentManager compMgr = (ComponentManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER);
68         try {
69             MailStore mailstore = (MailStore) compMgr.lookup("org.apache.james.services.MailStore");
70             DefaultConfiguration mailConf
71                 = new DefaultConfiguration("repository", "generated:ToRepository");
72             mailConf.setAttribute("destinationURL", repositoryPath);
73             mailConf.setAttribute("type", "MAIL");
74             repository = (MailRepository) mailstore.select(mailConf);
75         } catch (ComponentException cnfe) {
76             log("Failed to retrieve Store component:" + cnfe.getMessage());
77         } catch (Exception JavaDoc e) {
78             log("Failed to retrieve Store component:" + e.getMessage());
79         }
80
81     }
82
83     /**
84      * Store a mail in a particular repository.
85      *
86      * @param mail the mail to process
87      */

88     public void service(Mail genericmail) throws javax.mail.MessagingException JavaDoc {
89         MailImpl mail = (MailImpl)genericmail;
90         StringBuffer JavaDoc logBuffer =
91             new StringBuffer JavaDoc(160)
92                     .append("Storing mail ")
93                     .append(mail.getName())
94                     .append(" in ")
95                     .append(repositoryPath);
96         log(logBuffer.toString());
97         repository.store(mail);
98         if (!passThrough) {
99             mail.setState(Mail.GHOST);
100         }
101     }
102
103     /**
104      * Return a string describing this mailet.
105      *
106      * @return a string describing this mailet
107      */

108     public String JavaDoc getMailetInfo() {
109         return "ToRepository Mailet";
110     }
111 }
112
Popular Tags