| 1 | /* |
| 2 | * Copyright 2006-2009 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.configuration.xml; |
| 17 | |
| 18 | import java.util.Map; |
| 19 | import java.util.Set; |
| 20 | |
| 21 | import org.springframework.beans.PropertyValue; |
| 22 | import org.springframework.beans.factory.config.BeanDefinition; |
| 23 | import org.springframework.beans.factory.config.TypedStringValue; |
| 24 | import org.springframework.beans.factory.support.AbstractBeanDefinition; |
| 25 | import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 26 | import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 27 | import org.springframework.beans.factory.support.ManagedMap; |
| 28 | import org.springframework.beans.factory.xml.ParserContext; |
| 29 | import org.springframework.util.StringUtils; |
| 30 | import org.w3c.dom.Element; |
| 31 | |
| 32 | /** |
| 33 | * Utility methods used in parsing of the batch core namespace |
| 34 | * |
| 35 | * @author Thomas Risberg |
| 36 | */ |
| 37 | public class CoreNamespaceUtils { |
| 38 | |
| 39 | private static final String STEP_SCOPE_PROCESSOR_BEAN_NAME = "org.springframework.batch.core.scope.internalStepScope"; |
| 40 | |
| 41 | private static final String STEP_SCOPE_PROCESSOR_CLASS_NAME = "org.springframework.batch.core.scope.StepScope"; |
| 42 | |
| 43 | private static final String CUSTOM_EDITOR_CONFIGURER_CLASS_NAME = "org.springframework.beans.factory.config.CustomEditorConfigurer"; |
| 44 | |
| 45 | private static final String RANGE_ARRAY_CLASS_NAME = "org.springframework.batch.item.file.transform.Range[]"; |
| 46 | |
| 47 | private static final String RANGE_ARRAY_EDITOR_CLASS_NAME = "org.springframework.batch.item.file.transform.RangeArrayPropertyEditor"; |
| 48 | |
| 49 | private static final String CORE_NAMESPACE_POST_PROCESSOR_CLASS_NAME = "org.springframework.batch.core.configuration.xml.CoreNamespacePostProcessor"; |
| 50 | |
| 51 | protected static void autoregisterBeansForNamespace(ParserContext parserContext, Object source) { |
| 52 | checkForStepScope(parserContext, source); |
| 53 | addRangePropertyEditor(parserContext); |
| 54 | addCoreNamespacePostProcessor(parserContext); |
| 55 | } |
| 56 | |
| 57 | private static void checkForStepScope(ParserContext parserContext, Object source) { |
| 58 | boolean foundStepScope = false; |
| 59 | String[] beanNames = parserContext.getRegistry().getBeanDefinitionNames(); |
| 60 | for (String beanName : beanNames) { |
| 61 | BeanDefinition bd = parserContext.getRegistry().getBeanDefinition(beanName); |
| 62 | if (STEP_SCOPE_PROCESSOR_CLASS_NAME.equals(bd.getBeanClassName())) { |
| 63 | foundStepScope = true; |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | if (!foundStepScope) { |
| 68 | BeanDefinitionBuilder stepScopeBuilder = BeanDefinitionBuilder |
| 69 | .genericBeanDefinition(STEP_SCOPE_PROCESSOR_CLASS_NAME); |
| 70 | AbstractBeanDefinition abd = stepScopeBuilder.getBeanDefinition(); |
| 71 | abd.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); |
| 72 | abd.setSource(source); |
| 73 | parserContext.getRegistry().registerBeanDefinition(STEP_SCOPE_PROCESSOR_BEAN_NAME, abd); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Register a RangePropertyEditor if one does not already exist. |
| 79 | * |
| 80 | * @param parserContext |
| 81 | */ |
| 82 | @SuppressWarnings("unchecked") |
| 83 | private static void addRangePropertyEditor(ParserContext parserContext) { |
| 84 | BeanDefinitionRegistry registry = parserContext.getRegistry(); |
| 85 | if (!rangeArrayEditorAlreadyDefined(registry)) { |
| 86 | AbstractBeanDefinition customEditorConfigurer = BeanDefinitionBuilder.genericBeanDefinition( |
| 87 | CUSTOM_EDITOR_CONFIGURER_CLASS_NAME).getBeanDefinition(); |
| 88 | customEditorConfigurer.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); |
| 89 | ManagedMap editors = new ManagedMap(); |
| 90 | editors.put(RANGE_ARRAY_CLASS_NAME, RANGE_ARRAY_EDITOR_CLASS_NAME); |
| 91 | customEditorConfigurer.getPropertyValues().addPropertyValue("customEditors", editors); |
| 92 | registry.registerBeanDefinition(CUSTOM_EDITOR_CONFIGURER_CLASS_NAME, customEditorConfigurer); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | @SuppressWarnings("unchecked") |
| 97 | private static boolean rangeArrayEditorAlreadyDefined(BeanDefinitionRegistry registry) { |
| 98 | for (String beanName : registry.getBeanDefinitionNames()) { |
| 99 | BeanDefinition bd = registry.getBeanDefinition(beanName); |
| 100 | if (CUSTOM_EDITOR_CONFIGURER_CLASS_NAME.equals(bd.getBeanClassName())) { |
| 101 | PropertyValue pv = bd.getPropertyValues().getPropertyValue("customEditors"); |
| 102 | if (pv != null) { |
| 103 | for (Map.Entry entry : (Set<Map.Entry>) ((Map) pv.getValue()).entrySet()) { |
| 104 | if (entry.getKey() instanceof TypedStringValue) { |
| 105 | if (RANGE_ARRAY_CLASS_NAME.equals(((TypedStringValue) entry.getKey()).getValue())) { |
| 106 | return true; |
| 107 | } |
| 108 | } |
| 109 | else if (entry.getKey() instanceof String) { |
| 110 | if (RANGE_ARRAY_CLASS_NAME.equals((String) entry.getKey())) { |
| 111 | return true; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @param parserContext |
| 123 | */ |
| 124 | private static void addCoreNamespacePostProcessor(ParserContext parserContext) { |
| 125 | BeanDefinitionRegistry registry = parserContext.getRegistry(); |
| 126 | if (!coreNamespaceBeanPostProcessorAlreadyDefined(registry)) { |
| 127 | AbstractBeanDefinition postProcessorBeanDef = BeanDefinitionBuilder.genericBeanDefinition( |
| 128 | CORE_NAMESPACE_POST_PROCESSOR_CLASS_NAME).getBeanDefinition(); |
| 129 | postProcessorBeanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); |
| 130 | registry.registerBeanDefinition(CORE_NAMESPACE_POST_PROCESSOR_CLASS_NAME, postProcessorBeanDef); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | private static boolean coreNamespaceBeanPostProcessorAlreadyDefined(BeanDefinitionRegistry registry) { |
| 135 | for (String beanName : registry.getBeanDefinitionNames()) { |
| 136 | BeanDefinition bd = registry.getBeanDefinition(beanName); |
| 137 | if (CORE_NAMESPACE_POST_PROCESSOR_CLASS_NAME.equals(bd.getBeanClassName())) { |
| 138 | return true; |
| 139 | } |
| 140 | } |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Should this element be treated as incomplete? If it has a parent or is |
| 146 | * abstract, then it may not have all properties. |
| 147 | * |
| 148 | * @param element |
| 149 | * @return TRUE if the element is abstract or has a parent |
| 150 | */ |
| 151 | public static boolean isUnderspecified(Element element) { |
| 152 | return isAbstract(element) || StringUtils.hasText(element.getAttribute("parent")); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * @param element |
| 157 | * @return TRUE if the element is abstract |
| 158 | */ |
| 159 | public static boolean isAbstract(Element element) { |
| 160 | String abstractAttr = element.getAttribute("abstract"); |
| 161 | return StringUtils.hasText(abstractAttr) && Boolean.valueOf(abstractAttr); |
| 162 | } |
| 163 | |
| 164 | } |