JonasSQLWrapper.java

00001 /*
00002  *
00003  * JOnAS: Java(TM) Open Application Server
00004  * Copyright (C) 1999 Bull S.A.
00005  * Contact: jonas-team@objectweb.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  * --------------------------------------------------------------------------
00023  * $Id: JonasSQLWrapper.java,v 1.6 2004/10/29 23:33:37 ehardesty Exp $
00024  * --------------------------------------------------------------------------
00025  */
00026 
00027 package org.objectweb.jonas.resource;
00028 
00029 import java.lang.reflect.InvocationHandler;
00030 import java.lang.reflect.Method;
00031 import java.lang.reflect.Proxy;
00032 import java.sql.SQLException;
00033 import java.util.ArrayList;
00034 import java.util.List;
00035 
00036 import org.objectweb.util.monolog.api.BasicLevel;
00037 import org.objectweb.util.monolog.api.Logger;
00038 
00045 public class JonasSQLWrapper implements InvocationHandler {
00046 
00050     private static final int PROXY_FIXED_ARGS = 3;
00054     private Object connection = null;
00058     private SQLManager conman = null;
00062     private MCInfo mci = null;
00066     private String user = null;
00070     private Logger trace = null;
00071 
00075     private boolean invalid = false;
00079     private boolean supportsPreparedCache = true;
00080 
00089     public JonasSQLWrapper(Object pConn, MCInfo pMci, SQLManager pConman, Logger pTrace)
00090               throws Exception {
00091         connection = pConn;
00092         mci = pMci;
00093         conman = pConman;
00094         trace = pTrace;
00095         user = mci.mc.getMetaData().getUserName();
00096     }
00097 
00108     public static Object createSQLWrapper(Object pConn, MCInfo pMci,
00109                                            SQLManager pConman, Logger pTrace)
00110                  throws Exception {
00111         Class class1 = pConn.getClass();
00112         Class [] aclass = buildInterfaces(class1);
00113         return Proxy.newProxyInstance(class1.getClassLoader(), aclass,
00114                                        new JonasSQLWrapper(pConn, pMci, pConman, pTrace));
00115     }
00116 
00126     public Object invoke(Object obj, Method method, Object [] aobj)
00127         throws Throwable {
00128 
00129         Object retObj = null;
00130         if (method.getName().compareTo("prepareStatement") == 0) {
00131             retObj = prepareStatement(method.getParameterTypes(), aobj);
00132             if (retObj == null) {
00133                 retObj = method.invoke(connection, aobj);
00134             }
00135         } else {
00136             if (method.getName().compareTo("close") == 0) {
00137                 invalid = true;
00138             } else if (method.getName().compareTo("toString") == 0) {
00139                 return connection.toString();
00140             } else if (method.getName().compareTo("commit") == 0) {
00141                 trace.log(BasicLevel.DEBUG, "" + this);
00142             } else {
00143                 checkIfValid();
00144             }
00145             retObj = method.invoke(connection, aobj);
00146         }
00147         return retObj;
00148     }
00149 
00157     public Object prepareStatement(Class [] pTypes, Object [] pValues)
00158         throws Exception {
00159 
00160         checkIfValid();
00161 
00162         if (supportsPreparedCache) {
00163             try {
00164                 Class [] mTypes = new Class[PROXY_FIXED_ARGS + pTypes.length];
00165                 Object [] mValues = new Object[PROXY_FIXED_ARGS + pValues.length];
00166 
00167                 mTypes[0] = MCInfo.class;
00168                 mTypes[1] = Object.class;
00169                 mTypes[2] = String.class;
00170 
00171                 mValues[0] = mci;
00172                 mValues[1] = connection;
00173                 mValues[2] = user;
00174 
00175                 // Loop should be faster than System.arraycopy for small
00176                 // number of parameters
00177                 for (int i = 0; i < pTypes.length; i++) {
00178                     mTypes[PROXY_FIXED_ARGS + i] = pTypes[i];
00179                     mValues[PROXY_FIXED_ARGS + i] = pValues[i];
00180                 }
00181 
00182                 Method meth1 = conman.getClass().getMethod("getPStatement", mTypes);
00183                 return meth1.invoke(conman, mValues);
00184             } catch (Exception ex) {
00185                 ex.printStackTrace();
00186                 return null;
00187             }
00188         } else {
00189             return null;
00190         }
00191 
00192     }
00193 /*
00194 // JDK 1.4
00195     public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
00196         throws SQLException {
00197         checkIfValid();
00198         if (supportsPreparedCache) {
00199             return conman.getStatement(user, connection, sql, autoGeneratedKeys);
00200         } else {
00201             return con.prepareStatement(sql, autoGeneratedKeys);
00202         }
00203     }
00204 
00205 // JDK 1.4
00206     public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
00207         throws SQLException {
00208         checkIfValid();
00209         if (supportsPreparedCache) {
00210             return conman.getStatement(user, connection, sql, columnIndexes);
00211         } else {
00212             return con.prepareStatement(sql, columnIndexes);
00213         }
00214     }
00215 
00216 // JDK 1.4
00217     public PreparedStatement prepareStatement(String sql, int resultSetType,
00218                                                int resultSetConcurrency,
00219                                                int resultSetHoldability)
00220         throws SQLException {
00221         checkIfValid();
00222         if (supportsPreparedCache) {
00223             return conman.getStatement(user, connection, sql, resultSetType, resultSetConcurrency, resultSetHoldability);
00224         } else {
00225             return con.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
00226         }
00227     }
00228 
00229 // JDK 1.4
00230     public PreparedStatement prepareStatement(String sql, String[] columnNames)
00231         throws SQLException {
00232         checkIfValid();
00233         if (supportsPreparedCache) {
00234             return conman.getStatement(user, connection, sql, columnNames);
00235         } else {
00236             return con.prepareStatement(sql, columnNames);
00237         }
00238     }
00239 */
00240 
00245     public String toString() {
00246         return connection.toString();
00247     }
00248 
00253     private void checkIfValid() throws SQLException {
00254         if (invalid) {
00255             throw new SQLException("Connection is closed");
00256         }
00257     }
00258 
00259     /* Utility methods to build interfaces*/
00265     private static Class[] buildInterfaces(Class class1) {
00266         ArrayList arlist = new ArrayList();
00267         addToList(class1, arlist);
00268         Class [] aclass = new Class[arlist.size()];
00269         return (Class []) arlist.toArray(aclass);
00270     }
00271 
00277     private static void addToList(Class cls, List lst) {
00278         Class [] aclass = cls.getInterfaces();
00279         for (int i = 0; i < aclass.length; i++) {
00280             if (!lst.contains(aclass[i])) {
00281                 lst.add(aclass[i]);
00282             }
00283             addToList(aclass[i], lst);
00284         }
00285 
00286         Class class2 = cls.getSuperclass();
00287         if (class2 != null) {
00288             addToList(class2, lst);
00289         }
00290     }
00291 }

Generated on Tue Feb 15 15:05:19 2005 for JOnAS by  doxygen 1.3.9.1