Hi everyone,

I'm a first time poster to this group, so if I'm out of line or should be posting this elsewhere, please let me know.

This seemed like the most logical place to this post this message.

I wanted to share with you all an appender that I created to send email (similar to the SMTPAppender).

The code is as follows:

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.spi.LoggingEvent;

/**
 * This class has been created to provide support for SMTP
 * authentication and JNDI mail sessions.
 *
 * @author Lloyd Wilson
 * @since 1.0
 */
public class SMTPAuthorizationAppender extends AppenderSkeleton {

        private String email = null;
        private String lookup = null;
        private String subject = null;

        /**
         * Class constructor.
         *
         * @since 1.0
         */
        public SMTPAuthorizationAppender() {
                super();
                // Print debug.
                LogLog.debug("Appender initialized successfully.");
        }

        /**
         * This method will handle the mail session.
         *
         * @param event
         *              The logging event to send through email.
         * @since 1.0
         */
        public void append(LoggingEvent event) {
                Context context = null;
                MimeMessage message = null;
                Object object = null;
                Session session = null;
                StringBuffer buffer = null;
                try {
                        // Initialize context.
                        context = new InitialContext();
                        // Initialize object from lookup.
                        object = context.lookup(getLookup());
                        if(object != null) {
                                // Initialize session.
                                session = (Session)object;
                                // Initialize message.
                                message = new MimeMessage(session);
                                
message.addRecipient(MimeMessage.RecipientType.TO,
                                                new 
InternetAddress(getEmail()));
                                message.setSubject(getSubject());
                                // Initialize message buffer.
                                buffer = new StringBuffer();
                                buffer.append(layout.format(event));
                                
buffer.append(convertToString(event.getThrowableStrRep()));
                                // Initialize message body part.
                                message.setText(buffer.toString());
                                // Send message.
                                Transport.send(message);
                                
                        } else {
                                // Print debug.
                                LogLog.debug("Invalid message object returned from 
JDNI lookup.");
                        }

                } catch(Exception exception) {
                        // Print error.
                        errorHandler.error("Exception occurred while processing mail 
message.");
                        errorHandler.error("Exception " +
                                        exception);
                        // Print stack trace.
                        exception.printStackTrace();
                        
                } finally {
                        try {
                                // Close context.
                                if(context != null) {
                                        context.close();
                                }

                        } catch(Exception exception) {
                                // Print error.
                                errorHandler.error("Exception occurred while closing 
context.");
                                errorHandler.error("Exception " +
                                                exception);
                                // Print stack trace.
                                exception.printStackTrace();
                        }
                }
        }
        
        /**
         * This method will convert the exception stack from the logging
         * event for the email message.
         *
         * @param array
         *              The array containing the exception details.
         * @return
         *              The string representation of the logging event 
exception.
         */
        private final String convertToString(String[] array) {
                StringBuffer buffer = null;
                if(array != null &&
                        array.length > 0) {
                        // Initialize buffer.
                        buffer = new StringBuffer();
                        // Initialize buffer with exception details.
                        for(int i = 0; i < array.length; i++) {
                                buffer.append(array[i]);
                        }
                        // Return buffer.
                        return buffer.toString();
                }
                // Return empty string, not null.
                return "";
        }

        /**
         * This method will release the resources associated
         * with this appender.
         */
        public synchronized void close() {
                // Initialize properties back to null.
                this.email = null;
                this.lookup = null;
                this.subject = null;
                // Indicate that we've closed the appender.
                this.closed = true;
        }
        
        /**
         * This method will return the email address of where this
         * event will be sent.
         *
         * @return
         *              The email address.
         * @since 1.0
         */
        public String getEmail() {
                return email;
        }

        /**
         * This method will return the JNDI name of the
         * mail session to lookup.
         *
         * @return
         *              The JNDI name of the mail session to lookup.
         * @since 1.0
         */
        public String getLookup() {
                return lookup;
        }

        /**
         * This method will accept the subject of this message.
         *
         * @return
         *              The subject.
         * @since 1.0
         */
        public String getSubject() {
                return subject;
        }
        
        /**
         * This method will accept the email address of where this
         * event will be sent.
         *
         * @param email
         *              The email address.
         * @since 1.0
         */
        public void setEmail(String email) {
                this.email = email;
        }
        
        /**
         * This method will set the JNDI name of the
         * mail session to lookup.
         *
         * @param lookup
         *              The mail session JNDI name.
         * @since 1.0
         */
        public void setLookup(String lookup) {
                this.lookup = lookup;
        }

        /**
         * This method will accept the subject of this message.
         *
         * @param subject
         *              The subject.
         * @since 1.0
         */
        public void setSubject(String subject) {
                this.subject = subject;
        }

        /**
         * This method will return a boolean indicating that this
         * appender requires a layout. Default is "true".
         *
         * @return
         *              A true/false boolean wether or not this appender 
requires
         *              a layout. The default is "true".
         * @since 1.0
         */
        public boolean requiresLayout() {
                return true;
        }
}

The major (and ONLY real) reason for creating this appender was to send email using a SMTP server that required authentication. As an enhancement, I added support to do a JNDI lookup (ie. java:comp/env/mail/<some mail session definition>) to retrieve a javax.mail.Session object. The only thing I really didn't do at this point (no need to for our project) was to provide some backward compatibility for users not using a J2EE server. This code can obviously be modified to accomodate a broader audience, but nevertheless, I thought that this might be useful for others.

BTW, I'm not a regular contributor to this group. I apologize again if I sent this out of context or to the wrong mailing list.

Cheers,
Lloyd

Any questions, please feel free to ask.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to