1 package com.mycompany.listeners; 2 3 import java.io.ByteArrayOutputStream ; 4 import java.io.FileInputStream ; 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.util.Enumeration ; 8 import java.util.Properties ; 9 import java.util.StringTokenizer ; 10 11 import javax.mail.Session ; 12 import javax.mail.Transport ; 13 import javax.mail.internet.InternetAddress ; 14 import javax.mail.internet.MimeMessage ; 15 16 import com.puppycrawl.tools.checkstyle.DefaultLogger; 17 import com.puppycrawl.tools.checkstyle.api.AuditEvent; 18 import com.puppycrawl.tools.checkstyle.api.AuditListener; 19 import com.puppycrawl.tools.checkstyle.api.CheckstyleException; 20 import com.puppycrawl.tools.checkstyle.api.SeverityLevel; 21 22 53 public class MailLogger 54 implements AuditListener 55 { 56 57 private ByteArrayOutputStream mOutputStream; 58 59 60 private DefaultLogger mLogger; 61 62 63 private int mErrors; 64 65 68 public MailLogger() 69 { 70 mOutputStream = new ByteArrayOutputStream (); 71 mLogger = new DefaultLogger(mOutputStream, false); 72 mErrors = 0; 73 } 74 75 76 public void auditStarted(AuditEvent aEvt) 77 { 78 mLogger.auditStarted(aEvt); 79 } 80 81 85 public void auditFinished(AuditEvent aEvt) 86 { 87 mLogger.auditFinished(aEvt); 88 89 final Properties properties = System.getProperties(); 90 91 final Properties fileProperties = new Properties (); 94 final String filename = 95 (String ) properties.get("MailLogger.properties.file"); 96 if (filename != null) { 97 InputStream is = null; 98 try { 99 is = new FileInputStream (filename); 100 fileProperties.load(is); 101 } 102 catch (IOException ioe) { 103 ; 105 } 106 finally { 107 if (is != null) { 108 try { 109 is.close(); 110 } 111 catch (IOException e) { 112 ; 113 } 114 } 115 } 116 } 117 118 for (Enumeration e = fileProperties.keys(); e.hasMoreElements();) { 119 final String key = (String ) e.nextElement(); 120 final String value = fileProperties.getProperty(key); 121 properties.put(key, value); 122 } 123 124 final boolean success = (mErrors == 0); 125 final String prefix = success ? "success" : "failure"; 126 127 try { 128 final String mailhost = 129 getValue(properties, "mailhost", "localhost"); 130 final String from = getValue(properties, "from", null); 131 132 final String toList = getValue(properties, prefix + ".to", null); 133 final String subject = getValue(properties, prefix + ".subject", 134 (success) 135 ? "Checkstyle Audit Success" 136 : "Checkstyle Audit Failure"); 137 138 sendMail(mailhost, from, toList, subject, mOutputStream.toString()); 139 } 140 catch (Exception e) { 141 System.out.println("MailLogger failed to send e-mail!"); 142 e.printStackTrace(System.err); 143 } 144 } 145 146 147 public void fileStarted(AuditEvent aEvt) 148 { 149 mLogger.fileStarted(aEvt); 150 } 151 152 153 public void fileFinished(AuditEvent aEvt) 154 { 155 mLogger.fileFinished(aEvt); 156 } 157 158 159 public void addError(AuditEvent aEvt) 160 { 161 if (SeverityLevel.ERROR.equals(aEvt.getSeverityLevel())) { 162 mLogger.addError(aEvt); 163 mErrors++; 164 } 165 } 166 167 168 public void addException(AuditEvent aEvt, Throwable aThrowable) 169 { 170 mLogger.addException(aEvt, aThrowable); 171 mErrors++; 172 } 173 174 186 private String getValue(Properties aProperties, String aName, 187 String aDefaultValue) throws CheckstyleException 188 { 189 final String propertyName = "MailLogger." + aName; 190 String value = (String ) aProperties.get(propertyName); 191 192 if (value == null) { 193 value = aDefaultValue; 194 } 195 196 if (value == null) { 197 throw new CheckstyleException( 198 "Missing required parameter: " + propertyName); 199 } 200 201 return value; 202 } 203 213 private void sendMail(String aMailhost, String aFrom, String aToList, 214 String aSubject, String aText) 215 throws Exception 216 { 217 final Properties props = System.getProperties(); 219 220 props.put("mail.smtp.host", aMailhost); 222 223 final Session session = Session.getDefaultInstance(props, null); 225 226 final MimeMessage message = new MimeMessage (session); 228 229 message.setFrom(new InternetAddress (aFrom)); 230 final StringTokenizer t = new StringTokenizer (aToList, ", ", false); 231 while (t.hasMoreTokens()) { 232 message.addRecipient( 233 MimeMessage.RecipientType.TO, 234 new InternetAddress (t.nextToken())); 235 } 236 message.setSubject(aSubject); 237 message.setText(aText); 238 239 Transport.send(message); 240 } 241 } 242 | Popular Tags |