KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > support > nativejdbc > CommonsDbcpNativeJdbcExtractor


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jdbc.support.nativejdbc;
18
19 import java.lang.reflect.Method JavaDoc;
20 import java.lang.reflect.Modifier JavaDoc;
21 import java.sql.CallableStatement JavaDoc;
22 import java.sql.Connection JavaDoc;
23 import java.sql.PreparedStatement JavaDoc;
24 import java.sql.ResultSet JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.sql.Statement JavaDoc;
27
28 import org.springframework.util.ReflectionUtils;
29
30 /**
31  * Implementation of the NativeJdbcExtractor interface for the
32  * Jakarta Commons DBCP connection pool.
33  *
34  * <p>Returns the underlying native Connection, Statement, etc to application
35  * code instead of DBCP's wrapper implementations. The returned JDBC classes
36  * can then safely be cast, e.g. to <code>oracle.jdbc.OracleConnection</code>.
37  *
38  * <p>This NativeJdbcExtractor can be set just to <i>allow</i> working with a
39  * Commons DBCP DataSource: If a given object is not a Commons DBCP wrapper,
40  * it will be returned as-is.
41  *
42  * <p>Tested against Commons DBCP 1.1 and 1.2, but should also work with 1.0.
43  * Before Commons DBCP 1.1, DelegatingCallableStatement and DelegatingResultSet
44  * have not offered any means to access underlying delegates: As a consequence,
45  * <code>getNativeCallableStatement</code> and <code>getNativeResultSet</code>
46  * will not work with Commons DBCP 1.0.
47  *
48  * <p>Note that this version of CommonsDbcpNativeJdbcExtractor will work
49  * against the original Commons DBCP in <code>org.apache.commons.dbcp</code>
50  * as well as against Tomcat 5.5's relocated Commons DBCP version in the
51  * <code>org.apache.tomcat.dbcp.dbcp</code> package.
52  *
53  * @author Juergen Hoeller
54  * @since 25.08.2003
55  */

56 public class CommonsDbcpNativeJdbcExtractor extends NativeJdbcExtractorAdapter {
57
58     private static final String JavaDoc GET_INNERMOST_DELEGATE_METHOD_NAME = "getInnermostDelegate";
59
60
61     /**
62      * Extracts the innermost delegate from the given Commons DBCP object.
63      * Falls back to the given object if no underlying object found.
64      * @param obj the Commons DBCP Connection/Statement/ResultSet
65      * @return the underlying native Connection/Statement/ResultSet
66      */

67     private static Object JavaDoc getInnermostDelegate(Object JavaDoc obj) {
68         if (obj == null) {
69             return null;
70         }
71         try {
72             Class JavaDoc classToAnalyze = obj.getClass();
73             while (!Modifier.isPublic(classToAnalyze.getModifiers())) {
74                 classToAnalyze = classToAnalyze.getSuperclass();
75                 if (classToAnalyze == null) {
76                     // No public provider class found -> fall back to given object.
77
return obj;
78                 }
79             }
80             Method JavaDoc getInnermostDelegate = classToAnalyze.getMethod(GET_INNERMOST_DELEGATE_METHOD_NAME, (Class JavaDoc[]) null);
81             Object JavaDoc delegate = ReflectionUtils.invokeMethod(getInnermostDelegate, obj);
82             return (delegate != null ? delegate : obj);
83         }
84         catch (NoSuchMethodException JavaDoc ex) {
85             return obj;
86         }
87         catch (SecurityException JavaDoc ex) {
88             throw new IllegalStateException JavaDoc("Commons DBCP getInnermostDelegate method is not accessible: " + ex);
89         }
90     }
91
92
93     protected Connection JavaDoc doGetNativeConnection(Connection JavaDoc con) throws SQLException JavaDoc {
94         return (Connection JavaDoc) getInnermostDelegate(con);
95     }
96
97     public Statement JavaDoc getNativeStatement(Statement JavaDoc stmt) throws SQLException JavaDoc {
98         return (Statement JavaDoc) getInnermostDelegate(stmt);
99     }
100
101     public PreparedStatement JavaDoc getNativePreparedStatement(PreparedStatement JavaDoc ps) throws SQLException JavaDoc {
102         return (PreparedStatement JavaDoc) getNativeStatement(ps);
103     }
104
105     public CallableStatement JavaDoc getNativeCallableStatement(CallableStatement JavaDoc cs) throws SQLException JavaDoc {
106         return (CallableStatement JavaDoc) getNativeStatement(cs);
107     }
108
109     public ResultSet JavaDoc getNativeResultSet(ResultSet JavaDoc rs) throws SQLException JavaDoc {
110         return (ResultSet JavaDoc) getInnermostDelegate(rs);
111     }
112
113 }
114
Popular Tags