1 29 30 package com.caucho.sql; 31 32 import com.caucho.config.BuilderProgram; 33 import com.caucho.config.BuilderProgramContainer; 34 import com.caucho.config.Config; 35 import com.caucho.config.ConfigException; 36 import com.caucho.config.StringAttributeProgram; 37 import com.caucho.config.types.InitParam; 38 import com.caucho.log.Log; 39 import com.caucho.management.j2ee.J2EEManagedObject; 40 import com.caucho.management.j2ee.JDBCDriver; 41 import com.caucho.naming.Jndi; 42 import com.caucho.tools.profiler.ConnectionPoolDataSourceWrapper; 43 import com.caucho.tools.profiler.DriverWrapper; 44 import com.caucho.tools.profiler.ProfilerPoint; 45 import com.caucho.tools.profiler.ProfilerPointConfig; 46 import com.caucho.tools.profiler.XADataSourceWrapper; 47 import com.caucho.util.L10N; 48 49 import javax.resource.spi.ManagedConnectionFactory ; 50 import javax.sql.ConnectionPoolDataSource ; 51 import javax.sql.PooledConnection ; 52 import javax.sql.XADataSource ; 53 import java.sql.Connection ; 54 import java.sql.Driver ; 55 import java.sql.SQLException ; 56 import java.util.HashMap ; 57 import java.util.Iterator ; 58 import java.util.Properties ; 59 import java.util.logging.Level ; 60 import java.util.logging.Logger ; 61 62 65 public class DriverConfig { 66 protected static final Logger log = Log.open(DriverConfig.class); 67 private static final L10N L = new L10N(DriverConfig.class); 68 69 private static final int TYPE_UNKNOWN = 0; 70 private static final int TYPE_DRIVER = 1; 71 private static final int TYPE_POOL = 2; 72 private static final int TYPE_XA = 3; 73 private static final int TYPE_JCA = 4; 74 75 79 private static final String URL_PREFIX = "jdbc:caucho:" ; 80 81 85 public static final String PROPERTY_USER = "user" ; 86 90 public static final String PROPERTY_PASSWORD = "password" ; 91 92 private DBPoolImpl _dbPool; 93 94 private Class _driverClass; 95 96 private String _driverURL; 97 private String _user; 98 private String _password; 99 private Properties _info; 100 101 private BuilderProgramContainer _init = new BuilderProgramContainer(); 102 103 private int _driverType; 104 private Object _driverObject; 105 106 private ManagedConnectionFactory _jcaDataSource; 107 private ConnectionPoolDataSource _poolDataSource; 108 private XADataSource _xaDataSource; 109 private Driver _driver; 110 111 private ProfilerPoint _profilerPoint; 112 113 private boolean _isInit; 114 private boolean _isStarted; 115 116 120 public DriverConfig(DBPoolImpl pool) 121 { 122 _dbPool = pool; 123 124 _info = new Properties (); 125 } 126 127 130 public DBPoolImpl getDBPool() 131 { 132 return _dbPool; 133 } 134 135 138 public void setDriverType(String type) 139 throws ConfigException 140 { 141 if ("ConnectionPoolDataSource".equals(type)) { 142 _driverType = TYPE_POOL; 143 } 144 else if ("XADataSource".equals(type)) { 145 _driverType = TYPE_XA; 146 } 147 else if ("ManagedConnectionFactory".equals(type)) { 148 _driverType = TYPE_JCA; 149 } 150 else if ("Driver".equals(type)) { 151 _driverType = TYPE_DRIVER; 152 } 153 else 154 throw new ConfigException(L.l("'{0}' is an unknown driver type. Valid types are 'ConnectionPoolDataSource', 'XADataSource' and 'Driver'")); 155 } 156 157 160 public void setDataSource(Object dataSource) 161 throws ConfigException 162 { 163 if (dataSource instanceof String ) 164 dataSource = Jndi.lookup((String ) dataSource); 165 166 if (_driverType == TYPE_XA) 167 _xaDataSource = (XADataSource ) dataSource; 168 else if (_driverType == TYPE_POOL) 169 _poolDataSource = (ConnectionPoolDataSource ) dataSource; 170 else if (dataSource instanceof XADataSource ) 171 _xaDataSource = (XADataSource ) dataSource; 172 else if (dataSource instanceof ConnectionPoolDataSource ) 173 _poolDataSource = (ConnectionPoolDataSource ) dataSource; 174 else if (dataSource instanceof ManagedConnectionFactory ) 175 _jcaDataSource = (ManagedConnectionFactory ) dataSource; 176 else 177 throw new ConfigException(L.l("data-source `{0}' is of type `{1}' which does not implement XADataSource or ConnectionPoolDataSource.", 178 dataSource, 179 dataSource.getClass().getName())); 180 } 181 182 185 public Class getDriverClass() 186 { 187 return _driverClass; 188 } 189 190 193 public void setType(Class driverClass) 194 throws ConfigException 195 { 196 _driverClass = driverClass; 197 198 if (! Driver.class.isAssignableFrom(driverClass) && 199 ! XADataSource .class.isAssignableFrom(driverClass) && 200 ! ConnectionPoolDataSource .class.isAssignableFrom(driverClass) && 201 ! ManagedConnectionFactory .class.isAssignableFrom(driverClass)) 202 throw new ConfigException(L.l("`{0}' is not a valid database type.", 203 driverClass.getName())); 204 205 Config.checkCanInstantiate(driverClass); 206 } 207 208 211 public String getURL() 212 { 213 return _driverURL; 214 } 215 216 219 public void setURL(String url) 220 { 221 _driverURL = url; 222 } 223 224 227 public void addBuilderProgram(BuilderProgram program) 228 { 229 _init.addProgram(program); 230 } 231 232 235 public String getUser() 236 { 237 return _user; 238 } 239 240 243 public void setUser(String user) 244 { 245 _user = user; 246 } 247 248 251 public String getPassword() 252 { 253 return _password; 254 } 255 256 259 public void setPassword(String password) 260 { 261 _password = password; 262 } 263 264 271 public void setInitParam(InitParam initParam) 272 { 273 validateInitParam(); 274 275 HashMap <String ,String > paramMap = initParam.getParameters(); 276 277 Iterator <String > iter = paramMap.keySet().iterator(); 278 while (iter.hasNext()) { 279 String key = iter.next(); 280 281 _info.setProperty(key, paramMap.get(key)); 282 } 283 } 284 285 292 public void setInitParam(String key, String value) 293 { 294 _info.setProperty(key, value); 295 } 296 297 300 public Properties getInfo() 301 { 302 return _info; 303 } 304 305 308 public Driver getDriver() 309 throws SQLException 310 { 311 Object obj = getDriverObject(); 312 313 if (obj instanceof Driver) 314 return (Driver) obj; 315 else 316 return null; 317 } 318 319 322 public void setDriver(Driver driver) 323 throws SQLException 324 { 325 _driver = driver; 326 _driverObject = driver; 327 } 328 329 332 public ConnectionPoolDataSource getPoolDataSource() 333 throws SQLException 334 { 335 return _poolDataSource; 336 } 337 338 341 public void setPoolDataSource(ConnectionPoolDataSource pDataSource) 342 throws SQLException 343 { 344 _poolDataSource = pDataSource; 345 _driverObject = _poolDataSource; 346 } 347 348 351 public XADataSource getXADataSource() 352 { 353 return _xaDataSource; 354 } 355 356 359 public void setXADataSource(XADataSource xaDataSource) 360 throws SQLException 361 { 362 _xaDataSource = xaDataSource; 363 _driverObject = _xaDataSource; 364 } 365 366 369 public ManagedConnectionFactory getManagedConnectionFactory() 370 { 371 return _jcaDataSource; 372 } 373 374 377 public boolean isXATransaction() 378 { 379 return _xaDataSource != null && _dbPool.isXA(); 380 } 381 382 385 public boolean isLocalTransaction() 386 { 387 return _dbPool.isXA(); 388 } 389 390 396 public ProfilerPointConfig createProfilerPoint() 397 { 398 ProfilerPointConfig profilerPointConfig = new ProfilerPointConfig(); 399 400 profilerPointConfig.setName(getURL()); 401 profilerPointConfig.setCategorizing(true); 402 403 return profilerPointConfig; 404 } 405 406 409 public void setProfilerPoint(ProfilerPoint profilerPoint) 410 { 411 _profilerPoint = profilerPoint; 412 } 413 414 423 synchronized void initDataSource(boolean isTransactional, boolean isSpy) 424 throws SQLException 425 { 426 if (_isStarted) 427 return; 428 429 _isStarted = true; 430 431 if (_xaDataSource == null && _poolDataSource == null) { 432 initDriver(); 433 434 Object driverObject = getDriverObject(); 435 436 if (driverObject == null) { 437 throw new SQLExceptionWrapper(L.l("driver `{0}' has not been configured for pool {1}. <database> needs a <driver type='...'>.", 438 _driverClass, getDBPool().getName())); 439 } 440 441 if (_driverType == TYPE_XA) 442 _xaDataSource = (XADataSource ) _driverObject; 443 else if (_driverType == TYPE_POOL) 444 _poolDataSource = (ConnectionPoolDataSource ) _driverObject; 445 else if (_driverType == TYPE_DRIVER) 446 _driver = (Driver) _driverObject; 447 else if (driverObject instanceof XADataSource ) 448 _xaDataSource = (XADataSource ) _driverObject; 449 else if (_driverObject instanceof ConnectionPoolDataSource ) 450 _poolDataSource = (ConnectionPoolDataSource ) _driverObject; 451 else if (_driverObject instanceof ManagedConnectionFactory ) 452 _jcaDataSource = (ManagedConnectionFactory ) _driverObject; 453 else if (_driverObject instanceof Driver) 454 _driver = (Driver) _driverObject; 455 else 456 throw new SQLExceptionWrapper(L.l("driver `{0}' has not been configured for pool {1}. <database> needs a <driver type='...'>.", 457 _driverClass, getDBPool().getName())); 458 459 465 } 466 467 if (_profilerPoint != null) { 468 if (log.isLoggable(Level.FINE)) 469 log.fine(_profilerPoint.toString()); 470 471 if (_xaDataSource != null) 472 _xaDataSource = new XADataSourceWrapper(_profilerPoint, _xaDataSource); 473 else if (_poolDataSource != null) 474 _poolDataSource = new ConnectionPoolDataSourceWrapper(_profilerPoint, _poolDataSource); 475 else if (_driver != null) 476 _driver = new DriverWrapper(_profilerPoint, _driver); 477 } 478 479 if (_info.size() != 0) { 480 validateInitParam(); 481 } 482 483 J2EEManagedObject.register(new JDBCDriver(this)); 484 } 485 486 private void validateInitParam() 487 { 488 if (_jcaDataSource != null) { 489 throw new ConfigException(L.l("<init-param> cannot be used with a JCA data source. Use the init-param key as a tag, like <key>value</key>")); 490 } 491 else if (_poolDataSource != null) { 492 throw new ConfigException(L.l("<init-param> cannot be used with a ConnectionPoolDataSource. Use the init-param key as a tag, like <key>value</key>")); 493 } 494 else if (_xaDataSource != null) { 495 throw new ConfigException(L.l("<init-param> cannot be used with an XADataSource. Use the init-param key as a tag, like <key>value</key>")); 496 } 497 } 498 499 502 synchronized Object getDriverObject() 503 throws SQLException 504 { 505 if (_driverObject != null) 506 return _driverObject; 507 else if (_driverClass == null) 508 return null; 509 510 if (log.isLoggable(Level.CONFIG)) 511 log.config("loading driver: " + _driverClass.getName()); 512 513 try { 514 _driverObject = _driverClass.newInstance(); 515 } catch (Exception e) { 516 throw new SQLExceptionWrapper(e); 517 } 518 519 return _driverObject; 520 } 521 522 525 PooledConnection createPooledConnection(String user, String password) 526 throws SQLException 527 { 528 PooledConnection conn = null; 529 if (_xaDataSource != null) { 530 if (user == null && password == null) 531 conn = _xaDataSource.getXAConnection(); 532 else 533 conn = _xaDataSource.getXAConnection(user, password); 534 535 541 } 542 else if (_poolDataSource != null) { 543 549 550 if (user == null && password == null) 551 conn = _poolDataSource.getPooledConnection(); 552 else 553 conn = _poolDataSource.getPooledConnection(user, password); 554 } 555 556 return conn; 557 } 558 559 562 Connection createDriverConnection(String user, String password) 563 throws SQLException 564 { 565 if (_xaDataSource != null || _poolDataSource != null) 566 throw new IllegalStateException (); 567 568 if (_driver == null) 569 throw new IllegalStateException (); 570 571 Driver driver = _driver; 572 String url = getURL(); 573 574 if (url == null) 575 throw new SQLException (L.l("can't create connection with null url")); 576 577 Properties properties = getInfo(); 578 579 if (user != null) 580 properties.put("user", user); 581 else 582 properties.put("user", ""); 583 584 if (password != null) 585 properties.put("password", password); 586 else 587 properties.put("password", ""); 588 589 Connection conn; 590 if (driver != null) 591 conn = driver.connect(url, properties); 592 else 593 conn = java.sql.DriverManager.getConnection(url, properties); 594 595 return conn; 596 } 597 598 601 public void initDriver() 602 throws SQLException 603 { 604 if (_isInit) 605 return; 606 607 _isInit = true; 608 609 Object driverObject = getDriverObject(); 610 611 if (driverObject != null) { 612 } 613 else if (_xaDataSource != null || _poolDataSource != null) 614 return; 615 else { 616 throw new SQLExceptionWrapper(L.l("driver `{0}' has not been configured for pool {1}. <database> needs either a <data-source> or a <type>.", 617 _driverClass, getDBPool().getName())); 618 } 619 620 try { 621 if (_driverURL != null) { StringAttributeProgram program; 624 program = new StringAttributeProgram("url", _driverURL); 625 program.configure(driverObject); 626 } 627 } catch (Throwable e) { 628 if (driverObject instanceof Driver) 629 log.log(Level.FINEST, e.toString(), e); 630 else 631 throw new SQLExceptionWrapper(e); 632 } 633 634 try { 635 if (_user != null) { StringAttributeProgram program; 637 program = new StringAttributeProgram("user", _user); 638 program.configure(driverObject); 639 } 640 } catch (Throwable e) { 641 log.log(Level.FINEST, e.toString(), e); 642 643 if (! (driverObject instanceof Driver)) 644 throw new SQLExceptionWrapper(e); 645 } 646 647 try { 648 if (_password != null) { StringAttributeProgram program; 650 program = new StringAttributeProgram("password", _password); 651 program.configure(driverObject); 652 } 653 } catch (Throwable e) { 654 log.log(Level.FINEST, e.toString(), e); 655 656 if (! (driverObject instanceof Driver)) 657 throw new SQLExceptionWrapper(e); 658 } 659 660 try { 661 if (_init != null) { 662 _init.configure(driverObject); 663 _init = null; 664 } 665 666 Config.init(driverObject); 667 } catch (Throwable e) { 668 log.log(Level.FINE, e.toString(), e); 669 throw new SQLExceptionWrapper(e); 670 } 671 } 672 673 676 public String toString() 677 { 678 return "Driver[" + _driverURL + "]"; 679 } 680 } 681 | Popular Tags |