KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > c3p0 > DriverManagerDataSource


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.c3p0;
25
26 import java.beans.PropertyChangeEvent JavaDoc;
27 import java.beans.PropertyChangeListener JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.ObjectInputStream JavaDoc;
30 import java.io.ObjectOutputStream JavaDoc;
31 import java.io.PrintWriter JavaDoc;
32 import java.util.Properties JavaDoc;
33 import java.sql.Connection JavaDoc;
34 import java.sql.Driver JavaDoc;
35 import java.sql.DriverManager JavaDoc;
36 import java.sql.SQLException JavaDoc;
37 import javax.sql.DataSource JavaDoc;
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 JavaDoc
46 {
47     final static MLogger logger = MLog.getLogger( DriverManagerDataSource.class );
48
49     //MT: protected by this' lock
50
Driver JavaDoc driver;
51     
52     //MT: protected by this' lock
53
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 JavaDoc user = C3P0Config.initializeStringPropertyVar("user", null);
65         String JavaDoc 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 JavaDoc driverClassListener = new PropertyChangeListener JavaDoc()
77         {
78             public void propertyChange( PropertyChangeEvent JavaDoc evt )
79             {
80                 Object JavaDoc 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 JavaDoc
95     {
96         try
97         {
98             if (! isDriverClassLoaded())
99             {
100                 if (driverClass != null)
101                     Class.forName( driverClass );
102                 setDriverClassLoaded( true );
103             }
104         }
105         catch (ClassNotFoundException JavaDoc e)
106         {
107             if (logger.isLoggable(MLevel.WARNING))
108                 logger.log(MLevel.WARNING, "Could not load driverClass " + driverClass, e);
109         }
110     }
111
112     // should NOT be sync'ed -- driver() is sync'ed and that's enough
113
// sync'ing the method creates the danger that one freeze on connect
114
// blocks access to the entire DataSource
115

116     public Connection JavaDoc getConnection() throws SQLException JavaDoc
117     {
118         ensureDriverLoaded();
119
120         Connection JavaDoc out = driver().connect( jdbcUrl, properties );
121         if (out == null)
122             throw new SQLException JavaDoc("Apparently, jdbc URL '" + jdbcUrl + "' is not valid for the underlying " +
123                             "driver [" + driver() + "].");
124         return out;
125     }
126
127     // should NOT be sync'ed -- driver() is sync'ed and that's enough
128
// sync'ing the method creates the danger that one freeze on connect
129
// blocks access to the entire DataSource
130

131     public Connection JavaDoc getConnection(String JavaDoc username, String JavaDoc password) throws SQLException JavaDoc
132     {
133         ensureDriverLoaded();
134
135         Connection JavaDoc out = driver().connect( jdbcUrl, overrideProps(username, password) );
136         if (out == null)
137             throw new SQLException JavaDoc("Apparently, jdbc URL '" + jdbcUrl + "' is not valid for the underlying " +
138                             "driver [" + driver() + "].");
139         return out;
140     }
141
142     public PrintWriter JavaDoc getLogWriter() throws SQLException JavaDoc
143     { return DriverManager.getLogWriter(); }
144
145     public void setLogWriter(PrintWriter JavaDoc out) throws SQLException JavaDoc
146     { DriverManager.setLogWriter( out ); }
147
148     public int getLoginTimeout() throws SQLException JavaDoc
149     { return DriverManager.getLoginTimeout(); }
150
151     public void setLoginTimeout(int seconds) throws SQLException JavaDoc
152     { DriverManager.setLoginTimeout( seconds ); }
153
154     //overrides
155
public synchronized void setJdbcUrl(String JavaDoc jdbcUrl)
156     {
157         //System.err.println( "setJdbcUrl( " + jdbcUrl + " )");
158
//new Exception("DEBUG STACK TRACE").printStackTrace();
159
super.setJdbcUrl( jdbcUrl );
160         clearDriver();
161     }
162
163     //"virtual properties"
164
public synchronized void setUser(String JavaDoc user)
165     {
166         String JavaDoc 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 JavaDoc getUser()
179     {
180 // System.err.println("getUser() -- DriverManagerDataSource@" + System.identityHashCode( this ) +
181
// " using Properties@" + System.identityHashCode( properties ));
182
// new Exception("STACK TRACE DUMP").printStackTrace();
183
return properties.getProperty( SqlUtils.DRIVER_MANAGER_USER_PROPERTY );
184     }
185
186     public synchronized void setPassword(String JavaDoc password)
187     {
188         String JavaDoc 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 JavaDoc getPassword()
201     { return properties.getProperty( SqlUtils.DRIVER_MANAGER_PASSWORD_PROPERTY ); }
202
203     private final Properties JavaDoc overrideProps(String JavaDoc user, String JavaDoc password)
204     {
205         Properties JavaDoc overriding = (Properties JavaDoc) properties.clone(); //we are relying on a defensive clone in our base class!!!
206

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 JavaDoc driver() throws SQLException JavaDoc
221     {
222         //System.err.println( "driver() <-- " + this );
223
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 JavaDoc a, Object JavaDoc b )
232     { return (a == b || (a != null && a.equals(b))); }
233
234     // serialization stuff -- set up bound/constrained property event handlers on deserialization
235
private static final long serialVersionUID = 1;
236     private static final short VERSION = 0x0001;
237
238     private void writeObject( ObjectOutputStream JavaDoc oos ) throws IOException JavaDoc
239     {
240         oos.writeShort( VERSION );
241     }
242
243     private void readObject( ObjectInputStream JavaDoc ois ) throws IOException JavaDoc, ClassNotFoundException JavaDoc
244     {
245         short version = ois.readShort();
246         switch (version)
247         {
248         case VERSION:
249             setUpPropertyListeners();
250             break;
251         default:
252             throw new IOException JavaDoc("Unsupported Serialized Version: " + version);
253         }
254     }
255 }
256
Popular Tags