001/*
002 * Copyright 2018 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 javax.batch.operations.BatchRuntimeException;
019
020import org.springframework.batch.core.ItemReadListener;
021import org.springframework.batch.item.ItemReader;
022import org.springframework.util.Assert;
023
024/**
025 * Wrapper class to adapt the {@link javax.batch.api.chunk.listener.ItemReadListener} to
026 * a {@link ItemReadListener}.
027 * 
028 * @author Michael Minella
029 * @author Mahmoud Ben Hassine
030 *
031 * @param <T> type to be returned via a read on the associated {@link ItemReader}
032 * @since 3.0
033 */
034public class ItemReadListenerAdapter<T> implements ItemReadListener<T> {
035
036        private javax.batch.api.chunk.listener.ItemReadListener delegate;
037
038        public ItemReadListenerAdapter(javax.batch.api.chunk.listener.ItemReadListener delegate) {
039                Assert.notNull(delegate, "An ItemReadListener is required");
040                this.delegate = delegate;
041        }
042
043        @Override
044        public void beforeRead() {
045                try {
046                        delegate.beforeRead();
047                } catch (Exception e) {
048                        throw new BatchRuntimeException(e);
049                }
050        }
051
052        @Override
053        public void afterRead(T item) {
054                try {
055                        delegate.afterRead(item);
056                } catch (Exception e) {
057                        throw new BatchRuntimeException(e);
058                }
059        }
060
061        @Override
062        public void onReadError(Exception ex) {
063                try {
064                        delegate.onReadError(ex);
065                } catch (Exception e) {
066                        throw new BatchRuntimeException(e);
067                }
068        }
069}