001: package org.julp;
002:
003: import javax.sql.DataSource;
004: import java.io.Serializable;
005: import java.sql.*;
006: import java.util.Properties;
007: import java.util.StringTokenizer;
008:
009: public class BasicDataSourceImpl implements DataSource, Serializable {
010:
011: /** BasicDataSourceImpl has no pooling capability and should be used for testing or stand-along client */
012:
013: public BasicDataSourceImpl() {
014: if (Boolean.getBoolean("debug-julp")
015: || Boolean.getBoolean("debug-" + getClass().getName())) {
016: debug = true;
017: }
018: }
019:
020: protected transient Connection connection;
021: protected Driver driver;
022: protected Properties connectionProperties = null;
023: private String driverName;
024: private String dbURL;
025: private String userName;
026: private String password;
027: protected String connectionId = "";
028: protected boolean debug = false;
029: protected String connectionPropertiesDelimiter = "&";
030:
031: protected BasicDataSourceImpl clone() {
032: BasicDataSourceImpl newOne = new BasicDataSourceImpl();
033: try {
034: newOne.setUserName(userName);
035: newOne.setPassword(password);
036: newOne.setConnectionProperties(connectionProperties);
037: newOne.setDbURL(dbURL);
038: newOne.setDebug(debug);
039: newOne.setDriverName(driverName);
040: newOne.setLogWriter(DriverManager.getLogWriter());
041: newOne.setLoginTimeout(DriverManager.getLoginTimeout());
042: } catch (SQLException sqle) {
043: throw new RuntimeException(sqle);
044: }
045: return newOne;
046: }
047:
048: public java.sql.Connection getConnection() throws SQLException {
049: if (connection == null || connection.isClosed()) {
050: //if (connection == null || this.isConnectionClosed()){ //Sybase bug workaround??
051: if (debug)
052: System.out.println("julp ============= "
053: + new java.util.Date() + " " + this .getClass()
054: + "::" + this
055: + "::getConnection()::connection 1: "
056: + connection + " \n");
057: try {
058: driver = (Driver) Class.forName(getDriverName())
059: .newInstance();
060: if (this .getConnectionProperties() == null
061: || this .getConnectionProperties().isEmpty()) {
062: connection = DriverManager.getConnection(
063: getDbURL(), getUserName(), getPassword());
064: } else {
065: connection = DriverManager.getConnection(
066: getDbURL(), this .getConnectionProperties());
067: }
068: } catch (ClassNotFoundException ex) {
069: ex.printStackTrace();
070: throw new RuntimeException(ex);
071: } catch (SQLException sqle) {
072: sqle.printStackTrace();
073: throw new RuntimeException(sqle);
074: } catch (Exception e) {
075: e.printStackTrace();
076: throw new RuntimeException(e);
077: }
078: }
079: if (debug)
080: System.out.println("julp ============= "
081: + new java.util.Date() + " " + this .getClass()
082: + "::" + this + "::getConnection()::connection 2: "
083: + connection + " \n");
084: return connection;
085: }
086:
087: public Connection getConnection(String username, String password)
088: throws SQLException {
089: setUserName(userName);
090: setPassword(password);
091: return getConnection();
092: }
093:
094: public java.io.PrintWriter getLogWriter() throws SQLException {
095: return DriverManager.getLogWriter();
096: }
097:
098: public int getLoginTimeout() throws SQLException {
099: return DriverManager.getLoginTimeout();
100: }
101:
102: public void setLogWriter(java.io.PrintWriter out)
103: throws SQLException {
104: DriverManager.setLogWriter(out);
105: }
106:
107: public void setLoginTimeout(int seconds) throws SQLException {
108: DriverManager.setLoginTimeout(seconds);
109: }
110:
111: public java.lang.String getConnectionId() {
112: return connectionId;
113: }
114:
115: public boolean isDebug() {
116: return debug;
117: }
118:
119: public void setDebug(boolean debug) {
120: this .debug = debug;
121: }
122:
123: public String getDriverName() {
124: return this .driverName;
125: }
126:
127: public void setDriverName(String driverName) {
128: this .driverName = driverName;
129: }
130:
131: public java.lang.String getDbURL() {
132: return dbURL;
133: }
134:
135: public void setDbURL(java.lang.String dbURL) {
136: this .dbURL = dbURL;
137: }
138:
139: public java.lang.String getUserName() {
140: return userName;
141: }
142:
143: public void setUserName(java.lang.String userName) {
144: this .userName = userName;
145: }
146:
147: public java.lang.String getPassword() {
148: return password;
149: }
150:
151: public void setPassword(java.lang.String password) {
152: this .password = password;
153: }
154:
155: public void closeConnection() throws SQLException {
156: if (connection != null) {
157: //connection.rollback();//?? Sybase bug
158: connection.close();
159: connection = null;
160: }
161: }
162:
163: public void setConnectionProperties(String connectionProperties) {
164: StringTokenizer tokenizer = new StringTokenizer(
165: connectionProperties, connectionPropertiesDelimiter,
166: false);
167: if (this .connectionProperties == null) {
168: this .connectionProperties = new Properties();
169: } else {
170: this .connectionProperties.clear();
171: }
172: while (tokenizer.hasMoreTokens()) {
173: String token = tokenizer.nextToken();
174: int idx = token.indexOf("=");
175: if (idx == -1) {
176: throw new IllegalArgumentException(
177: "BasicDataSourceImpl::setConnectionProperties(String connectionProperties): "
178: + connectionProperties
179: + "(Argument format: <PROPERTY_NAME>=<PROPERTY_VALUE>[DELIMITER]...)");
180: }
181: this .connectionProperties.put(token.substring(0, idx)
182: .trim(), token.substring(idx + 1).trim());
183: }
184: }
185:
186: public Properties getConnectionProperties() {
187: return connectionProperties;
188: }
189:
190: public void setConnectionProperties(Properties connectionProperties) {
191: this .connectionProperties = connectionProperties;
192: }
193:
194: public String getConnectionPropertiesDelimiter() {
195: return connectionPropertiesDelimiter;
196: }
197:
198: public void setConnectionPropertiesDelimiter(
199: String connectionPropertiesDelimiter) {
200: this .connectionPropertiesDelimiter = connectionPropertiesDelimiter;
201: }
202:
203: public boolean isWrapperFor(Class iface) {
204: return false;
205: }
206:
207: public Class unwrap(Class iface) {
208: return null;
209: }
210: }
|