KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.Serializable JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 /**
29  * TransactionAttribute implementation that works out whether a given exception
30  * should cause transaction rollback by applying a number of rollback rules,
31  * both positive and negative. If no rules are relevant to the exception, it
32  * behaves like DefaultTransactionAttribute (rolling back on runtime exceptions).
33  *
34  * <p>TransactionAttributeEditor creates objects of this class.
35  *
36  * @author Rod Johnson
37  * @author Juergen Hoeller
38  * @since 09.04.2003
39  * @see TransactionAttributeEditor
40  */

41 public class RuleBasedTransactionAttribute extends DefaultTransactionAttribute implements Serializable JavaDoc {
42
43     /** Prefix for rollback-on-exception rules in description strings */
44     public static final String JavaDoc PREFIX_ROLLBACK_RULE = "-";
45
46     /** Prefix for commit-on-exception rules in description strings */
47     public static final String JavaDoc PREFIX_COMMIT_RULE = "+";
48
49
50     /** Static for optimal serializability */
51     private static final Log logger = LogFactory.getLog(RuleBasedTransactionAttribute.class);
52
53     private List JavaDoc rollbackRules;
54
55
56     /**
57      * Create a new RuleBasedTransactionAttribute, with default settings.
58      * Can be modified through bean property setters.
59      * @see #setPropagationBehavior
60      * @see #setIsolationLevel
61      * @see #setTimeout
62      * @see #setReadOnly
63      * @see #setName
64      * @see #setRollbackRules
65      */

66     public RuleBasedTransactionAttribute() {
67         super();
68     }
69
70     /**
71      * Copy constructor. Definition can be modified through bean property setters.
72      * @see #setPropagationBehavior
73      * @see #setIsolationLevel
74      * @see #setTimeout
75      * @see #setReadOnly
76      * @see #setName
77      * @see #setRollbackRules
78      */

79     public RuleBasedTransactionAttribute(RuleBasedTransactionAttribute other) {
80         super(other);
81         this.rollbackRules = new ArrayList JavaDoc(other.rollbackRules);
82     }
83
84     /**
85      * Create a new DefaultTransactionAttribute with the the given
86      * propagation behavior. Can be modified through bean property setters.
87      * @param propagationBehavior one of the propagation constants in the
88      * TransactionDefinition interface
89      * @param rollbackRules the list of RollbackRuleAttributes to apply
90      * @see #setIsolationLevel
91      * @see #setTimeout
92      * @see #setReadOnly
93      */

94     public RuleBasedTransactionAttribute(int propagationBehavior, List JavaDoc rollbackRules) {
95         super(propagationBehavior);
96         this.rollbackRules = rollbackRules;
97     }
98
99
100     /**
101      * Set the list of <code>RollbackRuleAttribute</code> objects
102      * (and/or <code>NoRollbackRuleAttribute</code> objects) to apply.
103      * @see RollbackRuleAttribute
104      * @see NoRollbackRuleAttribute
105      */

106     public void setRollbackRules(List JavaDoc rollbackRules) {
107         this.rollbackRules = rollbackRules;
108     }
109
110     /**
111      * Return the list of <code>RollbackRuleAttribute</code> objects
112      * (never <code>null</code>).
113      */

114     public List JavaDoc getRollbackRules() {
115         if (this.rollbackRules == null) {
116             this.rollbackRules = new LinkedList JavaDoc();
117         }
118         return this.rollbackRules;
119     }
120
121
122     /**
123      * Winning rule is the shallowest rule (that is, the closest in the
124      * inheritance hierarchy to the exception). If no rule applies (-1),
125      * return false.
126      * @see TransactionAttribute#rollbackOn(java.lang.Throwable)
127      */

128     public boolean rollbackOn(Throwable JavaDoc ex) {
129         if (logger.isDebugEnabled()) {
130             logger.debug("Applying rules to determine whether transaction should rollback on " + ex);
131         }
132
133         RollbackRuleAttribute winner = null;
134         int deepest = Integer.MAX_VALUE;
135
136         if (this.rollbackRules != null) {
137             for (Iterator JavaDoc it = this.rollbackRules.iterator(); it.hasNext();) {
138                 RollbackRuleAttribute rule = (RollbackRuleAttribute) it.next();
139                 int depth = rule.getDepth(ex);
140                 if (depth >= 0 && depth < deepest) {
141                     deepest = depth;
142                     winner = rule;
143                 }
144             }
145         }
146
147         if (logger.isDebugEnabled()) {
148             logger.debug("Winning rollback rule is: " + winner);
149         }
150
151         // User superclass behavior (rollback on unchecked) if no rule matches.
152
if (winner == null) {
153             logger.debug("No relevant rollback rule found: applying superclass default");
154             return super.rollbackOn(ex);
155         }
156             
157         return !(winner instanceof NoRollbackRuleAttribute);
158     }
159
160
161     public String JavaDoc toString() {
162         StringBuffer JavaDoc result = getDefinitionDescription();
163         if (this.rollbackRules != null) {
164             for (Iterator JavaDoc it = this.rollbackRules.iterator(); it.hasNext();) {
165                 RollbackRuleAttribute rule = (RollbackRuleAttribute) it.next();
166                 String JavaDoc sign = (rule instanceof NoRollbackRuleAttribute ? PREFIX_COMMIT_RULE : PREFIX_ROLLBACK_RULE);
167                 result.append(',').append(sign).append(rule.getExceptionName());
168             }
169         }
170         return result.toString();
171     }
172
173 }
174
Popular Tags