001/*
002 * Copyright 2006-2014 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 *      https://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 */
016package org.springframework.batch.sample.domain.mail.internal;
017
018import java.util.ArrayList;
019import java.util.List;
020
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.springframework.batch.item.mail.MailErrorHandler;
025import org.springframework.mail.MailMessage;
026
027/**
028 * This handler prints out failed messages with their exceptions. It also
029 * maintains a list of all failed messages it receives for lookup later by an
030 * assertion.
031 * 
032 * @author Dan Garrette
033 * @author Dave Syer
034 * 
035 * @since 2.1
036 */
037public class TestMailErrorHandler implements MailErrorHandler {
038        private static final Log LOGGER = LogFactory.getLog(TestMailErrorHandler.class);
039
040        private List<MailMessage> failedMessages = new ArrayList<MailMessage>();
041
042        @Override
043        public void handle(MailMessage failedMessage, Exception ex) {
044                this.failedMessages.add(failedMessage);
045                LOGGER.error("Mail message failed: " + failedMessage, ex);
046        }
047
048        public List<MailMessage> getFailedMessages() {
049                return failedMessages;
050        }
051
052        public void clear() {
053                this.failedMessages.clear();
054        }
055}