1 26 27 package org.objectweb.jonas.resource; 28 29 import java.lang.reflect.InvocationHandler ; 30 import java.lang.reflect.Method ; 31 import java.lang.reflect.Proxy ; 32 import java.sql.SQLException ; 33 import java.util.ArrayList ; 34 import java.util.List ; 35 36 import org.objectweb.util.monolog.api.BasicLevel; 37 import org.objectweb.util.monolog.api.Logger; 38 39 45 public class JonasSQLWrapper implements InvocationHandler { 46 47 50 private static final int PROXY_FIXED_ARGS = 3; 51 54 private Object connection = null; 55 58 private SQLManager conman = null; 59 62 private MCInfo mci = null; 63 66 private String user = null; 67 70 private Logger trace = null; 71 72 75 private boolean invalid = false; 76 79 private boolean supportsPreparedCache = true; 80 81 89 public JonasSQLWrapper(Object pConn, MCInfo pMci, SQLManager pConman, Logger pTrace) 90 throws Exception { 91 connection = pConn; 92 mci = pMci; 93 conman = pConman; 94 trace = pTrace; 95 user = mci.mc.getMetaData().getUserName(); 96 } 97 98 108 public static Object createSQLWrapper(Object pConn, MCInfo pMci, 109 SQLManager pConman, Logger pTrace) 110 throws Exception { 111 Class class1 = pConn.getClass(); 112 Class [] aclass = buildInterfaces(class1); 113 return Proxy.newProxyInstance(class1.getClassLoader(), aclass, 114 new JonasSQLWrapper(pConn, pMci, pConman, pTrace)); 115 } 116 117 126 public Object invoke(Object obj, Method method, Object [] aobj) 127 throws Throwable { 128 129 Object retObj = null; 130 if (method.getName().compareTo("prepareStatement") == 0) { 131 retObj = prepareStatement(method.getParameterTypes(), aobj); 132 if (retObj == null) { 133 retObj = method.invoke(connection, aobj); 134 } 135 } else { 136 if (method.getName().compareTo("close") == 0) { 137 invalid = true; 138 } else if (method.getName().compareTo("toString") == 0) { 139 return connection.toString(); 140 } else if (method.getName().compareTo("commit") == 0) { 141 if (trace.isLoggable(BasicLevel.DEBUG)) { 142 trace.log(BasicLevel.DEBUG, "" + this); 143 } 144 } else { 145 checkIfValid(); 146 } 147 retObj = method.invoke(connection, aobj); 148 } 149 return retObj; 150 } 151 152 159 public Object prepareStatement(Class [] pTypes, Object [] pValues) 160 throws Exception { 161 162 checkIfValid(); 163 164 if (supportsPreparedCache) { 165 try { 166 Class [] mTypes = new Class [PROXY_FIXED_ARGS + pTypes.length]; 167 Object [] mValues = new Object [PROXY_FIXED_ARGS + pValues.length]; 168 169 mTypes[0] = MCInfo.class; 170 mTypes[1] = Object .class; 171 mTypes[2] = String .class; 172 173 mValues[0] = mci; 174 mValues[1] = connection; 175 mValues[2] = user; 176 177 for (int i = 0; i < pTypes.length; i++) { 180 mTypes[PROXY_FIXED_ARGS + i] = pTypes[i]; 181 mValues[PROXY_FIXED_ARGS + i] = pValues[i]; 182 } 183 184 Method meth1 = conman.getClass().getMethod("getPStatement", mTypes); 185 return meth1.invoke(conman, mValues); 186 } catch (Exception ex) { 187 ex.printStackTrace(); 188 return null; 189 } 190 } else { 191 return null; 192 } 193 194 } 195 242 243 247 public String toString() { 248 return connection.toString(); 249 } 250 251 255 private void checkIfValid() throws SQLException { 256 if (invalid) { 257 throw new SQLException ("Connection is closed"); 258 } 259 } 260 261 262 267 private static Class [] buildInterfaces(Class class1) { 268 ArrayList arlist = new ArrayList (); 269 addToList(class1, arlist); 270 Class [] aclass = new Class [arlist.size()]; 271 return (Class []) arlist.toArray(aclass); 272 } 273 274 279 private static void addToList(Class cls, List lst) { 280 Class [] aclass = cls.getInterfaces(); 281 for (int i = 0; i < aclass.length; i++) { 282 if (!lst.contains(aclass[i])) { 283 lst.add(aclass[i]); 284 } 285 addToList(aclass[i], lst); 286 } 287 288 Class class2 = cls.getSuperclass(); 289 if (class2 != null) { 290 addToList(class2, lst); 291 } 292 } 293 } 294 | Popular Tags |