1 23 package com.sun.appserv.management.alert; 24 25 import javax.mail.Session ; 26 import javax.mail.Message ; 27 import javax.mail.Transport ; 28 import javax.mail.internet.InternetAddress ; 29 import javax.mail.internet.MimeMessage ; 30 import java.util.Properties ; 31 import java.util.Vector ; 32 import java.util.Enumeration ; 33 import java.util.StringTokenizer ; 34 import java.util.logging.Logger ; 35 import java.util.logging.Level ; 36 37 import javax.management.NotificationListener ; 38 import javax.management.Notification ; 39 40 45 public class MailAlert implements NotificationListener { 46 private String subject; 47 48 private String recipients; 49 50 private String mailResourceName; 51 52 private String fromAddress; 53 54 private InternetAddress fromSMTPAddress; 55 56 private String mailSMTPHost; 57 58 private boolean includeDiagnostics; 59 60 Logger alertLogger = null; 61 62 63 67 public MailAlert( ) { 68 alertLogger = LogDomains.getAlertLogger( ); 69 mailSMTPHost = "localhost"; 70 subject = "Alert from SJAS AppServer"; 71 setFromAddress("SJASAlert@sun.com"); 72 includeDiagnostics = false; 73 if( alertLogger.isLoggable( Level.FINE ) ) { 74 alertLogger.log( Level.FINE, "MailAlert instantiated" ); 75 } 76 } 77 78 81 public void setSubject( String subject ) { 82 this.subject = subject; 83 if( alertLogger.isLoggable( Level.FINE ) ) { 84 alertLogger.log( Level.FINE, 85 "setSubject called with -> " + subject ); 86 } 87 88 } 89 90 91 94 String getSubject( ) { return subject; } 95 96 100 public void setRecipients( String recipients ) { 101 this.recipients = recipients; 102 if( alertLogger.isLoggable( Level.FINE ) ) { 103 alertLogger.log( Level.FINE, 104 "setRecipients called with -> " + recipients ); 105 } 106 } 107 108 111 String getRecipients( ) { return recipients; } 112 113 117 public void setMailSMTPHost( String mailSMTPHost ) { 118 this.mailSMTPHost = mailSMTPHost; 119 if( alertLogger.isLoggable( Level.FINE ) ) { 120 alertLogger.log( Level.FINE, 121 "setMailSMTPHost called with ->" + mailSMTPHost ); 122 } 123 } 124 125 128 String getMailSMTPHost( ) { return mailSMTPHost; } 129 130 133 public void setMailResourceName( String mailResourceName ) { 134 this.mailResourceName = mailResourceName; 136 } 137 138 String getMailResourceName( ) { return mailResourceName; } 139 140 143 public void setFromAddress( String fromAddress ) { 144 if( alertLogger.isLoggable( Level.FINE ) ) { 145 alertLogger.log( Level.FINE, 146 "setFromAddress called with address ->" + fromAddress ); 147 } 148 this.fromAddress = fromAddress; 149 try { 150 fromSMTPAddress = new InternetAddress ( fromAddress ); 151 } catch( Exception e ) { 152 alertLogger.log( Level.FINE, 153 "Exception in MailAlert.setFromAddress ->" + e ); 154 } 156 } 157 158 String getFromAddress( ) { return fromAddress; } 159 160 165 public void setIncludeDiagnostics( boolean includeDiagnostics ) { 166 this.includeDiagnostics = includeDiagnostics; 167 } 168 169 boolean getIncludeDiagnostics( ) { return includeDiagnostics; } 170 171 174 public void handleNotification( Notification notification, Object handback) 175 { 176 try { 177 Properties props = System.getProperties( ); 178 props.put("mail.smtp.host", mailSMTPHost ); 179 Session session = Session.getDefaultInstance( props, null ); 180 MimeMessage message = new MimeMessage (session); 181 message.setFrom( fromSMTPAddress ); 182 message.setRecipients( Message.RecipientType.TO, recipients ); 183 message.setSubject( subject ); 184 message.setText( notification.toString() ); 185 Transport.send( message ); 186 187 } catch( Exception e ) { 188 alertLogger.log( Level.FINE, 189 "Exception in MailAlert.handleNotification ->" + e ); 190 } 195 } 196 197 198 201 public void setUnitTestData( String unitTestData ) { 202 alertLogger.log( Level.FINE, 203 "UnitTestData -> " + unitTestData ); 204 new UnitTest( 205 UnitTest.UNIT_TEST_MAIL_ALERT, unitTestData, this ).start(); 206 } 207 } 208 209 210 211 212 | Popular Tags |