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