1 /*****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20 package org.apache.james.transport.mailets;
21
22 import org.apache.avalon.cornerstone.services.store.Store;
23 import org.apache.avalon.framework.configuration.DefaultConfiguration;
24 import org.apache.avalon.framework.service.ServiceException;
25 import org.apache.avalon.framework.service.ServiceManager;
26 import org.apache.james.Constants;
27 import org.apache.james.services.MailRepository;
28 import org.apache.mailet.GenericMailet;
29 import org.apache.mailet.Mail;
30
31 /***
32 * Stores incoming Mail in the specified Repository.
33 * If the "passThrough" in confs is true the mail will be returned untouched in
34 * the pipe. If false will be destroyed.
35 * @version 1.0.0, 24/04/1999
36 *
37 * @version This is $Revision: 494012 $
38 */
39 public class ToRepository extends GenericMailet {
40
41 /***
42 * The repository where this mailet stores mail.
43 */
44 private MailRepository repository;
45
46 /***
47 * Whether this mailet should allow mails to be processed by additional mailets
48 * or mark it as finished.
49 */
50 private boolean passThrough = false;
51
52 /***
53 * The path to the repository
54 */
55 private String repositoryPath;
56
57 /***
58 * Initialize the mailet, loading configuration information.
59 */
60 public void init() {
61 repositoryPath = getInitParameter("repositoryPath");
62 try {
63 passThrough = new Boolean(getInitParameter("passThrough")).booleanValue();
64 } catch (Exception e) {
65
66 }
67
68 ServiceManager compMgr = (ServiceManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER);
69 try {
70 Store mailstore = (Store) compMgr.lookup(Store.ROLE);
71 DefaultConfiguration mailConf
72 = new DefaultConfiguration("repository", "generated:ToRepository");
73 mailConf.setAttribute("destinationURL", repositoryPath);
74 mailConf.setAttribute("type", "MAIL");
75 mailConf.setAttribute("CACHEKEYS", getInitParameter("CACHEKEYS","TRUE"));
76 repository = (MailRepository) mailstore.select(mailConf);
77 } catch (ServiceException cnfe) {
78 log("Failed to retrieve Store component:" + cnfe.getMessage());
79 } catch (Exception e) {
80 log("Failed to retrieve Store component:" + e.getMessage());
81 }
82
83 }
84
85 /***
86 * Store a mail in a particular repository.
87 *
88 * @param mail the mail to process
89 */
90 public void service(Mail mail) throws javax.mail.MessagingException {
91 StringBuffer logBuffer =
92 new StringBuffer(160)
93 .append("Storing mail ")
94 .append(mail.getName())
95 .append(" in ")
96 .append(repositoryPath);
97 log(logBuffer.toString());
98 repository.store(mail);
99 if (!passThrough) {
100 mail.setState(Mail.GHOST);
101 }
102 }
103
104 /***
105 * Return a string describing this mailet.
106 *
107 * @return a string describing this mailet
108 */
109 public String getMailetInfo() {
110 return "ToRepository Mailet";
111 }
112 }