KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > diag > ErrorMessages


1 /*
2
3    Derby - Class org.apache.derby.diag.ErrorMessages
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.diag;
23
24 import java.util.Hashtable JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.sql.ResultSetMetaData JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.sql.Types JavaDoc;
29 import java.util.Properties JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import org.apache.derby.vti.VTICosting;
34 import org.apache.derby.vti.VTIEnvironment;
35 import java.lang.Math JavaDoc;
36 import org.apache.derby.iapi.error.StandardException;
37 import org.apache.derby.iapi.error.ExceptionSeverity;
38 import org.apache.derby.iapi.services.i18n.MessageService;
39 import org.apache.derby.iapi.reference.Limits;
40 import org.apache.derby.iapi.util.StringUtil;
41
42 import org.apache.derby.vti.VTITemplate;
43 import org.apache.derby.vti.VTICosting;
44 import org.apache.derby.vti.VTIEnvironment;
45
46 import org.apache.derby.impl.jdbc.EmbedResultSetMetaData;
47 import org.apache.derby.iapi.sql.ResultColumnDescriptor;
48
49
50 /**
51  * ErrorMessage shows all the SQLStates, locale-sensitive error
52  * messages, and exception severities for a database.
53  *
54  * <p>To use it, query it as follows:</p>
55  * <PRE> SELECT* FROM NEW org.apache.derby.diag.ErrorMessages() AS EQ; </PRE>
56  * <P>The following columns will be returned:
57  * <UL><LI>SQL_STATE--VARCHAR(5) - nullable. The SQLState of the SQLException.<br>
58  * (The code returned by getSQLState() in SQLException.)</LI>
59  * <LI>MESSAGE--VARCHAR(32672) - nullable. The error message<br>
60  * (The code returned by getMessage() in SQLException.)</LI>
61  * <LI>SEVERITY--INTEGER - nullable. The Cloudscape code for the severity.<br>
62  * (The code returned by getErrorCode() in SQLException.)</LI>
63  * </UL>
64  *
65  */

66 public final class ErrorMessages extends VTITemplate implements VTICosting, java.security.PrivilegedAction JavaDoc {
67     
68     /* The name of the file containing all the SQLSTate codes.
69      * The class gets the SQLState code from the messages
70      * file (messages_en.properties). Then it uses StandardException to get
71      * the exception severities and locale-sensitive error messages.
72      */

73     
74
75         /** */
76     private Properties JavaDoc p;
77         /** */
78     private Enumeration JavaDoc keys;
79         /** */
80     private String JavaDoc k;
81         /** */
82     private String JavaDoc SQLState;
83         /** */
84     private String JavaDoc message;
85         /** */
86     private int severity;
87     
88
89         /** */
90     public ErrorMessages() throws IOException JavaDoc{
91         
92         loadProperties();
93     }
94
95         /** *
96          * @see java.sql.ResultSet#next
97          */

98     public boolean next() {
99         boolean retCode = true;
100
101         if (!keys.hasMoreElements()) {
102             close();
103             retCode = false;
104             return retCode;
105             
106         }
107
108         k = (String JavaDoc)keys.nextElement();
109
110         if (notAnException()) {
111             retCode = next();
112         }
113
114         if (retCode) {
115           SQLState =StandardException.getSQLStateFromIdentifier(k);
116           message = MessageService.getTextMessage(k);
117           message = StringUtil.truncate(message, Limits.DB2_VARCHAR_MAXWIDTH);
118         }
119         return retCode;
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          * @see java.sql.ResultSet#getMetaData
131          */

132     public ResultSetMetaData JavaDoc getMetaData() {
133         return metadata;
134     }
135
136     /** *
137      * @exception SQLException column at index is not found
138      * @see java.sql.ResultSet#getString
139      */

140     public String JavaDoc getString(int columnIndex) throws SQLException JavaDoc {
141         switch (columnIndex) {
142         case 1: return SQLState;
143         case 2: return message;
144         default: return super.getString(columnIndex); // throw an exception
145
}
146     }
147     /** *
148      * @exception SQLException column at index is not found
149      * @see java.sql.ResultSet#getInt
150      */

151     public int getInt(int columnIndex) throws SQLException JavaDoc {
152         switch (columnIndex) {
153         case 3: return severity;
154         default: return super.getInt(columnIndex); // throw an exception
155
}
156     }
157     
158     
159         /** */
160     private void loadProperties() throws IOException JavaDoc
161     {
162         p = new Properties JavaDoc();
163         for (int i = 0; i < 50; i++) {
164             msgFile = i;
165             InputStream JavaDoc is = (InputStream JavaDoc) java.security.AccessController.doPrivileged(this);
166             if (is == null)
167                 continue;
168
169             try {
170                 p.load(is);
171             } finally {
172                 try {
173                     is.close();
174                 } catch (IOException JavaDoc ioe) {
175                 }
176             }
177         }
178         keys = p.keys();
179     }
180
181         /** */
182     private boolean notAnException() {
183
184         if (k.length() < 5)
185             return true;
186         int tempSeverity = StandardException.getSeverityFromIdentifier(k);
187         //if the severity is not one of our customer-visible severity
188
//levels, it's just a message, not an SQLException
189
if (tempSeverity < (ExceptionSeverity.NO_APPLICABLE_SEVERITY + 1))
190       return true;
191     severity = tempSeverity;
192     return false;
193     }
194
195         
196     /*VTICosting methods*/
197         
198
199         /** */
200     public double getEstimatedRowCount(VTIEnvironment vtiEnvironment)
201     {
202         return 1000;
203     }
204
205         /** */
206     public double getEstimatedCostPerInstantiation(VTIEnvironment vtiEnvironment)
207     {
208         return 5000;
209     }
210
211         /** */
212     public boolean supportsMultipleInstantiations(VTIEnvironment vtiEnvironment)
213     {
214         return true;
215     }
216
217     private int msgFile;
218     
219     public final Object JavaDoc run() {
220         InputStream JavaDoc msg = getClass().getResourceAsStream("/org/apache/derby/loc/m" + msgFile + "_en.properties");
221         msgFile = 0;
222         return msg;
223
224     }
225
226     /*
227     ** Metadata
228     */

229     private static final ResultColumnDescriptor[] columnInfo = {
230
231         EmbedResultSetMetaData.getResultColumnDescriptor("SQL_STATE", Types.VARCHAR, true, 5),
232         EmbedResultSetMetaData.getResultColumnDescriptor("MESSAGE", Types.VARCHAR, true, Limits.DB2_VARCHAR_MAXWIDTH),
233         EmbedResultSetMetaData.getResultColumnDescriptor("SEVERITY", Types.INTEGER, true),
234     };
235     
236     private static final ResultSetMetaData JavaDoc metadata = new EmbedResultSetMetaData(columnInfo);
237
238 }
239
Popular Tags