| 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 14 class JDBCWrapper 15 { 16 private Channel log = Channel.forPackage(JDBCWrapper.class); 17 18 private Object [] EMPTY_OBJECT_ARRAY = new Object [0]; 19 private Class [] EMPTY_CLASS_ARRAY = new Class [0]; 20 21 private String trimFromLastPeriod(String 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 callMethod(Object target, String method, Class paramTypes[], Object params[]) 30 throws SQLException 31 { 32 Class targetClass = target.getClass(); 33 Class thisClass = this.getClass(); 34 try 35 { 36 Method m = targetClass.getMethod(method, paramTypes); 37 long time = System.currentTimeMillis(); 38 Object 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 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 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 callMethod(Object target, String methodName) 68 throws SQLException 69 { 70 return callMethod(target, methodName, EMPTY_CLASS_ARRAY, EMPTY_OBJECT_ARRAY); 71 } 72 73 private String toString(Object o[]) 74 { 75 if (o == null) 76 return "[null-array]"; 77 78 StringBuffer b = new StringBuffer (); 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 toString(Object o) 89 { 90 if (o == null) 91 return "<null>"; 92 93 if (o instanceof String ) 94 return "\"" + o.toString() + "\""; 95 96 return String.valueOf(o); 97 } 98 99 protected int callIntMethod(Object target, String methodName, Class paramTypes[], Object params[]) 100 throws SQLException 101 { 102 Integer value = (Integer )callMethod(target, methodName, paramTypes, params); 103 return value.intValue(); 104 } 105 106 protected float callFloatMethod(Object target, String methodName, Class paramTypes[], Object params[]) 107 throws SQLException 108 { 109 Float value = (Float )callMethod(target, methodName, paramTypes, params); 110 return value.floatValue(); 111 } 112 113 protected long callLongMethod(Object target, String methodName, Class paramTypes[], Object params[]) 114 throws SQLException 115 { 116 Long value = (Long )callMethod(target, methodName, paramTypes, params); 117 return value.longValue(); 118 } 119 120 protected boolean callBooleanMethod(Object target, String methodName, Class paramTypes[], Object params[]) 121 throws SQLException 122 { 123 Boolean value = (Boolean )callMethod(target, methodName, paramTypes, params); 124 return value.booleanValue(); 125 } 126 127 protected short callShortMethod(Object target, String methodName, Class paramTypes[], Object params[]) 128 throws SQLException 129 { 130 Short value = (Short )callMethod(target, methodName, paramTypes, params); 131 return value.shortValue(); 132 } 133 134 protected byte callByteMethod(Object target, String methodName, Class paramTypes[], Object params[]) 135 throws SQLException 136 { 137 Byte value = (Byte )callMethod(target, methodName, paramTypes, params); 138 return value.byteValue(); 139 } 140 141 protected double callDoubleMethod(Object target, String methodName, Class paramTypes[], Object params[]) 142 throws SQLException 143 { 144 Double value = (Double )callMethod(target, methodName, paramTypes, params); 145 return value.doubleValue(); 146 } 147 148 protected String callStringMethod(Object target, String methodName, Class paramTypes[], Object params[]) 149 throws SQLException 150 { 151 return (String )callMethod(target, methodName, paramTypes, params); 152 } 153 154 } 155 | Popular Tags |