1 | /* |
2 | |
3 | Derby - Class org.apache.derby.impl.jdbc.EmbedSQLException |
4 | |
5 | Copyright 1999, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.impl.jdbc; |
22 | |
23 | import org.apache.derby.iapi.error.StandardException; |
24 | |
25 | import java.sql.SQLException; |
26 | import java.io.PrintStream; |
27 | import java.io.PrintWriter; |
28 | |
29 | /** |
30 | This class is what gets send over the wire in client/server |
31 | configuration. When running embedded, this has the detailed |
32 | stack trace for exceptions. In case of client/server, server |
33 | has all the stack trace information but client doesn't get |
34 | the stack trace, just the sql exception. The reason for this |
35 | implementation is the stack trace information is more relevant |
36 | on the server side and it also decreases the size of client |
37 | jar file tremendously. |
38 | */ |
39 | public class EmbedSQLException extends SQLException { |
40 | |
41 | private transient Object[] arguments; |
42 | private String messageId; |
43 | |
44 | /** |
45 | Java exception that caused this exception, can be null. |
46 | */ |
47 | //Because it's transient, it doesn't get sent over to the client |
48 | //side and hence the classes which needs to be included in the |
49 | //client.jar file decreases 5 folds. |
50 | private transient Throwable javaException; |
51 | |
52 | /** |
53 | * Because SQLException does not have settable fields, |
54 | * the caller of the constructor must do message lookup, |
55 | * and pass the appropriate values here for message, messageId, |
56 | * and next exception. |
57 | */ |
58 | EmbedSQLException(String message, String messageId, |
59 | SQLException nextException, int severity, Object[] args) { |
60 | |
61 | super(message, StandardException.getSQLStateFromIdentifier(messageId), severity); |
62 | this.messageId = messageId; |
63 | arguments = args; |
64 | if (nextException !=null) |
65 | this.setNextException(nextException); |
66 | } |
67 | |
68 | public EmbedSQLException(String message, String messageId, |
69 | SQLException nextException, int severity, Throwable t, Object[] args) { |
70 | |
71 | super(message, StandardException.getSQLStateFromIdentifier(messageId), severity); |
72 | this.messageId = messageId; |
73 | arguments = args; |
74 | if (nextException !=null) |
75 | this.setNextException(nextException); |
76 | javaException = t; |
77 | } |
78 | |
79 | public Throwable getJavaException() { |
80 | return javaException; |
81 | } |
82 | |
83 | public String getMessageId() { |
84 | return messageId; |
85 | } |
86 | |
87 | public Object[] getArguments() { |
88 | return arguments; |
89 | } |
90 | |
91 | /** |
92 | Print the stack trace of the wrapped java exception or this |
93 | exception if there is none. |
94 | |
95 | @see Throwable#printStackTrace |
96 | */ |
97 | public void printStackTrace() { |
98 | Throwable je = getJavaException(); |
99 | if (je != null) |
100 | je.printStackTrace(); |
101 | else |
102 | super.printStackTrace(); |
103 | } |
104 | /** |
105 | Print the stack trace of the wrapped java exception or this |
106 | exception if there is none. |
107 | |
108 | @see Throwable#printStackTrace |
109 | */ |
110 | public void printStackTrace(PrintStream s) { |
111 | Throwable je = getJavaException(); |
112 | if (je != null) |
113 | je.printStackTrace(s); |
114 | else |
115 | super.printStackTrace(s); |
116 | } |
117 | /** |
118 | Print the stack trace of the wrapped java exception or this |
119 | exception if there is none. |
120 | |
121 | @see Throwable#printStackTrace |
122 | */ |
123 | public void printStackTrace(PrintWriter s) { |
124 | Throwable je = getJavaException(); |
125 | if (je != null) |
126 | je.printStackTrace(s); |
127 | else |
128 | super.printStackTrace(s); |
129 | } |
130 | |
131 | /* |
132 | ** Methods of Object |
133 | */ |
134 | |
135 | /** |
136 | Override Throwables toString() to avoid the class name |
137 | appearing in the message. |
138 | */ |
139 | public String toString() { |
140 | return "SQL Exception: " + getMessage(); |
141 | } |
142 | |
143 | /* |
144 | ** Some hack methods for 3.0.1. These will get cleaned up in main |
145 | ** with the exception re-work. |
146 | */ |
147 | private transient boolean simpleWrapper; |
148 | public static SQLException wrapStandardException(String message, String messageId, int code, Throwable se) { |
149 | EmbedSQLException csqle = new EmbedSQLException(message, messageId, (SQLException) null, code, se, (se instanceof StandardException) ? ((StandardException)se).getArguments() : null); |
150 | csqle.simpleWrapper = true; |
151 | return csqle; |
152 | } |
153 | public boolean isSimpleWrapper() { |
154 | return simpleWrapper; |
155 | } |
156 | } |