KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 /**
22  * Rule determining whether or not a given exception (and any subclasses) should
23  * cause a rollback. Multiple such rules can be applied to determine whether a
24  * transaction should commit or rollback after an exception has been thrown.
25  *
26  * @author Rod Johnson
27  * @since 09.04.2003
28  * @see NoRollbackRuleAttribute
29  */

30 public class RollbackRuleAttribute implements Serializable JavaDoc{
31     
32     public static final RollbackRuleAttribute ROLLBACK_ON_RUNTIME_EXCEPTIONS =
33             new RollbackRuleAttribute(RuntimeException JavaDoc.class);
34
35
36     /**
37      * Could hold exception, resolving class name but would always require FQN.
38      * This way does multiple string comparisons, but how often do we decide
39      * whether to roll back a transaction following an exception?
40      */

41     private final String JavaDoc exceptionName;
42
43
44     /**
45      * Preferred way to construct a RollbackRule, matching the exception class and
46      * subclasses. The exception class must be Throwable or a subclass of Throwable.
47      * @param clazz throwable class
48      */

49     public RollbackRuleAttribute(Class JavaDoc clazz) {
50         if (!Throwable JavaDoc.class.isAssignableFrom(clazz)) {
51             throw new IllegalArgumentException JavaDoc(
52                     "Cannot construct rollback rule from [" + clazz.getName() + "]: it's not a Throwable");
53         }
54         this.exceptionName = clazz.getName();
55     }
56
57     /**
58      * Construct a new RollbackRule for the given exception name.
59      * This can be a substring, with no wildcard support at present.
60      * A value of "ServletException" would match
61      * <code>javax.servlet.ServletException</code> and subclasses, for example.
62      * <p><b>NB:</b> Consider carefully how specific the pattern is, and whether
63      * to include package information (which isn't mandatory). For example,
64      * "Exception" will match nearly anything, and will probably hide other rules.
65      * "java.lang.Exception" would be correct if "Exception" was meant to define
66      * a rule for all checked exceptions. With more unusual exception names such
67      * as "BaseBusinessException" there's no need to use a FQN.
68      * @param exceptionName the exception pattern
69      * (can also be a fully qualified class name)
70      */

71     public RollbackRuleAttribute(String JavaDoc exceptionName) {
72         this.exceptionName = exceptionName;
73     }
74
75
76     /**
77      * Return the pattern for the exception name.
78      */

79     public String JavaDoc getExceptionName() {
80         return exceptionName;
81     }
82
83     /**
84      * Return the depth to the superclass matching.
85      * 0 means ex matches exactly. Returns -1 if there's no match.
86      * Otherwise, returns depth. Lowest depth wins.
87      */

88     public int getDepth(Throwable JavaDoc ex) {
89         return getDepth(ex.getClass(), 0);
90     }
91
92     private int getDepth(Class JavaDoc exceptionClass, int depth) {
93         if (exceptionClass.getName().indexOf(this.exceptionName) != -1) {
94             // Found it!
95
return depth;
96         }
97         // If we've gone as far as we can go and haven't found it...
98
if (exceptionClass.equals(Throwable JavaDoc.class)) {
99             return -1;
100         }
101         return getDepth(exceptionClass.getSuperclass(), depth + 1);
102     }
103
104
105     public boolean equals(Object JavaDoc other) {
106         if (this == other) {
107             return true;
108         }
109         if (!(other instanceof RollbackRuleAttribute)) {
110             return false;
111         }
112         RollbackRuleAttribute rhs = (RollbackRuleAttribute) other;
113         return this.exceptionName.equals(rhs.exceptionName);
114     }
115     
116     public int hashCode() {
117         return this.exceptionName.hashCode();
118     }
119
120     public String JavaDoc toString() {
121         return "RollbackRuleAttribute with pattern [" + this.exceptionName + "]";
122     }
123
124 }
125
Popular Tags