KickJava   Java API By Example, From Geeks To Geeks.

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


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.springframework.util.ReflectionUtils;
24
25 /**
26  * Implementation of the NativeJdbcExtractor interface for WebLogic Server
27  * 6.1+.
28  *
29  * <p>Returns the underlying native Connection to application code instead
30  * of WebLogic's wrapper implementation; unwraps the Connection for native
31  * statements. The returned JDBC classes can then safely be cast, e.g. to
32  * <code>oracle.jdbc.OracleConnection</code>.
33  *
34  * <p>This NativeJdbcExtractor can be set just to <i>allow</i> working
35  * with a WebLogic DataSource: If a given object is not a WebLogic
36  * Connection wrapper, it will be returned as-is.
37  *
38  * @author Thomas Risberg
39  * @author Juergen Hoeller
40  * @since 1.0.2
41  * @see #getNativeConnection
42  * @see weblogic.jdbc.extensions.WLConnection#getVendorConnection
43  */

44 public class WebLogicNativeJdbcExtractor extends NativeJdbcExtractorAdapter {
45
46     private static final String JavaDoc JDBC_EXTENSION_NAME = "weblogic.jdbc.extensions.WLConnection";
47
48
49     private final Class JavaDoc jdbcExtensionClass;
50
51     private final Method JavaDoc getVendorConnectionMethod;
52
53
54     /**
55      * This constructor retrieves the WebLogic JDBC extension interface,
56      * so we can get the underlying vendor connection using reflection.
57      */

58     public WebLogicNativeJdbcExtractor() {
59         try {
60             this.jdbcExtensionClass = getClass().getClassLoader().loadClass(JDBC_EXTENSION_NAME);
61             this.getVendorConnectionMethod = this.jdbcExtensionClass.getMethod("getVendorConnection", (Class JavaDoc[]) null);
62         }
63         catch (Exception JavaDoc ex) {
64             throw new IllegalStateException JavaDoc(
65                     "Could not initialize WebLogicNativeJdbcExtractor because WebLogic API classes are not available: " + ex);
66         }
67     }
68
69
70     /**
71      * Return <code>true</code>, as WebLogic returns wrapped Statements.
72      */

73     public boolean isNativeConnectionNecessaryForNativeStatements() {
74         return true;
75     }
76
77     /**
78      * Return <code>true</code>, as WebLogic returns wrapped PreparedStatements.
79      */

80     public boolean isNativeConnectionNecessaryForNativePreparedStatements() {
81         return true;
82     }
83
84     /**
85      * Return <code>true</code>, as WebLogic returns wrapped CallableStatements.
86      */

87     public boolean isNativeConnectionNecessaryForNativeCallableStatements() {
88         return true;
89     }
90
91     /**
92      * Retrieve the Connection via WebLogic's <code>getVendorConnection</code> method.
93      */

94     protected Connection JavaDoc doGetNativeConnection(Connection JavaDoc con) throws SQLException JavaDoc {
95         if (this.jdbcExtensionClass.isAssignableFrom(con.getClass())) {
96             return (Connection JavaDoc) ReflectionUtils.invokeMethod(this.getVendorConnectionMethod, con);
97         }
98         return con;
99     }
100
101 }
102
Popular Tags