KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > syslog > MailLog


1 package com.protomatter.syslog;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 import java.io.*;
54 import java.net.*;
55 import java.util.*;
56 import java.text.*;
57
58 import com.protomatter.util.*;
59 import com.protomatter.pool.*;
60
61 /**
62  * A logger that sends email.
63  *
64  * @see com.protomatter.syslog.xml.MailLog_Helper XML configuration class
65  */

66 public class MailLog
67 extends BasicLogger
68 {
69    private SMTPMailTransport transport = null;
70    private String JavaDoc smtpServer = null;
71    private int port = 25;
72    private String JavaDoc fromName = "Syslog";
73    private String JavaDoc fromAddress = null;
74    private Vector to = new Vector();
75    private Vector cc = new Vector();
76    private Vector bcc = new Vector();
77    private boolean html = false;
78    private String JavaDoc workQueue = null;
79
80    private SyslogMailSubjectFormatter
81      subjectFormat = new SimpleSyslogMailSubjectFormatter();
82
83    /**
84     * Create a new mail log that communicates with the given SMTP
85     * on port 25.
86     */

87    public MailLog(String JavaDoc workQueueName, String JavaDoc smtpServer)
88    {
89      this(workQueueName, smtpServer, 25);
90    }
91
92    /**
93     * Create a new mail log that communicates with the given SMTP
94     * on the given port.
95     */

96    public MailLog(String JavaDoc workQueueName, String JavaDoc smtpServer, int port)
97    {
98      this();
99      transport = new SMTPMailTransport(smtpServer, port);
100      setWorkQueue(workQueueName);
101    }
102
103    /**
104     * You will need to call the configure() method if you use this constructor.
105     */

106    public MailLog()
107    {
108      super();
109    }
110
111    /**
112     * Set the transport agent name.
113     */

114    public void setTransportAgent(String JavaDoc agentName)
115    {
116      transport.setTransportAgentName(agentName);
117    }
118    /**
119     * Get the transport agent name.
120     */

121    public String JavaDoc getTransportAgent()
122    {
123      return transport.getTransportAgentName();
124    }
125
126    /**
127     * Set the human-readable name that the message will appear to be from.
128     */

129    public void setFromName(String JavaDoc fromName)
130    {
131      this.fromName = fromName;
132    }
133    /**
134     * Get the human-readable name that the message will appear to be from.
135     */

136    public String JavaDoc getFromName()
137    {
138      return this.fromName;
139    }
140
141    /**
142     * Set the email address the message will appear to come from.
143     */

144    public void setFromAddress(String JavaDoc fromAddress)
145    {
146      this.fromAddress = fromAddress;
147    }
148    /**
149     * Get the email address the message will appear to come from.
150     */

151    public String JavaDoc getFromAddress()
152    {
153      return this.fromAddress;
154    }
155
156    /**
157     * Set the hostname of the mail server to use.
158     */

159    public void setMailServer(String JavaDoc mailServer)
160    {
161      this.smtpServer = mailServer;
162    }
163    /**
164     * Get the hostname of the mail server to use.
165     */

166    public String JavaDoc getMailServer()
167    {
168      return this.smtpServer;
169    }
170
171    /**
172     * Set the port to use on the mail server. Default is 25.
173     */

174    public void setMailPort(int port)
175    {
176      this.port = port;
177    }
178    /**
179     * Get the port to use on the mail server.
180     */

181    public int getMailPort()
182    {
183      return this.port;
184    }
185
186    /**
187     * Set the html-email flag. Default is false.
188     */

189    public void setHTML(boolean html)
190    {
191      this.html = html;
192    }
193    /**
194     * Get the html-email flag.
195     */

196    public boolean getHTML()
197    {
198      return this.html;
199    }
200
201    /**
202     * Set the list of email addresses which appear in the "To:" header.
203     */

204    public void setTo(Vector list)
205    {
206      this.to = list;
207    }
208    /**
209     * Get the list of email addresses which appear in the "To:" header.
210     */

211    public Vector getTo()
212    {
213      return this.to;
214    }
215
216    /**
217     * Set the list of email addresses which appear in the "Cc:" header.
218     */

219    public void setCC(Vector list)
220    {
221      this.cc = list;
222    }
223    /**
224     * Get the list of email addresses which appear in the "Cc:" header.
225     */

226    public Vector getCC()
227    {
228      return this.cc;
229    }
230
231    /**
232     * Set the list of email addresses which appear in the "Bcc:" header.
233     */

234    public void setBCC(Vector list)
235    {
236      this.bcc = list;
237    }
238    /**
239     * Get the list of email addresses which appear in the "Bcc:" header.
240     */

241    public Vector getBCC()
242    {
243      return this.bcc;
244    }
245
246    /**
247     * Set the name of the work queue that this logger
248     * will actually do work in. If it is null,
249     * everything will be done synchronously.
250     */

251    public void setWorkQueue(String JavaDoc workQueue)
252    {
253      this.workQueue = workQueue;
254    }
255
256    /**
257     * Get the name of the work queue that this logger
258     * will actually do work in. If this return null,
259     * everything will be done synchronously.
260     */

261    public String JavaDoc getWorkQueue()
262    {
263      return this.workQueue;
264    }
265
266    /**
267     * Set the message subject formatter.
268     */

269    public void setSubjectFormatter(SyslogMailSubjectFormatter subjectFormat)
270    {
271      this.subjectFormat = subjectFormat;
272    }
273
274    /**
275     * Get the message subject formatter.
276     */

277    public SyslogMailSubjectFormatter getSubjectFormatter()
278    {
279      return this.subjectFormat;
280    }
281
282    /**
283     * Log a message.
284     */

285    public final void log(SyslogMessage message)
286    {
287      if (transport == null)
288        return;
289
290      if (workQueue == null)
291      {
292        sendMail(message);
293      }
294      else
295      {
296        // ask syslog to send the message out in the background.
297
Syslog.addWork(workQueue, new MailLogRunnable(this, message));
298      }
299    }
300
301    private class MailLogRunnable
302    implements Runnable JavaDoc, ObjectPoolObject
303    {
304      private MailLog logger = null;
305      private SyslogMessage message = null;
306
307      public MailLogRunnable(MailLog logger, SyslogMessage message)
308      {
309        this.logger = logger;
310        this.message = message;
311      }
312
313      public void run()
314      {
315        logger.sendMail(message);
316      }
317
318      public void deleteObjectPoolObject()
319      {
320      }
321
322      public boolean isObjectPoolObjectValid()
323      {
324        return true;
325      }
326
327      public void beforeObjectPoolObjectCheckout()
328      {
329      }
330
331      public void afterObjectPoolObjectCheckin()
332      {
333      }
334    }
335
336    void sendMail(SyslogMessage message)
337    {
338      StringBuffer JavaDoc b = new StringBuffer JavaDoc(256);
339      formatLogEntry(b, message);
340
341      try
342      {
343        MailMessage m = new MailMessage();
344        m.setTo(to);
345        m.setFromAddress(fromAddress);
346        m.setFromName(fromName);
347        m.setCC(cc);
348        m.setBCC(bcc);
349        m.setSubject(subjectFormat.formatMessageSubject(message));
350
351        if (html)
352        {
353          MIMEMessage container = new MIMEMessage();
354          MIMEAttachment a
355            = new MIMEAttachment("text/html", "message body", b.toString());
356          container.addAttachment(a);
357          m.setBody(container);
358        }
359        else
360        {
361          m.setBody(b.toString());
362        }
363        transport.sendMessage(m);
364      }
365      catch (IOException ix)
366      {
367        System.err.println(MessageFormat.format(
368             Syslog.getResourceString(MessageConstants.MAILLOG_CANNOT_WRITE_SMTP_MESSAGE),
369             new Object JavaDoc[] { ix.toString() }));
370        ix.printStackTrace();
371      }
372      catch (MailException mx)
373      {
374        System.err.println(MessageFormat.format(
375             Syslog.getResourceString(MessageConstants.MAILLOG_CANNOT_WRITE_SMTP_MESSAGE),
376             new Object JavaDoc[] { mx.toString() }));
377        mx.printStackTrace();
378      }
379    }
380
381    /**
382     * Closes down the file and prepares for shutdown.
383     */

384    public synchronized void shutdown()
385    {
386      transport = null;
387    }
388
389    public void init()
390    {
391      transport = new SMTPMailTransport(smtpServer, port);
392    }
393
394    public void flush()
395    {
396      // do nothing.
397
}
398 }
399
Popular Tags