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 javax.batch.api.listener.StepListener;
019import javax.batch.operations.BatchRuntimeException;
020
021import org.springframework.batch.core.ExitStatus;
022import org.springframework.batch.core.StepExecution;
023import org.springframework.batch.core.StepExecutionListener;
024import org.springframework.util.Assert;
025
026/**
027 * Wrapper class to adapt the {@link StepListener} to
028 * a {@link StepExecutionListener}.
029 * 
030 * @author Michael Minella
031 * @since 3.0
032 */
033public class StepListenerAdapter implements StepExecutionListener {
034
035        private final StepListener delegate;
036
037        /**
038         * @param delegate instance of {@link StepListener}.
039         */
040        public StepListenerAdapter(StepListener delegate) {
041                Assert.notNull(delegate, "A listener is required");
042                this.delegate = delegate;
043        }
044
045        @Override
046        public void beforeStep(StepExecution stepExecution) {
047                try {
048                        delegate.beforeStep();
049                } catch (Exception e) {
050                        throw new BatchRuntimeException(e);
051                }
052        }
053
054        @Override
055        public ExitStatus afterStep(StepExecution stepExecution) {
056                try {
057                        delegate.afterStep();
058                } catch (Exception e) {
059                        throw new BatchRuntimeException(e);
060                }
061
062                return stepExecution.getExitStatus();
063        }
064}