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