1. /*
  2. * Copyright 2002-2004 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. package org.springframework.jdbc.support.nativejdbc;
  17. import java.lang.reflect.Method;
  18. import java.sql.Connection;
  19. import java.sql.SQLException;
  20. import org.springframework.dao.DataAccessResourceFailureException;
  21. /**
  22. * Implementation of the NativeJdbcExtractor interface for WebLogic Server.
  23. * Returns the underlying native Connection to application code instead of
  24. * WebLogic's wrapper implementation; unwraps the Connection for native statements.
  25. * The returned JDBC classes can then safely be cast, e.g. to OracleConnection.
  26. *
  27. * <p>This NativeJdbcExtractor can be set just to <i>allow</i> working with a
  28. * WebLogic DataSource: If a given object is not a WebLogic Connection wrapper,
  29. * it will be returned as-is.
  30. *
  31. * <p>Currently just tested with BEA WebLogic 8.1 SP2.
  32. *
  33. * @author Thomas Risberg
  34. * @author Juergen Hoeller
  35. * @since 31.05.2004
  36. * @see #getNativeConnection
  37. * @see weblogic.jdbc.extensions.WLConnection#getVendorConnection
  38. */
  39. public class WebLogicNativeJdbcExtractor extends NativeJdbcExtractorAdapter {
  40. private static final String JDBC_EXTENSION_NAME = "weblogic.jdbc.extensions.WLConnection";
  41. private final Class jdbcExtensionClass;
  42. private final Method getVendorConnectionMethod;
  43. /**
  44. * This constructor retrieves the WebLogic JDBC extension interface,
  45. * so we can get the underlying vendor connection using reflection.
  46. */
  47. public WebLogicNativeJdbcExtractor() throws ClassNotFoundException, NoSuchMethodException {
  48. this.jdbcExtensionClass = getClass().getClassLoader().loadClass(JDBC_EXTENSION_NAME);
  49. this.getVendorConnectionMethod = this.jdbcExtensionClass.getMethod("getVendorConnection", new Class[] {});
  50. }
  51. /**
  52. * Return true, as WebLogic returns wrapped Statements.
  53. */
  54. public boolean isNativeConnectionNecessaryForNativeStatements() {
  55. return true;
  56. }
  57. /**
  58. * Return true, as WebLogic returns wrapped PreparedStatements.
  59. */
  60. public boolean isNativeConnectionNecessaryForNativePreparedStatements() {
  61. return true;
  62. }
  63. /**
  64. * Return true, as WebLogic returns wrapped CallableStatements.
  65. */
  66. public boolean isNativeConnectionNecessaryForNativeCallableStatements() {
  67. return true;
  68. }
  69. /**
  70. * Retrieve the Connection via WebLogic's <code>getVendorConnection</code> method.
  71. */
  72. public Connection getNativeConnection(Connection con) throws SQLException {
  73. if (this.jdbcExtensionClass.isAssignableFrom(con.getClass())) {
  74. try {
  75. return (Connection) this.getVendorConnectionMethod.invoke(con, new Object[] {});
  76. }
  77. catch (Exception ex) {
  78. throw new DataAccessResourceFailureException("Could not invoke WebLogic's getVendorConnection method", ex);
  79. }
  80. }
  81. return con;
  82. }
  83. }