From 7e37d548b51c151df5a9eef49e2a573c2584e9f4 Mon Sep 17 00:00:00 2001 From: dhgarrette Date: Thu, 9 Apr 2009 20:15:54 +0000 Subject: [PATCH] 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[] --- .../configuration/xml/CoreNamespaceUtils.java | 69 ++++++++++++++++--- .../core/configuration/xml/JobParser.java | 1 + .../configuration/xml/TopLevelStepParser.java | 1 + .../batch/sample/config/common-context.xml | 3 - 4 files changed, 62 insertions(+), 12 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/CoreNamespaceUtils.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/CoreNamespaceUtils.java index d8cd6f48a..024867e2a 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/CoreNamespaceUtils.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/CoreNamespaceUtils.java @@ -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) 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")); } - + } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/JobParser.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/JobParser.java index 372bf1567..c839a5a69 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/JobParser.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/JobParser.java @@ -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); diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/TopLevelStepParser.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/TopLevelStepParser.java index e2e25f607..4b2c8b3e3 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/TopLevelStepParser.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/TopLevelStepParser.java @@ -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); } diff --git a/spring-batch-samples/src/main/resources/org/springframework/batch/sample/config/common-context.xml b/spring-batch-samples/src/main/resources/org/springframework/batch/sample/config/common-context.xml index ff202d65f..c67c7a91c 100644 --- a/spring-batch-samples/src/main/resources/org/springframework/batch/sample/config/common-context.xml +++ b/spring-batch-samples/src/main/resources/org/springframework/batch/sample/config/common-context.xml @@ -13,9 +13,6 @@ - - -