001/* 002 * Copyright 2013-2014 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.step.batchlet; 017 018import javax.batch.api.Batchlet; 019import javax.batch.operations.BatchRuntimeException; 020 021import org.springframework.batch.core.ExitStatus; 022import org.springframework.batch.core.StepContribution; 023import org.springframework.batch.core.scope.context.ChunkContext; 024import org.springframework.batch.core.step.tasklet.StoppableTasklet; 025import org.springframework.batch.repeat.RepeatStatus; 026import org.springframework.util.Assert; 027import org.springframework.util.StringUtils; 028 029/** 030 * 031 * @author Michael Minella 032 * @since 3.0 033 */ 034public class BatchletAdapter implements StoppableTasklet { 035 036 private Batchlet batchlet; 037 038 public BatchletAdapter(Batchlet batchlet) { 039 Assert.notNull(batchlet, "A Batchlet implementation is required"); 040 this.batchlet = batchlet; 041 } 042 043 @Override 044 public RepeatStatus execute(StepContribution contribution, 045 ChunkContext chunkContext) throws Exception { 046 String exitStatus; 047 try { 048 exitStatus = batchlet.process(); 049 } finally { 050 chunkContext.setComplete(); 051 } 052 053 if(StringUtils.hasText(exitStatus)) { 054 contribution.setExitStatus(new ExitStatus(exitStatus)); 055 } 056 057 058 return RepeatStatus.FINISHED; 059 } 060 061 @Override 062 public void stop() { 063 try { 064 batchlet.stop(); 065 } catch (Exception e) { 066 throw new BatchRuntimeException(e); 067 } 068 } 069}