1 16 17 package org.springframework.jdbc.datasource; 18 19 import java.lang.reflect.Method ; 20 import java.sql.Connection ; 21 import java.sql.SQLException ; 22 23 import javax.sql.DataSource ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 28 import org.springframework.util.ReflectionUtils; 29 import org.springframework.util.StringUtils; 30 31 69 public class WebSphereDataSourceAdapter extends IsolationLevelDataSourceAdapter { 70 71 protected final Log logger = LogFactory.getLog(getClass()); 72 73 private Class wsDataSourceClass; 74 75 private Method newJdbcConnSpecMethod; 76 77 private Method wsDataSourceGetConnectionMethod; 78 79 private Method setTransactionIsolationMethod; 80 81 private Method setReadOnlyMethod; 82 83 private Method setUserNameMethod; 84 85 private Method setPasswordMethod; 86 87 88 92 public WebSphereDataSourceAdapter() { 93 try { 94 this.wsDataSourceClass = getClass().getClassLoader().loadClass("com.ibm.websphere.rsadapter.WSDataSource"); 95 Class jdbcConnSpecClass = getClass().getClassLoader().loadClass("com.ibm.websphere.rsadapter.JDBCConnectionSpec"); 96 Class wsrraFactoryClass = getClass().getClassLoader().loadClass("com.ibm.websphere.rsadapter.WSRRAFactory"); 97 this.newJdbcConnSpecMethod = wsrraFactoryClass.getMethod("createJDBCConnectionSpec", null); 98 this.wsDataSourceGetConnectionMethod = 99 this.wsDataSourceClass.getMethod("getConnection", new Class [] {jdbcConnSpecClass}); 100 this.setTransactionIsolationMethod = 101 jdbcConnSpecClass.getMethod("setTransactionIsolation", new Class [] {int.class}); 102 this.setReadOnlyMethod = jdbcConnSpecClass.getMethod("setReadOnly", new Class [] {Boolean .class}); 103 this.setUserNameMethod = jdbcConnSpecClass.getMethod("setUserName", new Class [] {String .class}); 104 this.setPasswordMethod = jdbcConnSpecClass.getMethod("setPassword", new Class [] {String .class}); 105 } 106 catch (Exception ex) { 107 throw new IllegalStateException ( 108 "Could not initialize WebSphereDataSourceAdapter because WebSphere API classes are not available: " + ex); 109 } 110 } 111 112 116 public void afterPropertiesSet() { 117 super.afterPropertiesSet(); 118 119 if (!this.wsDataSourceClass.isInstance(getTargetDataSource())) { 120 throw new IllegalStateException ( 121 "Specified 'targetDataSource' is not a WebSphere WSDataSource: " + getTargetDataSource()); 122 } 123 } 124 125 126 132 protected Connection doGetConnection(String username, String password) throws SQLException { 133 Object connSpec = createConnectionSpec( 135 getCurrentIsolationLevel(), getCurrentReadOnlyFlag(), username, password); 136 if (logger.isDebugEnabled()) { 137 logger.debug("Obtaining JDBC Connection from WebSphere DataSource [" + 138 getTargetDataSource() + "], using ConnectionSpec [" + connSpec + "]"); 139 } 140 return (Connection ) ReflectionUtils.invokeMethod( 142 this.wsDataSourceGetConnectionMethod, getTargetDataSource(), new Object [] {connSpec}); 143 } 144 145 158 protected Object createConnectionSpec( 159 Integer isolationLevel, Boolean readOnlyFlag, String username, String password) throws SQLException { 160 161 Object connSpec = ReflectionUtils.invokeMethod(this.newJdbcConnSpecMethod, null); 162 if (isolationLevel != null) { 163 ReflectionUtils.invokeMethod(this.setTransactionIsolationMethod, connSpec, new Object [] {isolationLevel}); 164 } 165 if (readOnlyFlag != null) { 166 ReflectionUtils.invokeMethod(this.setReadOnlyMethod, connSpec, new Object [] {readOnlyFlag}); 167 } 168 if (StringUtils.hasLength(username)) { 171 ReflectionUtils.invokeMethod(this.setUserNameMethod, connSpec, new Object [] {username}); 172 ReflectionUtils.invokeMethod(this.setPasswordMethod, connSpec, new Object [] {password}); 173 } 174 return connSpec; 175 } 176 177 } 178 | Popular Tags |