001: /* ====================================================================
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowledgment may appear in the software
024: * itself, if and wherever such third-party acknowledgments
025: * normally appear.
026: *
027: * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
028: * must not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation. For more
052: * information on the Apache Software Foundation, please see
053: * <http://www.apache.org/>.
054: */
055: package org.apache.log.output.net;
056:
057: import java.util.Date;
058: import javax.mail.Address;
059: import javax.mail.Message;
060: import javax.mail.MessagingException;
061: import javax.mail.Session;
062: import javax.mail.Transport;
063: import javax.mail.internet.MimeMessage;
064: import org.apache.log.format.Formatter;
065: import org.apache.log.output.AbstractOutputTarget;
066:
067: /**
068: * Logkit output target that logs data via SMTP (ie. email, email gateways).
069: *
070: * @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
071: * @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
072: * @version CVS $Id: SMTPOutputLogTarget.java,v 1.6 2003/02/09 23:33:25 mcconnell Exp $
073: * @since 1.1.0
074: */
075: public class SMTPOutputLogTarget extends AbstractOutputTarget {
076: // Mail session
077: private final Session m_session;
078:
079: // Message to be sent
080: private Message m_message;
081:
082: // Address to sent mail to
083: private final Address[] m_toAddresses;
084:
085: // Address to mail is to be listed as sent from
086: private final Address m_fromAddress;
087:
088: // Mail subject
089: private final String m_subject;
090:
091: // Current size of mail, in units of log events
092: private int m_msgSize;
093:
094: // Maximum size of mail, in units of log events
095: private final int m_maxMsgSize;
096:
097: // Buffer containing current mail
098: private StringBuffer m_buffer;
099:
100: /**
101: * SMTPOutputLogTarget constructor, creates a logkit output target
102: * capable of logging to SMTP (ie. email, email gateway) targets.
103: *
104: * @param session mail session to be used
105: * @param toAddresses addresses logs should be sent to
106: * @param fromAddress address logs say they come from
107: * @param subject subject line logs should use
108: * @param maxMsgSize maximum size of any log mail, in units of log events
109: * @param formatter log formatter to use
110: */
111: public SMTPOutputLogTarget(final Session session,
112: final Address[] toAddresses, final Address fromAddress,
113: final String subject, final int maxMsgSize,
114: final Formatter formatter) {
115: super (formatter);
116:
117: // setup log target
118: m_maxMsgSize = maxMsgSize;
119: m_toAddresses = toAddresses;
120: m_fromAddress = fromAddress;
121: m_subject = subject;
122: m_session = session;
123:
124: // ready for business
125: open();
126: }
127:
128: /**
129: * Method to write data to the log target. Logging data is stored in
130: * an internal buffer until the size limit is reached. When this happens
131: * the data is sent to the SMTP target, and the buffer is reset for
132: * subsequent events.
133: *
134: * @param data logging data to be written to target
135: */
136: protected void write(final String data) {
137: try {
138: // ensure we have a message object available
139: if (m_message == null) {
140: m_message = new MimeMessage(m_session);
141: m_message.setFrom(m_fromAddress);
142: m_message.setRecipients(Message.RecipientType.TO,
143: m_toAddresses);
144: m_message.setSubject(m_subject);
145: m_message.setSentDate(new Date());
146: m_msgSize = 0;
147: m_buffer = new StringBuffer();
148: }
149:
150: // add the data to the buffer, separated by a newline
151: m_buffer.append(data);
152: m_buffer.append('\n');
153: ++m_msgSize;
154:
155: // send mail if message size has reached it's size limit
156: if (m_msgSize >= m_maxMsgSize) {
157: send();
158: }
159: } catch (MessagingException e) {
160: getErrorHandler().error("Error creating message", e, null);
161: }
162: }
163:
164: /**
165: * Closes this log target. Sends currently buffered message, if existing.
166: */
167: public synchronized void close() {
168: super .close();
169: send();
170: }
171:
172: /**
173: * Method to enable/disable debugging on the mail session.
174: *
175: * @param flag true to enable debugging, false to disable it
176: */
177: public void setDebug(boolean flag) {
178: m_session.setDebug(flag);
179: }
180:
181: /**
182: * Helper method to send the currently buffered message,
183: * if existing.
184: */
185: private void send() {
186: try {
187: if (m_message != null && m_buffer != null) {
188: m_message.setText(m_buffer.toString());
189: Transport.send(m_message);
190: m_message = null;
191: }
192: } catch (MessagingException e) {
193: getErrorHandler().error("Error sending message", e, null);
194: }
195: }
196: }
|