001: /*
002:
003: Derby - Class org.apache.derby.diag.ErrorMessages
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.diag;
023:
024: import java.util.Hashtable;
025: import java.util.Enumeration;
026: import java.sql.ResultSetMetaData;
027: import java.sql.SQLException;
028: import java.sql.Types;
029: import java.util.Properties;
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.util.Enumeration;
033: import org.apache.derby.vti.VTICosting;
034: import org.apache.derby.vti.VTIEnvironment;
035: import java.lang.Math;
036: import org.apache.derby.iapi.error.StandardException;
037: import org.apache.derby.iapi.error.ExceptionSeverity;
038: import org.apache.derby.iapi.services.i18n.MessageService;
039: import org.apache.derby.iapi.reference.Limits;
040: import org.apache.derby.iapi.util.StringUtil;
041:
042: import org.apache.derby.vti.VTITemplate;
043: import org.apache.derby.vti.VTICosting;
044: import org.apache.derby.vti.VTIEnvironment;
045:
046: import org.apache.derby.impl.jdbc.EmbedResultSetMetaData;
047: import org.apache.derby.iapi.sql.ResultColumnDescriptor;
048:
049: /**
050: * ErrorMessage shows all the SQLStates, locale-sensitive error
051: * messages, and exception severities for a database.
052: *
053: * <p>To use it, query it as follows:</p>
054: * <PRE> SELECT* FROM NEW org.apache.derby.diag.ErrorMessages() AS EQ; </PRE>
055: * <P>The following columns will be returned:
056: * <UL><LI>SQL_STATE--VARCHAR(5) - nullable. The SQLState of the SQLException.<br>
057: * (The code returned by getSQLState() in SQLException.)</LI>
058: * <LI>MESSAGE--VARCHAR(32672) - nullable. The error message<br>
059: * (The code returned by getMessage() in SQLException.)</LI>
060: * <LI>SEVERITY--INTEGER - nullable. The Cloudscape code for the severity.<br>
061: * (The code returned by getErrorCode() in SQLException.)</LI>
062: * </UL>
063: *
064: */
065: public final class ErrorMessages extends VTITemplate implements
066: VTICosting, java.security.PrivilegedAction {
067:
068: /* The name of the file containing all the SQLSTate codes.
069: * The class gets the SQLState code from the messages
070: * file (messages_en.properties). Then it uses StandardException to get
071: * the exception severities and locale-sensitive error messages.
072: */
073:
074: /** */
075: private Properties p;
076: /** */
077: private Enumeration keys;
078: /** */
079: private String k;
080: /** */
081: private String SQLState;
082: /** */
083: private String message;
084: /** */
085: private int severity;
086:
087: /** */
088: public ErrorMessages() throws IOException {
089:
090: loadProperties();
091: }
092:
093: /** *
094: * @see java.sql.ResultSet#next
095: */
096: public boolean next() {
097: boolean retCode = true;
098:
099: if (!keys.hasMoreElements()) {
100: close();
101: retCode = false;
102: return retCode;
103:
104: }
105:
106: k = (String) keys.nextElement();
107:
108: if (notAnException()) {
109: retCode = next();
110: }
111:
112: if (retCode) {
113: SQLState = StandardException.getSQLStateFromIdentifier(k);
114: message = MessageService.getTextMessage(k);
115: message = StringUtil.truncate(message,
116: Limits.DB2_VARCHAR_MAXWIDTH);
117: }
118: return retCode;
119: }
120:
121: /** *
122: * @see java.sql.ResultSet#close
123: */
124: public void close() {
125: p = null;
126: k = null;
127: keys = null;
128: }
129:
130: /** *
131: * @see java.sql.ResultSet#getMetaData
132: */
133: public ResultSetMetaData getMetaData() {
134: return metadata;
135: }
136:
137: /** *
138: * @exception SQLException column at index is not found
139: * @see java.sql.ResultSet#getString
140: */
141: public String getString(int columnIndex) throws SQLException {
142: switch (columnIndex) {
143: case 1:
144: return SQLState;
145: case 2:
146: return message;
147: default:
148: return super .getString(columnIndex); // throw an exception
149: }
150: }
151:
152: /** *
153: * @exception SQLException column at index is not found
154: * @see java.sql.ResultSet#getInt
155: */
156: public int getInt(int columnIndex) throws SQLException {
157: switch (columnIndex) {
158: case 3:
159: return severity;
160: default:
161: return super .getInt(columnIndex); // throw an exception
162: }
163: }
164:
165: /** */
166: private void loadProperties() throws IOException {
167: p = new Properties();
168: for (int i = 0; i < 50; i++) {
169: msgFile = i;
170: InputStream is = (InputStream) java.security.AccessController
171: .doPrivileged(this );
172: if (is == null)
173: continue;
174:
175: try {
176: p.load(is);
177: } finally {
178: try {
179: is.close();
180: } catch (IOException ioe) {
181: }
182: }
183: }
184: keys = p.keys();
185: }
186:
187: /** */
188: private boolean notAnException() {
189:
190: if (k.length() < 5)
191: return true;
192: int tempSeverity = StandardException
193: .getSeverityFromIdentifier(k);
194: //if the severity is not one of our customer-visible severity
195: //levels, it's just a message, not an SQLException
196: if (tempSeverity < (ExceptionSeverity.NO_APPLICABLE_SEVERITY + 1))
197: return true;
198: severity = tempSeverity;
199: return false;
200: }
201:
202: /*VTICosting methods*/
203:
204: /** */
205: public double getEstimatedRowCount(VTIEnvironment vtiEnvironment) {
206: return 1000;
207: }
208:
209: /** */
210: public double getEstimatedCostPerInstantiation(
211: VTIEnvironment vtiEnvironment) {
212: return 5000;
213: }
214:
215: /** */
216: public boolean supportsMultipleInstantiations(
217: VTIEnvironment vtiEnvironment) {
218: return true;
219: }
220:
221: private int msgFile;
222:
223: public final Object run() {
224: InputStream msg = getClass().getResourceAsStream(
225: "/org/apache/derby/loc/m" + msgFile + "_en.properties");
226: msgFile = 0;
227: return msg;
228:
229: }
230:
231: /*
232: ** Metadata
233: */
234: private static final ResultColumnDescriptor[] columnInfo = {
235:
236: EmbedResultSetMetaData.getResultColumnDescriptor(
237: "SQL_STATE", Types.VARCHAR, true, 5),
238: EmbedResultSetMetaData.getResultColumnDescriptor("MESSAGE",
239: Types.VARCHAR, true, Limits.DB2_VARCHAR_MAXWIDTH),
240: EmbedResultSetMetaData.getResultColumnDescriptor(
241: "SEVERITY", Types.INTEGER, true), };
242:
243: private static final ResultSetMetaData metadata = new EmbedResultSetMetaData(
244: columnInfo);
245:
246: }
|