KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > output > net > SMTPOutputLogTarget


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.log.output.net;
18
19 import java.util.Date JavaDoc;
20 import javax.mail.Address JavaDoc;
21 import javax.mail.Message JavaDoc;
22 import javax.mail.MessagingException JavaDoc;
23 import javax.mail.Session JavaDoc;
24 import javax.mail.Transport JavaDoc;
25 import javax.mail.internet.MimeMessage JavaDoc;
26 import org.apache.log.format.Formatter;
27 import org.apache.log.output.AbstractOutputTarget;
28
29 /** Logkit output target that logs data via SMTP.
30  *
31  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
32  * @author <a HREF="mailto:crafterm@apache.org">Marcus Crafter</a>
33  * @version CVS $Id: SMTPOutputLogTarget.java,v 1.10 2004/03/28 05:05:43 niclas Exp $
34  * @since 1.1.0
35  */

36 public class SMTPOutputLogTarget extends AbstractOutputTarget
37 {
38     // Mail session
39
private final Session JavaDoc m_session;
40
41     // Message to be sent
42
private Message JavaDoc m_message;
43
44     // Address to sent mail to
45
private final Address JavaDoc[] m_toAddresses;
46
47     // Address to mail is to be listed as sent from
48
private final Address JavaDoc m_fromAddress;
49
50     // Mail subject
51
private final String JavaDoc m_subject;
52
53     // Current size of mail, in units of log events
54
private int m_msgSize;
55
56     // Maximum size of mail, in units of log events
57
private final int m_maxMsgSize;
58
59     // Buffer containing current mail
60
private StringBuffer JavaDoc m_buffer;
61
62     /** SMTPOutputLogTarget constructor.
63      *
64      * It creates a logkit output target capable of logging to SMTP
65      * (ie. email, email gateway) targets.
66      *
67      * @param session mail session to be used
68      * @param toAddresses addresses logs should be sent to
69      * @param fromAddress address logs say they come from
70      * @param subject subject line logs should use
71      * @param maxMsgSize maximum size of any log mail, in units of log events
72      * @param formatter log formatter to use
73      */

74     public SMTPOutputLogTarget(
75         final Session JavaDoc session,
76         final Address JavaDoc[] toAddresses,
77         final Address JavaDoc fromAddress,
78         final String JavaDoc subject,
79         final int maxMsgSize,
80         final Formatter formatter )
81     {
82         super( formatter );
83
84         // setup log target
85
m_maxMsgSize = maxMsgSize;
86         m_toAddresses = toAddresses;
87         m_fromAddress = fromAddress;
88         m_subject = subject;
89         m_session = session;
90
91         // ready for business
92
open();
93     }
94
95     /** Method to write data to the log target.
96      *
97      * Logging data is stored in
98      * an internal buffer until the size limit is reached. When this happens
99      * the data is sent to the SMTP target, and the buffer is reset for
100      * subsequent events.
101      *
102      * @param data logging data to be written to target
103      */

104     protected void write( final String JavaDoc data )
105     {
106         try
107         {
108             // ensure we have a message object available
109
if( m_message == null )
110             {
111                 m_message = new MimeMessage JavaDoc( m_session );
112                 m_message.setFrom( m_fromAddress );
113                 m_message.setRecipients( Message.RecipientType.TO, m_toAddresses );
114                 m_message.setSubject( m_subject );
115                 m_message.setSentDate( new Date JavaDoc() );
116                 m_msgSize = 0;
117                 m_buffer = new StringBuffer JavaDoc();
118             }
119
120             // add the data to the buffer, separated by a newline
121
m_buffer.append( data );
122             m_buffer.append( '\n' );
123             ++m_msgSize;
124
125             // send mail if message size has reached it's size limit
126
if( m_msgSize >= m_maxMsgSize )
127             {
128                 send();
129             }
130         }
131         catch( MessagingException JavaDoc e )
132         {
133             getErrorHandler().error( "Error creating message", e, null );
134         }
135     }
136
137     /** Closes this log target.
138      *
139      * Sends currently buffered message, if existing.
140      */

141     public synchronized void close()
142     {
143         super.close();
144         send();
145     }
146
147     /**
148      * Method to enable/disable debugging on the mail session.
149      *
150      * @param flag true to enable debugging, false to disable it
151      */

152     public void setDebug( boolean flag )
153     {
154         m_session.setDebug( flag );
155     }
156
157     /**
158      * Helper method to send the currently buffered message,
159      * if existing.
160      */

161     private void send()
162     {
163         try
164         {
165             if( m_message != null && m_buffer != null )
166             {
167                 m_message.setText( m_buffer.toString() );
168                 Transport.send( m_message );
169                 m_message = null;
170             }
171         }
172         catch( MessagingException JavaDoc e )
173         {
174             getErrorHandler().error( "Error sending message", e, null );
175         }
176     }
177 }
178
179
Popular Tags