View Javadoc

1   /************************************************************************
2    * Copyright (c) 1999-2006 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.cornerstone.services.store.Store;
21  import org.apache.avalon.framework.configuration.DefaultConfiguration;
22  import org.apache.avalon.framework.service.ServiceException;
23  import org.apache.avalon.framework.service.ServiceManager;
24  import org.apache.james.Constants;
25  import org.apache.james.services.MailRepository;
26  import org.apache.mailet.GenericMailet;
27  import org.apache.mailet.Mail;
28  
29  /***
30   * Stores incoming Mail in the specified Repository.
31   * If the "passThrough" in confs is true the mail will be returned untouched in
32   * the pipe. If false will be destroyed.
33   * @version 1.0.0, 24/04/1999
34   *
35   * @version This is $Revision: 382444 $
36   */
37  public class ToRepository extends GenericMailet {
38  
39      /***
40       * The repository where this mailet stores mail.
41       */
42      private MailRepository repository;
43  
44      /***
45       * Whether this mailet should allow mails to be processed by additional mailets
46       * or mark it as finished.
47       */
48      private boolean passThrough = false;
49  
50      /***
51       * The path to the repository
52       */
53      private String repositoryPath;
54  
55      /***
56       * Initialize the mailet, loading configuration information.
57       */
58      public void init() {
59          repositoryPath = getInitParameter("repositoryPath");
60          try {
61              passThrough = new Boolean(getInitParameter("passThrough")).booleanValue();
62          } catch (Exception e) {
63              // Ignore exception, default to false
64          }
65  
66          ServiceManager compMgr = (ServiceManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER);
67          try {
68              Store mailstore = (Store) compMgr.lookup(Store.ROLE);
69              DefaultConfiguration mailConf
70                  = new DefaultConfiguration("repository", "generated:ToRepository");
71              mailConf.setAttribute("destinationURL", repositoryPath);
72              mailConf.setAttribute("type", "MAIL");
73              mailConf.setAttribute("CACHEKEYS", getInitParameter("CACHEKEYS","TRUE"));
74              repository = (MailRepository) mailstore.select(mailConf);
75          } catch (ServiceException cnfe) {
76              log("Failed to retrieve Store component:" + cnfe.getMessage());
77          } catch (Exception 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 mail) throws javax.mail.MessagingException {
89          StringBuffer logBuffer =
90              new StringBuffer(160)
91                      .append("Storing mail ")
92                      .append(mail.getName())
93                      .append(" in ")
94                      .append(repositoryPath);
95          log(logBuffer.toString());
96          repository.store(mail);
97          if (!passThrough) {
98              mail.setState(Mail.GHOST);
99          }
100     }
101 
102     /***
103      * Return a string describing this mailet.
104      *
105      * @return a string describing this mailet
106      */
107     public String getMailetInfo() {
108         return "ToRepository Mailet";
109     }
110 }