KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.diag.ErrorLogReader
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.io.BufferedReader JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.InputStreamReader JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29
30 import java.util.Hashtable JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.Properties JavaDoc;
33 import java.sql.ResultSetMetaData JavaDoc;
34 import java.sql.SQLException JavaDoc;
35 import java.sql.Types JavaDoc;
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 /**
44
45     ErrorLogReader is a virtual table interface (VTI) which contains all the statements
46     of "interest" in db2j.<!-- -->log or a specified file when
47     db2j.<!-- -->language.<!-- -->logStatementText=true.
48     
49     
50     <P>One use of this VTI is to determine the active transactions
51     and the SQL statements in those transactions at a given point in time, say
52     when a deadlock or lock timeout occurred. In order to do that, you must first
53     find the timestamp (timestampConstant) of interest in the error log.
54     The SQL to view the active transactions at a given in time is:
55     <PRE>SELECT vti.ts, threadid, cast(xid as int) as xid_int, cast(lccid as int) as lccid_int, logtext
56          FROM new org.apache.derby.diag.ErrorLogReader() vti,
57             (VALUES timestampConstant) t(ts)
58          WHERE vti.ts <= t.ts AND
59                 vti.ts >
60                     (SELECT MAX(ts) IS NULL ? '2000-01-01 00:00:00.1' : MAX(ts)
61                      FROM new org.apache.derby.diag.ErrorLogReader() vti_i
62                      WHERE (logtext LIKE 'Committing%' OR
63                             logtext LIKE 'Rolling%') AND
64                            vti.xid = vti_i.xid AND ts < t.ts)
65          ORDER BY xid_int, vti.ts
66     </PRE>
67
68     <P>The ErrorLogReader virtual table has the following columns:
69     <UL><LI>TS varchar(26) - the timestamp of the statement.</LI>
70     <LI>THREADID varchar(40) - the thread name.</LI>
71     <LI>XID varchar(15) - the transaction ID.</LI>
72     <LI>LCCID varchar(15) - the connection ID.</LI>
73     <LI>DATABASE varchar(128) - Database name
74     <LI>DRDAID varchar(50) - nullable. DRDA ID for network server session.
75     <LI>LOGTEXT long varchar - text of the statement or commit or rollback.</LI>
76     </UL>
77
78  */

79 public class ErrorLogReader extends VTITemplate
80 {
81     /*
82     ** private
83     */

84     private boolean gotFile;
85     private InputStreamReader JavaDoc inputFileStreamReader;
86     private InputStream JavaDoc inputStream;
87     private BufferedReader JavaDoc bufferedReader;
88     private String JavaDoc inputFileName;
89
90     // Variables for current row
91
private String JavaDoc 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 JavaDoc GMT_STRING = " GMT";
101     private static final String JavaDoc PARAMETERS_STRING = "Parameters:";
102     private static final String JavaDoc BEGIN_THREAD_STRING = "[";
103     private static final String JavaDoc END_THREAD_STRING = "]";
104     private static final String JavaDoc BEGIN_XID_STRING = "= ";
105     private static final String JavaDoc END_XID_STRING = ")";
106     private static final String JavaDoc BEGIN_DATABASE_STRING = "(DATABASE =";
107     private static final String JavaDoc END_DATABASE_STRING = ")";
108     private static final String JavaDoc BEGIN_DRDAID_STRING = "(DRDAID =";
109     private static final String JavaDoc END_DRDAID_STRING = ")";
110     private static final String JavaDoc BEGIN_EXECUTING_STRING = "Executing prepared";
111     private static final String JavaDoc END_EXECUTING_STRING = " :End prepared";
112
113
114     /**
115         ErrorLogReader() accesses the derby.log in
116         derby.system.home, if set, otherwise it looks in the current directory.
117         ErrorLogReader('filename') will access the specified
118         file name.
119      */

120     public ErrorLogReader()
121     {
122         String JavaDoc 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 JavaDoc inputFileName)
133     {
134         this.inputFileName = inputFileName;
135     }
136
137     /**
138         @see java.sql.ResultSet#getMetaData
139      */

140     public ResultSetMetaData JavaDoc getMetaData()
141     {
142         return metadata;
143     }
144
145     /**
146         @see java.sql.ResultSet#next
147         @exception SQLException If database-access error occurs.
148      */

149     public boolean next() throws SQLException JavaDoc
150     {
151         if (! gotFile)
152         {
153             gotFile = true;
154             try
155             {
156                 inputFileStreamReader = new InputStreamReader JavaDoc(new FileInputStream JavaDoc(inputFileName));
157                 bufferedReader = new BufferedReader JavaDoc(inputFileStreamReader, 32*1024);
158             }
159             catch (FileNotFoundException JavaDoc ex)
160             {
161                 throw new SQLException JavaDoc(ex.getMessage());
162             }
163         }
164
165         while (true)
166         {
167             try
168             {
169                 line = bufferedReader.readLine();
170             }
171             catch (java.io.IOException JavaDoc ioe)
172             {
173                 throw new SQLException JavaDoc(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             // Skip parameters
189
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     /**
203         @see java.sql.ResultSet#close
204      */

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 JavaDoc ioe)
215             {
216                 // eat exceptions during close;
217
}
218             finally
219             {
220                 bufferedReader = null;
221                 inputFileStreamReader = null;
222             }
223         }
224     }
225
226     /**
227         All columns in the Db2jLogReader VTI have a of String type.
228         @see java.sql.ResultSet#getString
229         @exception SQLException If database-access error occurs.
230      */

231     public String JavaDoc getString(int columnNumber)
232         throws SQLException JavaDoc
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                 /* Executing prepared statement is a special case as
254                  * it could span multiple lines
255                  */

256                 String JavaDoc 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                 /* We need to build string until we find the end of the text */
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 JavaDoc ioe)
283                     {
284                         throw new SQLException JavaDoc("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     /**
309         @see java.sql.ResultSet#wasNull
310      */

311     public boolean wasNull()
312     {
313         return false;
314     }
315
316     /* MetaData
317      */

318     
319     // column1: TS varchar(26) not null
320
// column2: THREADID varchar(40) not null
321
// column3: XID varchar(15) not null
322
// column4: LCCID varchar(15) not null
323
// column5: DATABASE varchar(128) not null
324
// column6: DRDAID varchar(50) nullable
325
// column5: LOGTEXT VARCHAR(max) not null
326
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 JavaDoc metadata = new EmbedResultSetMetaData(columnInfo);
336
337 }
338
339
340
Popular Tags