KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > mailet > GenericMailet


1 /***********************************************************************
2  * Copyright (c) 1999-2004 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.mailet;
19
20 import javax.mail.MessagingException JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 /**
24  * GenericMailet makes writing mailets easier. It provides simple
25  * versions of the lifecycle methods init and destroy and of the methods
26  * in the MailetConfig interface. GenericMailet also implements the log
27  * method, declared in the MailetContext interface.
28  * <p>
29  * To write a generic mailet, you need only override the abstract service
30  * method.
31  *
32  * @version 1.0.0, 24/04/1999
33  */

34 public abstract class GenericMailet implements Mailet, MailetConfig {
35     private MailetConfig config = null;
36
37     /**
38      * Called by the mailer container to indicate to a mailet that the
39      * mailet is being taken out of service.
40      */

41     public void destroy() {
42         //Do nothing
43
}
44
45     /**
46      * Returns a String containing the value of the named initialization
47      * parameter, or null if the parameter does not exist.
48      * <p>
49      * This method is supplied for convenience. It gets the value of the
50      * named parameter from the mailet's MailetConfig object.
51      *
52      * @param name - a String specifying the name of the initialization parameter
53      * @return a String containing the value of the initalization parameter
54      */

55     public String JavaDoc getInitParameter(String JavaDoc name) {
56         return config.getInitParameter(name);
57     }
58
59     /**
60      * Returns the names of the mailet's initialization parameters as an
61      * Iterator of String objects, or an empty Iterator if the mailet has no
62      * initialization parameters.
63      * <p>
64      * This method is supplied for convenience. It gets the parameter names from
65      * the mailet's MailetConfig object.
66      *
67      * @return an Iterator of String objects containing the names of
68      * the mailet's initialization parameters
69      */

70     public Iterator JavaDoc getInitParameterNames() {
71         return config.getInitParameterNames();
72     }
73
74     /**
75      * Returns this Mailet's MailetConfig object.
76      *
77      * @return the MailetConfig object that initialized this mailet
78      */

79     public MailetConfig getMailetConfig() {
80         return config;
81     }
82
83     /**
84      * Returns a reference to the MailetContext in which this mailet is
85      * running.
86      *
87      * @return the MailetContext object passed to this mailet by the init method
88      */

89     public MailetContext getMailetContext() {
90         return getMailetConfig().getMailetContext();
91     }
92
93     /**
94      * Returns information about the mailet, such as author, version, and
95      * copyright. By default, this method returns an empty string. Override
96      * this method to have it return a meaningful value.
97      *
98      * @return information about this mailet, by default an empty string
99      */

100     public String JavaDoc getMailetInfo() {
101         return "";
102     }
103
104     /**
105      * Returns the name of this mailet instance.
106      *
107      * @return the name of this mailet instance
108      */

109     public String JavaDoc getMailetName() {
110         return config.getMailetName();
111     }
112
113
114     /**
115      * <p>Called by the mailet container to indicate to a mailet that the
116      * mailet is being placed into service.</p>
117      *
118      * <p>This implementation stores the MailetConfig object it receives from
119      * the mailet container for later use. When overriding this form of the
120      * method, call super.init(config).</p>
121      *
122      * @param MailetConfig newconfig - the MailetConfig object that contains
123      * configutation information for this mailet
124      * @throws MessagingException
125      * if an exception occurs that interrupts the mailet's normal operation
126      */

127     public void init(MailetConfig newConfig) throws MessagingException JavaDoc {
128         config = newConfig;
129         init();
130     }
131
132     /**
133      * <p>A convenience method which can be overridden so that there's no
134      * need to call super.init(config).</p>
135      *
136      * Instead of overriding init(MailetConfig), simply override this
137      * method and it will be called by GenericMailet.init(MailetConfig config).
138      * The MailetConfig object can still be retrieved via getMailetConfig().
139      *
140      * @throws MessagingException
141      * if an exception occurs that interrupts the mailet's normal operation
142      */

143     public void init() throws MessagingException JavaDoc {
144         //Do nothing... can be overriden
145
}
146
147     /**
148      * Writes the specified message to a mailet log file, prepended by
149      * the mailet's name.
150      *
151      * @param message - a String specifying the message to be written to the log file
152      */

153     public void log(String JavaDoc message) {
154         StringBuffer JavaDoc logBuffer =
155             new StringBuffer JavaDoc(256)
156                     .append(getMailetName())
157                     .append(": ")
158                     .append(message);
159         getMailetContext().log(logBuffer.toString());
160     }
161
162     /**
163      * Writes an explanatory message and a stack trace for a given Throwable
164      * exception to the mailet log file, prepended by the mailet's name.
165      *
166      * @param message - a String that describes the error or exception
167      * @param t - the java.lang.Throwable to be logged
168      */

169     public void log(String JavaDoc message, Throwable JavaDoc t) {
170         StringBuffer JavaDoc logBuffer =
171             new StringBuffer JavaDoc(256)
172                     .append(config.getMailetName())
173                     .append(": ")
174                     .append(message);
175         getMailetContext().log(logBuffer.toString(), t);
176     }
177
178     /**
179      * <p>Called by the mailet container to allow the mailet to process a
180      * message.</p>
181      *
182      * <p>This method is declared abstract so subclasses must override it.</p>
183      *
184      * @param mail - the Mail object that contains the MimeMessage and
185      * routing information
186      * @throws javax.mail.MessagingException - if an exception occurs that interferes with the mailet's normal operation
187      */

188     public abstract void service(Mail mail) throws javax.mail.MessagingException JavaDoc;
189 }
190
191
192
Popular Tags