View Javadoc

1   /*
2    * Copyright 2006-2008 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  package org.springframework.batch.core.step.item;
17  
18  import java.util.List;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.springframework.batch.core.listener.ItemListenerSupport;
23  
24  /**
25   * Default implementation of the {@link ItemListenerSupport} class that
26   * writes all exceptions via commons logging. Since generics can't be used to
27   * ensure the list contains exceptions, any non exceptions will be logged out by
28   * calling toString on the object.
29   * 
30   * @author Lucas Ward
31   * 
32   */
33  public class DefaultItemFailureHandler extends ItemListenerSupport<Object,Object> {
34  
35  	protected static final Log logger = LogFactory
36  			.getLog(DefaultItemFailureHandler.class);
37  
38  	@Override
39  	public void onReadError(Exception ex) {
40  		try {
41  			logger.error("Error encountered while reading", ex);
42  		} catch (Exception exception) {
43  			logger.error("Invalid type for logging: [" + exception.toString()
44  					+ "]");
45  		}
46  	}
47  
48  	@Override
49  	public void onWriteError(Exception ex, List<? extends Object> item) {
50  		try {
51  			logger.error("Error encountered while writing item: [ " + item + "]", ex);
52  		} catch (Exception exception) {
53  			logger.error("Invalid type for logging: [" + exception.toString()
54  					+ "]");
55  		}
56  	}
57  
58  }