KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > support > JDOException


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * JDOException.java
26  *
27  * Created on March 8, 2000, 8:29 AM
28  */

29
30 package com.sun.jdo.api.persistence.support;
31
32 /** This is the root of all JDO Exceptions. It contains an optional
33  * nested Exception and an optional message.
34  * @author Craig Russell
35  * @version 0.1
36  */

37 public class JDOException extends java.lang.RuntimeException JavaDoc {
38
39   /** This exception was generated because of an exception in the runtime library.
40    * @serial the nested Exception
41    */

42   Exception JavaDoc nested;
43
44   /** This exception may be the result of incorrect parameters supplied
45    * to an API. This is the array from which the user can determine
46    * the cause of the problem.
47    * The failed Object array is transient because it might contain
48    * non-Serializable instances.
49    */

50   transient Object JavaDoc[] failed;
51
52   /**
53    * Creates a new <code>JDOException</code> without detail message.
54    */

55   public JDOException() {
56   }
57
58
59   /**
60    * Constructs a new <code>JDOException</code> with the specified detail message.
61    * @param msg the detail message.
62    */

63   public JDOException(String JavaDoc msg) {
64     super(msg);
65   }
66
67   /** Constructs a new <code>JDOException</code> with the specified detail message
68    * and nested Exception.
69    * @param msg the detail message.
70    * @param nested the nested <code>Exception</code>.
71    */

72   public JDOException(String JavaDoc msg, Exception JavaDoc nested) {
73     super(msg);
74     this.nested = nested;
75   }
76
77   /** Constructs a new <code>JDOException</code> with the specified detail message
78    * and failed object array.
79    * @param msg the detail message.
80    * @param failed the failed object array.
81    */

82   public JDOException(String JavaDoc msg, Object JavaDoc[] failed) {
83     super(msg);
84     this.failed = failed;
85   }
86
87   /** Constructs a new <code>JDOException</code> with the specified detail message,
88    * nested exception, and failed object array.
89    * @param msg the detail message.
90    * @param nested the nested <code>Exception</code>.
91    * @param failed the failed object array.
92    */

93   public JDOException(String JavaDoc msg, Exception JavaDoc nested, Object JavaDoc[] failed) {
94     super(msg);
95     this.nested = nested;
96     this.failed = failed;
97   }
98
99   /** The exception may need to add objects to an array of failed objects.
100    * @param o the failed object to add to an array.
101    */

102   public void addFailedObject(Object JavaDoc o) {
103     if (failed == null)
104     //Create new
105
failed = new Object JavaDoc[] {o};
106     else {
107     //Extend exisisting
108
int len = failed.length;
109     Object JavaDoc[] ofailed = failed;
110     failed = new Object JavaDoc[len + 1];
111     for (int i = 0; i < len; i++)
112         failed[i] = ofailed[i];
113
114     failed[len] = o;
115     }
116   }
117
118   /** The exception may include an array of failed objects.
119    * @return the failed object array.
120    */

121   public Object JavaDoc[] getFailedObjectArray() {
122     return failed;
123   }
124
125   /** The exception may have been caused by an Exception in the runtime.
126    * @return the nested Exception.
127    */

128   public Exception JavaDoc getNestedException() {
129     return nested;
130   }
131
132   /** The String representation includes the name of the class,
133    * the descriptive comment (if any),
134    * the String representation of the nested Exception (if any),
135    * and the String representation of the failed Object array (if any).
136    * @return the String.
137    */

138   public String JavaDoc toString() {
139     int len = 0;
140     if (failed != null) {
141         len = failed.length;
142     }
143     // calculate approximate size of the String to return
144
StringBuffer JavaDoc sb = new StringBuffer JavaDoc (100 + 10 * len);
145     sb.append (super.toString());
146     // include nested exception information
147
if (nested != null) {
148       sb.append ("\nNestedException: "); //NOI18N
149
sb.append (nested.toString());
150     }
151     // include failed object information
152
if (len > 0) {
153       sb.append ("\nFailedObjectArray: ["); //NOI18N
154
Object JavaDoc ofail = failed[0];
155       sb.append (JDOHelper.printObject(ofail));
156       for (int i=1; i<len; ++i) {
157         sb.append (", "); //NOI18N
158
ofail = failed[i];
159         sb.append (JDOHelper.printObject(ofail));
160       }
161       sb.append ("]"); //NOI18N
162
}
163     return sb.toString();
164   }
165
166   /**
167    * Prints this <code>JDOException</code> and its backtrace to the
168    * standard error output.
169    * Prints nested Throwables' stack trace as well.
170    */

171   public void printStackTrace() {
172     printStackTrace(System.err);
173   }
174
175   /**
176    * Prints this <code>JDOException</code> and its backtrace to the
177    * specified print stream.
178    * Prints nested Throwable's stack trace as well.
179    * @param s <code>PrintStream</code> to use for output
180    */

181   public void printStackTrace(java.io.PrintStream JavaDoc s) {
182     synchronized (s) {
183       super.printStackTrace(s);
184       if (nested != null) {
185         s.println("\nNestedStackTrace: "); //NOI18N
186
nested.printStackTrace(s);
187       }
188     }
189   }
190
191   /**
192    * Prints this <code>JDOException</code> and its backtrace to the specified
193    * print writer.
194    * Prints nested Throwable's stack trace as well.
195    * @param s <code>PrintWriter</code> to use for output
196    */

197   public void printStackTrace(java.io.PrintWriter JavaDoc s) {
198     synchronized (s) {
199       super.printStackTrace(s);
200       if (nested != null) {
201         s.println("\nNestedStackTrace: "); //NOI18N
202
nested.printStackTrace(s);
203       }
204     }
205   }
206
207 }
208
209
Popular Tags