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 java.io.Serializable; 019import java.util.Date; 020 021import javax.batch.runtime.BatchStatus; 022import javax.batch.runtime.Metric; 023 024import org.springframework.batch.core.ExitStatus; 025import org.springframework.batch.item.util.ExecutionContextUserSupport; 026import org.springframework.util.Assert; 027import org.springframework.util.ClassUtils; 028 029/** 030 * Implementation of the JsrStepExecution as defined in JSR-352. This implementation 031 * wraps a {@link org.springframework.batch.core.StepExecution} as it's source of 032 * data. 033 * 034 * @author Michael Minella 035 * @since 3.0 036 */ 037public class JsrStepExecution implements javax.batch.runtime.StepExecution{ 038 039 private final static String PERSISTENT_USER_DATA_KEY = "batch_jsr_persistentUserData"; 040 private final org.springframework.batch.core.StepExecution stepExecution; 041 // The API for the persistent user data is handled by the JsrStepContext which is why the name here is based on the JsrStepContext. 042 private final ExecutionContextUserSupport executionContextUserSupport = new ExecutionContextUserSupport(ClassUtils.getShortName(JsrStepContext.class)); 043 044 /** 045 * @param stepExecution The {@link org.springframework.batch.core.StepExecution} used 046 * as the basis for the data. 047 */ 048 public JsrStepExecution(org.springframework.batch.core.StepExecution stepExecution) { 049 Assert.notNull(stepExecution, "A StepExecution is required"); 050 051 this.stepExecution = stepExecution; 052 } 053 054 /* (non-Javadoc) 055 * @see javax.batch.runtime.JsrStepExecution#getStepExecutionId() 056 */ 057 @Override 058 public long getStepExecutionId() { 059 return stepExecution.getId(); 060 } 061 062 /* (non-Javadoc) 063 * @see javax.batch.runtime.JsrStepExecution#getStepName() 064 */ 065 @Override 066 public String getStepName() { 067 return stepExecution.getStepName(); 068 } 069 070 /* (non-Javadoc) 071 * @see javax.batch.runtime.JsrStepExecution#getBatchStatus() 072 */ 073 @Override 074 public BatchStatus getBatchStatus() { 075 return stepExecution.getStatus().getBatchStatus(); 076 } 077 078 /* (non-Javadoc) 079 * @see javax.batch.runtime.JsrStepExecution#getStartTime() 080 */ 081 @Override 082 public Date getStartTime() { 083 return stepExecution.getStartTime(); 084 } 085 086 /* (non-Javadoc) 087 * @see javax.batch.runtime.JsrStepExecution#getEndTime() 088 */ 089 @Override 090 public Date getEndTime() { 091 return stepExecution.getEndTime(); 092 } 093 094 /* (non-Javadoc) 095 * @see javax.batch.runtime.JsrStepExecution#getExitStatus() 096 */ 097 @Override 098 public String getExitStatus() { 099 ExitStatus status = stepExecution.getExitStatus(); 100 101 if(status == null) { 102 return null; 103 } else { 104 return status.getExitCode(); 105 } 106 } 107 108 /* (non-Javadoc) 109 * @see javax.batch.runtime.JsrStepExecution#getPersistentUserData() 110 */ 111 @Override 112 public Serializable getPersistentUserData() { 113 return (Serializable) stepExecution.getExecutionContext().get(executionContextUserSupport.getKey(PERSISTENT_USER_DATA_KEY)); 114 } 115 116 /* (non-Javadoc) 117 * @see javax.batch.runtime.JsrStepExecution#getMetrics() 118 */ 119 @Override 120 public Metric[] getMetrics() { 121 Metric[] metrics = new Metric[8]; 122 123 metrics[0] = new SimpleMetric(javax.batch.runtime.Metric.MetricType.COMMIT_COUNT, stepExecution.getCommitCount()); 124 metrics[1] = new SimpleMetric(javax.batch.runtime.Metric.MetricType.FILTER_COUNT, stepExecution.getFilterCount()); 125 metrics[2] = new SimpleMetric(javax.batch.runtime.Metric.MetricType.PROCESS_SKIP_COUNT, stepExecution.getProcessSkipCount()); 126 metrics[3] = new SimpleMetric(javax.batch.runtime.Metric.MetricType.READ_COUNT, stepExecution.getReadCount()); 127 metrics[4] = new SimpleMetric(javax.batch.runtime.Metric.MetricType.READ_SKIP_COUNT, stepExecution.getReadSkipCount()); 128 metrics[5] = new SimpleMetric(javax.batch.runtime.Metric.MetricType.ROLLBACK_COUNT, stepExecution.getRollbackCount()); 129 metrics[6] = new SimpleMetric(javax.batch.runtime.Metric.MetricType.WRITE_COUNT, stepExecution.getWriteCount()); 130 metrics[7] = new SimpleMetric(javax.batch.runtime.Metric.MetricType.WRITE_SKIP_COUNT, stepExecution.getWriteSkipCount()); 131 132 return metrics; 133 } 134}