1 16 17 package org.apache.commons.dbcp.cpdsadapter; 18 19 import java.util.Hashtable ; 20 import java.io.PrintWriter ; 21 import java.io.Serializable ; 22 import java.sql.DriverManager ; 23 import java.sql.SQLException ; 24 import javax.sql.PooledConnection ; 25 import javax.sql.ConnectionPoolDataSource ; 26 import javax.naming.Name ; 27 import javax.naming.Context ; 28 import javax.naming.Referenceable ; 29 import javax.naming.spi.ObjectFactory ; 30 import javax.naming.Reference ; 31 import javax.naming.RefAddr ; 32 import javax.naming.StringRefAddr ; 33 import javax.naming.NamingException ; 34 35 import org.apache.commons.pool.KeyedObjectPool; 36 import org.apache.commons.pool.impl.GenericKeyedObjectPool; 37 38 85 public class DriverAdapterCPDS 86 implements ConnectionPoolDataSource , Referenceable , Serializable , 87 ObjectFactory { 88 89 private static final String GET_CONNECTION_CALLED 90 = "A PooledConnection was already requested from this source, " 91 + "further initialization is not allowed."; 92 93 94 private String description; 95 96 private String password; 97 98 private String url; 99 100 private String user; 101 102 private String driver; 103 104 105 private int loginTimeout; 106 107 private PrintWriter logWriter = null; 108 109 private boolean poolPreparedStatements; 111 private int maxActive = 10; 112 private int maxIdle = 10; 113 private int _timeBetweenEvictionRunsMillis = -1; 114 private int _numTestsPerEvictionRun = -1; 115 private int _minEvictableIdleTimeMillis = -1; 116 117 private boolean getConnectionCalled = false; 118 119 122 public DriverAdapterCPDS() { 123 } 124 125 129 public PooledConnection getPooledConnection() throws SQLException { 130 return getPooledConnection(getUser(), getPassword()); 131 } 132 133 136 public PooledConnection getPooledConnection(String username, 137 String password) 138 throws SQLException { 139 getConnectionCalled = true; 140 148 KeyedObjectPool stmtPool = null; 149 if (isPoolPreparedStatements()) { 150 stmtPool = new GenericKeyedObjectPool(null, 151 getMaxActive(), GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, 0, 152 getMaxIdle(), false, false, getTimeBetweenEvictionRunsMillis(), 153 getNumTestsPerEvictionRun(), 154 getMinEvictableIdleTimeMillis(), false); 155 } 156 157 try { 160 return new PooledConnectionImpl( 161 DriverManager.getConnection(getUrl(), username, password), 162 stmtPool ); 163 } 164 catch (ClassCircularityError e) 165 { 166 return new PooledConnectionImpl( 167 DriverManager.getConnection(getUrl(), username, password), 168 stmtPool ); 169 } 170 } 171 172 175 178 public Reference getReference() throws NamingException { 179 String factory = getClass().getName(); 181 182 Reference ref = new Reference (getClass().getName(), factory, null); 183 184 ref.add(new StringRefAddr ("description", getDescription())); 185 ref.add(new StringRefAddr ("driver", getDriver())); 186 ref.add(new StringRefAddr ("loginTimeout", 187 String.valueOf(getLoginTimeout()))); 188 ref.add(new StringRefAddr ("password", getPassword())); 189 ref.add(new StringRefAddr ("user", getUser())); 190 ref.add(new StringRefAddr ("url", getUrl())); 191 192 ref.add(new StringRefAddr ("poolPreparedStatements", 193 String.valueOf(isPoolPreparedStatements()))); 194 ref.add(new StringRefAddr ("maxActive", 195 String.valueOf(getMaxActive()))); 196 ref.add(new StringRefAddr ("maxIdle", 197 String.valueOf(getMaxIdle()))); 198 ref.add(new StringRefAddr ("timeBetweenEvictionRunsMillis", 199 String.valueOf(getTimeBetweenEvictionRunsMillis()))); 200 ref.add(new StringRefAddr ("numTestsPerEvictionRun", 201 String.valueOf(getNumTestsPerEvictionRun()))); 202 ref.add(new StringRefAddr ("minEvictableIdleTimeMillis", 203 String.valueOf(getMinEvictableIdleTimeMillis()))); 204 205 return ref; 206 } 207 208 209 212 215 public Object getObjectInstance(Object refObj, Name name, 216 Context context, Hashtable env) 217 throws Exception { 218 DriverAdapterCPDS cpds = null; 221 if (refObj instanceof Reference ) { 222 Reference ref = (Reference )refObj; 223 if (ref.getClassName().equals(getClass().getName())) { 224 RefAddr ra = ref.get("description"); 225 if (ra != null && ra.getContent() != null) { 226 setDescription(ra.getContent().toString()); 227 } 228 229 ra = ref.get("driver"); 230 if (ra != null && ra.getContent() != null) { 231 setDriver(ra.getContent().toString()); 232 } 233 ra = ref.get("url"); 234 if (ra != null && ra.getContent() != null) { 235 setUrl(ra.getContent().toString()); 236 } 237 ra = ref.get("user"); 238 if (ra != null && ra.getContent() != null) { 239 setUser(ra.getContent().toString()); 240 } 241 ra = ref.get("password"); 242 if (ra != null && ra.getContent() != null) { 243 setPassword(ra.getContent().toString()); 244 } 245 246 ra = ref.get("poolPreparedStatements"); 247 if (ra != null && ra.getContent() != null) { 248 setPoolPreparedStatements( 249 Boolean.getBoolean(ra.getContent().toString())); 250 } 251 ra = ref.get("maxActive"); 252 if (ra != null && ra.getContent() != null) { 253 setMaxActive(Integer.parseInt(ra.getContent().toString())); 254 } 255 256 ra = ref.get("maxIdle"); 257 if (ra != null && ra.getContent() != null) { 258 setMaxIdle(Integer.parseInt(ra.getContent().toString())); 259 } 260 261 ra = ref.get("timeBetweenEvictionRunsMillis"); 262 if (ra != null && ra.getContent() != null) { 263 setTimeBetweenEvictionRunsMillis( 264 Integer.parseInt(ra.getContent().toString())); 265 } 266 267 ra = ref.get("numTestsPerEvictionRun"); 268 if (ra != null && ra.getContent() != null) { 269 setNumTestsPerEvictionRun( 270 Integer.parseInt(ra.getContent().toString())); 271 } 272 273 ra = ref.get("minEvictableIdleTimeMillis"); 274 if (ra != null && ra.getContent() != null) { 275 setMinEvictableIdleTimeMillis( 276 Integer.parseInt(ra.getContent().toString())); 277 } 278 279 cpds = this; 280 } 281 } 282 return cpds; 283 } 284 285 289 private void assertInitializationAllowed() throws IllegalStateException { 290 if (getConnectionCalled) { 291 throw new IllegalStateException (GET_CONNECTION_CALLED); 292 } 293 } 294 295 298 305 public String getDescription() { 306 return description; 307 } 308 309 316 public void setDescription(String v) { 317 this.description = v; 318 } 319 320 324 public String getPassword() { 325 return password; 326 } 327 328 332 public void setPassword(String v) { 333 assertInitializationAllowed(); 334 this.password = v; 335 } 336 337 341 public String getUrl() { 342 return url; 343 } 344 345 349 public void setUrl(String v) { 350 assertInitializationAllowed(); 351 this.url = v; 352 } 353 354 358 public String getUser() { 359 return user; 360 } 361 362 366 public void setUser(String v) { 367 assertInitializationAllowed(); 368 this.user = v; 369 } 370 371 375 public String getDriver() { 376 return driver; 377 } 378 379 384 public void setDriver(String v) throws ClassNotFoundException { 385 assertInitializationAllowed(); 386 this.driver = v; 387 Class.forName(v); 389 } 390 391 395 public int getLoginTimeout() { 396 return loginTimeout; 397 } 398 399 402 public PrintWriter getLogWriter() { 403 return logWriter; 404 } 405 406 410 public void setLoginTimeout(int seconds) { 411 loginTimeout = seconds; 412 } 413 414 417 public void setLogWriter(java.io.PrintWriter out) { 418 logWriter = out; 419 } 420 421 422 425 426 430 public boolean isPoolPreparedStatements() { 431 return poolPreparedStatements; 432 } 433 434 438 public void setPoolPreparedStatements(boolean v) { 439 assertInitializationAllowed(); 440 this.poolPreparedStatements = v; 441 } 442 443 447 public int getMaxActive() { 448 return (this.maxActive); 449 } 450 451 455 public void setMaxActive(int maxActive) { 456 assertInitializationAllowed(); 457 this.maxActive = maxActive; 458 } 459 460 464 public int getMaxIdle() { 465 return (this.maxIdle); 466 } 467 468 472 public void setMaxIdle(int maxIdle) { 473 assertInitializationAllowed(); 474 this.maxIdle = maxIdle; 475 } 476 477 485 public int getTimeBetweenEvictionRunsMillis() { 486 return _timeBetweenEvictionRunsMillis; 487 } 488 489 497 public void setTimeBetweenEvictionRunsMillis( 498 int timeBetweenEvictionRunsMillis) { 499 assertInitializationAllowed(); 500 _timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; 501 } 502 503 510 public int getNumTestsPerEvictionRun() { 511 return _numTestsPerEvictionRun; 512 } 513 514 525 public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) { 526 assertInitializationAllowed(); 527 _numTestsPerEvictionRun = numTestsPerEvictionRun; 528 } 529 530 538 public int getMinEvictableIdleTimeMillis() { 539 return _minEvictableIdleTimeMillis; 540 } 541 542 552 public void setMinEvictableIdleTimeMillis(int minEvictableIdleTimeMillis) { 553 assertInitializationAllowed(); 554 _minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; 555 } 556 } 557 | Popular Tags |