1 23 24 package org.dbforms.config; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.dbforms.conprovider.ConnectionFactory; 30 import org.dbforms.conprovider.ConnectionProviderPrefs; 31 32 import org.dbforms.util.Util; 33 34 import java.io.PrintWriter ; 35 36 import java.sql.Connection ; 37 import java.sql.DriverManager ; 38 import java.sql.SQLException ; 39 40 import java.util.Properties ; 41 42 import javax.naming.Context ; 43 import javax.naming.InitialContext ; 44 import javax.naming.NamingException ; 45 46 import javax.sql.DataSource ; 47 48 49 50 91 public class DbConnection implements javax.sql.DataSource { 92 private static Log logCat = LogFactory.getLog(DbConnection.class); 93 94 95 private ConnectionFactory connectionFactory = ConnectionFactory.instance(); 96 97 98 private Properties poolProperties; 99 100 101 private Properties properties; 102 103 104 private String conClass; 105 106 107 private String connectionPoolURL; 108 109 110 private String connectionProviderClass; 111 112 116 private String contextDataSource; 117 118 119 private String id; 120 121 122 private String isJndi = "false"; 123 124 125 private String name; 126 127 128 private String password; 129 130 131 private String username; 132 133 134 private boolean defaultConnection = false; 135 136 140 private boolean isFactorySetup = false; 141 142 143 private boolean isPropSetup = false; 144 145 146 private boolean jndi = false; 147 148 152 private boolean pow2 = false; 153 154 157 public DbConnection() { 158 properties = new java.util.Properties (); 159 poolProperties = new java.util.Properties (); 160 } 161 162 167 public void setConClass(String conClass) { 168 this.conClass = conClass; 169 } 170 171 172 177 public String getConClass() { 178 return conClass; 179 } 180 181 182 187 public Connection getConnection() { 188 Connection con = null; 189 190 if (jndi) { 193 con = getConnectionFromJNDI(name); 194 } 195 else if (pow2) { 198 con = getConnectionFromFactory(); 199 } 200 else { 202 con = getConnectionFromDriverManager(); 203 } 204 205 return con; 206 } 207 208 209 217 public Connection getConnection(String p_username, 218 String p_password) throws SQLException { 219 setUsername(p_username); 220 setPassword(p_password); 221 222 return getConnection(); 223 } 224 225 226 231 public void setConnectionPoolURL(String url) { 232 connectionPoolURL = url; 233 } 234 235 236 241 public String getConnectionPoolURL() { 242 return connectionPoolURL; 243 } 244 245 246 251 public void setConnectionProviderClass(String cpc) { 252 connectionProviderClass = cpc; 253 } 254 255 256 261 public String getConnectionProviderClass() { 262 return connectionProviderClass; 263 } 264 265 266 272 public void setContextDataSource(String contextDataSource) { 273 this.contextDataSource = contextDataSource; 274 } 275 276 277 283 public String getContextDataSource() { 284 return contextDataSource; 285 } 286 287 288 293 public void setDefaultConnection(boolean defaultConnection) { 294 this.defaultConnection = defaultConnection; 295 } 296 297 298 303 public boolean isDefaultConnection() { 304 return defaultConnection; 305 } 306 307 308 313 public void setDriverClassName(String driverClassName) { 314 setConClass(driverClassName); 315 } 316 317 318 323 public void setId(String id) { 324 this.id = id; 325 } 326 327 328 333 public String getId() { 334 return id; 335 } 336 337 338 343 public void setIsJndi(String isJndi) { 344 this.isJndi = isJndi; 345 jndi = Util.getTrue(isJndi); 346 } 347 348 349 354 public void setIsPow2(String isPow2) { 355 pow2 = Util.getTrue(isPow2); 356 } 357 358 359 364 public void setJdbcURL(String jdbcURL) { 365 setName(jdbcURL); 366 } 367 368 369 374 public void setLogWriter(PrintWriter out) throws SQLException { 375 throw new SQLException ("dbforms.error.not_supported"); 376 } 377 378 379 384 public PrintWriter getLogWriter() throws SQLException { 385 throw new SQLException ("dbforms.error.not_supported"); 386 } 387 388 389 394 public void setLoginTimeout(int seconds) throws SQLException { 395 throw new SQLException ("dbforms.error.not_supported"); 396 } 397 398 399 404 public int getLoginTimeout() throws SQLException { 405 throw new SQLException ("dbforms.error.not_supported"); 406 } 407 408 409 414 public void setName(String name) { 415 this.name = name; 416 } 417 418 419 424 public String getName() { 425 return name; 426 } 427 428 429 434 public void setPassword(String newpass) { 435 this.password = newpass; 436 } 437 438 439 444 public String getPassword() { 445 return password; 446 } 447 448 449 454 public void setUsername(String newuser) { 455 this.username = newuser; 456 } 457 458 459 464 public String getUsername() { 465 return username; 466 } 467 468 469 474 public void addPoolProperty(DbConnectionProperty prop) { 475 poolProperties.put(prop.getName(), prop.getValue()); 476 } 477 478 479 484 public void addProperty(DbConnectionProperty prop) { 485 properties.put(prop.getName(), prop.getValue()); 486 } 487 488 489 494 public String getisJndi() { 495 return isJndi; 496 } 497 498 499 504 public String toString() { 505 StringBuffer buf = new StringBuffer ("DbConnection = "); 506 507 buf.append("id=" + id) 508 .append(", name=" + name) 509 .append(", jndi=" + isJndi) 510 .append(", conClass=" + conClass) 511 .append(", username=" + username) 512 .append(", default=" + defaultConnection); 513 514 if (pow2) { 515 buf.append(", connectionProviderClass=" + connectionProviderClass) 516 .append(", connectionPoolURL=" + connectionPoolURL); 517 } 518 519 if (!properties.isEmpty()) { 520 buf.append(", jdbc properties: ") 521 .append(properties); 522 } 523 524 if (!poolProperties.isEmpty()) { 525 buf.append(", connection pool properties: ") 526 .append(poolProperties); 527 } 528 529 return buf.toString(); 531 } 532 533 534 540 private Connection getConnectionFromDriverManager() { 541 Connection con = null; 542 543 try { 544 Class.forName(conClass) 545 .newInstance(); 546 547 if (!properties.isEmpty()) { 548 if (!isPropSetup) { 549 properties.put("user", getUsername()); 550 properties.put("password", getPassword()); 551 isPropSetup = true; 552 } 553 554 con = DriverManager.getConnection(name, properties); 555 } else if (username != null) { 556 con = DriverManager.getConnection(name, username, password); 557 } else { 558 con = DriverManager.getConnection(name); 559 } 560 } catch (Exception e) { 561 logCat.error("::getConnectionFromDriverManager - cannot retrieve a connection from DriverManager", 562 e); 563 } 564 565 return con; 566 } 567 568 569 574 private Connection getConnectionFromFactory() { 575 Connection con = null; 576 577 try { 578 if (!isFactorySetup) { 579 setupConnectionFactory(); 580 } 581 582 con = connectionFactory.getConnection(); 583 } catch (Exception se) { 584 logCat.error("::getConnectionFromFactory - cannot retrieve a connection from the connectionFactory", 585 se); 586 } 587 588 return con; 589 } 590 591 592 599 607 private Connection getConnectionFromJNDI(String lookupString) { 608 Connection con = null; 609 610 try { 613 Context ctx = new InitialContext (); 614 DataSource ds = (DataSource ) ctx.lookup(lookupString); 615 616 if (ds != null) { 617 con = ds.getConnection(); 618 } else { 619 logCat.error("::getConnectionFromJNDI - DataSource object is null"); 620 } 621 } catch (NamingException ne) { 622 logCat.error("::getConnectionFromJNDI - cannot retrieve a connection from JNDI:", 623 ne); 624 } catch (Exception e) { 625 logCat.error("::getConnectionFromJNDI - exception:", e); 626 } 627 628 return con; 629 } 630 631 632 637 private void setupConnectionFactory() throws Exception { 638 ConnectionProviderPrefs prefs = new ConnectionProviderPrefs(); 639 640 prefs.setConnectionProviderClass(connectionProviderClass); 641 prefs.setConnectionPoolURL(connectionPoolURL); 642 prefs.setJdbcDriver(conClass); 643 prefs.setJdbcURL(name); 644 prefs.setUser(username); 645 prefs.setPassword(password); 646 prefs.setProperties(properties); 647 prefs.setPoolProperties(poolProperties); 648 prefs.setServletContext(DbFormsConfigRegistry.instance().lookup().getServletContext()); 649 connectionFactory.setProvider(prefs); 650 isFactorySetup = true; 651 } 652 } 653 | Popular Tags |