KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > jdbc > pool > JDBCWrapper


1 package com.protomatter.jdbc.pool;
2
3 import java.io.*;
4 import java.sql.*;
5
6 import java.lang.reflect.*;
7
8 import com.protomatter.syslog.Channel;
9 import com.protomatter.util.Debug;
10
11 /**
12  * A base wrapper class that uses reflection to call methods.
13  */

14 class JDBCWrapper
15 {
16     private Channel log = Channel.forPackage(JDBCWrapper.class);
17     
18     private Object JavaDoc[] EMPTY_OBJECT_ARRAY = new Object JavaDoc[0];
19     private Class JavaDoc[] EMPTY_CLASS_ARRAY = new Class JavaDoc[0];
20
21     private String JavaDoc trimFromLastPeriod(String JavaDoc s)
22     {
23         int index = s.lastIndexOf(".");
24         if (index != -1)
25             return s.substring(index +1);
26         return s;
27     }
28
29     protected Object JavaDoc callMethod(Object JavaDoc target, String JavaDoc method, Class JavaDoc paramTypes[], Object JavaDoc params[])
30     throws SQLException
31     {
32         Class JavaDoc targetClass = target.getClass();
33         Class JavaDoc thisClass = this.getClass();
34         try
35         {
36             Method m = targetClass.getMethod(method, paramTypes);
37             long time = System.currentTimeMillis();
38             Object JavaDoc returnValue = m.invoke(target, params);
39             time = System.currentTimeMillis() - time;
40             
41             if (m.getReturnType() == Void.TYPE)
42                 log.debug(this, trimFromLastPeriod(thisClass.getName()) + "." + method + "(" + toString(params) + ")"
43                     + " call took " + time + "ms");
44             else
45                 log.debug(this, trimFromLastPeriod(thisClass.getName()) + "." + method + "(" + toString(params) + ") = " + toString(returnValue)
46                     + " call took " + time + "ms");
47
48             return returnValue;
49         }
50         catch (InvocationTargetException tx)
51         {
52             Throwable JavaDoc thrownException = tx.getTargetException();
53             if (thrownException instanceof SQLException)
54             {
55                 SQLException thrown = (SQLException)thrownException;
56                 log.debug(this, trimFromLastPeriod(thisClass.getName()) + "." + method + "(" + toString(params) + ") threw " + thrown, thrown);
57                 throw thrown;
58             }
59             throw new SQLException("Protomatter JDBCWrapper can't call method " + targetClass.getName() + "." + method + "(" + toString(params) + "): " + tx.toString());
60         }
61         catch (Exception JavaDoc x)
62         {
63             throw new SQLException("Protomatter JDBCWrapper can't call method " + targetClass.getName() + "." + method + "(" + toString(params) + "): " + x.toString());
64         }
65     }
66
67     protected Object JavaDoc callMethod(Object JavaDoc target, String JavaDoc methodName)
68     throws SQLException
69     {
70         return callMethod(target, methodName, EMPTY_CLASS_ARRAY, EMPTY_OBJECT_ARRAY);
71     }
72
73     private String JavaDoc toString(Object JavaDoc o[])
74     {
75         if (o == null)
76             return "[null-array]";
77
78         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
79         for (int i=0; i<o.length; i++)
80         {
81             b.append(toString(o[i]));
82             if (i != (o.length -1))
83                 b.append(", ");
84         }
85         return b.toString();
86     }
87
88     private String JavaDoc toString(Object JavaDoc o)
89     {
90         if (o == null)
91             return "<null>";
92
93         if (o instanceof String JavaDoc)
94             return "\"" + o.toString() + "\"";
95
96         return String.valueOf(o);
97     }
98
99     protected int callIntMethod(Object JavaDoc target, String JavaDoc methodName, Class JavaDoc paramTypes[], Object JavaDoc params[])
100     throws SQLException
101     {
102         Integer JavaDoc value = (Integer JavaDoc)callMethod(target, methodName, paramTypes, params);
103         return value.intValue();
104     }
105
106     protected float callFloatMethod(Object JavaDoc target, String JavaDoc methodName, Class JavaDoc paramTypes[], Object JavaDoc params[])
107     throws SQLException
108     {
109         Float JavaDoc value = (Float JavaDoc)callMethod(target, methodName, paramTypes, params);
110         return value.floatValue();
111     }
112
113     protected long callLongMethod(Object JavaDoc target, String JavaDoc methodName, Class JavaDoc paramTypes[], Object JavaDoc params[])
114     throws SQLException
115     {
116         Long JavaDoc value = (Long JavaDoc)callMethod(target, methodName, paramTypes, params);
117         return value.longValue();
118     }
119
120     protected boolean callBooleanMethod(Object JavaDoc target, String JavaDoc methodName, Class JavaDoc paramTypes[], Object JavaDoc params[])
121     throws SQLException
122     {
123         Boolean JavaDoc value = (Boolean JavaDoc)callMethod(target, methodName, paramTypes, params);
124         return value.booleanValue();
125     }
126
127     protected short callShortMethod(Object JavaDoc target, String JavaDoc methodName, Class JavaDoc paramTypes[], Object JavaDoc params[])
128     throws SQLException
129     {
130         Short JavaDoc value = (Short JavaDoc)callMethod(target, methodName, paramTypes, params);
131         return value.shortValue();
132     }
133
134     protected byte callByteMethod(Object JavaDoc target, String JavaDoc methodName, Class JavaDoc paramTypes[], Object JavaDoc params[])
135     throws SQLException
136     {
137         Byte JavaDoc value = (Byte JavaDoc)callMethod(target, methodName, paramTypes, params);
138         return value.byteValue();
139     }
140
141     protected double callDoubleMethod(Object JavaDoc target, String JavaDoc methodName, Class JavaDoc paramTypes[], Object JavaDoc params[])
142     throws SQLException
143     {
144         Double JavaDoc value = (Double JavaDoc)callMethod(target, methodName, paramTypes, params);
145         return value.doubleValue();
146     }
147
148     protected String JavaDoc callStringMethod(Object JavaDoc target, String JavaDoc methodName, Class JavaDoc paramTypes[], Object JavaDoc params[])
149     throws SQLException
150     {
151         return (String JavaDoc)callMethod(target, methodName, paramTypes, params);
152     }
153
154 }
155
Popular Tags