View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
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.apache.jdo.impl.enhancer;
18  
19  import org.apache.jdo.util.I18NHelper;
20  
21  /***
22   * Thrown to indicate that the class-file enhancer failed to perform an
23   * operation due to an error.  The enhancer is guaranteed to remain in a
24   * consistent state.
25   *
26   * @author Michael Bouschen
27   */
28  public class EnhancerUserException
29      extends Exception
30  {
31      /*** The Throwable that caused this 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("org.apache.jdo.impl.enhancer.Bundle");
40  
41      /***
42       * Creates a new <code>EnhancerUserException</code> without detail
43       * message. 
44       */
45      public EnhancerUserException() 
46      {
47      }
48      
49      /***
50       * Creates a new <code>EnhancerUserException</code> with the specified
51       * detail message.
52       * @param message the detail message.
53       */
54      public EnhancerUserException(String message)
55      {
56          super(message);
57      }
58  
59      /*** 
60       * Creates a new <code>EnhancerUserException</code> with the specified 
61       * detail message and cause Throwable.
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 EnhancerUserException(String message, Throwable cause) 
68      {
69          super(message);
70          this.cause = cause;
71      }
72  
73      /*** 
74       * Returns the cause of this Exception or null if the cause is
75       * nonexistent or unknown. (The cause is the Throwable that caused this 
76       * Exception to get thrown.) 
77       * @return the cause of this Exception 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 Exception 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>EnhancerUserException</code> 
96       * instance.
97       */
98      public Throwable initCause(Throwable cause)
99      {
100         this.cause = cause;
101         return this;
102     }
103     
104     /*** 
105      * The <code>String</code> representation includes the name of the class,
106      * the descriptive comment (if any),
107      * and the <code>String</code> representation of the cause (if any). 
108      * @return the <code>String</code>.
109      */
110     public synchronized String toString() 
111     {
112         StringBuffer sb = new StringBuffer();
113         sb.append(super.toString());
114         // Do not include cause information, if called by printStackTrace;
115         // the stacktrace will include the cause anyway.
116         if ((cause != null) && !inPrintStackTrace) {
117             sb.append("\n");  //NOI18N
118             sb.append(msg.msg("MSG_CauseThrowable")); //NOI18N
119             sb.append("\n");  //NOI18N
120             sb.append(cause.toString()); //NOI18N
121         }
122         return sb.toString();
123     }
124   
125     /***
126      * Prints this <code>EnhancerUserException</code> and its backtrace to the 
127      * standard error output.
128      * Print cause Throwable's stack trace as well.
129      */
130     public void printStackTrace()
131     {
132         printStackTrace(System.err);
133     }
134 
135     /***
136      * Prints this <code>EnhancerUserException</code> and its backtrace to the 
137      * specified print stream.
138      * Print cause Throwable's stack trace as well.
139      * @param s <code>PrintStream</code> to use for output
140      */
141     public synchronized void printStackTrace(java.io.PrintStream s) 
142     { 
143         synchronized (s) {
144             inPrintStackTrace = true;
145             super.printStackTrace(s);
146             if (cause != null) {
147                 s.println(msg.msg("MSG_CauseThrowableStackTrace")); //NOI18N
148                 cause.printStackTrace(s);
149             }
150             inPrintStackTrace = false;
151         }
152     }
153     
154     /***
155      * Prints this <code>EnhancerUserException</code> and its backtrace to the specified
156      * print writer.
157      * Print cause Throwable' stack trace as well.
158      * @param s <code>PrintWriter</code> to use for output
159      */
160     public synchronized void printStackTrace(java.io.PrintWriter s) 
161     { 
162         synchronized (s) {
163             inPrintStackTrace = true;
164             super.printStackTrace(s);
165             if (cause != null) {
166                 s.println(msg.msg("MSG_CauseThrowableStackTrace") + ' '); //NOI18N
167                 cause.printStackTrace(s);
168             }
169             inPrintStackTrace = false;
170         }
171     }
172     
173 }