KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > transaction > config > TxAdviceBeanDefinitionParser


1 /*
2  * Copyright 2002-2007 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
17 package org.springframework.transaction.config;
18
19 import java.util.LinkedList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.w3c.dom.Element JavaDoc;
23
24 import org.springframework.beans.factory.config.TypedStringValue;
25 import org.springframework.beans.factory.support.BeanDefinitionBuilder;
26 import org.springframework.beans.factory.support.ManagedMap;
27 import org.springframework.beans.factory.support.RootBeanDefinition;
28 import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
29 import org.springframework.beans.factory.xml.ParserContext;
30 import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
31 import org.springframework.transaction.interceptor.NoRollbackRuleAttribute;
32 import org.springframework.transaction.interceptor.RollbackRuleAttribute;
33 import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
34 import org.springframework.transaction.interceptor.TransactionInterceptor;
35 import org.springframework.util.StringUtils;
36 import org.springframework.util.xml.DomUtils;
37
38 /**
39  * {@link org.springframework.beans.factory.xml.BeanDefinitionParser}
40  * for the <code>&lt;tx:advice&gt;</code> tag.
41  *
42  * @author Rob Harrop
43  * @author Juergen Hoeller
44  * @author Adrian Colyer
45  * @since 2.0
46  */

47 class TxAdviceBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
48
49     private static final String JavaDoc ATTRIBUTES = "attributes";
50
51     private static final String JavaDoc TIMEOUT = "timeout";
52
53     private static final String JavaDoc READ_ONLY = "read-only";
54
55     private static final String JavaDoc NAME_MAP = "nameMap";
56
57     private static final String JavaDoc PROPAGATION = "propagation";
58
59     private static final String JavaDoc ISOLATION = "isolation";
60
61     private static final String JavaDoc ROLLBACK_FOR = "rollback-for";
62
63     private static final String JavaDoc NO_ROLLBACK_FOR = "no-rollback-for";
64
65
66     protected Class JavaDoc getBeanClass(Element JavaDoc element) {
67         return TransactionInterceptor.class;
68     }
69
70     protected void doParse(Element JavaDoc element, ParserContext parserContext, BeanDefinitionBuilder builder) {
71         // Set the transaction manager property.
72
builder.addPropertyReference(TxNamespaceUtils.TRANSACTION_MANAGER_PROPERTY,
73                 element.getAttribute(TxNamespaceUtils.TRANSACTION_MANAGER_ATTRIBUTE));
74
75         List JavaDoc txAttributes = DomUtils.getChildElementsByTagName(element, ATTRIBUTES);
76         if (txAttributes.size() > 1) {
77             parserContext.getReaderContext().error(
78                     "Element <attributes> is allowed at most once inside element <advice>", element);
79         }
80         else if (txAttributes.size() == 1) {
81             // Using attributes source.
82
Element JavaDoc attributeSourceElement = (Element JavaDoc) txAttributes.get(0);
83             RootBeanDefinition attributeSourceDefinition = parseAttributeSource(attributeSourceElement, parserContext);
84             builder.addPropertyValue(TxNamespaceUtils.TRANSACTION_ATTRIBUTE_SOURCE, attributeSourceDefinition);
85         }
86         else {
87             // Assume annotations source.
88
Class JavaDoc sourceClass = TxNamespaceUtils.getAnnotationTransactionAttributeSourceClass();
89             builder.addPropertyValue(TxNamespaceUtils.TRANSACTION_ATTRIBUTE_SOURCE, new RootBeanDefinition(sourceClass));
90         }
91     }
92
93     private RootBeanDefinition parseAttributeSource(Element JavaDoc attrEle, ParserContext parserContext) {
94         List JavaDoc methods = DomUtils.getChildElementsByTagName(attrEle, "method");
95         ManagedMap transactionAttributeMap = new ManagedMap(methods.size());
96         transactionAttributeMap.setSource(parserContext.extractSource(attrEle));
97
98         for (int i = 0; i < methods.size(); i++) {
99             Element JavaDoc methodEle = (Element JavaDoc) methods.get(i);
100
101             String JavaDoc name = methodEle.getAttribute("name");
102             TypedStringValue nameHolder = new TypedStringValue(name);
103             nameHolder.setSource(parserContext.extractSource(methodEle));
104
105             RuleBasedTransactionAttribute attribute = new RuleBasedTransactionAttribute();
106             String JavaDoc propagation = methodEle.getAttribute(PROPAGATION);
107             String JavaDoc isolation = methodEle.getAttribute(ISOLATION);
108             String JavaDoc timeout = methodEle.getAttribute(TIMEOUT);
109             String JavaDoc readOnly = methodEle.getAttribute(READ_ONLY);
110             if (StringUtils.hasText(propagation)) {
111                 attribute.setPropagationBehaviorName(RuleBasedTransactionAttribute.PREFIX_PROPAGATION + propagation);
112             }
113             if (StringUtils.hasText(isolation)) {
114                 attribute.setIsolationLevelName(RuleBasedTransactionAttribute.PREFIX_ISOLATION + isolation);
115             }
116             if (StringUtils.hasText(timeout)) {
117                 try {
118                     attribute.setTimeout(Integer.parseInt(timeout));
119                 }
120                 catch (NumberFormatException JavaDoc ex) {
121                     parserContext.getReaderContext().error("Timeout must be an integer value: [" + timeout + "]", methodEle);
122                 }
123             }
124             if (StringUtils.hasText(readOnly)) {
125                 attribute.setReadOnly(Boolean.valueOf(methodEle.getAttribute(READ_ONLY)).booleanValue());
126             }
127
128             List JavaDoc rollbackRules = new LinkedList JavaDoc();
129             if (methodEle.hasAttribute(ROLLBACK_FOR)) {
130                 String JavaDoc rollbackForValue = methodEle.getAttribute(ROLLBACK_FOR);
131                 addRollbackRuleAttributesTo(rollbackRules,rollbackForValue);
132             }
133             if (methodEle.hasAttribute(NO_ROLLBACK_FOR)) {
134                 String JavaDoc noRollbackForValue = methodEle.getAttribute(NO_ROLLBACK_FOR);
135                 addNoRollbackRuleAttributesTo(rollbackRules,noRollbackForValue);
136             }
137             attribute.setRollbackRules(rollbackRules);
138
139             transactionAttributeMap.put(nameHolder, attribute);
140         }
141
142         RootBeanDefinition attributeSourceDefinition = new RootBeanDefinition(NameMatchTransactionAttributeSource.class);
143         attributeSourceDefinition.setSource(parserContext.extractSource(attrEle));
144         attributeSourceDefinition.getPropertyValues().addPropertyValue(NAME_MAP, transactionAttributeMap);
145         return attributeSourceDefinition;
146     }
147
148     private void addRollbackRuleAttributesTo(List JavaDoc rollbackRules, String JavaDoc rollbackForValue) {
149         String JavaDoc[] exceptionTypeNames = StringUtils.commaDelimitedListToStringArray(rollbackForValue);
150         for (int i = 0; i < exceptionTypeNames.length; i++) {
151             rollbackRules.add(new RollbackRuleAttribute(StringUtils.trimWhitespace(exceptionTypeNames[i])));
152         }
153     }
154
155     private void addNoRollbackRuleAttributesTo(List JavaDoc rollbackRules, String JavaDoc noRollbackForValue) {
156         String JavaDoc[] exceptionTypeNames = StringUtils.commaDelimitedListToStringArray(noRollbackForValue);
157         for (int i = 0; i < exceptionTypeNames.length; i++) {
158             rollbackRules.add(new NoRollbackRuleAttribute(StringUtils.trimWhitespace(exceptionTypeNames[i])));
159         }
160     }
161
162 }
163
Popular Tags