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.JobListener;
019import javax.batch.operations.BatchRuntimeException;
020
021import org.springframework.batch.core.JobExecution;
022import org.springframework.batch.core.JobExecutionListener;
023import org.springframework.util.Assert;
024
025/**
026 * Wrapper class to adapt the {@link JobListener} to
027 * a {@link JobExecutionListener}.
028 *
029 * @author Michael Minella
030 * @since 3.0
031 */
032public class JobListenerAdapter implements JobExecutionListener {
033
034        private JobListener delegate;
035
036        /**
037         * @param delegate to be delegated to
038         */
039        public JobListenerAdapter(JobListener delegate) {
040                Assert.notNull(delegate, "Delegate is required");
041                this.delegate = delegate;
042        }
043
044        @Override
045        public void beforeJob(JobExecution jobExecution) {
046                try {
047                        delegate.beforeJob();
048                } catch (Exception e) {
049                        throw new BatchRuntimeException(e);
050                }
051        }
052
053        @Override
054        public void afterJob(JobExecution jobExecution) {
055                try {
056                        delegate.afterJob();
057                } catch (Exception e) {
058                        throw new BatchRuntimeException(e);
059                }
060        }
061}