1 16 17 package org.springframework.jdbc.support.nativejdbc; 18 19 import java.lang.reflect.Method ; 20 import java.sql.CallableStatement ; 21 import java.sql.Connection ; 22 import java.sql.PreparedStatement ; 23 import java.sql.ResultSet ; 24 import java.sql.SQLException ; 25 import java.sql.Statement ; 26 27 import org.springframework.util.ReflectionUtils; 28 29 47 public class JBossNativeJdbcExtractor extends NativeJdbcExtractorAdapter { 48 49 private static final String WRAPPED_CONNECTION_NAME = "org.jboss.resource.adapter.jdbc.WrappedConnection"; 50 51 private static final String WRAPPED_STATEMENT_NAME = "org.jboss.resource.adapter.jdbc.WrappedStatement"; 52 53 private static final String WRAPPED_RESULT_SET_NAME = "org.jboss.resource.adapter.jdbc.WrappedResultSet"; 54 55 56 private Class wrappedConnectionClass; 57 58 private Class wrappedStatementClass; 59 60 private Class wrappedResultSetClass; 61 62 private Method getUnderlyingConnectionMethod; 63 64 private Method getUnderlyingStatementMethod; 65 66 private Method getUnderlyingResultSetMethod; 67 68 69 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 []) null); 80 this.getUnderlyingStatementMethod = 81 this.wrappedStatementClass.getMethod("getUnderlyingStatement", (Class []) null); 82 this.getUnderlyingResultSetMethod = 83 this.wrappedResultSetClass.getMethod("getUnderlyingResultSet", (Class []) null); 84 } 85 catch (Exception ex) { 86 throw new IllegalStateException ( 87 "Could not initialize JBossNativeJdbcExtractor because JBoss API classes are not available: " + ex); 88 } 89 } 90 91 92 95 protected Connection doGetNativeConnection(Connection con) throws SQLException { 96 if (this.wrappedConnectionClass.isAssignableFrom(con.getClass())) { 97 return (Connection ) ReflectionUtils.invokeMethod(this.getUnderlyingConnectionMethod, con); 98 } 99 return con; 100 } 101 102 105 public Statement getNativeStatement(Statement stmt) throws SQLException { 106 if (this.wrappedStatementClass.isAssignableFrom(stmt.getClass())) { 107 return (Statement ) ReflectionUtils.invokeMethod(this.getUnderlyingStatementMethod, stmt); 108 } 109 return stmt; 110 } 111 112 115 public PreparedStatement getNativePreparedStatement(PreparedStatement ps) throws SQLException { 116 return (PreparedStatement ) getNativeStatement(ps); 117 } 118 119 122 public CallableStatement getNativeCallableStatement(CallableStatement cs) throws SQLException { 123 return (CallableStatement ) getNativeStatement(cs); 124 } 125 126 129 public ResultSet getNativeResultSet(ResultSet rs) throws SQLException { 130 if (this.wrappedResultSetClass.isAssignableFrom(rs.getClass())) { 131 return (ResultSet ) ReflectionUtils.invokeMethod(this.getUnderlyingResultSetMethod, rs); 132 } 133 return rs; 134 } 135 136 } 137 | Popular Tags |