| 1 | package org.springframework.batch.core.configuration.xml; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.Collections; |
| 6 | import java.util.List; |
| 7 | |
| 8 | import org.springframework.batch.core.job.flow.Flow; |
| 9 | import org.springframework.batch.core.job.flow.FlowExecutionStatus; |
| 10 | import org.springframework.batch.core.job.flow.FlowExecutor; |
| 11 | import org.springframework.batch.core.job.flow.FlowHolder; |
| 12 | import org.springframework.batch.core.job.flow.State; |
| 13 | import org.springframework.batch.core.job.flow.support.SimpleFlow; |
| 14 | import org.springframework.batch.core.job.flow.support.StateTransition; |
| 15 | import org.springframework.batch.core.job.flow.support.state.AbstractState; |
| 16 | import org.springframework.batch.core.job.flow.support.state.StepState; |
| 17 | import org.springframework.beans.factory.FactoryBean; |
| 18 | import org.springframework.beans.factory.InitializingBean; |
| 19 | import org.springframework.util.Assert; |
| 20 | |
| 21 | /** |
| 22 | * Convenience factory for SimpleFlow instances for use in XML namespace. It |
| 23 | * replaces the states in the input with proxies that have a unique name formed |
| 24 | * from the flow name and the original state name (unless the name is already in |
| 25 | * that form, in which case it is not modified). |
| 26 | * |
| 27 | * @author Dave Syer |
| 28 | * |
| 29 | */ |
| 30 | public class SimpleFlowFactoryBean implements FactoryBean, InitializingBean { |
| 31 | |
| 32 | private String name; |
| 33 | |
| 34 | private List<StateTransition> stateTransitions; |
| 35 | |
| 36 | private String prefix; |
| 37 | |
| 38 | /** |
| 39 | * The name of the flow that is created by this factory. |
| 40 | * |
| 41 | * @param name the value of the name |
| 42 | */ |
| 43 | public void setName(String name) { |
| 44 | this.name = name; |
| 45 | this.prefix = name + "."; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * The raw state transitions for the flow. They will be transformed into |
| 50 | * proxies that have the same behaviour but unique names prefixed with the |
| 51 | * flow name. |
| 52 | * |
| 53 | * @param name the value of the name |
| 54 | */ |
| 55 | public void setStateTransitions(List<StateTransition> stateTransitions) { |
| 56 | this.stateTransitions = stateTransitions; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Check mandatory properties (name). |
| 61 | * |
| 62 | * @throws Exception |
| 63 | */ |
| 64 | public void afterPropertiesSet() throws Exception { |
| 65 | Assert.hasText(name, "The flow must have a name"); |
| 66 | } |
| 67 | |
| 68 | public Object getObject() throws Exception { |
| 69 | |
| 70 | SimpleFlow flow = new SimpleFlow(name); |
| 71 | |
| 72 | List<StateTransition> updatedTransitions = new ArrayList<StateTransition>(); |
| 73 | for (StateTransition stateTransition : stateTransitions) { |
| 74 | State state = getProxyState(stateTransition.getState()); |
| 75 | updatedTransitions.add(StateTransition.switchOriginAndDestination(stateTransition, state, |
| 76 | getNext(stateTransition.getNext()))); |
| 77 | } |
| 78 | |
| 79 | flow.setStateTransitions(updatedTransitions); |
| 80 | flow.afterPropertiesSet(); |
| 81 | return flow; |
| 82 | |
| 83 | } |
| 84 | |
| 85 | private String getNext(String next) { |
| 86 | if (next == null) { |
| 87 | return null; |
| 88 | } |
| 89 | return (next.startsWith(this.prefix) ? "" : this.prefix) + next; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Convenience method to get a state that proxies the input but with a |
| 94 | * different name, appropriate to this flow. If the state is a StepState |
| 95 | * then the step name is also changed. |
| 96 | * |
| 97 | * @param state |
| 98 | * @return |
| 99 | */ |
| 100 | private State getProxyState(State state) { |
| 101 | String oldName = state.getName(); |
| 102 | if (oldName.startsWith(prefix)) { |
| 103 | return state; |
| 104 | } |
| 105 | String stateName = prefix + oldName; |
| 106 | if (state instanceof StepState) { |
| 107 | return new StepState(stateName, ((StepState) state).getStep()); |
| 108 | } |
| 109 | return new DelegateState(stateName, state); |
| 110 | } |
| 111 | |
| 112 | public Class<?> getObjectType() { |
| 113 | return SimpleFlow.class; |
| 114 | } |
| 115 | |
| 116 | public boolean isSingleton() { |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * A State that proxies a delegate and changes its name but leaves its |
| 122 | * behaviour unchanged. |
| 123 | * |
| 124 | * @author Dave Syer |
| 125 | * |
| 126 | */ |
| 127 | private static class DelegateState extends AbstractState implements FlowHolder { |
| 128 | private final State state; |
| 129 | |
| 130 | private DelegateState(String name, State state) { |
| 131 | super(name); |
| 132 | this.state = state; |
| 133 | } |
| 134 | |
| 135 | public boolean isEndState() { |
| 136 | return state.isEndState(); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public FlowExecutionStatus handle(FlowExecutor executor) throws Exception { |
| 141 | return state.handle(executor); |
| 142 | } |
| 143 | |
| 144 | public Collection<Flow> getFlows() { |
| 145 | return (state instanceof FlowHolder) ? ((FlowHolder)state).getFlows() : Collections.<Flow>emptyList(); |
| 146 | } |
| 147 | |
| 148 | } |
| 149 | |
| 150 | } |