1 16 17 package org.springframework.mail; 18 19 import java.io.PrintStream ; 20 import java.io.PrintWriter ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 32 public class MailSendException extends MailException { 33 34 private Map failedMessages = new HashMap (); 35 36 37 41 public MailSendException(String msg) { 42 super(msg); 43 } 44 45 50 public MailSendException(String msg, Throwable cause) { 51 super(msg, cause); 52 } 53 54 60 public MailSendException(Map failedMessages) { 61 super(null); 62 this.failedMessages.putAll(failedMessages); 63 } 64 65 66 81 public final Map getFailedMessages() { 82 return failedMessages; 83 } 84 85 86 public String getMessage() { 87 StringBuffer sb = new StringBuffer (); 88 String superMsg = super.getMessage(); 89 sb.append(superMsg != null ? superMsg : "Failed messages: "); 90 for (Iterator subExs = getFailedMessages().values().iterator(); subExs.hasNext();) { 91 Throwable subEx = (Throwable ) subExs.next(); 92 sb.append(subEx.toString()); 93 if (subExs.hasNext()) { 94 sb.append("; "); 95 } 96 } 97 return sb.toString(); 98 } 99 100 public String toString() { 101 StringBuffer sb = new StringBuffer (); 102 sb.append(getClass().getName()).append("; nested exceptions ("); 103 sb.append(getFailedMessages().size()).append(") are:"); 104 int i = 0; 105 for (Iterator subExs = getFailedMessages().values().iterator(); subExs.hasNext();) { 106 Throwable subEx = (Throwable ) subExs.next(); 107 i++; 108 sb.append('\n').append("Failed message ").append(i).append(": "); 109 sb.append(subEx); 110 } 111 return sb.toString(); 112 } 113 114 public void printStackTrace(PrintStream ps) { 115 if (getFailedMessages().isEmpty()) { 116 super.printStackTrace(ps); 117 } 118 else { 119 ps.println(getClass().getName() + "; nested exception details (" + 120 getFailedMessages().size() + ") are:"); 121 int i = 0; 122 for (Iterator subExs = getFailedMessages().values().iterator(); subExs.hasNext();) { 123 Throwable subEx = (Throwable ) subExs.next(); 124 i++; 125 ps.println("Failed message " + i + ":"); 126 subEx.printStackTrace(ps); 127 } 128 } 129 } 130 131 public void printStackTrace(PrintWriter pw) { 132 if (getFailedMessages().isEmpty()) { 133 super.printStackTrace(pw); 134 } 135 else { 136 pw.println(getClass().getName() + "; nested exception details (" + 137 getFailedMessages().size() + ") are:"); 138 int i = 0; 139 for (Iterator subExs = getFailedMessages().values().iterator(); subExs.hasNext();) { 140 Throwable subEx = (Throwable ) subExs.next(); 141 i++; 142 pw.println("Failed message " + i + ":"); 143 subEx.printStackTrace(pw); 144 } 145 } 146 } 147 148 } 149 | Popular Tags |