StepContext
StepContext
StepContext源代码详解
从StepContext的源代码看,StepContext的构造方法中有StepExecution。
package org.springframework.batch.core.scope.context;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.UnexpectedJobExecutionException;
import org.springframework.batch.repeat.context.SynchronizedAttributeAccessor;
import org.springframework.util.Assert;
public class StepContext extends SynchronizedAttributeAccessor {
private StepExecution stepExecution;
private Map<String, Set<Runnable>> callbacks = new HashMap();
public StepContext(StepExecution stepExecution) {
Assert.notNull(stepExecution, "A StepContext must have a non-null StepExecution");
this.stepExecution = stepExecution;
}
public String getStepName() {
return this.stepExecution.getStepName();
}
public String getJobName() {
Assert.state(this.stepExecution.getJobExecution() != null, "StepExecution does not have a JobExecution");
Assert.state(this.stepExecution.getJobExecution().getJobInstance() != null, "StepExecution does not have a JobInstance");
return this.stepExecution.getJobExecution().getJobInstance().getJobName();
}
public Properties getSystemProperties() {
return System.getProperties();
}
public Map<String, Object> getStepExecutionContext() {
Map<String, Object> result = new HashMap();
Iterator i$ = this.stepExecution.getExecutionContext().entrySet().iterator();
while(i$.hasNext()) {
Map.Entry<String, Object> entry = (Map.Entry)i$.next();
result.put(entry.getKey(), entry.getValue());
}
return Collections.unmodifiableMap(result);
}
public Map<String, Object> getJobExecutionContext() {
Map<String, Object> result = new HashMap();
Iterator i$ = this.stepExecution.getJobExecution().getExecutionContext().entrySet().iterator();
while(i$.hasNext()) {
Map.Entry<String, Object> entry = (Map.Entry)i$.next();
result.put(entry.getKey(), entry.getValue());
}
return Collections.unmodifiableMap(result);
}
public Map<String, Object> getJobParameters() {
Map<String, Object> result = new HashMap();
Iterator i$ = this.stepExecution.getJobParameters().getParameters().entrySet().iterator();
while(i$.hasNext()) {
Map.Entry<String, JobParameter> entry = (Map.Entry)i$.next();
result.put(entry.getKey(), ((JobParameter)entry.getValue()).getValue());
}
return Collections.unmodifiableMap(result);
}
public void registerDestructionCallback(String name, Runnable callback) {
synchronized(this.callbacks) {
Set<Runnable> set = (Set)this.callbacks.get(name);
if (set == null) {
set = new HashSet();
this.callbacks.put(name, set);
}
((Set)set).add(callback);
}
}
private void unregisterDestructionCallbacks(String name) {
synchronized(this.callbacks) {
this.callbacks.remove(name);
}
}
public Object removeAttribute(String name) {
this.unregisterDestructionCallbacks(name);
return super.removeAttribute(name);
}
public void close() {
List<Exception> errors = new ArrayList();
Map<String, Set<Runnable>> copy = Collections.unmodifiableMap(this.callbacks);
Iterator i$ = copy.entrySet().iterator();
while(i$.hasNext()) {
Map.Entry<String, Set<Runnable>> entry = (Map.Entry)i$.next();
Set<Runnable> set = (Set)entry.getValue();
Iterator i$ = set.iterator();
while(i$.hasNext()) {
Runnable callback = (Runnable)i$.next();
if (callback != null) {
try {
callback.run();
} catch (RuntimeException var9) {
errors.add(var9);
}
}
}
}
if (!errors.isEmpty()) {
Exception error = (Exception)errors.get(0);
if (error instanceof RuntimeException) {
throw (RuntimeException)error;
} else {
throw new UnexpectedJobExecutionException("Could not close step context, rethrowing first of " + errors.size() + " exceptions.", error);
}
}
}
public StepExecution getStepExecution() {
return this.stepExecution;
}
public String getId() {
Assert.state(this.stepExecution.getId() != null, "StepExecution has no id. It must be saved before it can be used in step scope.");
return "execution#" + this.stepExecution.getId();
}
public boolean equals(Object other) {
if (!(other instanceof StepContext)) {
return false;
} else if (other == this) {
return true;
} else {
StepContext context = (StepContext)other;
return context.stepExecution == this.stepExecution ? true : this.stepExecution.equals(context.stepExecution);
}
}
public int hashCode() {
return this.stepExecution.hashCode();
}
public String toString() {
return super.toString() + ", stepExecutionContext=" + this.getStepExecutionContext() + ", jobExecutionContext=" + this.getJobExecutionContext() + ", jobParameters=" + this.getJobParameters();
}
}