EMMA Coverage Report (generated Thu May 22 12:08:10 CDT 2014)
[all classes][org.springframework.batch.item.mail.javamail]

COVERAGE SUMMARY FOR SOURCE FILE [MimeMessageItemWriter.java]

nameclass, %method, %block, %line, %
MimeMessageItemWriter.java100% (1/1)80%  (4/5)86%  (55/64)88%  (14/16)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class MimeMessageItemWriter100% (1/1)80%  (4/5)86%  (55/64)88%  (14/16)
afterPropertiesSet (): void 0%   (0/1)0%   (0/9)0%   (0/2)
MimeMessageItemWriter (): void 100% (1/1)100% (8/8)100% (2/2)
setJavaMailSender (JavaMailSender): void 100% (1/1)100% (4/4)100% (2/2)
setMailErrorHandler (MailErrorHandler): void 100% (1/1)100% (4/4)100% (2/2)
write (List): void 100% (1/1)100% (39/39)100% (8/8)

1/*
2 * Copyright 2006-2010 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 */
16package org.springframework.batch.item.mail.javamail;
17 
18import java.util.List;
19import java.util.Map;
20import java.util.Map.Entry;
21 
22import javax.mail.internet.MimeMessage;
23 
24import org.springframework.batch.item.ItemWriter;
25import org.springframework.batch.item.mail.DefaultMailErrorHandler;
26import org.springframework.batch.item.mail.MailErrorHandler;
27import org.springframework.beans.factory.InitializingBean;
28import org.springframework.mail.MailException;
29import org.springframework.mail.MailSendException;
30import org.springframework.mail.javamail.JavaMailSender;
31import org.springframework.mail.javamail.MimeMailMessage;
32import org.springframework.util.Assert;
33 
34/**
35 * <p>
36 * A simple {@link ItemWriter} that can send mail messages. If it fails there is
37 * no guarantee about which of the messages were sent, but the ones that failed
38 * can be picked up in the error handler. Because the mail protocol is not
39 * transactional, failures should be dealt with here if possible rather than
40 * allowing them to be rethrown (which is the default).
41 * </p>
42 * 
43 * <p>
44 * Delegates the actual sending of messages to a {@link JavaMailSender}, using the
45 * batch method {@link JavaMailSender#send(MimeMessage[])}, which normally uses
46 * a single server connection for the whole batch (depending on the
47 * implementation). The efficiency of for large volumes of messages (repeated
48 * calls to the item writer) might be improved by the use of a special
49 * {@link JavaMailSender} that caches connections to the server in between
50 * calls.
51 * </p>
52 * 
53 * <p>
54 * Stateless, so automatically restartable.
55 * </p>
56 * 
57 * @author Dave Syer
58 * 
59 * @since 2.1
60 * 
61 */
62public class MimeMessageItemWriter implements ItemWriter<MimeMessage> {
63 
64        private JavaMailSender mailSender;
65 
66        private MailErrorHandler mailErrorHandler = new DefaultMailErrorHandler();
67 
68        /**
69         * A {@link JavaMailSender} to be used to send messages in {@link #write(List)}.
70         * 
71         * @param mailSender
72         */
73        public void setJavaMailSender(JavaMailSender mailSender) {
74                this.mailSender = mailSender;
75        }
76 
77        /**
78         * The handler for failed messages. Defaults to a
79         * {@link DefaultMailErrorHandler}.
80         * 
81         * @param mailErrorHandler the mail error handler to set
82         */
83        public void setMailErrorHandler(MailErrorHandler mailErrorHandler) {
84                this.mailErrorHandler = mailErrorHandler;
85        }
86 
87        /**
88         * Check mandatory properties (mailSender).
89         * 
90         * @throws IllegalStateException if the mandatory properties are not set
91         * 
92         * @see InitializingBean#afterPropertiesSet()
93         */
94        public void afterPropertiesSet() throws IllegalStateException {
95                Assert.state(mailSender != null, "A MailSender must be provided.");
96        }
97 
98        /**
99         * @param items the items to send
100         * @see ItemWriter#write(List)
101         */
102    @Override
103        public void write(List<? extends MimeMessage> items) throws MailException {
104                try {
105                        mailSender.send(items.toArray(new MimeMessage[items.size()]));
106                }
107                catch (MailSendException e) {
108                        @SuppressWarnings("unchecked")
109                        Map<Object, Exception> failedMessages = e.getFailedMessages();
110                        for (Entry<Object, Exception> entry : failedMessages.entrySet()) {
111                                mailErrorHandler.handle(new MimeMailMessage((MimeMessage)entry.getKey()), entry.getValue());
112                        }
113                }
114        }
115 
116}

[all classes][org.springframework.batch.item.mail.javamail]
EMMA 2.0.5312 (C) Vladimir Roubtsov