KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > mail > MailSendException


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.mail;
18
19 import java.io.PrintStream JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 /**
26  * Exception thrown when a mail sending error is encountered.
27  * Can register failed messages with their exceptions.
28  *
29  * @author Dmitriy Kopylenko
30  * @author Juergen Hoeller
31  */

32 public class MailSendException extends MailException {
33
34     private Map JavaDoc failedMessages = new HashMap JavaDoc();
35
36
37     /**
38      * Constructor for MailSendException.
39      * @param msg the detail message
40      */

41     public MailSendException(String JavaDoc msg) {
42         super(msg);
43     }
44
45     /**
46      * Constructor for MailSendException.
47      * @param msg the detail message
48      * @param cause the root cause from the mail API in use
49      */

50     public MailSendException(String JavaDoc msg, Throwable JavaDoc cause) {
51         super(msg, cause);
52     }
53
54     /**
55      * Constructor for registration of failed messages, with the
56      * messages that failed as keys, and the thrown exceptions as values.
57      * <p>The messages should be the same that were originally passed
58      * to the invoked send method.
59      */

60     public MailSendException(Map JavaDoc failedMessages) {
61         super(null);
62         this.failedMessages.putAll(failedMessages);
63     }
64
65
66     /**
67      * Return a Map with the failed messages as keys, and the thrown exceptions
68      * as values. Note that a general mail server connection failure will not
69      * result in failed messages being returned here: A message will only be
70      * contained here if actually sending it was attempted but failed.
71      * <p>The messages will be the same that were originally passed to the
72      * invoked send method, that is, SimpleMailMessages in case of using
73      * the generic MailSender interface.
74      * <p>In case of sending MimeMessage instances via JavaMailSender,
75      * the messages will be of type MimeMessage.
76      * @return the Map of failed messages as keys and thrown exceptions as
77      * values, or an empty Map if no failed messages
78      * @see SimpleMailMessage
79      * @see javax.mail.internet.MimeMessage
80      */

81     public final Map JavaDoc getFailedMessages() {
82         return failedMessages;
83     }
84
85
86     public String JavaDoc getMessage() {
87         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
88         String JavaDoc superMsg = super.getMessage();
89         sb.append(superMsg != null ? superMsg : "Failed messages: ");
90         for (Iterator JavaDoc subExs = getFailedMessages().values().iterator(); subExs.hasNext();) {
91             Throwable JavaDoc subEx = (Throwable JavaDoc) 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 JavaDoc toString() {
101         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
102         sb.append(getClass().getName()).append("; nested exceptions (");
103         sb.append(getFailedMessages().size()).append(") are:");
104         int i = 0;
105         for (Iterator JavaDoc subExs = getFailedMessages().values().iterator(); subExs.hasNext();) {
106             Throwable JavaDoc subEx = (Throwable JavaDoc) 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 JavaDoc 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 JavaDoc subExs = getFailedMessages().values().iterator(); subExs.hasNext();) {
123                 Throwable JavaDoc subEx = (Throwable JavaDoc) subExs.next();
124                 i++;
125                 ps.println("Failed message " + i + ":");
126                 subEx.printStackTrace(ps);
127             }
128         }
129     }
130
131     public void printStackTrace(PrintWriter JavaDoc 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 JavaDoc subExs = getFailedMessages().values().iterator(); subExs.hasNext();) {
140                 Throwable JavaDoc subEx = (Throwable JavaDoc) subExs.next();
141                 i++;
142                 pw.println("Failed message " + i + ":");
143                 subEx.printStackTrace(pw);
144             }
145         }
146     }
147
148 }
149
Popular Tags