1 54 package org.oddjob.dummy; 55 56 58 import java.io.File ; 59 import java.util.Enumeration ; 60 import java.util.StringTokenizer ; 61 import java.util.Vector ; 62 import org.apache.tools.ant.BuildException; 63 import org.apache.tools.ant.DirectoryScanner; 64 import org.apache.tools.ant.Project; 65 import org.apache.tools.ant.Task; 66 import org.apache.tools.ant.taskdefs.email.EmailAddress; 67 import org.apache.tools.ant.taskdefs.email.Message; 68 import org.apache.tools.ant.types.EnumeratedAttribute; 69 import org.apache.tools.ant.types.FileSet; 70 71 86 public class DummyEmailTask 87 extends Task { 88 89 public static final String AUTO = "auto"; 90 91 public static final String MIME = "mime"; 92 93 public static final String UU = "uu"; 94 95 public static final String PLAIN = "plain"; 96 97 98 101 public static class Encoding extends EnumeratedAttribute { 102 107 public String [] getValues() { 108 return new String [] {AUTO, MIME, UU, PLAIN}; 109 } 110 } 111 112 private String encoding = AUTO; 113 114 private String host = "localhost"; 115 private int port = 25; 116 117 private String subject = null; 118 119 private Message message = null; 120 121 private boolean failOnError = true; 122 private boolean includeFileNames = false; 123 private String messageMimeType = null; 124 125 126 private EmailAddress from = null; 127 128 private Vector replyToList = new Vector (); 129 130 private Vector toList = new Vector (); 131 132 private Vector ccList = new Vector (); 133 134 private Vector bccList = new Vector (); 135 136 137 private Vector files = new Vector (); 138 private Vector filesets = new Vector (); 139 140 private String charset = null; 141 142 private String user = null; 143 144 private String password = null; 145 146 private boolean SSL = false; 147 148 153 public void setUser(String user) { 154 this.user = user; 155 } 156 157 162 public void setPassword(String password) { 163 this.password = password; 164 } 165 166 171 public void setSSL(boolean SSL) { 172 this.SSL = SSL; 173 } 174 175 180 public void setEncoding(Encoding encoding) { 181 this.encoding = encoding.getValue(); 182 } 183 184 185 190 public void setMailport(int port) { 191 this.port = port; 192 } 193 194 195 200 public void setMailhost(String host) { 201 this.host = host; 202 } 203 204 205 210 public void setSubject(String subject) { 211 this.subject = subject; 212 } 213 214 215 220 public void setMessage(String message) { 221 if (this.message != null) { 222 throw new BuildException("Only one message can be sent in an " 223 + "email"); 224 } 225 226 this.message = new Message(message); 227 this.message.setProject(getProject()); 228 } 229 230 231 236 public void setMessageFile(File file) { 237 if (this.message != null) { 238 throw new BuildException("Only one message can be sent in an " 239 + "email"); 240 } 241 242 this.message = new Message(file); 243 this.message.setProject(getProject()); 244 } 245 246 247 253 public void setMessageMimeType(String type) { 254 this.messageMimeType = type; 255 } 256 257 258 264 public void addMessage(Message message) 265 throws BuildException { 266 if (this.message != null) { 267 throw new BuildException("Only one message can be sent in an " 268 + "email"); 269 } 270 271 this.message = message; 272 } 273 274 275 280 public void addFrom(EmailAddress address) { 281 if (this.from != null) { 282 throw new BuildException("Emails can only be from one address"); 283 } 284 285 this.from = address; 286 } 287 288 289 294 public void setFrom(String address) { 295 if (this.from != null) { 296 throw new BuildException("Emails can only be from one address"); 297 } 298 299 this.from = new EmailAddress(address); 300 } 301 302 303 309 public void addReplyTo(EmailAddress address) { 310 this.replyToList.add(address); 311 } 312 313 314 320 public void setReplyTo(String address) { 321 this.replyToList.add(new EmailAddress(address)); 322 } 323 324 325 330 public void addTo(EmailAddress address) { 331 toList.addElement(address); 332 } 333 334 335 340 public void setToList(String list) { 341 StringTokenizer tokens = new StringTokenizer (list, ","); 342 343 while (tokens.hasMoreTokens()) { 344 toList.addElement(new EmailAddress(tokens.nextToken())); 345 } 346 } 347 348 349 354 public void addCc(EmailAddress address) { 355 ccList.addElement(address); 356 } 357 358 359 364 public void setCcList(String list) { 365 StringTokenizer tokens = new StringTokenizer (list, ","); 366 367 while (tokens.hasMoreTokens()) { 368 ccList.addElement(new EmailAddress(tokens.nextToken())); 369 } 370 } 371 372 373 378 public void addBcc(EmailAddress address) { 379 bccList.addElement(address); 380 } 381 382 383 388 public void setBccList(String list) { 389 StringTokenizer tokens = new StringTokenizer (list, ","); 390 391 while (tokens.hasMoreTokens()) { 392 bccList.addElement(new EmailAddress(tokens.nextToken())); 393 } 394 } 395 396 397 402 public void setFailOnError(boolean failOnError) { 403 this.failOnError = failOnError; 404 } 405 406 407 412 public void setFiles(String filenames) { 413 StringTokenizer t = new StringTokenizer (filenames, ", "); 414 415 while (t.hasMoreTokens()) { 416 files.addElement(getProject().resolveFile(t.nextToken())); 417 } 418 } 419 420 421 426 public void addFileset(FileSet fs) { 427 filesets.addElement(fs); 428 } 429 430 431 437 public void setIncludefilenames(boolean includeFileNames) { 438 this.includeFileNames = includeFileNames; 439 } 440 441 442 447 public boolean getIncludeFileNames() { 448 return includeFileNames; 449 } 450 451 452 453 public void execute() { 454 Message savedMessage = message; 455 Vector savedFiles = (Vector ) files.clone(); 456 457 try { 458 459 if (message == null) { 461 message = new Message(); 462 message.setProject(getProject()); 463 } 464 465 if (from == null || from.getAddress() == null) { 467 throw new BuildException("A from element is required"); 468 } 469 470 if (toList.isEmpty() && ccList.isEmpty() && bccList.isEmpty()) { 472 throw new BuildException("At least one of to,cc or bcc must " 473 + "be supplied"); 474 } 475 476 if (messageMimeType != null) { 478 if (message.isMimeTypeSpecified()) { 479 throw new BuildException("The mime type can only be " 480 + "specified in one location"); 481 } else { 482 message.setMimeType(messageMimeType); 483 } 484 } 485 if (charset != null) { 487 if (message.getCharset() != null) { 488 throw new BuildException("The charset can only be " 489 + "specified in one location"); 490 } else { 491 message.setCharset(charset); 492 } 493 } 494 495 Enumeration e = filesets.elements(); 497 498 while (e.hasMoreElements()) { 499 FileSet fs = (FileSet) e.nextElement(); 500 501 DirectoryScanner ds = fs.getDirectoryScanner(getProject()); 502 String [] includedFiles = ds.getIncludedFiles(); 503 File baseDir = ds.getBasedir(); 504 505 for (int j = 0; j < includedFiles.length; ++j) { 506 File file = new File (baseDir, includedFiles[j]); 507 508 files.addElement(file); 509 } 510 } 511 512 log("Sending email: " + subject, Project.MSG_INFO); 514 log("From " + from, Project.MSG_VERBOSE); 515 log("ReplyTo " + replyToList, Project.MSG_VERBOSE); 516 log("To " + toList, Project.MSG_VERBOSE); 517 log("Cc " + ccList, Project.MSG_VERBOSE); 518 log("Bcc " + bccList, Project.MSG_VERBOSE); 519 520 int count = files.size(); 522 523 log("Sent email with " + count + " attachment" 524 + (count == 1 ? "" : "s"), Project.MSG_INFO); 525 } catch (BuildException e) { 526 log("Failed to send email", Project.MSG_WARN); 527 if (failOnError) { 528 throw e; 529 } 530 } catch (Exception e) { 531 log("Failed to send email", Project.MSG_WARN); 532 if (failOnError) { 533 throw new BuildException(e); 534 } 535 } finally { 536 message = savedMessage; 537 files = savedFiles; 538 } 539 } 540 546 public void setCharset(String charset) { 547 this.charset = charset; 548 } 549 555 public String getCharset() { 556 return charset; 557 } 558 } 559 560 | Popular Tags |