FileDocCategorySizeDatePackage
MailAlert.javaAPI DocGlassfish v2 API7647Fri May 04 22:32:52 BST 2007com.sun.appserv.management.alert

MailAlert

public class MailAlert extends Object implements NotificationListener
Class MailAlert sends out alerts through E-mail.
AUTHOR:
Hemanth Puttaswamy

Fields Summary
private String
subject
private String
recipients
private String
mailResourceName
private String
fromAddress
private InternetAddress
fromSMTPAddress
private String
mailSMTPHost
private boolean
includeDiagnostics
Logger
alertLogger
Constructors Summary
public MailAlert()
Zero Arg constructor that will be used by the Alert Configuration framework.



                      
       
        alertLogger = LogDomains.getAlertLogger( ); 
        mailSMTPHost = "localhost";
        subject = "Alert from SJAS AppServer";
        setFromAddress("SJASAlert@sun.com");
        includeDiagnostics = false;
        if( alertLogger.isLoggable( Level.FINE ) ) {
            alertLogger.log( Level.FINE, "MailAlert instantiated" );
        }
    
Methods Summary
java.lang.StringgetFromAddress()

 return fromAddress; 
booleangetIncludeDiagnostics()

 return includeDiagnostics; 
java.lang.StringgetMailResourceName()

 return mailResourceName; 
java.lang.StringgetMailSMTPHost()
getter for Testing purpose.

 return mailSMTPHost; 
java.lang.StringgetRecipients()
getter for testing purpose.

 return recipients; 
java.lang.StringgetSubject()
getter added for testing purpose.

 return subject; 
public voidhandleNotification(javax.management.Notification notification, java.lang.Object handback)
javax.managament.NotificationHandler interface implementation.

        try {
            Properties props = System.getProperties( );
            props.put("mail.smtp.host", mailSMTPHost );
            Session session = Session.getDefaultInstance( props, null );
            MimeMessage message = new MimeMessage(session);
            message.setFrom( fromSMTPAddress );
            message.setRecipients( Message.RecipientType.TO, recipients );
            message.setSubject( subject );
            message.setText( notification.toString() );
            Transport.send( message );
            
        } catch( Exception e ) {
            alertLogger.log( Level.FINE, 
                "Exception in MailAlert.handleNotification ->" + e );
            // _REVISIT_: Add the appropriate exception and make sure there
            // is no recursion here.
            // Add a new Key Value Pair to makesure that WARNING message here
            // doesn't result in an alert resulting in a loop
        }
    
public voidsetFromAddress(java.lang.String fromAddress)
From Address for the e-mail alert.

        if( alertLogger.isLoggable( Level.FINE ) ) {
            alertLogger.log( Level.FINE, 
                "setFromAddress called with address ->" + fromAddress );
        }
        this.fromAddress = fromAddress;
        try {
            fromSMTPAddress = new InternetAddress( fromAddress );
        } catch( Exception e ) {
            alertLogger.log( Level.FINE, 
                "Exception in MailAlert.setFromAddress ->" + e );
            // _REVISIT_: Exception log and make sure there is no recursion
        }
    
public voidsetIncludeDiagnostics(boolean includeDiagnostics)
If IncludeDiagnostics is true, then Diagnostics will be sent along with the error. Note: Diagnostics may be available for some of the Logged message alerts only.

        this.includeDiagnostics = includeDiagnostics;
    
public voidsetMailResourceName(java.lang.String mailResourceName)
A JNDI name that will be used to look up the MailResource.

        //_REVISIT_: Currently not using it.
        this.mailResourceName = mailResourceName;
    
public voidsetMailSMTPHost(java.lang.String mailSMTPHost)
Set's the SMTP Host used to send out the mail. This is usually a mail proxy.

        this.mailSMTPHost = mailSMTPHost;
        if( alertLogger.isLoggable( Level.FINE ) ) {
            alertLogger.log( Level.FINE, 
                "setMailSMTPHost called with ->" + mailSMTPHost );
        }
    
public voidsetRecipients(java.lang.String recipients)
Set's the recipient e-mail addresses to recieve alerts. This is a comma separated list of e-mail ids.

        this.recipients = recipients;
        if( alertLogger.isLoggable( Level.FINE ) ) {
            alertLogger.log( Level.FINE,
                "setRecipients called with -> " + recipients );
        }
    
public voidsetSubject(java.lang.String subject)
Set's the subjectLine for the E-mail Alert.

        this.subject = subject;
        if( alertLogger.isLoggable( Level.FINE ) ) {
            alertLogger.log( Level.FINE,
                "setSubject called with -> " + subject );
        }
      
    
public voidsetUnitTestData(java.lang.String unitTestData)
These are Unit Test Method provided for internal testing only.

        alertLogger.log( Level.FINE, 
            "UnitTestData -> " + unitTestData );
        new UnitTest( 
            UnitTest.UNIT_TEST_MAIL_ALERT, unitTestData, this ).start();