1 19 20 package org.apache.cayenne.conn; 21 22 import java.io.PrintWriter ; 23 import java.sql.Connection ; 24 import java.sql.Driver ; 25 import java.sql.DriverManager ; 26 import java.sql.SQLException ; 27 import java.util.Properties ; 28 29 import javax.sql.DataSource ; 30 31 import org.apache.cayenne.util.Util; 32 33 38 public class DriverDataSource implements DataSource { 39 40 protected Driver driver; 41 42 protected String connectionUrl; 43 protected String userName; 44 protected String password; 45 46 protected ConnectionEventLoggingDelegate logger; 47 48 53 private static Driver loadDriver(String driverClassName) throws SQLException { 54 55 Class driverClass; 56 try { 57 driverClass = Class.forName(driverClassName, true, Thread 58 .currentThread() 59 .getContextClassLoader()); 60 } 61 catch (Exception ex) { 62 throw new SQLException ("Can not load JDBC driver named '" 63 + driverClassName 64 + "': " 65 + ex.getMessage()); 66 } 67 68 try { 69 return (Driver ) driverClass.newInstance(); 70 } 71 catch (Exception ex) { 72 throw new SQLException ("Error instantiating driver '" 73 + driverClassName 74 + "': " 75 + ex.getMessage()); 76 } 77 } 78 79 82 public DriverDataSource(String driverClassName, String connectionUrl) 83 throws SQLException { 84 this(driverClassName, connectionUrl, null, null); 85 } 86 87 90 public DriverDataSource(String driverClassName, String connectionUrl, 91 String userName, String password) throws SQLException { 92 93 setDriverClassName(driverClassName); 94 95 this.connectionUrl = connectionUrl; 96 this.userName = userName; 97 this.password = password; 98 } 99 100 105 public DriverDataSource(Driver driver, String connectionUrl, String userName, 106 String password) { 107 108 this.driver = driver; 109 this.connectionUrl = connectionUrl; 110 this.userName = userName; 111 this.password = password; 112 } 113 114 118 public Connection getConnection() throws SQLException { 119 return getConnection(userName, password); 121 } 122 123 127 public Connection getConnection(String userName, String password) throws SQLException { 128 try { 129 if (logger != null) { 130 logger.logConnect(connectionUrl, userName, password); 131 } 132 133 Connection c = null; 134 135 if (driver == null) { 136 c = DriverManager.getConnection(connectionUrl, userName, password); 137 } 138 else { 139 Properties connectProperties = new Properties (); 140 141 if (userName != null) { 142 connectProperties.put("user", userName); 143 } 144 145 if (password != null) { 146 connectProperties.put("password", password); 147 } 148 c = driver.connect(connectionUrl, connectProperties); 149 } 150 151 154 if (c == null) { 155 throw new SQLException ("Can't establish connection: " + connectionUrl); 156 } 157 158 if (logger != null) { 159 logger.logConnectSuccess(); 160 } 161 162 return c; 163 } 164 catch (SQLException sqlex) { 165 if (logger != null) { 166 logger.logConnectFailure(sqlex); 167 } 168 169 throw sqlex; 170 } 171 } 172 173 public int getLoginTimeout() throws SQLException { 174 return -1; 175 } 176 177 public void setLoginTimeout(int seconds) throws SQLException { 178 } 180 181 public PrintWriter getLogWriter() throws SQLException { 182 return DriverManager.getLogWriter(); 183 } 184 185 public void setLogWriter(PrintWriter out) throws SQLException { 186 DriverManager.setLogWriter(out); 187 } 188 189 public ConnectionEventLoggingDelegate getLogger() { 190 return logger; 191 } 192 193 public void setLogger(ConnectionEventLoggingDelegate delegate) { 194 logger = delegate; 195 } 196 197 200 public String getConnectionUrl() { 201 return connectionUrl; 202 } 203 204 207 public void setConnectionUrl(String connectionUrl) { 208 this.connectionUrl = connectionUrl; 209 } 210 211 214 public String getPassword() { 215 return password; 216 } 217 218 221 public void setPassword(String password) { 222 this.password = password; 223 } 224 225 228 public String getUserName() { 229 return userName; 230 } 231 232 235 public void setUserName(String userName) { 236 this.userName = userName; 237 } 238 239 public String getDriverClassName() { 240 return driver != null ? driver.getClass().getName() : null; 241 } 242 243 public void setDriverClassName(String driverClassName) throws SQLException { 244 if (!Util.nullSafeEquals(getDriverClassName(), driverClassName)) { 245 this.driver = driverClassName != null ? loadDriver(driverClassName) : null; 246 } 247 } 248 } 249 | Popular Tags |