1 22 package org.enhydra.jdbc.standard; 23 24 import java.sql.Connection ; 25 import java.sql.DriverManager ; 26 import java.sql.SQLException ; 27 import java.util.Hashtable ; 28 import javax.naming.Context ; 29 import javax.naming.Name ; 30 import javax.naming.NamingException ; 31 import javax.naming.Reference ; 32 import javax.naming.StringRefAddr ; 33 import javax.sql.DataSource ; 34 import org.enhydra.jdbc.core.CoreDataSource; 35 import java.sql.Driver ; 36 import java.util.Properties ; 37 import org.enhydra.jdbc.util.Logger; 38 import org.apache.commons.logging.LogFactory; 39 40 49 public class StandardDataSource extends CoreDataSource implements DataSource { 50 51 transient Driver driver; 53 String driverName; String url; private int transIsolation; private boolean loadedFromCCL = false; 57 58 61 public StandardDataSource() { 62 super(); 64 driver = null; 65 driverName = ""; 66 url = ""; 67 transIsolation = -1; setLogger(new Logger(LogFactory.getLog("org.enhydra.jdbc.xapool"))); 69 } 70 71 protected StandardDataSource(Driver drv) throws SQLException { 72 this(); 73 driver = drv; 74 driverName = drv.getClass().getName(); 75 setLogger(new Logger(LogFactory.getLog("org.enhydra.jdbc.xapool"))); 76 } 77 78 82 public String getDriverName() { 83 return driverName; 84 } 85 86 90 public String getUrl() { 91 return url; 92 } 93 94 99 public void setDriverName(String driverName) throws SQLException { 100 if (!this.driverName.equals(driverName)) { 101 this.driverName = driverName; 102 driver = null; 103 } 104 112 } 113 114 118 public void setUrl(String url) { 119 this.url = url; 120 } 121 122 126 public void setTransactionIsolation(int level) { 127 transIsolation = level; 128 } 129 130 134 public int getTransactionIsolation() { 135 return transIsolation; 136 } 137 138 143 synchronized public Connection getConnection() throws SQLException { 144 return getConnection(user, password); 145 } 146 147 154 synchronized public Connection getConnection(String u, String p) 155 throws SQLException { 156 Connection ret = null; Properties prop = new Properties (); 158 if (u != null) 159 prop.put("user", u); 160 if (p != null) 161 prop.put("password", p); 162 if (url == null) { } else { if (driver == null) { 166 try { 167 driver = (Driver ) Class.forName(driverName).newInstance(); 168 loadedFromCCL = false; 169 log.debug( 170 "StandardDataSource:getConnection a new driver instance is created"); 171 } catch (Exception e) { 172 try { 173 driver = 174 (Driver ) Class 175 .forName( 176 driverName, 177 true, 178 Thread 179 .currentThread() 180 .getContextClassLoader()) 181 .newInstance(); 182 loadedFromCCL = true; 183 } catch (Exception e2) { 184 throw new SQLException ( 185 "Error trying to load driver: " 186 + driverName 187 + " : " 188 + e2.getMessage()); 189 } 190 } 191 } 192 201 try { 202 if (loadedFromCCL) { 203 ret = driver.connect(url, prop); 204 } else { 206 ret = DriverManager.getConnection(url, prop); 207 } 209 int transIsolation = getTransactionIsolation(); 210 if (transIsolation >= 0) { 211 ret.setTransactionIsolation(transIsolation); 212 } 213 log.debug( 214 "StandardDataSource:getConnection Connection from DriverManager is returned"); 215 } catch (SQLException e) { 216 throw new SQLException ( 217 "Cannot get connection for URL " 218 + url 219 + " : " 220 + e.getMessage()); 221 } 222 } 223 return ret; 224 } 225 226 229 public Reference getReference() throws NamingException { 230 234 Reference ref = 235 new Reference (getClass().getName(), getClass().getName(), null); 236 ref.add(new StringRefAddr ("driverName", getDriverName())); 237 ref.add(new StringRefAddr ("url", getUrl())); 238 ref.add(new StringRefAddr ("user", getUser())); 239 ref.add(new StringRefAddr ("password", getPassword())); 240 ref.add(new StringRefAddr ("description", getDescription())); 241 ref.add( 242 new StringRefAddr ( 243 "loginTimeout", 244 Integer.toString(getLoginTimeout()))); 245 ref.add( 246 new StringRefAddr ( 247 "transIsolation", 248 Integer.toString(getTransactionIsolation()))); 249 log.debug("StandardDataSource:getReference object returned"); 250 return ref; 251 } 252 253 256 public Object getObjectInstance( 257 Object refObj, 258 Name name, 259 Context nameCtx, 260 Hashtable env) 261 throws Exception { 262 Reference ref = (Reference ) refObj; 263 this.setDriverName((String ) ref.get("driverName").getContent()); 264 this.setUrl((String ) ref.get("url").getContent()); 265 this.setUser((String ) ref.get("user").getContent()); 266 this.setPassword((String ) ref.get("password").getContent()); 267 this.setDescription((String ) ref.get("description").getContent()); 268 this.setLoginTimeout( 269 Integer.parseInt((String ) ref.get("loginTimeout").getContent())); 270 this.setTransactionIsolation( 271 Integer.parseInt((String ) ref.get("transIsolation").getContent())); 272 return this; 273 } 274 275 public boolean equals(Object obj) { 276 if (this == obj) { 277 return true; 278 } 279 if (obj == null) { 280 return false; 281 } 282 return false; 283 299 } 300 301 304 305 public String toString() { 306 StringBuffer sb = new StringBuffer (); 307 sb.append("StandardDataSource:\n"); 308 sb.append(" driver=<" + driver +">\n"); 309 sb.append(" url=<" + url + ">\n"); 310 sb.append(" user=<" + user + ">\n"); 311 sb.append(super.toString()); 312 313 return sb.toString(); 314 } 315 316 public int hashCode() { 317 return toString().hashCode(); 318 } 319 } | Popular Tags |