1 package net.jaekl.cfb.analyze;
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
5 import java.util.ArrayList;
7 import net.jaekl.cfb.CfbBundle;
8 import net.jaekl.cfb.Config;
9 import net.jaekl.qd.util.MailException;
10 import net.jaekl.qd.util.MimePart;
11 import net.jaekl.qd.util.SendMail;
13 public class Notifier {
14 private static final String TEXT_HTML = "text/html";
19 public Notifier(CfbBundle bundle, Config config) {
24 public void sendEmailIfNeeded(PrintWriter pw, HtmlReport report) {
25 Delta delta = report.getDelta();
26 if ((delta.getNumNew() > 0) || (delta.getNumFixed() > 0)) {
27 sendEmail(pw, report);
31 void sendEmail(PrintWriter pw, HtmlReport report) {
32 SendMail sendMail = new SendMail();
34 sendMail.setSmtpHost(m_config.getMailSmtpHost());
36 ArrayList<String> recipients = m_config.getNotify();
37 if (recipients.size() < 1) {
41 PrintWriter mailWriter = null;
44 String earlier = report.getDelta().getEarlier().constructVersionText(m_bundle);
45 String later = report.getDelta().getLater().constructVersionText(m_bundle);
47 sendMail.setFrom(m_config.getMailFrom());
48 sendMail.setSubject(m_bundle.get(CfbBundle.CFB_MAIL_SUBJECT, earlier, later));
50 for (String recipient : recipients) {
51 sendMail.addTo(recipient);
54 StringWriter sw = new StringWriter();
55 mailWriter = new PrintWriter(sw);
56 report.write(mailWriter);
59 MimePart part = new MimePart(TEXT_HTML, sw.toString());
60 sendMail.addPart(part);
63 catch (MailException exc) {
64 StringBuilder toList = new StringBuilder();
65 for (String recipient : recipients) {
66 if (toList.length() > 0) {
69 toList.append(recipient);
71 pw.println(m_bundle.get(CfbBundle.CANNOT_SEND_MAIL, toList.toString(), exc.toString()));
72 exc.printStackTrace(pw);
75 if (null != mailWriter) {