/
RollbackRuleAttribute.java
159 lines (139 loc) · 5.64 KB
/
RollbackRuleAttribute.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.transaction.interceptor;
import java.io.Serializable;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Rule determining whether or not a given exception should cause a rollback.
*
* <p>Multiple such rules can be applied to determine whether a transaction
* should commit or rollback after an exception has been thrown.
*
* @author Rod Johnson
* @author Sam Brannen
* @since 09.04.2003
* @see NoRollbackRuleAttribute
*/
@SuppressWarnings("serial")
public class RollbackRuleAttribute implements Serializable{
/**
* The {@linkplain RollbackRuleAttribute rollback rule} for
* {@link RuntimeException RuntimeExceptions}.
*/
public static final RollbackRuleAttribute ROLLBACK_ON_RUNTIME_EXCEPTIONS =
new RollbackRuleAttribute(RuntimeException.class);
/**
* Could hold exception, resolving class name but would always require FQN.
* This way does multiple string comparisons, but how often do we decide
* whether to roll back a transaction following an exception?
*/
private final String exceptionPattern;
/**
* Create a new instance of the {@code RollbackRuleAttribute} class
* for the given {@code exceptionType}.
* <p>This is the preferred way to construct a rollback rule that matches
* the supplied exception type, its subclasses, and its nested classes.
* @param exceptionType exception type; must be {@link Throwable} or a subclass
* of {@code Throwable}
* @throws IllegalArgumentException if the supplied {@code exceptionType} is
* not a {@code Throwable} type or is {@code null}
*/
public RollbackRuleAttribute(Class<?> exceptionType) {
Assert.notNull(exceptionType, "'exceptionType' cannot be null");
if (!Throwable.class.isAssignableFrom(exceptionType)) {
throw new IllegalArgumentException(
"Cannot construct rollback rule from [" + exceptionType.getName() + "]: it's not a Throwable");
}
this.exceptionPattern = exceptionType.getName();
}
/**
* Create a new instance of the {@code RollbackRuleAttribute} class
* for the given {@code exceptionPattern}.
* <p>This can be a substring, with no wildcard support at present. A value
* of "ServletException" would match
* {@code javax.servlet.ServletException} and subclasses, for example.
* <p><b>NB:</b> Consider carefully how specific the pattern is, and
* whether to include package information (which is not mandatory). For
* example, "Exception" will match nearly anything, and will probably hide
* other rules. "java.lang.Exception" would be correct if "Exception" was
* meant to define a rule for all checked exceptions. With more unique
* exception names such as "BaseBusinessException" there's no need to use a
* fully package-qualified name.
* @param exceptionPattern the exception name pattern; can also be a fully
* package-qualified class name
* @throws IllegalArgumentException if the supplied {@code exceptionPattern}
* is {@code null} or empty
*/
public RollbackRuleAttribute(String exceptionPattern) {
Assert.hasText(exceptionPattern, "'exceptionPattern' cannot be null or empty");
this.exceptionPattern = exceptionPattern;
}
/**
* Get the configured exception name pattern that this rule uses for matching.
* @see #getDepth(Throwable)
*/
public String getExceptionName() {
return this.exceptionPattern;
}
/**
* Return the depth of the superclass matching, with the following semantics.
* <ul>
* <li>{@code -1} means this rule does not match the supplied {@code exception}.</li>
* <li>{@code 0} means this rule matches the supplied {@code exception} exactly.</li>
* <li>Any other positive value means this rule matches the supplied {@code exception}
* within the superclass hierarchy, where the value is the number of levels in the
* class hierarchy between the supplied {@code exception} and the exception against
* which this rule matches directly.</li>
* </ul>
* <p>When comparing roll back rules that match against a given exception, a rule
* with a lower matching depth wins. For example, a direct match ({@code depth == 0})
* wins over a match in the superclass hierarchy ({@code depth > 0}).
*/
public int getDepth(Throwable exception) {
return getDepth(exception.getClass(), 0);
}
private int getDepth(Class<?> exceptionClass, int depth) {
if (exceptionClass.getName().contains(this.exceptionPattern)) {
// Found it!
return depth;
}
// If we've gone as far as we can go and haven't found it...
if (exceptionClass == Throwable.class) {
return -1;
}
return getDepth(exceptionClass.getSuperclass(), depth + 1);
}
@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!(other instanceof RollbackRuleAttribute)) {
return false;
}
RollbackRuleAttribute rhs = (RollbackRuleAttribute) other;
return this.exceptionPattern.equals(rhs.exceptionPattern);
}
@Override
public int hashCode() {
return this.exceptionPattern.hashCode();
}
@Override
public String toString() {
return "RollbackRuleAttribute with pattern [" + this.exceptionPattern + "]";
}
}