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.cfb.store.Run;
10 import net.jaekl.qd.util.MailException;
11 import net.jaekl.qd.util.MimePart;
12 import net.jaekl.qd.util.SendMail;
14 public class Notifier {
15 private static final String TEXT_HTML = "text/html";
20 public Notifier(CfbBundle bundle, Config config) {
25 public void sendEmailIfNeeded(PrintWriter pw, HtmlReport report) {
26 Delta delta = report.getDelta();
27 if ((delta.getNumNew() > 0) || (delta.getNumFixed() > 0)) {
28 sendEmail(pw, report);
32 // --- end of public interface ---
34 String constructSubject(HtmlReport report) {
36 Run earlierRun = report.getDelta().getEarlier();
37 if (null == earlierRun) {
38 earlier = m_bundle.get(CfbBundle.NO_EARLIER_RUN);
41 earlier = report.getDelta().getEarlier().constructVersionText(m_bundle);
44 String later = report.getDelta().getLater().constructVersionText(m_bundle);
46 String subject = m_bundle.get(CfbBundle.CFB_MAIL_SUBJECT, earlier, later);
50 SendMail createSendMail() {
51 return new SendMail();
54 void sendEmail(PrintWriter pw, HtmlReport report) {
55 SendMail sendMail = createSendMail();
56 sendMail.setSmtpHost(m_config.getMailSmtpHost());
58 ArrayList<String> recipients = m_config.getNotify();
59 if (recipients.size() < 1) {
63 PrintWriter mailWriter = null;
66 sendMail.setSubject(constructSubject(report));
67 sendMail.setFrom(m_config.getMailFrom());
69 for (String recipient : recipients) {
70 sendMail.addTo(recipient);
73 StringWriter sw = new StringWriter();
74 mailWriter = new PrintWriter(sw);
75 report.write(mailWriter);
78 MimePart part = new MimePart(TEXT_HTML, sw.toString());
79 sendMail.addPart(part);
82 catch (MailException exc) {
83 StringBuilder toList = new StringBuilder();
84 for (String recipient : recipients) {
85 if (toList.length() > 0) {
88 toList.append(recipient);
90 pw.println(m_bundle.get(CfbBundle.CANNOT_SEND_MAIL, toList.toString(), exc.toString()));
91 exc.printStackTrace(pw);
94 if (null != mailWriter) {