BATCH-1034: regsitering StepScope if it is not already registered

This commit is contained in:
trisberg
2009-02-11 22:21:46 +00:00
parent 5289d20178
commit 766941104f
6 changed files with 132 additions and 1 deletions

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2006-2009 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
*
* http://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 org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
/**
* utility methods used in parsing of the batch core namespace
*
* @author Thomas Risberg
*/
public class CoreNamespaceUtils {
protected static void checkForStepScope(ParserContext parserContext) {
final String stepScopeClassName = "org.springframework.batch.core.scope.StepScope";
boolean foundStepScope = false;
String[] beanNames = parserContext.getRegistry().getBeanDefinitionNames();
for (String beanName : beanNames) {
BeanDefinition bd = parserContext.getRegistry().getBeanDefinition(beanName);
if (stepScopeClassName.equals(bd.getBeanClassName())) {
foundStepScope = true;
break;
}
}
if (!foundStepScope) {
BeanDefinitionBuilder stepScopeBuilder =
BeanDefinitionBuilder.genericBeanDefinition(stepScopeClassName);
AbstractBeanDefinition abd = stepScopeBuilder.getBeanDefinition();
parserContext.getRegistry().registerBeanDefinition(stepScopeClassName, abd);
}
}
}

View File

@@ -50,6 +50,8 @@ public class JobParser extends AbstractSingleBeanDefinitionParser {
*/
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
CoreNamespaceUtils.checkForStepScope(parserContext);
String jobName = element.getAttribute("id");
builder.addConstructorArgValue(jobName);
@@ -86,5 +88,5 @@ public class JobParser extends AbstractSingleBeanDefinitionParser {
}
}
}

View File

@@ -31,6 +31,7 @@ public class TopLevelStepParser extends AbstractBeanDefinitionParser {
@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
CoreNamespaceUtils.checkForStepScope(parserContext);
StandaloneStepParser stepParser = new StandaloneStepParser();
return stepParser.parse(element, parserContext);
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright 2006-2009 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
*
* http://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 static org.junit.Assert.assertTrue;
import java.util.Map;
import org.junit.Test;
import org.springframework.batch.core.scope.StepScope;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Thomas Risberg
*/
public class AutoRegisteringStepScopeTests {
@SuppressWarnings("unchecked")
@Test
public void testJobElement() throws Exception {
ConfigurableApplicationContext ctx =
new ClassPathXmlApplicationContext("org/springframework/batch/core/configuration/xml/AutoRegisteringStepScopeForJobElementTests-context.xml");
Map<String, Object> beans = ctx.getBeansOfType(StepScope.class);
assertTrue("StepScope not defined properly", beans.size() == 1);
}
@SuppressWarnings("unchecked")
@Test
public void testStepElement() throws Exception {
ConfigurableApplicationContext ctx =
new ClassPathXmlApplicationContext("org/springframework/batch/core/configuration/xml/AutoRegisteringStepScopeForStepElementTests-context.xml");
Map<String, Object> beans = ctx.getBeansOfType(StepScope.class);
assertTrue("StepScope not defined properly", beans.size() == 1);
}
}

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/batch" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<beans:import resource="common-context.xml" />
<job id="job">
<step id="step" tasklet="tasklet"/>
</job>
<beans:bean id="tasklet" class="org.springframework.batch.core.configuration.xml.TestTasklet"/>
</beans:beans>

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/batch" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<beans:import resource="common-context.xml" />
<step id="step" tasklet="tasklet"/>
<beans:bean id="tasklet" class="org.springframework.batch.core.configuration.xml.TestTasklet"/>
</beans:beans>