KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.CallableStatement JavaDoc;
21 import java.sql.Connection JavaDoc;
22 import java.sql.PreparedStatement JavaDoc;
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.sql.Statement JavaDoc;
26
27 import org.springframework.util.ReflectionUtils;
28
29 /**
30  * Implementation of the NativeJdbcExtractor interface for JBoss 3.2.4+.
31  *
32  * <p>Returns the underlying native Connection, Statement, etc to
33  * application code instead of JBoss' wrapper implementations.
34  * The returned JDBC classes can then safely be cast, e.g. to
35  * <code>oracle.jdbc.OracleConnection</code>.
36  *
37  * <p>This NativeJdbcExtractor can be set just to <i>allow</i> working with
38  * a JBoss connection pool: If a given object is not a JBoss wrapper,
39  * it will be returned as-is.
40  *
41  * @author Juergen Hoeller
42  * @since 03.01.2004
43  * @see org.jboss.resource.adapter.jdbc.WrappedConnection#getUnderlyingConnection
44  * @see org.jboss.resource.adapter.jdbc.WrappedStatement#getUnderlyingStatement
45  * @see org.jboss.resource.adapter.jdbc.WrappedResultSet#getUnderlyingResultSet
46  */

47 public class JBossNativeJdbcExtractor extends NativeJdbcExtractorAdapter {
48
49     private static final String JavaDoc WRAPPED_CONNECTION_NAME = "org.jboss.resource.adapter.jdbc.WrappedConnection";
50
51     private static final String JavaDoc WRAPPED_STATEMENT_NAME = "org.jboss.resource.adapter.jdbc.WrappedStatement";
52
53     private static final String JavaDoc WRAPPED_RESULT_SET_NAME = "org.jboss.resource.adapter.jdbc.WrappedResultSet";
54
55
56     private Class JavaDoc wrappedConnectionClass;
57
58     private Class JavaDoc wrappedStatementClass;
59
60     private Class JavaDoc wrappedResultSetClass;
61
62     private Method JavaDoc getUnderlyingConnectionMethod;
63
64     private Method JavaDoc getUnderlyingStatementMethod;
65
66     private Method JavaDoc getUnderlyingResultSetMethod;
67
68
69     /**
70      * This constructor retrieves JBoss JDBC wrapper classes,
71      * so we can get the underlying vendor connection using reflection.
72      */

73     public JBossNativeJdbcExtractor() {
74         try {
75             this.wrappedConnectionClass = getClass().getClassLoader().loadClass(WRAPPED_CONNECTION_NAME);
76             this.wrappedStatementClass = getClass().getClassLoader().loadClass(WRAPPED_STATEMENT_NAME);
77             this.wrappedResultSetClass = getClass().getClassLoader().loadClass(WRAPPED_RESULT_SET_NAME);
78             this.getUnderlyingConnectionMethod =
79                 this.wrappedConnectionClass.getMethod("getUnderlyingConnection", (Class JavaDoc[]) null);
80             this.getUnderlyingStatementMethod =
81                 this.wrappedStatementClass.getMethod("getUnderlyingStatement", (Class JavaDoc[]) null);
82             this.getUnderlyingResultSetMethod =
83                 this.wrappedResultSetClass.getMethod("getUnderlyingResultSet", (Class JavaDoc[]) null);
84         }
85         catch (Exception JavaDoc ex) {
86             throw new IllegalStateException JavaDoc(
87                     "Could not initialize JBossNativeJdbcExtractor because JBoss API classes are not available: " + ex);
88         }
89     }
90
91
92     /**
93      * Retrieve the Connection via JBoss' <code>getUnderlyingConnection</code> method.
94      */

95     protected Connection JavaDoc doGetNativeConnection(Connection JavaDoc con) throws SQLException JavaDoc {
96         if (this.wrappedConnectionClass.isAssignableFrom(con.getClass())) {
97             return (Connection JavaDoc) ReflectionUtils.invokeMethod(this.getUnderlyingConnectionMethod, con);
98         }
99         return con;
100     }
101
102     /**
103      * Retrieve the Connection via JBoss' <code>getUnderlyingStatement</code> method.
104      */

105     public Statement JavaDoc getNativeStatement(Statement JavaDoc stmt) throws SQLException JavaDoc {
106         if (this.wrappedStatementClass.isAssignableFrom(stmt.getClass())) {
107             return (Statement JavaDoc) ReflectionUtils.invokeMethod(this.getUnderlyingStatementMethod, stmt);
108         }
109         return stmt;
110     }
111
112     /**
113      * Retrieve the Connection via JBoss' <code>getUnderlyingStatement</code> method.
114      */

115     public PreparedStatement JavaDoc getNativePreparedStatement(PreparedStatement JavaDoc ps) throws SQLException JavaDoc {
116         return (PreparedStatement JavaDoc) getNativeStatement(ps);
117     }
118
119     /**
120      * Retrieve the Connection via JBoss' <code>getUnderlyingStatement</code> method.
121      */

122     public CallableStatement JavaDoc getNativeCallableStatement(CallableStatement JavaDoc cs) throws SQLException JavaDoc {
123         return (CallableStatement JavaDoc) getNativeStatement(cs);
124     }
125
126     /**
127      * Retrieve the Connection via JBoss' <code>getUnderlyingResultSet</code> method.
128      */

129     public ResultSet JavaDoc getNativeResultSet(ResultSet JavaDoc rs) throws SQLException JavaDoc {
130         if (this.wrappedResultSetClass.isAssignableFrom(rs.getClass())) {
131             return (ResultSet JavaDoc) ReflectionUtils.invokeMethod(this.getUnderlyingResultSetMethod, rs);
132         }
133         return rs;
134     }
135     
136 }
137
Popular Tags