001/*
002 * Copyright 2013-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.ItemProcessListener;
021import org.springframework.lang.Nullable;
022import org.springframework.util.Assert;
023
024/**
025 * Wrapper class for {@link javax.batch.api.chunk.listener.ItemProcessListener}
026 *
027 * @author Michael Minella
028 * @author Mahmoud Ben Hassine
029 *
030 * @param <T> input type
031 * @param <S> output type
032 * @since 3.0
033 */
034public class ItemProcessListenerAdapter<T,S> implements ItemProcessListener<T, S> {
035
036        private javax.batch.api.chunk.listener.ItemProcessListener delegate;
037
038        /**
039         * @param delegate to be called within the batch lifecycle
040         */
041        public ItemProcessListenerAdapter(javax.batch.api.chunk.listener.ItemProcessListener delegate) {
042                Assert.notNull(delegate, "An ItemProcessListener is required");
043                this.delegate = delegate;
044        }
045
046        @Override
047        public void beforeProcess(T item) {
048                try {
049                        delegate.beforeProcess(item);
050                } catch (Exception e) {
051                        throw new BatchRuntimeException(e);
052                }
053        }
054
055        @Override
056        public void afterProcess(T item, @Nullable S result) {
057                try {
058                        delegate.afterProcess(item, result);
059                } catch (Exception e) {
060                        throw new BatchRuntimeException(e);
061                }
062        }
063
064        @Override
065        public void onProcessError(T item, Exception e) {
066                try {
067                        delegate.onProcessError(item, e);
068                } catch (Exception e1) {
069                        throw new BatchRuntimeException(e1);
070                }
071        }
072}