1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 package org.osjava.sj.loader;
34
35 import java.sql.Connection;
36 import java.sql.SQLException;
37 import javax.sql.DataSource;
38 import java.sql.DriverManager;
39 import java.io.PrintWriter;
40 import java.util.Properties;
41
42 /***
43 * A basic implementation of a DataSource.
44 */
45 public class SJDataSource implements DataSource {
46
47 private PrintWriter pw;
48 private String username;
49 private String password;
50 private String url;
51 private String driver;
52
53
54 private Properties properties;
55
56 /***
57 * if a connection pool has been built, its url is stored in here
58 */
59 private String poolUrl = null;
60
61 public SJDataSource(String driver, String url, String username, String password, Properties properties) {
62 ensureLoaded(driver);
63 this.driver = driver;
64 this.url = url;
65 this.pw = new PrintWriter(System.err);
66 this.username = username;
67 this.password = password;
68 this.properties = properties;
69 }
70
71
72 private static boolean ensureLoaded(String name) {
73 try {
74 Class.forName(name).newInstance();
75 return true;
76 } catch (Exception e) {
77 return false;
78 }
79 }
80
81 public Connection getConnection() throws SQLException {
82 return this.getConnection(this.username, this.password);
83 }
84
85 /***
86 * returns a connection to the database specified in the properties and
87 * creates a connection pool, if neccessary
88 */
89 public Connection getConnection(String username, String password) throws SQLException {
90 String tmpUrl = this.url;
91
92 String pool = properties.getProperty("pool");
93 if (pool != null) {
94 synchronized (SJDataSource.class) {
95 if (poolUrl == null) {
96 PoolSetup.setupConnection(pool, url, username, password, properties);
97 poolUrl = PoolSetup.getUrl(pool);
98 }
99 }
100 tmpUrl = poolUrl;
101 }
102
103 if(username == null || password == null) {
104 return DriverManager.getConnection(tmpUrl);
105 }
106 return DriverManager.getConnection(tmpUrl, username, password);
107 }
108
109 public PrintWriter getLogWriter() throws SQLException {
110 return pw;
111 }
112
113 public int getLoginTimeout() throws SQLException {
114 return 0;
115 }
116
117 public void setLogWriter(PrintWriter pw) throws SQLException {
118 this.pw = pw;
119 }
120
121 public void setLoginTimeout(int timeout) throws SQLException {
122
123 }
124
125 public String toString() {
126 return driver + "::::" + url + "::::" + username;
127 }
128
129 public boolean equals(Object obj) {
130 if(obj == null) {
131 return false;
132 }
133 if(obj.getClass() != this.getClass()) {
134 return false;
135 }
136 SJDataSource other = (SJDataSource) obj;
137
138 return other.url.equals(this.url) &&
139 other.driver.equals(this.driver) &&
140 other.username.equals(this.username);
141 }
142
143 public int hashCode() {
144 return this.url.hashCode() & this.username.hashCode() & this.driver.hashCode();
145 }
146
147
148 public boolean isWrapperFor(Class<?> iface) throws SQLException {
149 return false;
150 }
151
152
153 public <T> T unwrap(Class<T> iface) throws SQLException {
154 throw new SQLException("This object is not a wrapper");
155 }
156
157 }
158