KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > jdbc > EmbedSQLException


1 /*
2
3    Derby - Class org.apache.derby.impl.jdbc.EmbedSQLException
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.jdbc;
23
24 import org.apache.derby.iapi.error.StandardException;
25
26 import java.sql.SQLException JavaDoc;
27 import java.io.PrintStream JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29
30 /**
31     This class is what gets send over the wire in client/server
32     configuration. When running embedded, this has the detailed
33     stack trace for exceptions. In case of client/server, server
34     has all the stack trace information but client doesn't get
35     the stack trace, just the sql exception. The reason for this
36     implementation is the stack trace information is more relevant
37     on the server side and it also decreases the size of client
38     jar file tremendously.
39 */

40 public class EmbedSQLException extends SQLException JavaDoc {
41
42     private transient Object JavaDoc[] arguments;
43     private String JavaDoc messageId;
44
45     /**
46         Java exception that caused this exception, can be null.
47     */

48     //Because it's transient, it doesn't get sent over to the client
49
//side and hence the classes which needs to be included in the
50
//client.jar file decreases 5 folds.
51
private transient Throwable JavaDoc javaException;
52
53     /**
54      * Because SQLException does not have settable fields,
55      * the caller of the constructor must do message lookup,
56      * and pass the appropriate values here for message, messageId,
57      * and next exception.
58      */

59     EmbedSQLException(String JavaDoc message, String JavaDoc messageId,
60         SQLException JavaDoc nextException, int severity, Object JavaDoc[] args) {
61
62         super(message, StandardException.getSQLStateFromIdentifier(messageId), severity);
63         this.messageId = messageId;
64         arguments = args;
65         if (nextException !=null)
66             this.setNextException(nextException);
67     }
68
69     public EmbedSQLException(String JavaDoc message, String JavaDoc messageId,
70         SQLException JavaDoc nextException, int severity, Throwable JavaDoc t, Object JavaDoc[] args) {
71
72         super(message, StandardException.getSQLStateFromIdentifier(messageId), severity);
73         this.messageId = messageId;
74         arguments = args;
75         if (nextException !=null)
76             this.setNextException(nextException);
77         javaException = t;
78     }
79     
80     public Throwable JavaDoc getJavaException() {
81         return javaException;
82     }
83
84     public String JavaDoc getMessageId() {
85         return messageId;
86     }
87
88     public Object JavaDoc[] getArguments() {
89         return arguments;
90     }
91
92     /**
93         Print the stack trace of the wrapped java exception or this
94         exception if there is none.
95
96         @see Throwable#printStackTrace
97     */

98     public void printStackTrace() {
99         Throwable JavaDoc je = getJavaException();
100         if (je != null)
101             je.printStackTrace();
102         else
103             super.printStackTrace();
104     }
105     /**
106         Print the stack trace of the wrapped java exception or this
107         exception if there is none.
108
109         @see Throwable#printStackTrace
110     */

111     public void printStackTrace(PrintStream JavaDoc s) {
112         Throwable JavaDoc je = getJavaException();
113         if (je != null)
114             je.printStackTrace(s);
115         else
116             super.printStackTrace(s);
117     }
118     /**
119         Print the stack trace of the wrapped java exception or this
120         exception if there is none.
121
122         @see Throwable#printStackTrace
123     */

124     public void printStackTrace(PrintWriter JavaDoc s) {
125         Throwable JavaDoc je = getJavaException();
126         if (je != null)
127             je.printStackTrace(s);
128         else
129             super.printStackTrace(s);
130     }
131
132     /*
133     ** Methods of Object
134     */

135
136     /**
137         Override Throwables toString() to avoid the class name
138         appearing in the message.
139     */

140     public String JavaDoc toString() {
141         // We use java.sql.SQLException rather than the default toString(),
142
// which returns org.apache.derby.impl.jdbc.EmbedSQLException, so
143
// that (a) we're not exposing an internal class name and (b) so
144
// this is consistent with the network client, where SQLExceptions
145
// are vanilla java.sql classes and not our own subclass
146
return "java.sql.SQLException: " + getMessage();
147     }
148
149     /*
150     ** Some hack methods for 3.0.1. These will get cleaned up in main
151     ** with the exception re-work.
152     */

153     private transient boolean simpleWrapper;
154     public static SQLException JavaDoc wrapStandardException(String JavaDoc message, String JavaDoc messageId, int code, Throwable JavaDoc se) {
155         EmbedSQLException csqle = new EmbedSQLException(message, messageId, (SQLException JavaDoc) null, code, se, (se instanceof StandardException) ? ((StandardException)se).getArguments() : null);
156         csqle.simpleWrapper = true;
157         return csqle;
158     }
159     public boolean isSimpleWrapper() {
160         return simpleWrapper;
161     }
162 }
163
Popular Tags