View Javadoc

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