1 21 22 package org.apache.derby.diag; 23 24 import java.util.Hashtable ; 25 import java.util.Enumeration ; 26 import java.sql.ResultSetMetaData ; 27 import java.sql.SQLException ; 28 import java.sql.Types ; 29 import java.util.Properties ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.util.Enumeration ; 33 import org.apache.derby.vti.VTICosting; 34 import org.apache.derby.vti.VTIEnvironment; 35 import java.lang.Math ; 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 66 public final class ErrorMessages extends VTITemplate implements VTICosting, java.security.PrivilegedAction { 67 68 73 74 75 76 private Properties p; 77 78 private Enumeration keys; 79 80 private String k; 81 82 private String SQLState; 83 84 private String message; 85 86 private int severity; 87 88 89 90 public ErrorMessages() throws IOException { 91 92 loadProperties(); 93 } 94 95 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 )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 124 public void close() { 125 p = null; 126 k = null; 127 keys = null; 128 } 129 132 public ResultSetMetaData getMetaData() { 133 return metadata; 134 } 135 136 140 public String getString(int columnIndex) throws SQLException { 141 switch (columnIndex) { 142 case 1: return SQLState; 143 case 2: return message; 144 default: return super.getString(columnIndex); } 146 } 147 151 public int getInt(int columnIndex) throws SQLException { 152 switch (columnIndex) { 153 case 3: return severity; 154 default: return super.getInt(columnIndex); } 156 } 157 158 159 160 private void loadProperties() throws IOException 161 { 162 p = new Properties (); 163 for (int i = 0; i < 50; i++) { 164 msgFile = i; 165 InputStream is = (InputStream ) 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 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 (tempSeverity < (ExceptionSeverity.NO_APPLICABLE_SEVERITY + 1)) 190 return true; 191 severity = tempSeverity; 192 return false; 193 } 194 195 196 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 run() { 220 InputStream msg = getClass().getResourceAsStream("/org/apache/derby/loc/m" + msgFile + "_en.properties"); 221 msgFile = 0; 222 return msg; 223 224 } 225 226 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 metadata = new EmbedResultSetMetaData(columnInfo); 237 238 } 239 | Popular Tags |