Source Code Cross Referenced for SMTPOutputLogTarget.java in  » Development » LogKit » org » apache » log » output » net » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » LogKit » org.apache.log.output.net 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.