KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > transaction > interceptor > TransactionAttributeEditor


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
17 package org.springframework.transaction.interceptor;
18
19 import java.beans.PropertyEditorSupport JavaDoc;
20
21 import org.springframework.util.StringUtils;
22
23 /**
24  * PropertyEditor for {@link TransactionAttribute} objects. Accepts a String of form
25  * <p><code>PROPAGATION_NAME,ISOLATION_NAME,readOnly,timeout_NNNN,+Exception1,-Exception2</code>
26  * <p>where only propagation code is required. For example:
27  * <p><code>PROPAGATION_MANDATORY,ISOLATION_DEFAULT</code>
28  *
29  * <p>The tokens can be in <strong>any</strong> order. Propagation and isolation codes
30  * must use the names of the constants in the TransactionDefinition class. Timeout values
31  * are in seconds. If no timeout is specified, the transaction manager will apply a default
32  * timeout specific to the particular transaction manager.
33  *
34  * <p>A "+" before an exception name substring indicates that transactions should commit
35  * even if this exception is thrown; a "-" that they should roll back.
36  *
37  * @author Rod Johnson
38  * @author Juergen Hoeller
39  * @since 24.04.2003
40  * @see org.springframework.transaction.TransactionDefinition
41  * @see org.springframework.core.Constants
42  */

43 public class TransactionAttributeEditor extends PropertyEditorSupport JavaDoc {
44
45     /**
46      * Format is PROPAGATION_NAME,ISOLATION_NAME,readOnly,timeout_NNNN,+Exception1,-Exception2.
47      * Null or the empty string means that the method is non transactional.
48      * @see java.beans.PropertyEditor#setAsText(java.lang.String)
49      */

50     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
51         if (StringUtils.hasLength(text)) {
52             // tokenize it with ","
53
String JavaDoc[] tokens = StringUtils.commaDelimitedListToStringArray(text);
54             RuleBasedTransactionAttribute attr = new RuleBasedTransactionAttribute();
55             for (int i = 0; i < tokens.length; i++) {
56                 // Trim leading and trailing whitespace.
57
String JavaDoc token = StringUtils.trimWhitespace(tokens[i].trim());
58                 // Check whether token contains illegal whitespace within text.
59
if (StringUtils.containsWhitespace(token)) {
60                     throw new IllegalArgumentException JavaDoc(
61                             "Transaction attribute token contains illegal whitespace: [" + token + "]");
62                 }
63                 // Check token type.
64
if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_PROPAGATION)) {
65                     attr.setPropagationBehaviorName(token);
66                 }
67                 else if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_ISOLATION)) {
68                     attr.setIsolationLevelName(token);
69                 }
70                 else if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_TIMEOUT)) {
71                     String JavaDoc value = token.substring(DefaultTransactionAttribute.PREFIX_TIMEOUT.length());
72                     attr.setTimeout(Integer.parseInt(value));
73                 }
74                 else if (token.equals(RuleBasedTransactionAttribute.READ_ONLY_MARKER)) {
75                     attr.setReadOnly(true);
76                 }
77                 else if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_COMMIT_RULE)) {
78                     attr.getRollbackRules().add(new NoRollbackRuleAttribute(token.substring(1)));
79                 }
80                 else if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_ROLLBACK_RULE)) {
81                     attr.getRollbackRules().add(new RollbackRuleAttribute(token.substring(1)));
82                 }
83                 else {
84                     throw new IllegalArgumentException JavaDoc("Invalid transaction attribute token: [" + token + "]");
85                 }
86             }
87             setValue(attr);
88         }
89         else {
90             setValue(null);
91         }
92     }
93
94 }
95
Popular Tags