001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.transport.mailets;
019:
020: import org.apache.avalon.cornerstone.services.store.Store;
021: import org.apache.avalon.framework.configuration.DefaultConfiguration;
022: import org.apache.avalon.framework.service.ServiceException;
023: import org.apache.avalon.framework.service.ServiceManager;
024: import org.apache.james.Constants;
025: import org.apache.james.services.MailRepository;
026: import org.apache.mailet.GenericMailet;
027: import org.apache.mailet.Mail;
028:
029: /**
030: * Stores incoming Mail in the specified Repository.
031: * If the "passThrough" in confs is true the mail will be returned untouched in
032: * the pipe. If false will be destroyed.
033: * @version 1.0.0, 24/04/1999
034: *
035: * @version This is $Revision: 494012 $
036: */
037: public class ToRepository extends GenericMailet {
038:
039: /**
040: * The repository where this mailet stores mail.
041: */
042: private MailRepository repository;
043:
044: /**
045: * Whether this mailet should allow mails to be processed by additional mailets
046: * or mark it as finished.
047: */
048: private boolean passThrough = false;
049:
050: /**
051: * The path to the repository
052: */
053: private String repositoryPath;
054:
055: /**
056: * Initialize the mailet, loading configuration information.
057: */
058: public void init() {
059: repositoryPath = getInitParameter("repositoryPath");
060: try {
061: passThrough = new Boolean(getInitParameter("passThrough"))
062: .booleanValue();
063: } catch (Exception e) {
064: // Ignore exception, default to false
065: }
066:
067: ServiceManager compMgr = (ServiceManager) getMailetContext()
068: .getAttribute(Constants.AVALON_COMPONENT_MANAGER);
069: try {
070: Store mailstore = (Store) compMgr.lookup(Store.ROLE);
071: DefaultConfiguration mailConf = new DefaultConfiguration(
072: "repository", "generated:ToRepository");
073: mailConf.setAttribute("destinationURL", repositoryPath);
074: mailConf.setAttribute("type", "MAIL");
075: mailConf.setAttribute("CACHEKEYS", getInitParameter(
076: "CACHEKEYS", "TRUE"));
077: repository = (MailRepository) mailstore.select(mailConf);
078: } catch (ServiceException cnfe) {
079: log("Failed to retrieve Store component:"
080: + cnfe.getMessage());
081: } catch (Exception e) {
082: log("Failed to retrieve Store component:" + e.getMessage());
083: }
084:
085: }
086:
087: /**
088: * Store a mail in a particular repository.
089: *
090: * @param mail the mail to process
091: */
092: public void service(Mail mail) throws javax.mail.MessagingException {
093: StringBuffer logBuffer = new StringBuffer(160).append(
094: "Storing mail ").append(mail.getName()).append(" in ")
095: .append(repositoryPath);
096: log(logBuffer.toString());
097: repository.store(mail);
098: if (!passThrough) {
099: 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 getMailetInfo() {
109: return "ToRepository Mailet";
110: }
111: }
|