KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > config > ExecutorBeanDefinitionParser


1 /*
2  * Copyright 2002-2006 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.webflow.config;
17
18 import org.springframework.beans.factory.support.AbstractBeanDefinition;
19 import org.springframework.beans.factory.support.BeanDefinitionBuilder;
20 import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
21 import org.springframework.beans.factory.xml.BeanDefinitionParser;
22 import org.springframework.beans.factory.xml.ParserContext;
23 import org.springframework.util.StringUtils;
24 import org.springframework.util.xml.DomUtils;
25 import org.w3c.dom.Element JavaDoc;
26
27 /**
28  * {@link BeanDefinitionParser} for the <code>&lt;executor&gt;</code> tag.
29  *
30  * @author Ben Hale
31  */

32 class ExecutorBeanDefinitionParser extends AbstractBeanDefinitionParser {
33
34     // elements and attributes
35

36     private static final String JavaDoc EXECUTION_ATTRIBUTES_ELEMENT = "execution-attributes";
37
38     private static final String JavaDoc EXECUTION_LISTENERS_ELEMENT = "execution-listeners";
39
40     private static final String JavaDoc REGISTRY_REF_ATTRIBUTE = "registry-ref";
41
42     private static final String JavaDoc REPOSITORY_TYPE_ATTRIBUTE = "repository-type";
43
44     // properties
45

46     private static final String JavaDoc DEFINITION_LOCATOR_PROPERTY = "definitionLocator";
47
48     private static final String JavaDoc EXECUTION_ATTRIBUTES_PROPERTY = "executionAttributes";
49
50     private static final String JavaDoc EXECUTION_LISTENER_LOADER_PROPERTY = "executionListenerLoader";
51
52     private static final String JavaDoc REPOSITORY_TYPE_PROPERTY = "repositoryType";
53
54     protected AbstractBeanDefinition parseInternal(Element JavaDoc element, ParserContext parserContext) {
55         BeanDefinitionBuilder definitionBuilder = BeanDefinitionBuilder
56                 .rootBeanDefinition(FlowExecutorFactoryBean.class);
57         definitionBuilder.addPropertyReference(DEFINITION_LOCATOR_PROPERTY, getRegistryRef(element));
58         addExecutionAttributes(element, parserContext, definitionBuilder);
59         addExecutionListenerLoader(element, parserContext, definitionBuilder);
60         definitionBuilder.addPropertyValue(REPOSITORY_TYPE_PROPERTY, getRepositoryType(element));
61         return definitionBuilder.getBeanDefinition();
62     }
63
64     /**
65      * Returns the name of the registry detailed in the bean definition.
66      * @param element the element to extract the registry name from
67      * @return the name of the registry
68      */

69     private String JavaDoc getRegistryRef(Element JavaDoc element) {
70         String JavaDoc registryRef = element.getAttribute(REGISTRY_REF_ATTRIBUTE);
71         if (!StringUtils.hasText(registryRef)) {
72             throw new IllegalArgumentException JavaDoc("The 'registry-ref' attribute of the 'executor' element must have a value");
73         }
74         return registryRef;
75     }
76
77     /**
78      * Returns the name of the repository type enum field detailed in the bean
79      * definition.
80      * @param element the element to extract the repository type from
81      * @return the type of the repository
82      */

83     private String JavaDoc getRepositoryType(Element JavaDoc element) {
84         return element.getAttribute(REPOSITORY_TYPE_ATTRIBUTE).toUpperCase();
85     }
86
87     /**
88      * Parse execution attribute definitions contained in given element.
89      */

90     private void addExecutionAttributes(Element JavaDoc element, ParserContext parserContext,
91             BeanDefinitionBuilder definitionBuilder) {
92         Element JavaDoc attributesElement = DomUtils.getChildElementByTagName(element, EXECUTION_ATTRIBUTES_ELEMENT);
93         if (attributesElement != null) {
94             definitionBuilder.addPropertyValue(EXECUTION_ATTRIBUTES_PROPERTY, parserContext.getDelegate()
95                     .parseCustomElement(attributesElement, definitionBuilder.getBeanDefinition()));
96         }
97     }
98
99     /**
100      * Parse execution listener definitions contained in given element.
101      */

102     private void addExecutionListenerLoader(Element JavaDoc element, ParserContext parserContext,
103             BeanDefinitionBuilder definitionBuilder) {
104         Element JavaDoc listenersElement = DomUtils.getChildElementByTagName(element, EXECUTION_LISTENERS_ELEMENT);
105         if (listenersElement != null) {
106             definitionBuilder.addPropertyValue(EXECUTION_LISTENER_LOADER_PROPERTY, parserContext.getDelegate()
107                     .parseCustomElement(listenersElement, definitionBuilder.getBeanDefinition()));
108         }
109     }
110 }
Popular Tags