001/* 002 * Copyright 2013 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.core.jsr; 017 018import java.util.List; 019 020import javax.batch.operations.BatchRuntimeException; 021 022import org.springframework.batch.core.ItemWriteListener; 023import org.springframework.batch.item.ItemWriter; 024import org.springframework.util.Assert; 025 026/** 027 * Wrapper class to adapt the {@link javax.batch.api.chunk.listener.ItemWriteListener} to 028 * a {@link ItemWriteListener}. 029 * 030 * @author Michael Minella 031 * 032 * @param <S> type to be written by the associated {@link ItemWriter} 033 * @since 3.0 034 */ 035public class ItemWriteListenerAdapter<S> implements ItemWriteListener<S> { 036 037 private javax.batch.api.chunk.listener.ItemWriteListener delegate; 038 039 public ItemWriteListenerAdapter(javax.batch.api.chunk.listener.ItemWriteListener delegate) { 040 Assert.notNull(delegate, "An ItemWriteListener is required"); 041 this.delegate = delegate; 042 } 043 044 @SuppressWarnings("unchecked") 045 @Override 046 public void beforeWrite(List<? extends S> items) { 047 try { 048 delegate.beforeWrite((List<Object>) items); 049 } catch (Exception e) { 050 throw new BatchRuntimeException(e); 051 } 052 } 053 054 @SuppressWarnings("unchecked") 055 @Override 056 public void afterWrite(List<? extends S> items) { 057 try { 058 delegate.afterWrite((List<Object>) items); 059 } catch (Exception e) { 060 throw new BatchRuntimeException(e); 061 } 062 } 063 064 @SuppressWarnings("unchecked") 065 @Override 066 public void onWriteError(Exception exception, List<? extends S> items) { 067 try { 068 delegate.onWriteError((List<Object>) items, exception); 069 } catch (Exception e) { 070 throw new BatchRuntimeException(e); 071 } 072 } 073}