001: package simpleorm.core;
002:
003: import java.sql.Connection;
004: import java.util.*;
005: import java.io.File;
006: import java.io.FileInputStream;
007: import java.io.PrintStream;
008:
009: /**
010: * Provides a source of JDBC connections. It has been introduced
011: * because some key generation
012: * algorithms require the creation of separate connections to create the
013: * actual key value. So just passing a single jdbc connection object into
014: * SConnection.attach is not sufficient.<p>
015: *
016: * If no connection pooling is used then just do:-
017: * <pre>
018: * Class.forName(dbDriver);
019: * SDataSource ds = new SDataSource(dburl, dbUserName, dbPassword);
020: * // ds.setSDriverName("..."); to use a non-default driver
021: * SConnection.attach(ds, "TestCon");
022: * </pre>
023: *
024: * attach calls openDBConnection which simply calls
025: * <code>java.sql.DriverManager.getConnection(url, jdbcProperties)</code><p>
026: *
027: * It is intended that this class can be subclassed, especially to enable
028: * connection pooling. It does not directly implement javax.sql.DataSource
029: * to avoid dependencies on Java 1.4 and EJBs.<p>
030: *
031: * @see SDataSourceJavaX is normally used for connection pooling.
032: *
033: */
034: public class SDataSource {
035:
036: String url;
037: Properties jdbcProperties;
038:
039: /**
040: * Constructor used to provide connections expicitly.
041: * Normally SConfiguration.Default is used instead.
042: * secondary is only required for certain key generation algorithms.
043: * driverName normally null, in which case a default will be inferred from the Connection.
044: * Note that this means that a new configuration is needed each time ###.
045: */
046: public SDataSource(String url, Properties props) {
047: this .url = url;
048: jdbcProperties = props;
049: }
050:
051: public SDataSource(String url) {
052: this .url = url;
053: }
054:
055: public SDataSource(String url, String username, String password) {
056: this .url = url;
057: setUsername(username);
058: setPassword(password);
059: }
060:
061: public SDataSource() {
062: }
063:
064: public Properties getJdbcProperties() {
065: return jdbcProperties;
066: }
067:
068: public void setJdbcProperties(Properties jdbcProperties) {
069: this .jdbcProperties = jdbcProperties;
070: }
071:
072: public String getUrl() {
073: return url;
074: }
075:
076: public void setUrl(String url) {
077: this .url = url;
078: }
079:
080: /** Sets "user" property. Creates new Properties if ncessary.*/
081: public void setUsername(String username) {
082: if (jdbcProperties == null)
083: jdbcProperties = new Properties();
084: jdbcProperties.put("user", username);
085: }
086:
087: /** Sets "password" property. Creates new Properties if ncessary.*/
088: public void setPassword(String password) {
089: if (jdbcProperties == null)
090: jdbcProperties = new Properties();
091: jdbcProperties.put("password", password);
092: }
093:
094: /**
095: * The main connection used by SimpleORM.
096: * (Some applications might just provide an explicit connection to SConnection.attach
097: * in which case this is never called.)
098: */
099: protected Connection openPrimaryDBConnection() {
100: return openDBConnection();
101: }
102:
103: /**
104: * Secondary connection(s) are mainly used for generating primary keys
105: * using some table based algorithms.
106: */
107: protected Connection openSecondaryDBConnection() {
108: return openDBConnection();
109: }
110:
111: /**
112: * Called by obtainPrimary|SecondaryJDBCConnection
113: */
114: protected Connection openDBConnection() {
115: Connection con;
116: try {
117: con = java.sql.DriverManager.getConnection(url,
118: jdbcProperties);
119: } catch (Exception ex) {
120: throw new SException.JDBC("Opening " + url, ex);
121: }
122: return con;
123:
124: }
125:
126: String sDriverName;
127:
128: /**
129: * An override for the name of the SDriver. Normally this just
130: * returns null and SDriver.getSDriver picks the
131: * SDriver based on the URL.
132: */
133: public String getSDriverName() {
134: return sDriverName;
135: }
136:
137: public void setSDriverName(String driver) {
138: sDriverName = driver;
139: }
140:
141: }
|