KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > resource > JonasSQLWrapper


1 /*
2  *
3  * JOnAS: Java(TM) Open Application Server
4  * Copyright (C) 1999 Bull S.A.
5  * Contact: jonas-team@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  * USA
21  *
22  * --------------------------------------------------------------------------
23  * $Id: JonasSQLWrapper.java,v 1.7 2005/04/28 08:43:25 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.resource;
28
29 import java.lang.reflect.InvocationHandler JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31 import java.lang.reflect.Proxy JavaDoc;
32 import java.sql.SQLException JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.List JavaDoc;
35
36 import org.objectweb.util.monolog.api.BasicLevel;
37 import org.objectweb.util.monolog.api.Logger;
38
39 /**
40  * SQL Connection Wrapper
41  *
42  * @author Eric Hardesty
43  * Contributor(s):
44  */

45 public class JonasSQLWrapper implements InvocationHandler JavaDoc {
46
47     /**
48      * Number of fixed arguments
49      */

50     private static final int PROXY_FIXED_ARGS = 3;
51     /**
52      * Real connection object
53      */

54     private Object JavaDoc connection = null;
55     /**
56      * Connection manager holding the PreparedStatement cache
57      */

58     private SQLManager conman = null;
59     /**
60      * MCInfo for the connection
61      */

62     private MCInfo mci = null;
63     /**
64      * Userid of the connection
65      */

66     private String JavaDoc user = null;
67     /**
68      * Logger object
69      */

70     private Logger trace = null;
71
72     /**
73      * Determine if statement is still valid
74      */

75     private boolean invalid = false;
76     /**
77      * Determine if statement cache is in use
78      */

79     private boolean supportsPreparedCache = true;
80
81     /**
82      * PreparedStatement wrapper
83      * @param pConn JDBC connection object
84      * @param pMci MCInfo object associated with the connection
85      * @param pConman SQLManager object holding PreparedStatement cache
86      * @param pTrace Logger object to use
87      * @throws Exception if an error occurs
88      */

89     public JonasSQLWrapper(Object JavaDoc pConn, MCInfo pMci, SQLManager pConman, Logger pTrace)
90               throws Exception JavaDoc {
91         connection = pConn;
92         mci = pMci;
93         conman = pConman;
94         trace = pTrace;
95         user = mci.mc.getMetaData().getUserName();
96     }
97
98     /**
99      * Returns a proxy for the sql Connection
100      *
101      * @param pConn JDBC connection object
102      * @param pMci MCInfo object associated with the connection
103      * @param pConman SQLManager object holding PreparedStatement cache
104      * @param pTrace Logger object to use
105      * @return Object of the preparedStatement proxy
106      * @throws Exception if an error occurs
107      */

108     public static Object JavaDoc createSQLWrapper(Object JavaDoc pConn, MCInfo pMci,
109                                            SQLManager pConman, Logger pTrace)
110                  throws Exception JavaDoc {
111         Class JavaDoc class1 = pConn.getClass();
112         Class JavaDoc [] aclass = buildInterfaces(class1);
113         return Proxy.newProxyInstance(class1.getClassLoader(), aclass,
114                                        new JonasSQLWrapper(pConn, pMci, pConman, pTrace));
115     }
116
117     /**
118      * Invoke call on the proxy
119      *
120      * @param obj the proxy instance that the method was invoked on
121      * @param method the Method instance
122      * @param aobj an array of objects containing the values of the arguments
123      * @throws Throwable if an error occurs
124      * @return Object the returned from the method invocation on the proxy instance
125      */

126     public Object JavaDoc invoke(Object JavaDoc obj, Method JavaDoc method, Object JavaDoc [] aobj)
127         throws Throwable JavaDoc {
128
129         Object JavaDoc 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     /**
153      * Invoke correct preparedStatement
154      * @param pTypes Class [] of parameter types
155      * @param pValues Class [] of parameter values
156      * @return Object the result of invoking the preparedStatement
157      * @throws Exception if any Exception occurs
158      */

159     public Object JavaDoc prepareStatement(Class JavaDoc [] pTypes, Object JavaDoc [] pValues)
160         throws Exception JavaDoc {
161
162         checkIfValid();
163
164         if (supportsPreparedCache) {
165             try {
166                 Class JavaDoc [] mTypes = new Class JavaDoc[PROXY_FIXED_ARGS + pTypes.length];
167                 Object JavaDoc [] mValues = new Object JavaDoc[PROXY_FIXED_ARGS + pValues.length];
168
169                 mTypes[0] = MCInfo.class;
170                 mTypes[1] = Object JavaDoc.class;
171                 mTypes[2] = String JavaDoc.class;
172
173                 mValues[0] = mci;
174                 mValues[1] = connection;
175                 mValues[2] = user;
176
177                 // Loop should be faster than System.arraycopy for small
178
// number of parameters
179
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 JavaDoc meth1 = conman.getClass().getMethod("getPStatement", mTypes);
185                 return meth1.invoke(conman, mValues);
186             } catch (Exception JavaDoc ex) {
187                 ex.printStackTrace();
188                 return null;
189             }
190         } else {
191             return null;
192         }
193
194     }
195 /*
196 // JDK 1.4
197     public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
198         throws SQLException {
199         checkIfValid();
200         if (supportsPreparedCache) {
201             return conman.getStatement(user, connection, sql, autoGeneratedKeys);
202         } else {
203             return con.prepareStatement(sql, autoGeneratedKeys);
204         }
205     }
206
207 // JDK 1.4
208     public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
209         throws SQLException {
210         checkIfValid();
211         if (supportsPreparedCache) {
212             return conman.getStatement(user, connection, sql, columnIndexes);
213         } else {
214             return con.prepareStatement(sql, columnIndexes);
215         }
216     }
217
218 // JDK 1.4
219     public PreparedStatement prepareStatement(String sql, int resultSetType,
220                                                int resultSetConcurrency,
221                                                int resultSetHoldability)
222         throws SQLException {
223         checkIfValid();
224         if (supportsPreparedCache) {
225             return conman.getStatement(user, connection, sql, resultSetType, resultSetConcurrency, resultSetHoldability);
226         } else {
227             return con.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
228         }
229     }
230
231 // JDK 1.4
232     public PreparedStatement prepareStatement(String sql, String[] columnNames)
233         throws SQLException {
234         checkIfValid();
235         if (supportsPreparedCache) {
236             return conman.getStatement(user, connection, sql, columnNames);
237         } else {
238             return con.prepareStatement(sql, columnNames);
239         }
240     }
241 */

242
243     /**
244      * Return a string describing this object
245      * @return String representing this object
246      */

247     public String JavaDoc toString() {
248         return connection.toString();
249     }
250
251     /**
252      * Check if the wrapper is valid
253      * @throws SQLException if the wrapper is invalid
254      */

255     private void checkIfValid() throws SQLException JavaDoc {
256         if (invalid) {
257             throw new SQLException JavaDoc("Connection is closed");
258         }
259     }
260
261     /* Utility methods to build interfaces*/
262     /**
263      * Build the class interfaces from the class object
264      * @param class1 Class to retrieve the interfaces
265      * @return Class [] all interfaces
266      */

267     private static Class JavaDoc[] buildInterfaces(Class JavaDoc class1) {
268         ArrayList JavaDoc arlist = new ArrayList JavaDoc();
269         addToList(class1, arlist);
270         Class JavaDoc [] aclass = new Class JavaDoc[arlist.size()];
271         return (Class JavaDoc []) arlist.toArray(aclass);
272     }
273
274     /**
275      * Add each of the interfaces
276      * @param cls Class to determine what interfaces are implemented
277      * @param lst List of interfaces
278      */

279     private static void addToList(Class JavaDoc cls, List JavaDoc lst) {
280         Class JavaDoc [] 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 JavaDoc class2 = cls.getSuperclass();
289         if (class2 != null) {
290             addToList(class2, lst);
291         }
292     }
293 }
294
Popular Tags