001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.mail;
018:
019: import java.io.PrintStream;
020: import java.io.PrintWriter;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: /**
026: * Exception thrown when a mail sending error is encountered.
027: * Can register failed messages with their exceptions.
028: *
029: * @author Dmitriy Kopylenko
030: * @author Juergen Hoeller
031: */
032: public class MailSendException extends MailException {
033:
034: private Map failedMessages = new HashMap();
035:
036: /**
037: * Constructor for MailSendException.
038: * @param msg the detail message
039: */
040: public MailSendException(String msg) {
041: super (msg);
042: }
043:
044: /**
045: * Constructor for MailSendException.
046: * @param msg the detail message
047: * @param cause the root cause from the mail API in use
048: */
049: public MailSendException(String msg, Throwable cause) {
050: super (msg, cause);
051: }
052:
053: /**
054: * Constructor for registration of failed messages, with the
055: * messages that failed as keys, and the thrown exceptions as values.
056: * <p>The messages should be the same that were originally passed
057: * to the invoked send method.
058: */
059: public MailSendException(Map failedMessages) {
060: super (null);
061: this .failedMessages.putAll(failedMessages);
062: }
063:
064: /**
065: * Return a Map with the failed messages as keys, and the thrown exceptions
066: * as values. Note that a general mail server connection failure will not
067: * result in failed messages being returned here: A message will only be
068: * contained here if actually sending it was attempted but failed.
069: * <p>The messages will be the same that were originally passed to the
070: * invoked send method, that is, SimpleMailMessages in case of using
071: * the generic MailSender interface.
072: * <p>In case of sending MimeMessage instances via JavaMailSender,
073: * the messages will be of type MimeMessage.
074: * @return the Map of failed messages as keys and thrown exceptions as
075: * values, or an empty Map if no failed messages
076: * @see SimpleMailMessage
077: * @see javax.mail.internet.MimeMessage
078: */
079: public final Map getFailedMessages() {
080: return failedMessages;
081: }
082:
083: public String getMessage() {
084: StringBuffer sb = new StringBuffer();
085: String super Msg = super .getMessage();
086: sb.append(super Msg != null ? super Msg : "Failed messages: ");
087: for (Iterator subExs = getFailedMessages().values().iterator(); subExs
088: .hasNext();) {
089: Throwable subEx = (Throwable) subExs.next();
090: sb.append(subEx.toString());
091: if (subExs.hasNext()) {
092: sb.append("; ");
093: }
094: }
095: return sb.toString();
096: }
097:
098: public String toString() {
099: StringBuffer sb = new StringBuffer();
100: sb.append(getClass().getName()).append("; nested exceptions (");
101: sb.append(getFailedMessages().size()).append(") are:");
102: int i = 0;
103: for (Iterator subExs = getFailedMessages().values().iterator(); subExs
104: .hasNext();) {
105: Throwable subEx = (Throwable) subExs.next();
106: i++;
107: sb.append('\n').append("Failed message ").append(i).append(
108: ": ");
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: } else {
118: ps.println(getClass().getName()
119: + "; nested exception details ("
120: + getFailedMessages().size() + ") are:");
121: int i = 0;
122: for (Iterator subExs = getFailedMessages().values()
123: .iterator(); subExs.hasNext();) {
124: Throwable subEx = (Throwable) subExs.next();
125: i++;
126: ps.println("Failed message " + i + ":");
127: subEx.printStackTrace(ps);
128: }
129: }
130: }
131:
132: public void printStackTrace(PrintWriter pw) {
133: if (getFailedMessages().isEmpty()) {
134: super .printStackTrace(pw);
135: } else {
136: pw.println(getClass().getName()
137: + "; nested exception details ("
138: + getFailedMessages().size() + ") are:");
139: int i = 0;
140: for (Iterator subExs = getFailedMessages().values()
141: .iterator(); subExs.hasNext();) {
142: Throwable subEx = (Throwable) subExs.next();
143: i++;
144: pw.println("Failed message " + i + ":");
145: subEx.printStackTrace(pw);
146: }
147: }
148: }
149:
150: }
|