View Javadoc

1   /*
2   Copyright (c) 2005, CodeSmarts
3    All rights reserved.
4   
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions are
7   met:
8   	* 	Redistributions of source code must retain the above copyright
9   notice, this list of conditions and the following disclaimer.
10  	* 	Redistributions in binary form must reproduce the above
11  copyright notice, this list of conditions and the following disclaimer
12  in the documentation and/or other materials provided with the
13  distribution.
14  	* 	Neither the name of the CodeSmarts nor the names of its
15  contributors may be used to endorse or promote products derived from
16  this software without specific prior written permission.
17  
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
19  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
22  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30  
31  package net.codesmarts.log4j;
32  import org.apache.commons.mail.EmailException;
33  import org.apache.commons.mail.HtmlEmail;
34  import org.apache.commons.mail.SimpleEmail;
35  
36  /***
37   * Base class for appenders that file bug reports as emails to specified addresses
38   * @author Fred McCann
39   */
40  public abstract class AbstractEmailBugReportAppender extends AbstractBugReportAppender {
41  
42      private String to;
43      private String from;
44      private String smtpServer;
45      private String subjectPrepend = "";
46      private boolean html = false;
47      
48      /***
49       * Implementing class decides how to create email message body
50       * @param report
51       * @return
52       */
53      public abstract String getMessage(BugReport report);
54      
55      /***
56       * Implementing class decides how to create email message body
57       * @param report
58       * @return
59       */
60      public String getHTMLMessage(BugReport report) {
61          return null;
62      }
63      
64      /***
65       * Implementing class decides how to create email subject
66       * @param report
67       * @return
68       */    
69      public abstract String getSubject(BugReport report);
70       
71      /***
72       * @see net.codesmarts.log4j.BugReportAppender#append(net.codesmarts.log4j.BugReport)
73       */
74      public synchronized void append(BugReport report) {
75          String[] addresses = to.split(",");
76          String subject = subjectPrepend+getSubject(report);
77          
78          for (int x = 0; x < addresses.length; x++) {
79              if (html==true) {            
80                  try {
81  	                HtmlEmail email = new HtmlEmail(); 
82  	                email.setHostName(smtpServer);
83  	                email.setFrom(from);
84  	                email.addTo(addresses[x]);
85  	                email.setSubject(subject);
86  	               
87  	                String htmlMessage = getHTMLMessage(report);
88  	                if (htmlMessage!=null)
89  	                    email.setHtmlMsg(htmlMessage);
90  	                email.setMsg(getMessage(report));
91  	                
92  	                email.send();
93                  }
94                  catch (EmailException e) {
95                      getErrorHandler().error("Could not send message: "+subject+" to: "+
96                              addresses[x]+" from: "+from+". "+e.getMessage());
97                  }
98              }
99              else {
100                 try {
101 	                SimpleEmail email = new SimpleEmail(); 
102 	                email.setHostName(smtpServer);
103 	                email.setFrom(from);
104 	                email.addTo(addresses[x]);
105 	                email.setSubject(subject);
106 	                email.setMsg(getMessage(report));
107 	                email.send();            
108                 }
109                 catch (EmailException e) {
110                     getErrorHandler().error("Could not send message: "+subject+" to: "+
111                             addresses[x]+" from: "+from+". "+e.getMessage());
112                 }                
113             }
114         }
115     }
116 
117     /***
118      * @see net.codesmarts.log4j.AbstractBugReportAppender#init()
119      */
120     public void init() {
121         // make sure we have required fields
122         boolean good = true;
123         
124         if (smtpServer==null) {
125             getErrorHandler().error("Must set SMTP server for "+this.getClass().getName());
126             good = false;
127         }
128 
129         if (from==null) {
130             getErrorHandler().error("Must set \"from\" email address for "+this.getClass().getName());
131             good = false;
132         }
133 
134         if (to==null) {
135             getErrorHandler().error("Must set \"to\" email address for "+this.getClass().getName());
136             good = false;
137         }
138 
139         if (good=false)
140             close();        
141     }
142    
143     /***
144      * Set the email address from which to send messages
145      * @param from The from to set.
146      */
147     public void setFrom(String from) {
148         this.from = from;
149     }
150     
151     /***
152      * Set the SMTP server to use for message delivery
153      * @param smtpServer The smtpServer to set.
154      */
155     public void setSmtpServer(String smtpServer) {
156         this.smtpServer = smtpServer;
157     }
158     
159     /***
160      * Set comma delimited list of message recipients
161      * @param to The to to set.
162      */
163     public void setTo(String to) {
164         this.to = to;
165     }
166     
167     /***
168      * Set this to true to send messages in HTML format; defaults to false
169      * @param html The html to set.
170      */
171     public void setHtml(boolean html) {
172         this.html = html;
173     }
174     
175     /***
176      * Set optional string to prepend to all messages
177      * @param subjectPrepend The subjectPrepend to set.
178      */
179     public void setSubjectPrepend(String subjectPrepend) {
180         this.subjectPrepend = subjectPrepend;
181     }
182 }