1 22 23 24 package com.mchange.v2.c3p0; 25 26 import java.beans.PropertyChangeEvent ; 27 import java.beans.PropertyChangeListener ; 28 import java.io.IOException ; 29 import java.io.ObjectInputStream ; 30 import java.io.ObjectOutputStream ; 31 import java.io.PrintWriter ; 32 import java.util.Properties ; 33 import java.sql.Connection ; 34 import java.sql.Driver ; 35 import java.sql.DriverManager ; 36 import java.sql.SQLException ; 37 import javax.sql.DataSource ; 38 import com.mchange.v2.sql.SqlUtils; 39 import com.mchange.v2.log.MLevel; 40 import com.mchange.v2.log.MLog; 41 import com.mchange.v2.log.MLogger; 42 import com.mchange.v2.c3p0.cfg.C3P0Config; 43 import com.mchange.v2.c3p0.impl.DriverManagerDataSourceBase; 44 45 public final class DriverManagerDataSource extends DriverManagerDataSourceBase implements DataSource 46 { 47 final static MLogger logger = MLog.getLogger( DriverManagerDataSource.class ); 48 49 Driver driver; 51 52 boolean driver_class_loaded = false; 54 55 public DriverManagerDataSource() 56 { this( true ); } 57 58 public DriverManagerDataSource(boolean autoregister) 59 { 60 super( autoregister ); 61 62 setUpPropertyListeners(); 63 64 String user = C3P0Config.initializeStringPropertyVar("user", null); 65 String password = C3P0Config.initializeStringPropertyVar("password", null); 66 67 if (user != null) 68 this.setUser( user ); 69 70 if (password != null) 71 this.setPassword( password ); 72 } 73 74 private void setUpPropertyListeners() 75 { 76 PropertyChangeListener driverClassListener = new PropertyChangeListener () 77 { 78 public void propertyChange( PropertyChangeEvent evt ) 79 { 80 Object val = evt.getNewValue(); 81 if ( "driverClass".equals( evt.getPropertyName() ) ) 82 setDriverClassLoaded( false ); 83 } 84 }; 85 this.addPropertyChangeListener( driverClassListener ); 86 } 87 88 private synchronized boolean isDriverClassLoaded() 89 { return driver_class_loaded; } 90 91 private synchronized void setDriverClassLoaded(boolean dcl) 92 { this.driver_class_loaded = dcl; } 93 94 private void ensureDriverLoaded() throws SQLException 95 { 96 try 97 { 98 if (! isDriverClassLoaded()) 99 { 100 if (driverClass != null) 101 Class.forName( driverClass ); 102 setDriverClassLoaded( true ); 103 } 104 } 105 catch (ClassNotFoundException e) 106 { 107 if (logger.isLoggable(MLevel.WARNING)) 108 logger.log(MLevel.WARNING, "Could not load driverClass " + driverClass, e); 109 } 110 } 111 112 116 public Connection getConnection() throws SQLException 117 { 118 ensureDriverLoaded(); 119 120 Connection out = driver().connect( jdbcUrl, properties ); 121 if (out == null) 122 throw new SQLException ("Apparently, jdbc URL '" + jdbcUrl + "' is not valid for the underlying " + 123 "driver [" + driver() + "]."); 124 return out; 125 } 126 127 131 public Connection getConnection(String username, String password) throws SQLException 132 { 133 ensureDriverLoaded(); 134 135 Connection out = driver().connect( jdbcUrl, overrideProps(username, password) ); 136 if (out == null) 137 throw new SQLException ("Apparently, jdbc URL '" + jdbcUrl + "' is not valid for the underlying " + 138 "driver [" + driver() + "]."); 139 return out; 140 } 141 142 public PrintWriter getLogWriter() throws SQLException 143 { return DriverManager.getLogWriter(); } 144 145 public void setLogWriter(PrintWriter out) throws SQLException 146 { DriverManager.setLogWriter( out ); } 147 148 public int getLoginTimeout() throws SQLException 149 { return DriverManager.getLoginTimeout(); } 150 151 public void setLoginTimeout(int seconds) throws SQLException 152 { DriverManager.setLoginTimeout( seconds ); } 153 154 public synchronized void setJdbcUrl(String jdbcUrl) 156 { 157 super.setJdbcUrl( jdbcUrl ); 160 clearDriver(); 161 } 162 163 public synchronized void setUser(String user) 165 { 166 String oldUser = this.getUser(); 167 if (! eqOrBothNull( user, oldUser )) 168 { 169 if (user != null) 170 properties.put( SqlUtils.DRIVER_MANAGER_USER_PROPERTY, user ); 171 else 172 properties.remove( SqlUtils.DRIVER_MANAGER_USER_PROPERTY ); 173 174 pcs.firePropertyChange("user", oldUser, user); 175 } 176 } 177 178 public synchronized String getUser() 179 { 180 return properties.getProperty( SqlUtils.DRIVER_MANAGER_USER_PROPERTY ); 184 } 185 186 public synchronized void setPassword(String password) 187 { 188 String oldPass = this.getPassword(); 189 if (! eqOrBothNull( password, oldPass )) 190 { 191 if (password != null) 192 properties.put( SqlUtils.DRIVER_MANAGER_PASSWORD_PROPERTY, password ); 193 else 194 properties.remove( SqlUtils.DRIVER_MANAGER_PASSWORD_PROPERTY ); 195 196 pcs.firePropertyChange("password", oldPass, password); 197 } 198 } 199 200 public synchronized String getPassword() 201 { return properties.getProperty( SqlUtils.DRIVER_MANAGER_PASSWORD_PROPERTY ); } 202 203 private final Properties overrideProps(String user, String password) 204 { 205 Properties overriding = (Properties ) properties.clone(); 207 if (user != null) 208 overriding.put(SqlUtils.DRIVER_MANAGER_USER_PROPERTY, user); 209 else 210 overriding.remove(SqlUtils.DRIVER_MANAGER_USER_PROPERTY); 211 212 if (password != null) 213 overriding.put(SqlUtils.DRIVER_MANAGER_PASSWORD_PROPERTY, password); 214 else 215 overriding.remove(SqlUtils.DRIVER_MANAGER_PASSWORD_PROPERTY); 216 217 return overriding; 218 } 219 220 private synchronized Driver driver() throws SQLException 221 { 222 if (driver == null) 224 driver = DriverManager.getDriver( jdbcUrl ); 225 return driver; 226 } 227 228 private synchronized void clearDriver() 229 { driver = null; } 230 231 private static boolean eqOrBothNull( Object a, Object b ) 232 { return (a == b || (a != null && a.equals(b))); } 233 234 private static final long serialVersionUID = 1; 236 private static final short VERSION = 0x0001; 237 238 private void writeObject( ObjectOutputStream oos ) throws IOException 239 { 240 oos.writeShort( VERSION ); 241 } 242 243 private void readObject( ObjectInputStream ois ) throws IOException , ClassNotFoundException 244 { 245 short version = ois.readShort(); 246 switch (version) 247 { 248 case VERSION: 249 setUpPropertyListeners(); 250 break; 251 default: 252 throw new IOException ("Unsupported Serialized Version: " + version); 253 } 254 } 255 } 256 | Popular Tags |