Skip to content

Commit 152abb1

Browse files
committed
Allow property injection into any artifact rather than just JSR interface impls.
* Remove isBatchArtifact check * Reduce BatchArtifact.BatchArtifactType into BatchArtifactType * Import JSR JSL definition from Spring config loaded by JsrJobOperator, update test for property injection into Tasklet and cleanup pre-parsing checks
1 parent 6fa04a2 commit 152abb1

16 files changed

+124
-167
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/support/BatchArtifact.java

-116
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.core.jsr.configuration.support;
17+
18+
/**
19+
* <p>
20+
* Enum to identify batch artifact types.
21+
* </p>
22+
*
23+
* @author Chris Schaefer
24+
* @since 3.0
25+
*/
26+
public enum BatchArtifactType {
27+
STEP,
28+
STEP_ARTIFACT,
29+
ARTIFACT,
30+
JOB
31+
}

spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/support/BatchPropertyBeanPostProcessor.java

-4
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,6 @@ public class BatchPropertyBeanPostProcessor implements BeanPostProcessor, BeanFa
7373

7474
@Override
7575
public Object postProcessBeforeInitialization(final Object artifact, String artifactName) throws BeansException {
76-
if (! BatchArtifact.isBatchArtifact(artifact)) {
77-
return artifact;
78-
}
79-
8076
Properties artifactProperties = getArtifactProperties(artifactName);
8177

8278
if (artifactProperties.isEmpty()) {

spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/BatchletParser.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.batch.core.jsr.configuration.xml;
1717

18-
import org.springframework.batch.core.jsr.configuration.support.BatchArtifact;
18+
import org.springframework.batch.core.jsr.configuration.support.BatchArtifactType;
1919
import org.springframework.batch.core.step.tasklet.Tasklet;
2020
import org.springframework.beans.factory.config.BeanDefinition;
2121
import org.springframework.beans.factory.config.RuntimeBeanReference;
@@ -50,6 +50,6 @@ public void parseBatchlet(Element batchletElement, AbstractBeanDefinition bd, Pa
5050
bd.setRole(BeanDefinition.ROLE_SUPPORT);
5151
bd.setSource(parserContext.extractSource(batchletElement));
5252

53-
new PropertyParser(taskletRef, parserContext, BatchArtifact.BatchArtifactType.STEP_ARTIFACT, stepName).parseProperties(batchletElement);
53+
new PropertyParser(taskletRef, parserContext, BatchArtifactType.STEP_ARTIFACT, stepName).parseProperties(batchletElement);
5454
}
5555
}

spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/ChunkParser.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import java.util.List;
1919

2020
import org.springframework.batch.core.configuration.xml.ExceptionElementParser;
21-
import org.springframework.batch.core.jsr.configuration.support.BatchArtifact;
21+
import org.springframework.batch.core.jsr.configuration.support.BatchArtifactType;
2222
import org.springframework.batch.core.step.item.ChunkOrientedTasklet;
2323
import org.springframework.batch.item.ItemProcessor;
2424
import org.springframework.batch.item.ItemReader;
@@ -129,19 +129,19 @@ private void parseChildElement(Element element, ParserContext parserContext,
129129
propertyValues.addPropertyValue("stepItemReader", new RuntimeBeanReference(artifactName));
130130
}
131131

132-
new PropertyParser(artifactName, parserContext, BatchArtifact.BatchArtifactType.STEP_ARTIFACT, stepName).parseProperties(nestedElement);
132+
new PropertyParser(artifactName, parserContext, BatchArtifactType.STEP_ARTIFACT, stepName).parseProperties(nestedElement);
133133
} else if(name.equals(PROCESSOR_ELEMENT)) {
134134
if (StringUtils.hasText(artifactName)) {
135135
propertyValues.addPropertyValue("stepItemProcessor", new RuntimeBeanReference(artifactName));
136136
}
137137

138-
new PropertyParser(artifactName, parserContext, BatchArtifact.BatchArtifactType.STEP_ARTIFACT, stepName).parseProperties(nestedElement);
138+
new PropertyParser(artifactName, parserContext, BatchArtifactType.STEP_ARTIFACT, stepName).parseProperties(nestedElement);
139139
} else if(name.equals(WRITER_ELEMENT)) {
140140
if (StringUtils.hasText(artifactName)) {
141141
propertyValues.addPropertyValue("stepItemWriter", new RuntimeBeanReference(artifactName));
142142
}
143143

144-
new PropertyParser(artifactName, parserContext, BatchArtifact.BatchArtifactType.STEP_ARTIFACT, stepName).parseProperties(nestedElement);
144+
new PropertyParser(artifactName, parserContext, BatchArtifactType.STEP_ARTIFACT, stepName).parseProperties(nestedElement);
145145
} else if(name.equals(SKIPPABLE_EXCEPTION_CLASSES_ELEMENT)) {
146146
ManagedMap exceptionClasses = new ExceptionElementParser().parse(element, parserContext, SKIPPABLE_EXCEPTION_CLASSES_ELEMENT);
147147
if(exceptionClasses != null) {
@@ -177,7 +177,7 @@ private void parseCustomCheckpointAlgorithm(Element element, ParserContext parse
177177
propertyValues.addPropertyValue("stepChunkCompletionPolicy", new RuntimeBeanReference(name));
178178
}
179179

180-
new PropertyParser(name, parserContext, BatchArtifact.BatchArtifactType.STEP_ARTIFACT, stepName).parseProperties(checkpointAlgorithmElement);
180+
new PropertyParser(name, parserContext, BatchArtifactType.STEP_ARTIFACT, stepName).parseProperties(checkpointAlgorithmElement);
181181
} else if(elements.size() > 1){
182182
parserContext.getReaderContext().error(
183183
"The <checkpoint-algorithm/> element may not appear more than once in a single <"

spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/JsrBeanDefinitionDocumentReader.java

+22-10
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
3131
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
3232
import org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader;
33+
import org.springframework.util.ClassUtils;
3334
import org.w3c.dom.Element;
3435
import org.w3c.dom.NamedNodeMap;
3536
import org.w3c.dom.Node;
@@ -244,23 +245,24 @@ private void transformDocument(Element root) {
244245
referenceCountMap.put(resolvedValue, 0);
245246
}
246247

247-
// possibly fully qualified class name in ref tag in the jobXML
248-
if(!registry.containsBeanDefinition(resolvedValue)) {
248+
boolean isClass = isClass(resolvedValue);
249+
Integer referenceCount = referenceCountMap.get(resolvedValue);
250+
251+
// possibly fully qualified class name in ref tag in the JSL or pointer to bean/artifact ref.
252+
if(isClass && !registry.containsBeanDefinition(resolvedValue)) {
249253
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(resolvedValue)
250254
.getBeanDefinition();
251255
beanDefinition.setScope("step");
252256
registry.registerBeanDefinition(resolvedValue, beanDefinition);
253-
newNodeValue = resolvedValue;
254-
}
255257

256-
if (referenceCountMap.containsKey(resolvedValue)) {
257-
Integer referenceCount = referenceCountMap.get(resolvedValue);
258-
referenceCount++;
259-
referenceCountMap.put(resolvedValue, referenceCount);
258+
newNodeValue = resolvedValue;
259+
} else {
260+
if(registry.containsBeanDefinition(resolvedValue)) {
261+
referenceCount++;
262+
referenceCountMap.put(resolvedValue, referenceCount);
260263

261-
newNodeValue = resolvedValue + referenceCount;
264+
newNodeValue = resolvedValue + referenceCount;
262265

263-
if(registry.containsBeanDefinition(resolvedValue)) {
264266
BeanDefinition beanDefinition = registry.getBeanDefinition(resolvedValue);
265267
registry.registerBeanDefinition(newNodeValue, beanDefinition);
266268
}
@@ -282,6 +284,16 @@ private void transformDocument(Element root) {
282284
}
283285
}
284286

287+
private boolean isClass(String className) {
288+
try {
289+
Class.forName(className, false, ClassUtils.getDefaultClassLoader());
290+
} catch (ClassNotFoundException e) {
291+
return false;
292+
}
293+
294+
return true;
295+
}
296+
285297
protected Properties getJobParameters() {
286298
return propertyMap.get(JOB_PARAMETERS_KEY_NAME);
287299
}

spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/JsrDecisionParser.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import java.util.Collection;
1919

2020
import org.springframework.batch.core.job.flow.JobExecutionDecider;
21-
import org.springframework.batch.core.jsr.configuration.support.BatchArtifact;
21+
import org.springframework.batch.core.jsr.configuration.support.BatchArtifactType;
2222
import org.springframework.batch.core.jsr.job.flow.support.state.JsrStepState;
2323
import org.springframework.beans.factory.config.BeanDefinition;
2424
import org.springframework.beans.factory.config.RuntimeBeanReference;
@@ -62,7 +62,7 @@ public Collection<BeanDefinition> parse(Element element, ParserContext parserCon
6262
factoryDefinition.setAttribute("jobParserJobFactoryBeanRef", jobFactoryRef);
6363
}
6464

65-
new PropertyParser(refAttribute, parserContext, BatchArtifact.BatchArtifactType.STEP_ARTIFACT, idAttribute).parseProperties(element);
65+
new PropertyParser(refAttribute, parserContext, BatchArtifactType.STEP_ARTIFACT, idAttribute).parseProperties(element);
6666

6767
return FlowParser.getNextElements(parserContext, stateBuilder.getBeanDefinition(), element);
6868
}

spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/JsrJobParser.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,7 @@
1717

1818
import org.springframework.batch.core.configuration.xml.CoreNamespaceUtils;
1919
import org.springframework.batch.core.jsr.JsrStepContextFactoryBean;
20-
import org.springframework.batch.core.jsr.configuration.support.BatchArtifact;
20+
import org.springframework.batch.core.jsr.configuration.support.BatchArtifactType;
2121
import org.springframework.beans.factory.config.BeanDefinition;
2222
import org.springframework.beans.factory.support.AbstractBeanDefinition;
2323
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
@@ -61,7 +61,7 @@ protected void doParse(Element element, ParserContext parserContext, BeanDefinit
6161
builder.addPropertyValue("restartable", restartableAttribute);
6262
}
6363

64-
new PropertyParser(jobName, parserContext, BatchArtifact.BatchArtifactType.JOB).parseProperties(element);
64+
new PropertyParser(jobName, parserContext, BatchArtifactType.JOB).parseProperties(element);
6565

6666
BeanDefinition flowDef = new FlowParser(jobName, jobName).parse(element, parserContext);
6767
builder.addPropertyValue("flow", flowDef);

spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/ListenerParser.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import java.util.List;
1919

20-
import org.springframework.batch.core.jsr.configuration.support.BatchArtifact;
20+
import org.springframework.batch.core.jsr.configuration.support.BatchArtifactType;
2121
import org.springframework.beans.factory.config.BeanDefinition;
2222
import org.springframework.beans.factory.config.RuntimeBeanReference;
2323
import org.springframework.beans.factory.parsing.CompositeComponentDefinition;
@@ -135,8 +135,8 @@ private String getListenerScope() {
135135
return SCOPE_STEP;
136136
}
137137

138-
private BatchArtifact.BatchArtifactType getBatchArtifactType(String stepName) {
139-
return (stepName != null && !"".equals(stepName)) ? BatchArtifact.BatchArtifactType.STEP_ARTIFACT
140-
: BatchArtifact.BatchArtifactType.ARTIFACT;
138+
private BatchArtifactType getBatchArtifactType(String stepName) {
139+
return (stepName != null && !"".equals(stepName)) ? BatchArtifactType.STEP_ARTIFACT
140+
: BatchArtifactType.ARTIFACT;
141141
}
142142
}

spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/PartitionParser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.concurrent.ConcurrentLinkedQueue;
2020
import java.util.concurrent.locks.ReentrantLock;
2121

22-
import org.springframework.batch.core.jsr.configuration.support.BatchArtifact.BatchArtifactType;
22+
import org.springframework.batch.core.jsr.configuration.support.BatchArtifactType;
2323
import org.springframework.batch.core.jsr.partition.JsrPartitionHandler;
2424
import org.springframework.beans.MutablePropertyValues;
2525
import org.springframework.beans.factory.config.RuntimeBeanReference;

0 commit comments

Comments
 (0)