1 24 25 package org.objectweb.cjdbc.driver; 26 27 import java.io.PrintWriter ; 28 import java.io.Serializable ; 29 import java.sql.SQLException ; 30 import java.util.Properties ; 31 32 import javax.naming.NamingException ; 33 import javax.naming.Reference ; 34 import javax.naming.Referenceable ; 35 import javax.naming.StringRefAddr ; 36 37 47 public class DataSource 48 implements 49 javax.sql.DataSource , 50 Referenceable , 51 Serializable 52 { 53 54 55 protected static final String URL_PROPERTY = "url"; 56 protected static final String USER_PROPERTY = Driver.USER_PROPERTY; 57 protected static final String PASSWORD_PROPERTY = Driver.PASSWORD_PROPERTY; 58 protected static final String DRIVER_CLASSNAME = "org.objectweb.cjdbc.driver.Driver"; 59 protected static final String FACTORY_CLASSNAME = "org.objectweb.cjdbc.driver.DataSourceFactory"; 60 protected static final String DESCRIPTION_PROPERTY = "description"; 61 62 63 protected static Driver driver = null; 64 static 65 { 66 try 67 { 68 driver = (Driver) Class.forName(DRIVER_CLASSNAME).newInstance(); 69 } 70 catch (Exception e) 71 { 72 throw new RuntimeException ("Can't load " + DRIVER_CLASSNAME); 73 } 74 } 75 76 77 protected String url = null; 78 protected String user = null; 79 protected String password = null; 80 protected PrintWriter logWriter = null; 81 82 85 public DataSource() 86 { 87 } 88 89 99 public java.sql.Connection getConnection() throws SQLException 100 { 101 return getConnection(user, password); 102 } 103 104 113 public java.sql.Connection getConnection(String user, String password) 114 throws SQLException 115 { 116 if (user == null) 117 { 118 user = ""; 119 } 120 if (password == null) 121 { 122 password = ""; 123 } 124 Properties props = new Properties (); 125 props.put(USER_PROPERTY, user); 126 props.put(PASSWORD_PROPERTY, password); 127 128 return getConnection(props); 129 } 130 131 137 public void setLogWriter(PrintWriter output) throws SQLException 138 { 139 logWriter = output; 140 } 141 142 147 public java.io.PrintWriter getLogWriter() 148 { 149 return logWriter; 150 } 151 152 158 public void setLoginTimeout(int seconds) throws SQLException 159 { 160 } 161 162 168 public int getLoginTimeout() throws SQLException 169 { 170 return 0; 171 } 172 173 183 public Reference getReference() throws NamingException 184 { 185 Reference ref = new Reference (getClass().getName(), FACTORY_CLASSNAME, null); 186 ref.add(new StringRefAddr (DESCRIPTION_PROPERTY, getDescription())); 187 ref.add(new StringRefAddr (USER_PROPERTY, getUser())); 188 ref.add(new StringRefAddr (PASSWORD_PROPERTY, password)); 189 ref.add(new StringRefAddr (URL_PROPERTY, getUrl())); 190 return ref; 191 } 192 193 197 202 public String getDescription() 203 { 204 return "C-JDBC " + driver.getMajorVersion() + "." 205 + driver.getMinorVersion() + " Datasource"; 206 } 207 208 215 public void setUrl(String url) 216 { 217 this.url = url; 218 } 219 220 227 public void setURL(String url) 228 { 229 setUrl(url); 230 } 231 232 238 public String getUrl() 239 { 240 return url; 241 } 242 243 249 public String getURL() 250 { 251 return getUrl(); 252 } 253 254 262 public void setUser(String userName) 263 { 264 user = userName; 265 } 266 267 273 public String getUser() 274 { 275 return user; 276 } 277 278 287 public void setPassword(String pwd) 288 { 289 password = pwd; 290 } 291 292 302 protected java.sql.Connection getConnection(Properties props) 303 throws SQLException 304 { 305 return driver.connect(url, props); 306 } 307 308 } 309 | Popular Tags |