KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > alert > MailAlert


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.appserv.management.alert;
24
25 import javax.mail.Session JavaDoc;
26 import javax.mail.Message JavaDoc;
27 import javax.mail.Transport JavaDoc;
28 import javax.mail.internet.InternetAddress JavaDoc;
29 import javax.mail.internet.MimeMessage JavaDoc;
30 import java.util.Properties JavaDoc;
31 import java.util.Vector JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.StringTokenizer JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35 import java.util.logging.Level JavaDoc;
36
37 import javax.management.NotificationListener JavaDoc;
38 import javax.management.Notification JavaDoc;
39
40 /**
41  * Class MailAlert sends out alerts through E-mail.
42  *
43  * @AUTHOR: Hemanth Puttaswamy
44  */

45 public class MailAlert implements NotificationListener JavaDoc {
46     private String JavaDoc subject;
47     
48     private String JavaDoc recipients;
49
50     private String JavaDoc mailResourceName;
51
52     private String JavaDoc fromAddress;
53
54     private InternetAddress JavaDoc fromSMTPAddress;
55
56     private String JavaDoc mailSMTPHost;
57
58     private boolean includeDiagnostics;
59
60     Logger JavaDoc alertLogger = null;
61
62
63     /**
64      * Zero Arg constructor that will be used by the Alert Configuration
65      * framework.
66      */

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     /**
79      * Set's the subjectLine for the E-mail Alert.
80      */

81     public void setSubject( String JavaDoc 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     /**
92      * getter added for testing purpose.
93      */

94     String JavaDoc getSubject( ) { return subject; }
95
96     /**
97      * Set's the recipient e-mail addresses to recieve alerts. This is a
98      * comma separated list of e-mail ids.
99      */

100     public void setRecipients( String JavaDoc 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     /**
109      * getter for testing purpose.
110      */

111     String JavaDoc getRecipients( ) { return recipients; }
112
113     /**
114      * Set's the SMTP Host used to send out the mail. This is usually a
115      * mail proxy.
116      */

117     public void setMailSMTPHost( String JavaDoc 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     /**
126      * getter for Testing purpose.
127      */

128     String JavaDoc getMailSMTPHost( ) { return mailSMTPHost; }
129
130     /**
131      * A JNDI name that will be used to look up the MailResource.
132      */

133     public void setMailResourceName( String JavaDoc mailResourceName ) {
134         //_REVISIT_: Currently not using it.
135
this.mailResourceName = mailResourceName;
136     }
137
138     String JavaDoc getMailResourceName( ) { return mailResourceName; }
139
140     /**
141      * From Address for the e-mail alert.
142      */

143     public void setFromAddress( String JavaDoc 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 JavaDoc( fromAddress );
151         } catch( Exception JavaDoc e ) {
152             alertLogger.log( Level.FINE,
153                 "Exception in MailAlert.setFromAddress ->" + e );
154             // _REVISIT_: Exception log and make sure there is no recursion
155
}
156     }
157
158     String JavaDoc getFromAddress( ) { return fromAddress; }
159
160     /**
161      * If IncludeDiagnostics is true, then Diagnostics will be sent along with
162      * the error. Note: Diagnostics may be available for some of the Logged
163      * message alerts only.
164      */

165     public void setIncludeDiagnostics( boolean includeDiagnostics ) {
166         this.includeDiagnostics = includeDiagnostics;
167     }
168
169     boolean getIncludeDiagnostics( ) { return includeDiagnostics; }
170
171     /**
172      * javax.managament.NotificationHandler interface implementation.
173      */

174     public void handleNotification( Notification JavaDoc notification, Object JavaDoc handback)
175     {
176         try {
177             Properties JavaDoc props = System.getProperties( );
178             props.put("mail.smtp.host", mailSMTPHost );
179             Session JavaDoc session = Session.getDefaultInstance( props, null );
180             MimeMessage JavaDoc message = new MimeMessage JavaDoc(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 JavaDoc e ) {
188             alertLogger.log( Level.FINE,
189                 "Exception in MailAlert.handleNotification ->" + e );
190             // _REVISIT_: Add the appropriate exception and make sure there
191
// is no recursion here.
192
// Add a new Key Value Pair to makesure that WARNING message here
193
// doesn't result in an alert resulting in a loop
194
}
195     }
196
197
198     /**
199      * These are Unit Test Method provided for internal testing only.
200      */

201     public void setUnitTestData( String JavaDoc 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