01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
18:
19: import org.springframework.util.Assert;
20:
21: /**
22: * Exception thrown when a general transaction system error is encountered,
23: * like on commit or rollback.
24: *
25: * @author Juergen Hoeller
26: * @since 24.03.2003
27: */
28: public class TransactionSystemException extends TransactionException {
29:
30: private Throwable applicationException;
31:
32: /**
33: * Constructor for TransactionSystemException.
34: * @param msg the detail message
35: */
36: public TransactionSystemException(String msg) {
37: super (msg);
38: }
39:
40: /**
41: * Constructor for TransactionSystemException.
42: * @param msg the detail message
43: * @param cause the root cause from the transaction API in use
44: */
45: public TransactionSystemException(String msg, Throwable cause) {
46: super (msg, cause);
47: }
48:
49: /**
50: * Set an application exception that was thrown before this transaction exception,
51: * preserving the original exception despite the overriding TransactionSystemException.
52: * @param ex the application exception
53: * @throws IllegalStateException if this TransactionSystemException already holds an
54: * application exception
55: */
56: public void initApplicationException(Throwable ex) {
57: Assert.notNull(ex, "Application exception must not be null");
58: if (this .applicationException != null) {
59: throw new IllegalStateException(
60: "Already holding an application exception: "
61: + this .applicationException);
62: }
63: this .applicationException = ex;
64: }
65:
66: /**
67: * Return the application exception that was thrown before this transaction exception,
68: * if any.
69: * @return the application exception, or <code>null</code> if none set
70: */
71: public final Throwable getApplicationException() {
72: return this .applicationException;
73: }
74:
75: /**
76: * Return the exception that was the first to be thrown within the failed transaction:
77: * i.e. the application exception, if any, or the TransactionSystemException's own cause.
78: * @return the original exception, or <code>null</code> if there was none
79: */
80: public Throwable getOriginalException() {
81: return (this .applicationException != null ? this .applicationException
82: : getCause());
83: }
84:
85: public boolean contains(Class exType) {
86: return super.contains(exType)
87: || (exType != null && exType
88: .isInstance(this.applicationException));
89: }
90:
91: }
|