1 17 18 package org.sape.carbon.services.sql.connection; 19 20 import java.io.PrintWriter ; 21 import java.sql.Connection ; 22 import java.sql.SQLException ; 23 import java.util.Hashtable ; 24 25 import javax.naming.Context ; 26 import javax.naming.InitialContext ; 27 import javax.naming.NamingException ; 28 import javax.sql.DataSource ; 29 30 import org.sape.carbon.core.component.ComponentConfiguration; 31 import org.sape.carbon.core.component.lifecycle.Configurable; 32 import org.sape.carbon.core.component.lifecycle.StateTransitionException; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 37 49 public class NamedDataSourceConnectionFactory 50 implements DataSourceConnectionFactory, Configurable { 51 56 protected DataSource dataSource = null; 57 61 protected String dataSourceUserId = null; 62 63 67 protected String dataSourcePassword = null; 68 69 70 73 private Log log = LogFactory.getLog(this.getClass()); 74 75 85 public Connection getConnection() 86 throws SQLException { 87 88 if (log.isTraceEnabled()) { 89 log.trace("Obtaining JDBC Connection"); 90 } 91 92 Connection conn = null; 93 if (null == this.dataSourceUserId 96 || this.dataSourceUserId.equals("")) { 97 98 conn = dataSource.getConnection(); 99 } else { 100 conn = this.getConnection(this.dataSourceUserId, 101 this.dataSourcePassword); 102 } 103 return conn; 104 } 105 106 115 public Connection getConnection(String overridingDataSourceUserId, 116 String overridingDataSourcePassword) 117 throws SQLException { 118 119 if (log.isTraceEnabled()) { 120 log.trace("Obtaining JDBC Connection. " 121 + "(Overriding DataSource UserId and Password)"); 122 } 123 124 Connection conn = 125 dataSource.getConnection(overridingDataSourceUserId, 126 overridingDataSourcePassword); 127 return conn; 128 } 129 130 134 public PrintWriter getLogWriter() throws SQLException { 135 return dataSource.getLogWriter(); 136 } 137 138 142 public int getLoginTimeout() throws SQLException { 143 return dataSource.getLoginTimeout(); 144 } 145 146 150 public void setLogWriter(PrintWriter printWriter) throws SQLException { 151 dataSource.setLogWriter(printWriter); 152 } 153 154 158 public void setLoginTimeout(int param) throws SQLException { 159 dataSource.setLoginTimeout(param); 160 } 161 162 170 public void configure(ComponentConfiguration configuration) { 171 NamedDataSourceConnectionFactoryConfiguration config = 172 (NamedDataSourceConnectionFactoryConfiguration) configuration; 173 174 String jndiName = config.getJndiName(); 176 String initialContextFactory = config.getInitialContextFactory(); 178 String providerUrl = config.getProviderUrl(); 180 String jndiUserId = config.getJndiUserId(); 182 String jndiPassword = config.getJndiPassword(); 184 this.dataSourceUserId = config.getDataSourceUserId(); 186 this.dataSourcePassword = config.getDataSourcePassword(); 188 189 try { 190 if (log.isTraceEnabled()) { 192 log.trace("Fetching Initial Context"); 193 } 194 Context ic = fetchInitialContext(initialContextFactory, 195 providerUrl, 196 jndiUserId, 197 jndiPassword); 198 199 if (log.isTraceEnabled()) { 201 log.trace("Obtaining DataSource from:" 202 + "[" 203 + jndiName 204 + "]"); 205 } 206 207 this.dataSource = (DataSource ) ic.lookup(jndiName); 208 209 } catch (NamingException ne) { 210 throw new StateTransitionException(this.getClass(), 211 "Error Obtaining DataSource: [" 212 + jndiName 213 + "]" 214 + "from JNDI service: [" 215 + providerUrl 216 + "]", 217 ne); 218 } 219 } 220 221 240 protected Context fetchInitialContext(String initialContextFactory, 241 String providerUrl, 242 String jndiUserId, 243 String jndiPassword) 244 throws NamingException { 245 Context context = null; 247 Hashtable environment = new Hashtable (); 248 249 if (null != initialContextFactory) { 253 environment.put(Context.INITIAL_CONTEXT_FACTORY, 254 initialContextFactory); 255 } 256 if (null != providerUrl) { 257 environment.put(Context.PROVIDER_URL, providerUrl); 258 } 259 if (null != jndiUserId) { 260 environment.put(Context.SECURITY_PRINCIPAL, jndiUserId); 261 } 262 if (null != jndiPassword) { 263 environment.put(Context.SECURITY_CREDENTIALS, jndiPassword); 264 } 265 if (environment.size() > 0) { 268 context = new InitialContext (environment); 274 } else { 275 context = new InitialContext (); 280 } 281 282 return context; 284 } 285 } 286 | Popular Tags |