1 /*
2 * Copyright (C) The Apache Software Foundation. All rights reserved.
3 *
4 * This software is published under the terms of the Apache Software License
5 * version 1.1, a copy of which has been included with this distribution in
6 * the LICENSE file.
7 */
8 package org.apache.mailet;
9
10 import javax.mail.MessagingException;
11 import java.util.Iterator;
12
13 /***
14 * GenericMailet makes writing mailets easier. It provides simple
15 * versions of the lifecycle methods init and destroy and of the methods
16 * in the MailetConfig interface. GenericMailet also implements the log
17 * method, declared in the MailetContext interface.
18 * <p>
19 * To write a generic mailet, you need only override the abstract service
20 * method.
21 *
22 * @version 1.0.0, 24/04/1999
23 * @author Federico Barbieri <scoobie@pop.systemy.it>
24 * @author Stefano Mazzocchi <stefano@apache.org>
25 * @author Pierpaolo Fumagalli <pier@apache.org>
26 * @author Serge Knystautas <sergek@lokitech.com>
27 */
28 public abstract class GenericMailet implements Mailet, MailetConfig {
29 private MailetConfig config = null;
30
31 /***
32 * Called by the mailer container to indicate to a mailet that the
33 * mailet is being taken out of service.
34 */
35 public void destroy() {
36 //Do nothing
37 }
38
39 /***
40 * Returns a String containing the value of the named initialization
41 * parameter, or null if the parameter does not exist.
42 * <p>
43 * This method is supplied for convenience. It gets the value of the
44 * named parameter from the mailet's MailetConfig object.
45 *
46 * @param name - a String specifying the name of the initialization parameter
47 * @return String a String containing the value of the initalization parameter
48 */
49 public String getInitParameter(String name) {
50 return config.getInitParameter(name);
51 }
52
53 /***
54 * Returns the names of the mailet's initialization parameters as an
55 * Iterator of String objects, or an empty Iterator if the mailet has no
56 * initialization parameters.
57 * <p>
58 * This method is supplied for convenience. It gets the parameter names from
59 * the mailet's MailetConfig object.
60 *
61 * @return Iterator an iterator of String objects containing the names of
62 * the mailet's initialization parameters
63 */
64 public Iterator getInitParameterNames() {
65 return config.getInitParameterNames();
66 }
67
68 /***
69 * Returns this matcher's MailetConfig object.
70 *
71 * @return MailetConfig the MailetConfig object that initialized this mailet
72 */
73 public MailetConfig getMailetConfig() {
74 return config;
75 }
76
77 /***
78 * Returns a reference to the MailetContext in which this mailet is
79 * running.
80 *
81 * @return MailetContext the MailetContext object passed to this mailet by the init method
82 */
83 public MailetContext getMailetContext() {
84 return getMailetConfig().getMailetContext();
85 }
86
87 /***
88 * Returns information about the mailet, such as author, version, and
89 * copyright. By default, this method returns an empty string. Override
90 * this method to have it return a meaningful value.
91 *
92 * @return String information about this mailet, by default an empty string
93 */
94 public String getMailetInfo() {
95 return "";
96 }
97
98 /***
99 * Returns the name of this mailet instance.
100 *
101 * @return the name of this mailet instance
102 */
103 public String getMailetName() {
104 return config.getMailetName();
105 }
106
107
108 /***
109 * <p>Called by the mailet container to indicate to a mailet that the
110 * mailet is being placed into service.</p>
111 *
112 * <p>This implementation stores the MailetConfig object it receives from
113 * the mailet container for alter use. When overriding this form of the
114 * method, call super.init(config).</p>
115 *
116 * @param MailetConfig config - the MailetConfig object that contains
117 * configutation information for this mailet
118 * @throws MessagingException
119 * if an exception occurs that interrupts the mailet's normal operation
120 */
121 public void init(MailetConfig newConfig) throws MessagingException {
122 config = newConfig;
123 init();
124 }
125
126 /***
127 * <p>A convenience method which can be overridden so that there's no
128 * need to call super.init(config).</p>
129 *
130 * Instead of overriding init(MailetConfig), simply override this
131 * method and it will be called by GenericMailet.init(MailetConfig config).
132 * The MailetConfig object can still be retrieved via getMailetConfig().
133 *
134 * @throws MessagingException
135 * if an exception occurs that interrupts the mailet's normal operation
136 */
137 public void init() throws MessagingException {
138 //Do nothing... can be overriden
139 }
140
141 /***
142 * Writes the specified message to a mailet log file, prepended by
143 * the mailet's name.
144 *
145 * @param msg - a String specifying the message to be written to the log file
146 */
147 public void log(String message) {
148 StringBuffer logBuffer =
149 new StringBuffer(256)
150 .append(getMailetName())
151 .append(": ")
152 .append(message);
153 getMailetContext().log(logBuffer.toString());
154 }
155
156 /***
157 * Writes an explanatory message and a stack trace for a given Throwable
158 * exception to the mailet log file, prepended by the mailet's name.
159 *
160 * @param message - a String that describes the error or exception
161 * @param t - the java.lang.Throwable error or exception
162 */
163 public void log(String message, Throwable t) {
164 StringBuffer logBuffer =
165 new StringBuffer(256)
166 .append(config.getMailetName())
167 .append(": ")
168 .append(message);
169 getMailetContext().log(logBuffer.toString(), t);
170 }
171
172 /***
173 * <p>Called by the mailet container to allow the mailet to process a
174 * message.</p>
175 *
176 * <p>This method is declared abstract so subclasses must override it.</p>
177 *
178 * @param mail - the Mail object that contains the MimeMessage and
179 * routing information
180 * @throws javax.mail.MessagingException - if an exception occurs that interferes with the mailet's normal operation
181 * occurred
182 */
183 public abstract void service(Mail mail) throws javax.mail.MessagingException;
184 }
185
186
This page was automatically generated by Maven