001: /*
002:
003: Derby - Class org.apache.derby.impl.jdbc.EmbedSQLException
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.jdbc;
023:
024: import org.apache.derby.iapi.error.StandardException;
025:
026: import java.sql.SQLException;
027: import java.io.PrintStream;
028: import java.io.PrintWriter;
029:
030: /**
031: This class is what gets send over the wire in client/server
032: configuration. When running embedded, this has the detailed
033: stack trace for exceptions. In case of client/server, server
034: has all the stack trace information but client doesn't get
035: the stack trace, just the sql exception. The reason for this
036: implementation is the stack trace information is more relevant
037: on the server side and it also decreases the size of client
038: jar file tremendously.
039: */
040: public class EmbedSQLException extends SQLException {
041:
042: private transient Object[] arguments;
043: private String messageId;
044:
045: /**
046: Java exception that caused this exception, can be null.
047: */
048: //Because it's transient, it doesn't get sent over to the client
049: //side and hence the classes which needs to be included in the
050: //client.jar file decreases 5 folds.
051: private transient Throwable javaException;
052:
053: /**
054: * Because SQLException does not have settable fields,
055: * the caller of the constructor must do message lookup,
056: * and pass the appropriate values here for message, messageId,
057: * and next exception.
058: */
059: EmbedSQLException(String message, String messageId,
060: SQLException nextException, int severity, Object[] args) {
061:
062: super (message, StandardException
063: .getSQLStateFromIdentifier(messageId), severity);
064: this .messageId = messageId;
065: arguments = args;
066: if (nextException != null)
067: this .setNextException(nextException);
068: }
069:
070: public EmbedSQLException(String message, String messageId,
071: SQLException nextException, int severity, Throwable t,
072: Object[] args) {
073:
074: super (message, StandardException
075: .getSQLStateFromIdentifier(messageId), severity);
076: this .messageId = messageId;
077: arguments = args;
078: if (nextException != null)
079: this .setNextException(nextException);
080: javaException = t;
081: }
082:
083: public Throwable getJavaException() {
084: return javaException;
085: }
086:
087: public String getMessageId() {
088: return messageId;
089: }
090:
091: public Object[] getArguments() {
092: return arguments;
093: }
094:
095: /**
096: Print the stack trace of the wrapped java exception or this
097: exception if there is none.
098:
099: @see Throwable#printStackTrace
100: */
101: public void printStackTrace() {
102: Throwable je = getJavaException();
103: if (je != null)
104: je.printStackTrace();
105: else
106: super .printStackTrace();
107: }
108:
109: /**
110: Print the stack trace of the wrapped java exception or this
111: exception if there is none.
112:
113: @see Throwable#printStackTrace
114: */
115: public void printStackTrace(PrintStream s) {
116: Throwable je = getJavaException();
117: if (je != null)
118: je.printStackTrace(s);
119: else
120: super .printStackTrace(s);
121: }
122:
123: /**
124: Print the stack trace of the wrapped java exception or this
125: exception if there is none.
126:
127: @see Throwable#printStackTrace
128: */
129: public void printStackTrace(PrintWriter s) {
130: Throwable je = getJavaException();
131: if (je != null)
132: je.printStackTrace(s);
133: else
134: super .printStackTrace(s);
135: }
136:
137: /*
138: ** Methods of Object
139: */
140:
141: /**
142: Override Throwables toString() to avoid the class name
143: appearing in the message.
144: */
145: public String toString() {
146: // We use java.sql.SQLException rather than the default toString(),
147: // which returns org.apache.derby.impl.jdbc.EmbedSQLException, so
148: // that (a) we're not exposing an internal class name and (b) so
149: // this is consistent with the network client, where SQLExceptions
150: // are vanilla java.sql classes and not our own subclass
151: return "java.sql.SQLException: " + getMessage();
152: }
153:
154: /*
155: ** Some hack methods for 3.0.1. These will get cleaned up in main
156: ** with the exception re-work.
157: */
158: private transient boolean simpleWrapper;
159:
160: public static SQLException wrapStandardException(String message,
161: String messageId, int code, Throwable se) {
162: EmbedSQLException csqle = new EmbedSQLException(
163: message,
164: messageId,
165: (SQLException) null,
166: code,
167: se,
168: (se instanceof StandardException) ? ((StandardException) se)
169: .getArguments()
170: : null);
171: csqle.simpleWrapper = true;
172: return csqle;
173: }
174:
175: public boolean isSimpleWrapper() {
176: return simpleWrapper;
177: }
178: }
|