EMMA Coverage Report (generated Fri Aug 21 15:59:46 BST 2009)
[all classes][org.springframework.batch.core.configuration.xml]

COVERAGE SUMMARY FOR SOURCE FILE [CoreNamespaceUtils.java]

nameclass, %method, %block, %line, %
CoreNamespaceUtils.java100% (1/1)89%  (8/9)95%  (235/247)95%  (55/58)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CoreNamespaceUtils100% (1/1)89%  (8/9)95%  (235/247)95%  (55/58)
CoreNamespaceUtils (): void 0%   (0/1)0%   (0/3)0%   (0/1)
rangeArrayEditorAlreadyDefined (BeanDefinitionRegistry): boolean 100% (1/1)88%  (65/74)85%  (11/13)
addCoreNamespacePostProcessor (ParserContext): void 100% (1/1)100% (18/18)100% (7/7)
addRangePropertyEditor (ParserContext): void 100% (1/1)100% (32/32)100% (10/10)
autoregisterBeansForNamespace (ParserContext, Object): void 100% (1/1)100% (8/8)100% (4/4)
checkForStepScope (ParserContext, Object): void 100% (1/1)100% (55/55)100% (15/15)
coreNamespaceBeanPostProcessorAlreadyDefined (BeanDefinitionRegistry): boolean 100% (1/1)100% (30/30)100% (5/5)
isAbstract (Element): boolean 100% (1/1)100% (15/15)100% (2/2)
isUnderspecified (Element): boolean 100% (1/1)100% (12/12)100% (1/1)

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 */
16package org.springframework.batch.core.configuration.xml;
17 
18import java.util.Map;
19import java.util.Set;
20 
21import org.springframework.beans.PropertyValue;
22import org.springframework.beans.factory.config.BeanDefinition;
23import org.springframework.beans.factory.config.TypedStringValue;
24import org.springframework.beans.factory.support.AbstractBeanDefinition;
25import org.springframework.beans.factory.support.BeanDefinitionBuilder;
26import org.springframework.beans.factory.support.BeanDefinitionRegistry;
27import org.springframework.beans.factory.support.ManagedMap;
28import org.springframework.beans.factory.xml.ParserContext;
29import org.springframework.util.StringUtils;
30import org.w3c.dom.Element;
31 
32/**
33 * Utility methods used in parsing of the batch core namespace
34 * 
35 * @author Thomas Risberg
36 */
37public 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}

[all classes][org.springframework.batch.core.configuration.xml]
EMMA 2.0.5312 (C) Vladimir Roubtsov