1 21 22 package org.apache.derby.diag; 23 24 import java.io.BufferedReader ; 25 import java.io.FileNotFoundException ; 26 import java.io.InputStream ; 27 import java.io.InputStreamReader ; 28 import java.io.FileInputStream ; 29 30 import java.util.Hashtable ; 31 import java.util.Enumeration ; 32 import java.util.Properties ; 33 import java.sql.ResultSetMetaData ; 34 import java.sql.SQLException ; 35 import java.sql.Types ; 36 import org.apache.derby.vti.VTITemplate; 37 import org.apache.derby.iapi.reference.Limits; 38 import org.apache.derby.iapi.util.StringUtil; 39 40 import org.apache.derby.iapi.sql.ResultColumnDescriptor; 41 import org.apache.derby.impl.jdbc.EmbedResultSetMetaData; 42 43 79 public class ErrorLogReader extends VTITemplate 80 { 81 84 private boolean gotFile; 85 private InputStreamReader inputFileStreamReader; 86 private InputStream inputStream; 87 private BufferedReader bufferedReader; 88 private String inputFileName; 89 90 private String line; 92 private int gmtIndex; 93 private int threadIndex; 94 private int xidIndex; 95 private int lccidIndex; 96 private int databaseIndex; 97 private int drdaidIndex; 98 99 100 private static final String GMT_STRING = " GMT"; 101 private static final String PARAMETERS_STRING = "Parameters:"; 102 private static final String BEGIN_THREAD_STRING = "["; 103 private static final String END_THREAD_STRING = "]"; 104 private static final String BEGIN_XID_STRING = "= "; 105 private static final String END_XID_STRING = ")"; 106 private static final String BEGIN_DATABASE_STRING = "(DATABASE ="; 107 private static final String END_DATABASE_STRING = ")"; 108 private static final String BEGIN_DRDAID_STRING = "(DRDAID ="; 109 private static final String END_DRDAID_STRING = ")"; 110 private static final String BEGIN_EXECUTING_STRING = "Executing prepared"; 111 private static final String END_EXECUTING_STRING = " :End prepared"; 112 113 114 120 public ErrorLogReader() 121 { 122 String home = System.getProperty("derby.system.home"); 123 124 inputFileName = "derby.log"; 125 126 if (home != null) 127 { 128 inputFileName = home + "/" + inputFileName; 129 } 130 } 131 132 public ErrorLogReader(String inputFileName) 133 { 134 this.inputFileName = inputFileName; 135 } 136 137 140 public ResultSetMetaData getMetaData() 141 { 142 return metadata; 143 } 144 145 149 public boolean next() throws SQLException 150 { 151 if (! gotFile) 152 { 153 gotFile = true; 154 try 155 { 156 inputFileStreamReader = new InputStreamReader (new FileInputStream (inputFileName)); 157 bufferedReader = new BufferedReader (inputFileStreamReader, 32*1024); 158 } 159 catch (FileNotFoundException ex) 160 { 161 throw new SQLException (ex.getMessage()); 162 } 163 } 164 165 while (true) 166 { 167 try 168 { 169 line = bufferedReader.readLine(); 170 } 171 catch (java.io.IOException ioe) 172 { 173 throw new SQLException (ioe.getMessage()); 174 } 175 176 if (line == null) 177 { 178 return false; 179 } 180 181 gmtIndex = line.indexOf(GMT_STRING); 182 threadIndex = line.indexOf(BEGIN_THREAD_STRING); 183 xidIndex = line.indexOf(BEGIN_XID_STRING); 184 lccidIndex = line.indexOf(BEGIN_XID_STRING, xidIndex + 1); 185 databaseIndex = line.indexOf(BEGIN_DATABASE_STRING, lccidIndex + 1); 186 drdaidIndex = line.indexOf(BEGIN_DRDAID_STRING, databaseIndex + 1); 187 188 if (line.indexOf(PARAMETERS_STRING) != -1) 190 { 191 continue; 192 } 193 194 if (gmtIndex != -1 && threadIndex != -1 && xidIndex != -1 && 195 databaseIndex != -1) 196 { 197 return true; 198 } 199 } 200 } 201 202 205 public void close() 206 { 207 if (bufferedReader != null) 208 { 209 try 210 { 211 bufferedReader.close(); 212 inputFileStreamReader.close(); 213 } 214 catch (java.io.IOException ioe) 215 { 216 } 218 finally 219 { 220 bufferedReader = null; 221 inputFileStreamReader = null; 222 } 223 } 224 } 225 226 231 public String getString(int columnNumber) 232 throws SQLException 233 { 234 switch (columnNumber) 235 { 236 case 1: 237 return line.substring(0, gmtIndex); 238 239 case 2: 240 return line.substring(threadIndex + 1, line.indexOf(END_THREAD_STRING)); 241 242 case 3: 243 return line.substring(xidIndex + 2, line.indexOf(END_XID_STRING, xidIndex)); 244 245 case 4: 246 return line.substring(lccidIndex + 2, line.indexOf(END_XID_STRING, lccidIndex)); 247 248 case 5: 249 return line.substring(databaseIndex + BEGIN_DATABASE_STRING.length(), line.indexOf(END_DATABASE_STRING, databaseIndex)); 250 case 6: 251 return line.substring(drdaidIndex + BEGIN_DRDAID_STRING.length(), line.indexOf(END_DRDAID_STRING, drdaidIndex)); 252 case 7: 253 256 String output; 257 if (line.indexOf(BEGIN_EXECUTING_STRING) == -1) 258 { 259 output = line.substring(line.indexOf(END_DRDAID_STRING, drdaidIndex) + 3); 260 } 261 else 262 { 263 264 265 int endIndex = line.indexOf(END_EXECUTING_STRING, drdaidIndex); 266 if (endIndex == -1) 267 { 268 output = line.substring(line.indexOf(END_DRDAID_STRING, drdaidIndex) + 3); 269 } 270 else 271 { 272 output = line.substring(line.indexOf(END_XID_STRING, drdaidIndex) + 3, 273 endIndex); 274 } 275 276 while (endIndex == -1) 277 { 278 try 279 { 280 line = bufferedReader.readLine(); 281 } 282 catch (java.io.IOException ioe) 283 { 284 throw new SQLException ("Error reading file " + ioe); 285 } 286 endIndex = line.indexOf(END_EXECUTING_STRING); 287 if (endIndex == -1) 288 { 289 output = output + line; 290 } 291 else 292 { 293 output = output + line.substring(0, endIndex); 294 } 295 } 296 } 297 298 output = StringUtil.truncate(output, Limits.DB2_VARCHAR_MAXWIDTH); 299 300 return output; 301 302 default: 303 return ""; 304 } 305 } 306 307 308 311 public boolean wasNull() 312 { 313 return false; 314 } 315 316 318 319 private static final ResultColumnDescriptor[] columnInfo = { 327 EmbedResultSetMetaData.getResultColumnDescriptor("TS", Types.VARCHAR, false, 26), 328 EmbedResultSetMetaData.getResultColumnDescriptor("THREADID", Types.VARCHAR, false, 40), 329 EmbedResultSetMetaData.getResultColumnDescriptor("XID", Types.VARCHAR, false, 15), 330 EmbedResultSetMetaData.getResultColumnDescriptor("LCCID", Types.VARCHAR, false, 15), 331 EmbedResultSetMetaData.getResultColumnDescriptor("DATABASE", Types.VARCHAR, false, 128), 332 EmbedResultSetMetaData.getResultColumnDescriptor("DRDAID", Types.VARCHAR, true, 50), 333 EmbedResultSetMetaData.getResultColumnDescriptor("LOGTEXT",Types.VARCHAR, false, Limits.DB2_VARCHAR_MAXWIDTH) 334 }; 335 private static final ResultSetMetaData metadata = new EmbedResultSetMetaData(columnInfo); 336 337 } 338 339 340 | Popular Tags |