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 java.util.Properties;
019import java.util.concurrent.atomic.AtomicBoolean;
020
021import javax.batch.runtime.BatchStatus;
022
023import org.springframework.batch.core.ExitStatus;
024import org.springframework.batch.core.JobExecution;
025import org.springframework.lang.Nullable;
026import org.springframework.util.Assert;
027
028/**
029 * Wrapper class to provide the {@link javax.batch.runtime.context.JobContext} functionality
030 * as specified in JSR-352.  Wrapper delegates to the underlying {@link JobExecution} to
031 * obtain the related contextual information.
032 *
033 * @author Michael Minella
034 * @author Chris Schaefer
035 * @author Mahmoud Ben Hassine
036 * @since 3.0
037 */
038public class JsrJobContext implements javax.batch.runtime.context.JobContext {
039        private Object transientUserData;
040        private Properties properties;
041        private JobExecution jobExecution;
042        private AtomicBoolean exitStatusSet = new AtomicBoolean();
043
044        public void setJobExecution(JobExecution jobExecution) {
045                Assert.notNull(jobExecution, "A JobExecution is required");
046                this.jobExecution = jobExecution;
047        }
048
049        public void setProperties(@Nullable Properties properties) {
050                this.properties = properties != null ? properties : new Properties();
051        }
052
053        /* (non-Javadoc)
054         * @see javax.batch.runtime.context.JobContext#getJobName()
055         */
056        @Override
057        public String getJobName() {
058                return jobExecution.getJobInstance().getJobName();
059        }
060
061        /* (non-Javadoc)
062         * @see javax.batch.runtime.context.JobContext#getTransientUserData()
063         */
064        @Override
065        public Object getTransientUserData() {
066                return transientUserData;
067        }
068
069        /* (non-Javadoc)
070         * @see javax.batch.runtime.context.JobContext#setTransientUserData(java.lang.Object)
071         */
072        @Override
073        public void setTransientUserData(Object data) {
074                transientUserData = data;
075        }
076
077        /* (non-Javadoc)
078         * @see javax.batch.runtime.context.JobContext#getInstanceId()
079         */
080        @Override
081        public long getInstanceId() {
082                return jobExecution.getJobInstance().getId();
083        }
084
085        /* (non-Javadoc)
086         * @see javax.batch.runtime.context.JobContext#getExecutionId()
087         */
088        @Override
089        public long getExecutionId() {
090                return jobExecution.getId();
091        }
092
093        /* (non-Javadoc)
094         * @see javax.batch.runtime.context.JobContext#getProperties()
095         */
096        @Override
097        public Properties getProperties() {
098                return properties;
099        }
100
101        /* (non-Javadoc)
102         * @see javax.batch.runtime.context.JobContext#getBatchStatus()
103         */
104        @Override
105        public BatchStatus getBatchStatus() {
106                return jobExecution.getStatus().getBatchStatus();
107        }
108
109        /* (non-Javadoc)
110         * @see javax.batch.runtime.context.JobContext#getExitStatus()
111         */
112        @Override
113        @Nullable
114        public String getExitStatus() {
115                return exitStatusSet.get() ? jobExecution.getExitStatus().getExitCode() : null;
116        }
117
118        /* (non-Javadoc)
119         * @see javax.batch.runtime.context.JobContext#setExitStatus(java.lang.String)
120         */
121        @Override
122        public void setExitStatus(String status) {
123                jobExecution.setExitStatus(new ExitStatus(status));
124                exitStatusSet.set(true);
125        }
126}