1 18 package org.apache.tools.ant.listener; 19 20 import java.io.FileInputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.PrintStream ; 24 import java.util.Hashtable ; 25 import java.util.Vector ; 26 import java.util.Properties ; 27 import java.util.Enumeration ; 28 import java.util.StringTokenizer ; 29 30 import org.apache.tools.ant.BuildEvent; 31 import org.apache.tools.ant.BuildException; 32 import org.apache.tools.ant.DefaultLogger; 33 import org.apache.tools.ant.Project; 34 import org.apache.tools.ant.taskdefs.email.EmailAddress; 35 import org.apache.tools.ant.taskdefs.email.Message; 36 import org.apache.tools.ant.taskdefs.email.Mailer; 37 import org.apache.tools.ant.util.ClasspathUtils; 38 import org.apache.tools.ant.util.DateUtils; 39 import org.apache.tools.ant.util.StringUtils; 40 import org.apache.tools.mail.MailMessage; 41 42 69 public class MailLogger extends DefaultLogger { 70 71 private StringBuffer buffer = new StringBuffer (); 72 73 78 public void buildFinished(BuildEvent event) { 79 super.buildFinished(event); 80 81 Project project = event.getProject(); 82 Hashtable properties = project.getProperties(); 83 84 Properties fileProperties = new Properties (); 87 String filename = (String ) properties.get("MailLogger.properties.file"); 88 if (filename != null) { 89 InputStream is = null; 90 try { 91 is = new FileInputStream (filename); 92 fileProperties.load(is); 93 } catch (IOException ioe) { 94 } finally { 96 if (is != null) { 97 try { 98 is.close(); 99 } catch (IOException e) { 100 } 102 } 103 } 104 } 105 106 for (Enumeration e = fileProperties.keys(); e.hasMoreElements();) { 107 String key = (String ) e.nextElement(); 108 String value = fileProperties.getProperty(key); 109 properties.put(key, project.replaceProperties(value)); 110 } 111 112 boolean success = (event.getException() == null); 113 String prefix = success ? "success" : "failure"; 114 115 try { 116 boolean notify = Project.toBoolean(getValue(properties, 117 prefix + ".notify", "on")); 118 119 if (!notify) { 120 return; 121 } 122 123 String mailhost = getValue(properties, "mailhost", "localhost"); 124 int port = Integer.parseInt(getValue(properties, "port", 125 String.valueOf(MailMessage.DEFAULT_PORT))); 126 String user = getValue(properties, "user", ""); 127 String password = getValue(properties, "password", ""); 128 boolean ssl = Project.toBoolean(getValue(properties, 129 "ssl", "off")); 130 String from = getValue(properties, "from", null); 131 String replytoList = getValue(properties, "replyto", ""); 132 String toList = getValue(properties, prefix + ".to", null); 133 String subject = getValue(properties, prefix + ".subject", 134 (success) ? "Build Success" : "Build Failure"); 135 if (user.equals("") && password.equals("") && !ssl) { 136 sendMail(mailhost, port, from, replytoList, toList, 137 subject, buffer.substring(0)); 138 } else { 139 sendMimeMail(event.getProject(), mailhost, port, user, 140 password, ssl, from, replytoList, toList, 141 subject, buffer.substring(0)); 142 } 143 } catch (Exception e) { 144 System.out.println("MailLogger failed to send e-mail!"); 145 e.printStackTrace(System.err); 146 } 147 } 148 149 150 155 protected void log(String message) { 156 buffer.append(message).append(StringUtils.LINE_SEP); 157 } 158 159 160 172 private String getValue(Hashtable properties, String name, 173 String defaultValue) throws Exception { 174 String propertyName = "MailLogger." + name; 175 String value = (String ) properties.get(propertyName); 176 177 if (value == null) { 178 value = defaultValue; 179 } 180 181 if (value == null) { 182 throw new Exception ("Missing required parameter: " + propertyName); 183 } 184 185 return value; 186 } 187 188 189 200 private void sendMail(String mailhost, int port, String from, String replyToList, String toList, 201 String subject, String message) throws IOException { 202 MailMessage mailMessage = new MailMessage(mailhost, port); 203 mailMessage.setHeader("Date", DateUtils.getDateForHeader()); 204 205 mailMessage.from(from); 206 if (!replyToList.equals("")) { 207 StringTokenizer t = new StringTokenizer (replyToList, ", ", false); 208 while (t.hasMoreTokens()) { 209 mailMessage.replyto(t.nextToken()); 210 } 211 } 212 StringTokenizer t = new StringTokenizer (toList, ", ", false); 213 while (t.hasMoreTokens()) { 214 mailMessage.to(t.nextToken()); 215 } 216 217 mailMessage.setSubject(subject); 218 219 PrintStream ps = mailMessage.getPrintStream(); 220 ps.println(message); 221 222 mailMessage.sendAndClose(); 223 } 224 238 private void sendMimeMail(Project project, String host, int port, 239 String user, String password, boolean ssl, 240 String from, String replyToString, 241 String toString, String subject, 242 String message) { 243 Mailer mailer = null; 245 try { 246 mailer = (Mailer) ClasspathUtils.newInstance( 247 "org.apache.tools.ant.taskdefs.email.MimeMailer", 248 MailLogger.class.getClassLoader(), Mailer.class); 249 } catch (BuildException e) { 250 Throwable t = e.getCause() == null ? e : e.getCause(); 251 log("Failed to initialise MIME mail: " + t.getMessage()); 252 return; 253 } 254 Vector replyToList = vectorizeEmailAddresses(replyToString); 255 mailer.setHost(host); 256 mailer.setPort(port); 257 mailer.setUser(user); 258 mailer.setPassword(password); 259 mailer.setSSL(ssl); 260 Message mymessage = new Message(message); 261 mymessage.setProject(project); 262 mailer.setMessage(mymessage); 263 mailer.setFrom(new EmailAddress(from)); 264 mailer.setReplyToList(replyToList); 265 Vector toList = vectorizeEmailAddresses(toString); 266 mailer.setToList(toList); 267 mailer.setCcList(new Vector ()); 268 mailer.setBccList(new Vector ()); 269 mailer.setFiles(new Vector ()); 270 mailer.setSubject(subject); 271 mailer.send(); 272 } 273 private Vector vectorizeEmailAddresses(String listString) { 274 Vector emailList = new Vector (); 275 StringTokenizer tokens = new StringTokenizer (listString, ","); 276 while (tokens.hasMoreTokens()) { 277 emailList.addElement(new EmailAddress(tokens.nextToken())); 278 } 279 return emailList; 280 } 281 } 282 283 284 | Popular Tags |