1 16 17 package org.springframework.jdbc.support.nativejdbc; 18 19 import java.lang.reflect.Method ; 20 import java.lang.reflect.Modifier ; 21 import java.sql.CallableStatement ; 22 import java.sql.Connection ; 23 import java.sql.PreparedStatement ; 24 import java.sql.ResultSet ; 25 import java.sql.SQLException ; 26 import java.sql.Statement ; 27 28 import org.springframework.util.ReflectionUtils; 29 30 56 public class CommonsDbcpNativeJdbcExtractor extends NativeJdbcExtractorAdapter { 57 58 private static final String GET_INNERMOST_DELEGATE_METHOD_NAME = "getInnermostDelegate"; 59 60 61 67 private static Object getInnermostDelegate(Object obj) { 68 if (obj == null) { 69 return null; 70 } 71 try { 72 Class classToAnalyze = obj.getClass(); 73 while (!Modifier.isPublic(classToAnalyze.getModifiers())) { 74 classToAnalyze = classToAnalyze.getSuperclass(); 75 if (classToAnalyze == null) { 76 return obj; 78 } 79 } 80 Method getInnermostDelegate = classToAnalyze.getMethod(GET_INNERMOST_DELEGATE_METHOD_NAME, (Class []) null); 81 Object delegate = ReflectionUtils.invokeMethod(getInnermostDelegate, obj); 82 return (delegate != null ? delegate : obj); 83 } 84 catch (NoSuchMethodException ex) { 85 return obj; 86 } 87 catch (SecurityException ex) { 88 throw new IllegalStateException ("Commons DBCP getInnermostDelegate method is not accessible: " + ex); 89 } 90 } 91 92 93 protected Connection doGetNativeConnection(Connection con) throws SQLException { 94 return (Connection ) getInnermostDelegate(con); 95 } 96 97 public Statement getNativeStatement(Statement stmt) throws SQLException { 98 return (Statement ) getInnermostDelegate(stmt); 99 } 100 101 public PreparedStatement getNativePreparedStatement(PreparedStatement ps) throws SQLException { 102 return (PreparedStatement ) getNativeStatement(ps); 103 } 104 105 public CallableStatement getNativeCallableStatement(CallableStatement cs) throws SQLException { 106 return (CallableStatement ) getNativeStatement(cs); 107 } 108 109 public ResultSet getNativeResultSet(ResultSet rs) throws SQLException { 110 return (ResultSet ) getInnermostDelegate(rs); 111 } 112 113 } 114 | Popular Tags |