1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jdo.model;
18
19 import org.apache.jdo.util.I18NHelper;
20
21 /***
22 * This exception indicates a Model problem. It is the root for all
23 * checked model exceptions.
24 *
25 * @author Michael Bouschen
26 * @since JDO 1.0.1
27 */
28 public class ModelException
29 extends Exception
30 {
31 /*** The throwable that caused this model exception to be thrown. */
32 private Throwable cause;
33
34 /*** Flag indicating whether printStackTrace is being executed. */
35 private boolean inPrintStackTrace = false;
36
37 /*** I18N support */
38 private static I18NHelper msg =
39 I18NHelper.getInstance(ModelException.class);
40
41 /***
42 * Creates new <code>ModelException</code> without detail message.
43 */
44 public ModelException()
45 {
46 }
47
48 /***
49 * Constructs a <code>ModelException</code> with the specified detail
50 * message.
51 * @param message the detail message.
52 */
53 public ModelException(String message)
54 {
55 super(message);
56 }
57
58 /***
59 * Constructs a new <code>ModelException</code> with the specified
60 * detail message and cause.
61 * @param message the detail message.
62 * @param cause the cause (which is saved for later retrieval by the
63 * {@link #getCause()} method). (A null value is permitted, and
64 * indicates that the cause is nonexistent or unknown.)
65 */
66 public ModelException(String message, Throwable cause)
67 {
68 super(message);
69 this.cause = cause;
70 }
71
72 /***
73 * Returns the cause of this throwable or null if the cause is
74 * nonexistent or unknown. (The cause is the throwable that caused this
75 * throwable to get thrown.)
76 * @return the cause of this throwable or null if the cause is
77 * nonexistent or unknown.
78 */
79 public synchronized Throwable getCause()
80 {
81
82
83
84
85 return inPrintStackTrace ? null : cause;
86 }
87
88 /***
89 * Initializes the cause of this throwable to the specified value. (The
90 * cause is the throwable that caused this throwable to get thrown.)
91 * @param cause the cause (which is saved for later retrieval by the
92 * {@link #getCause()} method). (A null value is permitted, and
93 * indicates that the cause is nonexistent or unknown.)
94 * @return a reference to this <code>ModelException</code> instance.
95 */
96 public Throwable initCause(Throwable cause)
97 {
98 this.cause = cause;
99 return this;
100 }
101
102 /***
103 * Returns the error message string of this throwable object.
104 * @return the error message string of this <code>ModelException</code>
105 * object if it was created with an error message string, the error
106 * message of the cause if it was not created a message but the cause
107 * has a message, or <code>null</code> if neither has an error message.
108 */
109 public String getMessage()
110 {
111 String message = super.getMessage();
112 if ((message == null) || (message.length() == 0)) {
113
114
115 if (cause != null)
116 message = cause.getMessage();
117 }
118 return message;
119 }
120
121 /***
122 * The <code>String</code> representation includes the name of the class,
123 * the descriptive comment (if any),
124 * and the <code>String</code> representation of the cause (if any).
125 * @return the <code>String</code>.
126 */
127 public synchronized String toString()
128 {
129 StringBuffer sb = new StringBuffer();
130 sb.append(super.toString());
131
132
133 if ((cause != null) && !inPrintStackTrace) {
134 sb.append("\n");
135 sb.append(msg.msg("MSG_CauseThrowable"));
136 sb.append("\n");
137 sb.append(cause.toString());
138 }
139 return sb.toString();
140 }
141
142 /***
143 * Prints this <code>ModelException</code> and its backtrace to the
144 * standard error output.
145 * Print cause Throwable's stack trace as well.
146 */
147 public void printStackTrace()
148 {
149 printStackTrace(System.err);
150 }
151
152 /***
153 * Prints this <code>ModelException</code> and its backtrace to the
154 * specified print stream.
155 * Print cause Throwable's stack trace as well.
156 * @param s <code>PrintStream</code> to use for output
157 */
158 public synchronized void printStackTrace(java.io.PrintStream s)
159 {
160 synchronized (s) {
161 inPrintStackTrace = true;
162 super.printStackTrace(s);
163 if (cause != null) {
164 s.println(msg.msg("MSG_CauseThrowableStackTrace"));
165 cause.printStackTrace(s);
166 }
167 inPrintStackTrace = false;
168 }
169 }
170
171 /***
172 * Prints this <code>ModelException</code> and its backtrace to the specified
173 * print writer.
174 * Print cause Throwable' stack trace as well.
175 * @param s <code>PrintWriter</code> to use for output
176 */
177 public synchronized void printStackTrace(java.io.PrintWriter s)
178 {
179 synchronized (s) {
180 inPrintStackTrace = true;
181 super.printStackTrace(s);
182 if (cause != null) {
183 s.println(msg.msg("MSG_CauseThrowableStackTrace"));
184 cause.printStackTrace(s);
185 }
186 inPrintStackTrace = false;
187 }
188 }
189
190 }