KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > message > TraceObject


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.message;
6
7 import java.io.FileWriter JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.PrintWriter JavaDoc;
10 import java.math.BigDecimal JavaDoc;
11 import java.sql.SQLException JavaDoc;
12
13 import org.h2.engine.Constants;
14 import org.h2.util.StringUtils;
15
16 /**
17  *
18  * @author tgdmuth6
19  *
20  */

21 public class TraceObject {
22     public static final int CALLABLE_STATEMENT = 0, CONNECTION = 1, DATABASE_META_DATA = 2,
23         PREPARED_STATEMENT = 3, RESULT_SET = 4, RESULT_SET_META_DATA = 5,
24         SAVEPOINT = 6, SQL_EXCEPTION = 7, STATEMENT = 8, BLOB = 9, CLOB = 10,
25         PARAMETER_META_DATA = 11;
26     public static final int DATASOURCE = 12, XA_DATASOURCE = 13, XID = 14;
27     
28     private static int LAST = XID + 1;
29     private Trace trace;
30     private static final int[] ID = new int[LAST];
31     private static final String JavaDoc[] PREFIX = {
32         "call", "conn", "dbMeta", "prep", "rs", "rsMeta", "sp", "ex", "stat", "blob", "clob", "pMeta",
33         "ds", "xads", "xid"
34     };
35     private int type, id;
36     
37     protected void setTrace(Trace trace, int type, int id) {
38         this.trace = trace;
39         this.type = type;
40         this.id = id;
41     }
42     
43     protected int getTraceId() {
44         return id;
45     }
46     
47     /**
48      * INTERNAL
49      */

50     public String JavaDoc toString() {
51         return PREFIX[type] + id ;
52     }
53     
54     protected int getNextId(int type) {
55         return ID[type]++;
56     }
57     
58     protected boolean debug() {
59         return trace.debug();
60     }
61     
62     protected boolean info() {
63         return trace.info();
64     }
65
66     protected Trace getTrace() {
67         return trace;
68     }
69     
70     protected void debugCodeAssign(String JavaDoc className, int type, int id) {
71         if(!trace.debug()) {
72             return;
73         }
74         trace.debugCode(className + " " + toString() + " = ");
75     }
76     
77     protected void infoCodeAssign(String JavaDoc className, int type, int id) {
78         if(!trace.info()) {
79             return;
80         }
81         trace.infoCode(className + " " + toString() + " = ");
82     }
83     
84     protected void debugCodeCall(String JavaDoc text) {
85         if(!trace.debug()) {
86             return;
87         }
88         trace.debugCode(toString() + "." + text + "();");
89     }
90     
91     protected void debugCodeCall(String JavaDoc text, long param) {
92         if(!trace.debug()) {
93             return;
94         }
95         trace.debugCode(toString() + "." + text + "("+param+");");
96     }
97     
98     protected void debugCodeCall(String JavaDoc text, String JavaDoc param) {
99         if(!trace.debug()) {
100             return;
101         }
102         trace.debugCode(toString() + "." + text + "("+quote(param)+");");
103     }
104     
105     protected void debugCode(String JavaDoc text) {
106         if(!trace.debug()) {
107             return;
108         }
109         trace.debugCode(toString() + "." + text);
110     }
111     
112     protected String JavaDoc quote(String JavaDoc s) {
113         return StringUtils.quoteJavaString(s);
114     }
115     
116     protected String JavaDoc quoteTime(java.sql.Time JavaDoc x) {
117         if(x == null) {
118             return "null";
119         }
120         return "Time.valueOf(\"" + x.toString() + "\")";
121     }
122
123     protected String JavaDoc quoteTimestamp(java.sql.Timestamp JavaDoc x) {
124         if(x == null) {
125             return "null";
126         }
127         return "Timestamp.valueOf(\"" + x.toString() + "\")";
128     }
129     
130     protected String JavaDoc quoteDate(java.sql.Date JavaDoc x) {
131         if(x == null) {
132             return "null";
133         }
134         return "Date.valueOf(\"" + x.toString() + "\")";
135     }
136     
137     protected String JavaDoc quoteBigDecimal(BigDecimal JavaDoc x) {
138         if(x == null) {
139             return "null";
140         }
141         return "new BigDecimal(\"" + x.toString() + "\")";
142     }
143     
144     protected String JavaDoc quoteBytes(byte[] x) {
145         if(x == null) {
146             return "null";
147         }
148         return "new byte[" + x.length + "]";
149     }
150     
151     protected String JavaDoc quoteArray(String JavaDoc[] s) {
152         return StringUtils.quoteJavaStringArray(s);
153     }
154     
155     protected String JavaDoc quoteIntArray(int[] s) {
156         return StringUtils.quoteJavaIntArray(s);
157     }
158     
159     protected SQLException JavaDoc logAndConvert(Throwable JavaDoc e) {
160         if(Constants.LOG_ALL_ERRORS) {
161             synchronized(this.getClass()) {
162                 // e.printStackTrace();
163
try {
164                     FileWriter JavaDoc writer = new FileWriter JavaDoc(Constants.LOG_ALL_ERRORS_FILE, true);
165                     PrintWriter JavaDoc p = new PrintWriter JavaDoc(writer);
166                     e.printStackTrace(p);
167                     p.close();
168                     writer.close();
169                 } catch(IOException JavaDoc e2) {
170                     e2.printStackTrace();
171                 }
172             }
173         }
174         if(trace == null) {
175             TraceSystem.traceThrowable(e);
176         } else {
177             if(e instanceof SQLException JavaDoc) {
178                 trace.error("SQLException", e);
179                 return (SQLException JavaDoc)e;
180             } else {
181                 trace.error("Uncaught Exception", e);
182             }
183         }
184         return Message.convert(e);
185     }
186     
187 }
188
Popular Tags