BATCH-1172: Register RangeArrayPropertyEditor automatically

*New method addRangePropertyEditor() added to CoreNamespaceUtils
   *Called by JobParser and TopLevelStepParser
   *Registers a new CustomEditorConfigurer if no CustomEditorConfigurer currently exists with an entry for Range[]
This commit is contained in:
dhgarrette
2009-04-09 20:15:54 +00:00
parent e10eca009b
commit 7e37d548b5
4 changed files with 62 additions and 12 deletions

View File

@@ -15,9 +15,16 @@
*/
package org.springframework.batch.core.configuration.xml;
import java.util.Map;
import java.util.Set;
import org.springframework.batch.item.file.transform.RangeArrayPropertyEditor;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.TypedStringValue;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
@@ -28,16 +35,17 @@ import org.w3c.dom.Element;
* @author Thomas Risberg
*/
public class CoreNamespaceUtils {
public static final String STEP_SCOPE_PROCESSOR_BEAN_NAME =
"org.springframework.batch.core.scope.internalStepScope";
public static final String STEP_SCOPE_PROCESSOR_CLASS_NAME =
"org.springframework.batch.core.scope.StepScope";
private static final String STEP_SCOPE_PROCESSOR_BEAN_NAME = "org.springframework.batch.core.scope.internalStepScope";
private static final String STEP_SCOPE_PROCESSOR_CLASS_NAME = "org.springframework.batch.core.scope.StepScope";
private static final String CUSTOM_EDITOR_CONFIGURER_CLASS_NAME = "org.springframework.beans.factory.config.CustomEditorConfigurer";
private static final String RANGE_ARRAY_CLASS_NAME = "org.springframework.batch.item.file.transform.Range[]";
protected static void checkForStepScope(ParserContext parserContext, Object source) {
boolean foundStepScope = false;
String[] beanNames = parserContext.getRegistry().getBeanDefinitionNames();
for (String beanName : beanNames) {
@@ -48,8 +56,8 @@ public class CoreNamespaceUtils {
}
}
if (!foundStepScope) {
BeanDefinitionBuilder stepScopeBuilder =
BeanDefinitionBuilder.genericBeanDefinition(STEP_SCOPE_PROCESSOR_CLASS_NAME);
BeanDefinitionBuilder stepScopeBuilder = BeanDefinitionBuilder
.genericBeanDefinition(STEP_SCOPE_PROCESSOR_CLASS_NAME);
AbstractBeanDefinition abd = stepScopeBuilder.getBeanDefinition();
abd.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
abd.setSource(source);
@@ -57,6 +65,49 @@ public class CoreNamespaceUtils {
}
}
/**
* Register a RangeProperyEditor if one does not already exist.
*
* @param parserContext
*/
@SuppressWarnings("unchecked")
protected static void addRangePropertyEditor(ParserContext parserContext) {
BeanDefinitionRegistry registry = parserContext.getRegistry();
if (!rangeArrayEditorAlreadyDefined(registry)) {
BeanDefinitionBuilder stepScopeBuilder = BeanDefinitionBuilder
.genericBeanDefinition(CUSTOM_EDITOR_CONFIGURER_CLASS_NAME);
AbstractBeanDefinition abd = stepScopeBuilder.getBeanDefinition();
abd.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
ManagedMap editors = new ManagedMap();
editors.put(RANGE_ARRAY_CLASS_NAME, RangeArrayPropertyEditor.class);
abd.getPropertyValues().addPropertyValue("customEditors", editors);
registry.registerBeanDefinition(CUSTOM_EDITOR_CONFIGURER_CLASS_NAME, abd);
}
}
@SuppressWarnings("unchecked")
private static boolean rangeArrayEditorAlreadyDefined(BeanDefinitionRegistry registry) {
for (String beanName : registry.getBeanDefinitionNames()) {
BeanDefinition bd = registry.getBeanDefinition(beanName);
if (CUSTOM_EDITOR_CONFIGURER_CLASS_NAME.equals(bd.getBeanClassName())) {
Map editors = (Map) bd.getPropertyValues().getPropertyValue("customEditors").getValue();
for (Map.Entry entry : (Set<Map.Entry>) editors.entrySet()) {
if (entry.getKey() instanceof TypedStringValue) {
if (RANGE_ARRAY_CLASS_NAME.equals(((TypedStringValue) entry.getKey()).getValue())) {
return true;
}
}
else if (entry.getKey() instanceof String) {
if (RANGE_ARRAY_CLASS_NAME.equals((String) entry.getKey())) {
return true;
}
}
}
}
}
return false;
}
/**
* Should this element be treated as incomplete? If it has a parent or is
* abstract, then it may not have all properties.
@@ -67,5 +118,5 @@ public class CoreNamespaceUtils {
public static boolean isUnderspecified(Element element) {
return Boolean.valueOf(element.getAttribute("abstract")) || StringUtils.hasText(element.getAttribute("parent"));
}
}

View File

@@ -60,6 +60,7 @@ public class JobParser extends AbstractSingleBeanDefinitionParser {
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
CoreNamespaceUtils.checkForStepScope(parserContext, parserContext.extractSource(element));
CoreNamespaceUtils.addRangePropertyEditor(parserContext);
String jobName = element.getAttribute("id");
builder.addConstructorArgValue(jobName);

View File

@@ -35,6 +35,7 @@ public class TopLevelStepParser extends AbstractBeanDefinitionParser {
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
CoreNamespaceUtils.checkForStepScope(parserContext, parserContext.extractSource(element));
CoreNamespaceUtils.addRangePropertyEditor(parserContext);
return stepParser.parse(element, parserContext);
}

View File

@@ -13,9 +13,6 @@
<entry key="int[]">
<bean class="org.springframework.batch.support.IntArrayPropertyEditor" />
</entry>
<entry key="org.springframework.batch.item.file.transform.Range[]">
<bean class="org.springframework.batch.item.file.transform.RangeArrayPropertyEditor" />
</entry>
<entry key="java.util.Date">
<bean class="org.springframework.beans.propertyeditors.CustomDateEditor">
<constructor-arg>