KickJava   Java API By Example, From Geeks To Geeks.

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


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.Connection JavaDoc;
21 import java.sql.SQLException JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import org.springframework.util.ReflectionUtils;
27
28 /**
29  * Implementation of the NativeJdbcExtractor interface for WebSphere.
30  *
31  * <p>Returns the underlying native Connection to application code instead
32  * of WebSphere's wrapper implementation; unwraps the Connection for
33  * native statements. The returned JDBC classes can then safely be cast,
34  * e.g. to <code>oracle.jdbc.OracleConnection</code>.
35  *
36  * <p>This NativeJdbcExtractor can be set just to <i>allow</i> working
37  * with a WebSphere DataSource: If a given object is not a WebSphere
38  * Connection wrapper, it will be returned as-is.
39  *
40  * <p>Supports both WebSphere 5 and WebSphere 4. Currently tested with
41  * IBM WebSphere 5.1.0, 5.0.2 and 4.0.6. Thanks to Dave Keller and Victor
42  * for figuring out how to do the unwrapping on WebSphere 5 and 4!
43  *
44  * @author Juergen Hoeller
45  * @since 1.1
46  * @see com.ibm.ws.rsadapter.jdbc.WSJdbcConnection
47  * @see com.ibm.ws.rsadapter.jdbc.WSJdbcUtil#getNativeConnection
48  * @see com.ibm.ejs.cm.proxy.ConnectionProxy#getPhysicalConnection
49  */

50 public class WebSphereNativeJdbcExtractor extends NativeJdbcExtractorAdapter {
51
52     private static final String JavaDoc JDBC_ADAPTER_CONNECTION_NAME_5 = "com.ibm.ws.rsadapter.jdbc.WSJdbcConnection";
53
54     private static final String JavaDoc JDBC_ADAPTER_UTIL_NAME_5 = "com.ibm.ws.rsadapter.jdbc.WSJdbcUtil";
55
56     private static final String JavaDoc CONNECTION_PROXY_NAME_4 = "com.ibm.ejs.cm.proxy.ConnectionProxy";
57
58
59     protected final Log logger = LogFactory.getLog(getClass());
60
61     private Class JavaDoc webSphere5ConnectionClass;
62
63     private Class JavaDoc webSphere4ConnectionClass;
64
65     private Method JavaDoc webSphere5NativeConnectionMethod;
66
67     private Method JavaDoc webSphere4PhysicalConnectionMethod;
68
69
70     /**
71      * This constructor retrieves WebSphere JDBC adapter classes,
72      * so we can get the underlying vendor connection using reflection.
73      */

74     public WebSphereNativeJdbcExtractor() {
75         // Detect WebSphere 5 connection classes.
76
try {
77             logger.debug("Trying WebSphere 5 Connection: " + JDBC_ADAPTER_CONNECTION_NAME_5);
78             this.webSphere5ConnectionClass = getClass().getClassLoader().loadClass(JDBC_ADAPTER_CONNECTION_NAME_5);
79             Class JavaDoc jdbcAdapterUtilClass = getClass().getClassLoader().loadClass(JDBC_ADAPTER_UTIL_NAME_5);
80             this.webSphere5NativeConnectionMethod =
81                     jdbcAdapterUtilClass.getMethod("getNativeConnection", new Class JavaDoc[] {this.webSphere5ConnectionClass});
82         }
83         catch (Exception JavaDoc ex) {
84             logger.debug("Could not find WebSphere 5 connection pool classes", ex);
85         }
86
87         // Detect WebSphere 4 connection classes.
88
// Might also be found on WebSphere 5, for version 4 DataSources.
89
try {
90             logger.debug("Trying WebSphere 4 Connection: " + CONNECTION_PROXY_NAME_4);
91             this.webSphere4ConnectionClass = getClass().getClassLoader().loadClass(CONNECTION_PROXY_NAME_4);
92             this.webSphere4PhysicalConnectionMethod =
93                     this.webSphere4ConnectionClass.getMethod("getPhysicalConnection", (Class JavaDoc[]) null);
94         }
95         catch (Exception JavaDoc ex) {
96             logger.debug("Could not find WebSphere 4 connection pool classes", ex);
97         }
98     }
99     
100
101     /**
102      * Return <code>true</code>, as WebSphere returns wrapped Statements.
103      */

104     public boolean isNativeConnectionNecessaryForNativeStatements() {
105         return true;
106     }
107
108     /**
109      * Return <code>true</code>, as WebSphere returns wrapped PreparedStatements.
110      */

111     public boolean isNativeConnectionNecessaryForNativePreparedStatements() {
112         return true;
113     }
114
115     /**
116      * Return <code>true</code>, as WebSphere returns wrapped CallableStatements.
117      */

118     public boolean isNativeConnectionNecessaryForNativeCallableStatements() {
119         return true;
120     }
121
122     /**
123      * Retrieve the Connection via WebSphere's <code>getNativeConnection</code> method.
124      */

125     protected Connection JavaDoc doGetNativeConnection(Connection JavaDoc con) throws SQLException JavaDoc {
126         // WebSphere 5 connection?
127
if (this.webSphere5ConnectionClass != null &&
128                 this.webSphere5ConnectionClass.isAssignableFrom(con.getClass())) {
129             // WebSphere 5's WSJdbcUtil.getNativeConnection(wsJdbcConnection)
130
return (Connection JavaDoc) ReflectionUtils.invokeMethod(
131                     this.webSphere5NativeConnectionMethod, null, new Object JavaDoc[] {con});
132         }
133
134         // WebSphere 4 connection (or version 4 connection on WebSphere 5)?
135
else if (this.webSphere4ConnectionClass != null &&
136                 this.webSphere4ConnectionClass.isAssignableFrom(con.getClass())) {
137             // WebSphere 4's connectionProxy.getPhysicalConnection()
138
return (Connection JavaDoc) ReflectionUtils.invokeMethod(this.webSphere4PhysicalConnectionMethod, con);
139         }
140
141         // No known WebSphere connection -> return as-is.
142
else {
143             if (logger.isDebugEnabled()) {
144                 logger.debug("Connection [" + con + "] is not a WebSphere 5/4 connection, returning as-is");
145             }
146             return con;
147         }
148     }
149
150 }
151
Popular Tags