-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
SplitParser.java
119 lines (102 loc) · 4.36 KB
/
SplitParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Copyright 2006-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.configuration.xml;
import java.util.Collection;
import java.util.List;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.core.task.TaskExecutor;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
/**
* Internal parser for the <split/> elements inside a job. A split element
* optionally references a bean definition for a {@link TaskExecutor} and goes on to list
* a set of transitions to other states with <next on="pattern" to="stepName"/>.
* Used by the {@link JobParser}.
*
* @see JobParser
* @author Dave Syer
*
*/
public class SplitParser {
private static final String PARENT_ATTR = "parent";
private final String jobFactoryRef;
/**
* Construct a {@link InlineFlowParser} by using the provided job repository
* reference.
*
* @param jobFactoryRef The reference to the {@link JobParserJobFactoryBean}
* from the enclosing tag.
*/
public SplitParser(String jobFactoryRef) {
this.jobFactoryRef = jobFactoryRef;
}
/**
* Parse the split and turn it into a list of transitions.
*
* @param element The <split/gt; element to parse
* @param parserContext The parser context for the bean factory
* @return a collection of bean definitions for
* {@link org.springframework.batch.core.job.flow.support.StateTransition} instances
* objects
*/
public Collection<BeanDefinition> parse(Element element, ParserContext parserContext) {
String idAttribute = element.getAttribute("id");
BeanDefinitionBuilder stateBuilder = BeanDefinitionBuilder
.genericBeanDefinition("org.springframework.batch.core.job.flow.support.state.SplitState");
String taskExecutorBeanId = element.getAttribute("task-executor");
if (StringUtils.hasText(taskExecutorBeanId)) {
RuntimeBeanReference taskExecutorRef = new RuntimeBeanReference(taskExecutorBeanId);
stateBuilder.addPropertyValue("taskExecutor", taskExecutorRef);
}
List<Element> flowElements = DomUtils.getChildElementsByTagName(element, "flow");
if (flowElements.size() < 2) {
parserContext.getReaderContext().error("A <split/> must contain at least two 'flow' elements.", element);
}
Collection<BeanDefinition> flows = new ManagedList<>();
int i = 0;
String prefix = idAttribute;
for (Element nextElement : flowElements) {
String ref = nextElement.getAttribute(PARENT_ATTR);
if (StringUtils.hasText(ref)) {
if (nextElement.getElementsByTagName("*").getLength() > 0) {
parserContext.getReaderContext().error(
"A <flow/> in a <split/> must have ref= or nested <flow/>, but not both.", nextElement);
}
AbstractBeanDefinition flowDefinition = new GenericBeanDefinition();
flowDefinition.setParentName(ref);
MutablePropertyValues propertyValues = flowDefinition.getPropertyValues();
propertyValues.addPropertyValue("name", prefix + "." + i);
flows.add(flowDefinition);
}
else {
InlineFlowParser flowParser = new InlineFlowParser(prefix + "." + i, jobFactoryRef);
flows.add(flowParser.parse(nextElement, parserContext));
}
i++;
}
stateBuilder.addConstructorArgValue(flows);
stateBuilder.addConstructorArgValue(prefix);
return InlineFlowParser.getNextElements(parserContext, stateBuilder.getBeanDefinition(), element);
}
}