1 16 17 package org.apache.commons.dbcp; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.sql.CallableStatement ; 22 import java.sql.Connection ; 23 import java.sql.DatabaseMetaData ; 24 import java.sql.Driver ; 25 import java.sql.DriverManager ; 26 import java.sql.DriverPropertyInfo ; 27 import java.sql.PreparedStatement ; 28 import java.sql.SQLException ; 29 import java.sql.SQLWarning ; 30 import java.sql.Statement ; 31 import java.util.HashMap ; 32 import java.util.Map ; 33 import java.util.NoSuchElementException ; 34 import java.util.Properties ; 35 import java.util.Set ; 36 37 import org.apache.commons.jocl.JOCLContentHandler; 38 import org.apache.commons.pool.ObjectPool; 39 import org.xml.sax.SAXException ; 40 41 42 51 public class PoolingDriver implements Driver { 52 53 static { 54 try { 55 DriverManager.registerDriver(new PoolingDriver()); 56 } catch(Exception e) { 57 } 58 } 59 60 61 protected static HashMap _pools = new HashMap (); 62 63 64 private static boolean accessToUnderlyingConnectionAllowed = false; 65 66 public PoolingDriver() { 67 } 68 69 74 public static synchronized boolean isAccessToUnderlyingConnectionAllowed() { 75 return accessToUnderlyingConnectionAllowed; 76 } 77 78 85 public static synchronized void setAccessToUnderlyingConnectionAllowed(boolean allow) { 86 accessToUnderlyingConnectionAllowed = allow; 87 } 88 89 95 public synchronized ObjectPool getPool(String name) { 96 try { 97 return getConnectionPool(name); 98 } 99 catch (Exception e) { 100 throw new DbcpException(e); 101 } 102 } 103 104 public synchronized ObjectPool getConnectionPool(String name) throws SQLException { 105 ObjectPool pool = (ObjectPool)(_pools.get(name)); 106 if(null == pool) { 107 InputStream in = this.getClass().getResourceAsStream(String.valueOf(name) + ".jocl"); 108 if(null != in) { 109 JOCLContentHandler jocl = null; 110 try { 111 jocl = JOCLContentHandler.parse(in); 112 } 113 catch (SAXException e) { 114 throw new SQLNestedException("Could not parse configuration file", e); 115 } 116 catch (IOException e) { 117 throw new SQLNestedException("Could not load configuration file", e); 118 } 119 if(jocl.getType(0).equals(String .class)) { 120 pool = getPool((String )(jocl.getValue(0))); 121 if(null != pool) { 122 registerPool(name,pool); 123 } 124 } else { 125 pool = ((PoolableConnectionFactory)(jocl.getValue(0))).getPool(); 126 if(null != pool) { 127 registerPool(name,pool); 128 } 129 } 130 } 131 else { 132 throw new SQLException ("Configuration file not found"); 133 } 134 } 135 return pool; 136 } 137 138 public synchronized void registerPool(String name, ObjectPool pool) { 139 _pools.put(name,pool); 140 } 141 142 public synchronized void closePool(String name) throws SQLException { 143 ObjectPool pool = (ObjectPool) _pools.get(name); 144 if (pool != null) { 145 _pools.remove(name); 146 try { 147 pool.close(); 148 } 149 catch (Exception e) { 150 throw new SQLNestedException("Error closing pool " + name, e); 151 } 152 } 153 } 154 155 public synchronized String [] getPoolNames() throws SQLException { 156 Set names = _pools.keySet(); 157 return (String []) names.toArray(new String [names.size()]); 158 } 159 160 public boolean acceptsURL(String url) throws SQLException { 161 try { 162 return url.startsWith(URL_PREFIX); 163 } catch(NullPointerException e) { 164 return false; 165 } 166 } 167 168 public Connection connect(String url, Properties info) throws SQLException { 169 if(acceptsURL(url)) { 170 ObjectPool pool = getConnectionPool(url.substring(URL_PREFIX_LEN)); 171 if(null == pool) { 172 throw new SQLException ("No pool found for " + url + "."); 173 } else { 174 try { 175 Connection conn = (Connection )(pool.borrowObject()); 176 if (conn != null) { 177 conn = new PoolGuardConnectionWrapper(conn); 178 } 179 return conn; 180 } catch(SQLException e) { 181 throw e; 182 } catch(NoSuchElementException e) { 183 throw new SQLNestedException("Cannot get a connection, pool exhausted", e); 184 } catch(RuntimeException e) { 185 throw e; 186 } catch(Exception e) { 187 throw new SQLNestedException("Cannot get a connection, general error", e); 188 } 189 } 190 } else { 191 return null; 192 } 193 } 194 195 public int getMajorVersion() { 196 return MAJOR_VERSION; 197 } 198 199 public int getMinorVersion() { 200 return MINOR_VERSION; 201 } 202 203 public boolean jdbcCompliant() { 204 return true; 205 } 206 207 public DriverPropertyInfo [] getPropertyInfo(String url, Properties info) { 208 return new DriverPropertyInfo [0]; 209 } 210 211 212 protected static String URL_PREFIX = "jdbc:apache:commons:dbcp:"; 213 protected static int URL_PREFIX_LEN = URL_PREFIX.length(); 214 215 protected static int MAJOR_VERSION = 1; 217 protected static int MINOR_VERSION = 0; 218 219 223 private class PoolGuardConnectionWrapper extends DelegatingConnection { 224 225 private Connection delegate; 226 227 PoolGuardConnectionWrapper(Connection delegate) { 228 super(delegate); 229 this.delegate = delegate; 230 } 231 232 protected void checkOpen() throws SQLException { 233 if(delegate == null) { 234 throw new SQLException ("Connection is closed."); 235 } 236 } 237 238 public void close() throws SQLException { 239 checkOpen(); 240 this.delegate.close(); 241 this.delegate = null; 242 super.setDelegate(null); 243 } 244 245 public boolean isClosed() throws SQLException { 246 if (delegate == null) { 247 return true; 248 } 249 return delegate.isClosed(); 250 } 251 252 public void clearWarnings() throws SQLException { 253 checkOpen(); 254 delegate.clearWarnings(); 255 } 256 257 public void commit() throws SQLException { 258 checkOpen(); 259 delegate.commit(); 260 } 261 262 public Statement createStatement() throws SQLException { 263 checkOpen(); 264 return delegate.createStatement(); 265 } 266 267 public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { 268 checkOpen(); 269 return delegate.createStatement(resultSetType, resultSetConcurrency); 270 } 271 272 public boolean equals(Object obj) { 273 if (delegate == null){ 274 return false; 275 } 276 return delegate.equals(obj); 277 } 278 279 public boolean getAutoCommit() throws SQLException { 280 checkOpen(); 281 return delegate.getAutoCommit(); 282 } 283 284 public String getCatalog() throws SQLException { 285 checkOpen(); 286 return delegate.getCatalog(); 287 } 288 289 public DatabaseMetaData getMetaData() throws SQLException { 290 checkOpen(); 291 return delegate.getMetaData(); 292 } 293 294 public int getTransactionIsolation() throws SQLException { 295 checkOpen(); 296 return delegate.getTransactionIsolation(); 297 } 298 299 public Map getTypeMap() throws SQLException { 300 checkOpen(); 301 return delegate.getTypeMap(); 302 } 303 304 public SQLWarning getWarnings() throws SQLException { 305 checkOpen(); 306 return delegate.getWarnings(); 307 } 308 309 public int hashCode() { 310 if (delegate == null){ 311 return 0; 312 } 313 return delegate.hashCode(); 314 } 315 316 public boolean isReadOnly() throws SQLException { 317 checkOpen(); 318 return delegate.isReadOnly(); 319 } 320 321 public String nativeSQL(String sql) throws SQLException { 322 checkOpen(); 323 return delegate.nativeSQL(sql); 324 } 325 326 public CallableStatement prepareCall(String sql) throws SQLException { 327 checkOpen(); 328 return delegate.prepareCall(sql); 329 } 330 331 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { 332 checkOpen(); 333 return delegate.prepareCall(sql, resultSetType, resultSetConcurrency); 334 } 335 336 public PreparedStatement prepareStatement(String sql) throws SQLException { 337 checkOpen(); 338 return delegate.prepareStatement(sql); 339 } 340 341 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { 342 checkOpen(); 343 return delegate.prepareStatement(sql, resultSetType, resultSetConcurrency); 344 } 345 346 public void rollback() throws SQLException { 347 checkOpen(); 348 delegate.rollback(); 349 } 350 351 public void setAutoCommit(boolean autoCommit) throws SQLException { 352 checkOpen(); 353 delegate.setAutoCommit(autoCommit); 354 } 355 356 public void setCatalog(String catalog) throws SQLException { 357 checkOpen(); 358 delegate.setCatalog(catalog); 359 } 360 361 public void setReadOnly(boolean readOnly) throws SQLException { 362 checkOpen(); 363 delegate.setReadOnly(readOnly); 364 } 365 366 public void setTransactionIsolation(int level) throws SQLException { 367 checkOpen(); 368 delegate.setTransactionIsolation(level); 369 } 370 371 public void setTypeMap(Map map) throws SQLException { 372 checkOpen(); 373 delegate.setTypeMap(map); 374 } 375 376 public String toString() { 377 if (delegate == null){ 378 return null; 379 } 380 return delegate.toString(); 381 } 382 383 386 387 388 public int getHoldability() throws SQLException { 389 checkOpen(); 390 return delegate.getHoldability(); 391 } 392 393 public void setHoldability(int holdability) throws SQLException { 394 checkOpen(); 395 delegate.setHoldability(holdability); 396 } 397 398 public java.sql.Savepoint setSavepoint() throws SQLException { 399 checkOpen(); 400 return delegate.setSavepoint(); 401 } 402 403 public java.sql.Savepoint setSavepoint(String name) throws SQLException { 404 checkOpen(); 405 return delegate.setSavepoint(name); 406 } 407 408 public void releaseSavepoint(java.sql.Savepoint savepoint) throws SQLException { 409 checkOpen(); 410 delegate.releaseSavepoint(savepoint); 411 } 412 413 public void rollback(java.sql.Savepoint savepoint) throws SQLException { 414 checkOpen(); 415 delegate.rollback(savepoint); 416 } 417 418 public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { 419 checkOpen(); 420 return delegate.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability); 421 } 422 423 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { 424 checkOpen(); 425 return delegate.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability); 426 } 427 428 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { 429 checkOpen(); 430 return delegate.prepareStatement(sql, autoGeneratedKeys); 431 } 432 433 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { 434 checkOpen(); 435 return delegate.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability); 436 } 437 438 public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { 439 checkOpen(); 440 return delegate.prepareStatement(sql, columnIndexes); 441 } 442 443 public PreparedStatement prepareStatement(String sql, String [] columnNames) throws SQLException { 444 checkOpen(); 445 return delegate.prepareStatement(sql, columnNames); 446 } 447 448 449 450 453 public Connection getDelegate() { 454 if (isAccessToUnderlyingConnectionAllowed()) { 455 return super.getDelegate(); 456 } else { 457 return null; 458 } 459 } 460 461 464 public Connection getInnermostDelegate() { 465 if (isAccessToUnderlyingConnectionAllowed()) { 466 return super.getInnermostDelegate(); 467 } else { 468 return null; 469 } 470 } 471 } 472 } 473
| Popular Tags
|