1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.batch.core.configuration.xml;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.List;
22
23 import org.springframework.batch.core.job.flow.Flow;
24 import org.springframework.batch.core.job.flow.FlowExecutionStatus;
25 import org.springframework.batch.core.job.flow.FlowExecutor;
26 import org.springframework.batch.core.job.flow.FlowHolder;
27 import org.springframework.batch.core.job.flow.State;
28 import org.springframework.batch.core.job.flow.support.SimpleFlow;
29 import org.springframework.batch.core.job.flow.support.StateTransition;
30 import org.springframework.batch.core.job.flow.support.state.AbstractState;
31 import org.springframework.batch.core.job.flow.support.state.StepState;
32 import org.springframework.beans.factory.FactoryBean;
33 import org.springframework.beans.factory.InitializingBean;
34 import org.springframework.util.Assert;
35
36
37
38
39
40
41
42
43
44
45 @SuppressWarnings("rawtypes")
46 public class SimpleFlowFactoryBean implements FactoryBean, InitializingBean {
47
48 private String name;
49
50 private List<StateTransition> stateTransitions;
51
52 private String prefix;
53
54
55
56
57
58
59 public void setName(String name) {
60 this.name = name;
61 this.prefix = name + ".";
62 }
63
64
65
66
67
68
69
70
71 public void setStateTransitions(List<StateTransition> stateTransitions) {
72 this.stateTransitions = stateTransitions;
73 }
74
75
76
77
78
79
80 @Override
81 public void afterPropertiesSet() throws Exception {
82 Assert.hasText(name, "The flow must have a name");
83 }
84
85 @Override
86 public Object getObject() throws Exception {
87
88 SimpleFlow flow = new SimpleFlow(name);
89
90 List<StateTransition> updatedTransitions = new ArrayList<StateTransition>();
91 for (StateTransition stateTransition : stateTransitions) {
92 State state = getProxyState(stateTransition.getState());
93 updatedTransitions.add(StateTransition.switchOriginAndDestination(stateTransition, state,
94 getNext(stateTransition.getNext())));
95 }
96
97 flow.setStateTransitions(updatedTransitions);
98 flow.afterPropertiesSet();
99 return flow;
100
101 }
102
103 private String getNext(String next) {
104 if (next == null) {
105 return null;
106 }
107 return (next.startsWith(this.prefix) ? "" : this.prefix) + next;
108 }
109
110
111
112
113
114
115
116
117
118 private State getProxyState(State state) {
119 String oldName = state.getName();
120 if (oldName.startsWith(prefix)) {
121 return state;
122 }
123 String stateName = prefix + oldName;
124 if (state instanceof StepState) {
125 return new StepState(stateName, ((StepState) state).getStep());
126 }
127 return new DelegateState(stateName, state);
128 }
129
130 @Override
131 public Class<?> getObjectType() {
132 return SimpleFlow.class;
133 }
134
135 @Override
136 public boolean isSingleton() {
137 return true;
138 }
139
140
141
142
143
144
145
146
147 private static class DelegateState extends AbstractState implements FlowHolder {
148 private final State state;
149
150 private DelegateState(String name, State state) {
151 super(name);
152 this.state = state;
153 }
154
155 @Override
156 public boolean isEndState() {
157 return state.isEndState();
158 }
159
160 @Override
161 public FlowExecutionStatus handle(FlowExecutor executor) throws Exception {
162 return state.handle(executor);
163 }
164
165 @Override
166 public Collection<Flow> getFlows() {
167 return (state instanceof FlowHolder) ? ((FlowHolder)state).getFlows() : Collections.<Flow>emptyList();
168 }
169
170 }
171
172 }