1 /***
2 * WrappingDriverManager.java
3 *
4 * This file is part of the creme library.
5 * The creme library intends to ease the development effort of large
6 * applications by providing easy to use support classes.
7 *
8 * Copyright (C) 2002 Denis Bregeon
9 *
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 * contact information: dbregeon@sourceforge.net
26 */
27
28 package org.jcreme.sql;
29
30 import java.io.PrintWriter;
31 import java.sql.Connection;
32 import java.sql.Driver;
33 import java.sql.DriverManager;
34 import java.sql.SQLException;
35 import java.util.Enumeration;
36 import java.util.Properties;
37
38 /***
39 * This class is intended to wrap the original DriverManager so that all the
40 * connections are returned from a wrapping driver. It enables to specify a
41 * Driver that will provide all the connections (WrappedConnection for
42 * instance). The Driver can also make use of ConnectionPools.
43 *
44 *
45 * @author $Author: dbregeon $
46 * @version $Revision: 1.1 $
47 */
48 public class WrappingDriverManager {
49 private static Driver wrappingDriver = null;
50
51 /***
52 * Creates a new instance of CremeDriverManager
53 */
54 private WrappingDriverManager() {
55
56 }
57
58 /***
59 * Retrieves the log writer.
60 *
61 * The <code>getLogWriter</code> and <code>setLogWriter</code> methods
62 * should be used instead of the <code>get/setlogStream</code> methods,
63 * which are deprecated.
64 *
65 * @return a <code>java.io.PrintWriter</code> object
66 * @see #setLogWriter
67 * @since 1.2
68 */
69 public static PrintWriter getLogWriter() {
70 return DriverManager.getLogWriter();
71 }
72
73 /***
74 * Attempts to establish a connection to the given database URL. The
75 * <code>DriverManager</code> attempts to select an appropriate driver
76 * from the set of registered JDBC drivers.
77 *
78 * @param url
79 * a database url of the form
80 * <code> jdbc:<em>subprotocol</em>:<em>subname</em></code>
81 * @return a connection to the URL
82 * @exception SQLException
83 * if a database access error occurs
84 */
85 public static Connection getConnection(String url) throws SQLException {
86 return WrappingDriverManager.getConnection(url, new Properties());
87 }
88
89 /***
90 * Sets the logging/tracing <code>PrintWriter</code> object that is used
91 * by the <code>DriverManager</code> and all drivers.
92 * <P>
93 * There is a minor versioning problem created by the introduction of the
94 * method <code>setLogWriter</code>. The method <code>setLogWriter</code>
95 * cannot create a <code>PrintStream</code> object that will be returned
96 * by <code>getLogStream</code> ---the Java platform does not provide a
97 * backward conversion. As a result, a new application that uses
98 * <code>setLogWriter</code> and also uses a JDBC 1.0 driver that uses
99 * <code>getLogStream</code> will likely not see debugging information
100 * written by that driver.
101 * <P>
102 * In the Java 2 SDK, Standard Edition, version 1.3 release, this method
103 * checks to see that there is an <code>SQLPermission</code> object before
104 * setting the logging stream. If a <code>SecurityManager</code> exists
105 * and its <code>checkPermission</code> method denies setting the log
106 * writer, this method throws a <code>java.lang.SecurityException</code>.
107 *
108 * @param out
109 * the new logging/tracing <code>PrintStream</code> object;
110 * <code>null</code> to disable logging and tracing
111 * @throws SecurityException
112 * if a security manager exists and its
113 * <code>checkPermission</code> method denies setting the log
114 * writer
115 *
116 * @see SecurityManager#checkPermission(java.security.Permission)
117 * @see #getLogWriter
118 * @since 1.2
119 */
120 public static void setLogWriter(PrintWriter out) {
121 DriverManager.setLogWriter(out);
122 }
123
124 /***
125 * Attempts to establish a connection to the given database URL. The
126 * <code>DriverManager</code> attempts to select an appropriate driver
127 * from the set of registered JDBC drivers.
128 *
129 * @param url
130 * a database url of the form
131 * <code> jdbc:<em>subprotocol</em>:<em>subname</em></code>
132 * @param info
133 * a list of arbitrary string tag/value pairs as connection
134 * arguments; normally at least a "user" and "password" property
135 * should be included
136 * @return a Connection to the URL
137 * @exception SQLException
138 * if a database access error occurs
139 */
140 public static Connection getConnection(String url, Properties info)
141 throws SQLException {
142 Connection conn = null;
143 if (wrappingDriver == null) {
144 conn = DriverManager.getConnection(url, info);
145 } else {
146 conn = wrappingDriver.connect(url, info);
147 }
148 return conn;
149 }
150
151 /***
152 * Attempts to establish a connection to the given database URL. The
153 * <code>DriverManager</code> attempts to select an appropriate driver
154 * from the set of registered JDBC drivers.
155 *
156 * @param url
157 * a database url of the form
158 * <code>jdbc:<em>subprotocol</em>:<em>subname</em></code>
159 * @param user
160 * the database user on whose behalf the connection is being made
161 * @param password
162 * the user's password
163 * @return a connection to the URL
164 * @exception SQLException
165 * if a database access error occurs
166 */
167 public static Connection getConnection(String url, String user,
168 String password) throws SQLException {
169 Properties info = new Properties();
170 if (user != null) {
171 info.put("user", user);
172 }
173 if (password != null) {
174 info.put("password", password);
175 }
176 return WrappingDriverManager.getConnection(url, info);
177 }
178
179 /***
180 * Attempts to locate a driver that understands the given URL. The
181 * <code>DriverManager</code> attempts to select an appropriate driver
182 * from the set of registered JDBC drivers.
183 *
184 * @param url
185 * a database URL of the form
186 * <code>jdbc:<em>subprotocol</em>:<em>subname</em></code>
187 * @return a <code>Driver</code> object representing a driver that can
188 * connect to the given URL
189 * @exception SQLException
190 * if a database access error occurs
191 */
192 public static Driver getDriver(String url) throws SQLException {
193 return DriverManager.getDriver(url);
194 }
195
196 /***
197 * Registers the given driver with the <code>DriverManager</code>. A
198 * newly-loaded driver class should call the method
199 * <code>registerDriver</code> to make itself known to the
200 * <code>DriverManager</code>.
201 *
202 * @param driver
203 * the new JDBC Driver that is to be registered with the
204 * <code>DriverManager</code>
205 * @exception SQLException
206 * if a database access error occurs
207 */
208 public static void registerDriver(Driver driver) throws SQLException {
209 DriverManager.registerDriver(driver);
210 }
211
212 /***
213 * Drops a driver from the <code>DriverManager</code>'s list. Applets can
214 * only deregister drivers from their own classloaders.
215 *
216 * @param driver
217 * the JDBC Driver to drop
218 * @exception SQLException
219 * if a database access error occurs
220 */
221 public static void deregisterDriver(Driver driver) throws SQLException {
222 DriverManager.deregisterDriver(driver);
223 }
224
225 /***
226 * Retrieves an Enumeration with all of the currently loaded JDBC drivers to
227 * which the current caller has access.
228 *
229 * <P>
230 * <B>Note: </B> The classname of a driver can be found using <CODE>
231 * d.getClass().getName()</CODE>
232 *
233 * @return the list of JDBC Drivers loaded by the caller's class loader
234 */
235 public static Enumeration getDrivers() {
236 return DriverManager.getDrivers();
237 }
238
239 /***
240 * Sets the maximum time in seconds that a driver will wait while attempting
241 * to connect to a database.
242 *
243 * @param seconds
244 * the login time limit in seconds
245 * @see #getLoginTimeout
246 */
247 public static void setLoginTimeout(int seconds) {
248 DriverManager.setLoginTimeout(seconds);
249 }
250
251 /***
252 * Gets the maximum time in seconds that a driver can wait when attempting
253 * to log in to a database.
254 *
255 * @return the driver login time limit in seconds
256 * @see #setLoginTimeout
257 */
258 public static int getLoginTimeout() {
259 return DriverManager.getLoginTimeout();
260 }
261
262 /***
263 * Prints a message to the current JDBC log stream.
264 *
265 * @param message
266 * a log or tracing message
267 */
268 public static void println(String message) {
269 DriverManager.println(message);
270 }
271
272 /***
273 * Enables to set the Driver that is used to wrap the connections.
274 *
275 * @param driver
276 * the driver that will provide the connections.
277 */
278 public static void setWrappingDriver(Driver driver) {
279 wrappingDriver = driver;
280 }
281 }