KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mysql > jdbc > jdbc2 > optional > MysqlDataSource


1 /*
2    Copyright (C) 2002 MySQL AB
3
4       This program is free software; you can redistribute it and/or modify
5       it under the terms of the GNU General Public License as published by
6       the Free Software Foundation; either version 2 of the License, or
7       (at your option) any later version.
8
9       This program is distributed in the hope that it will be useful,
10       but WITHOUT ANY WARRANTY; without even the implied warranty of
11       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12       GNU General Public License for more details.
13
14       You should have received a copy of the GNU General Public License
15       along with this program; if not, write to the Free Software
16       Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18  */

19 package com.mysql.jdbc.jdbc2.optional;
20
21 import java.io.PrintWriter JavaDoc;
22 import java.io.Serializable JavaDoc;
23
24 import java.sql.SQLException JavaDoc;
25
26 import java.util.Properties JavaDoc;
27
28 import javax.naming.NamingException JavaDoc;
29 import javax.naming.Reference JavaDoc;
30 import javax.naming.Referenceable JavaDoc;
31 import javax.naming.StringRefAddr JavaDoc;
32
33 import javax.sql.DataSource JavaDoc;
34
35
36 /**
37  * A JNDI DataSource for a Mysql JDBC connection
38  *
39  * @author Mark Matthews
40  */

41 public class MysqlDataSource implements DataSource JavaDoc, Referenceable JavaDoc, Serializable JavaDoc {
42     /** The driver to create connections with */
43     protected static com.mysql.jdbc.Driver mysqlDriver = null;
44
45     static {
46         try {
47             mysqlDriver = (com.mysql.jdbc.Driver) Class.forName(
48                     "com.mysql.jdbc.Driver").newInstance();
49         } catch (Exception JavaDoc E) {
50             throw new RuntimeException JavaDoc(
51                 "Can not load Driver class com.mysql.jdbc.Driver");
52         }
53     }
54
55     /** Log stream */
56     protected PrintWriter JavaDoc logWriter = null;
57
58     /** Database Name */
59     protected String JavaDoc databaseName = null;
60
61     /** Character Encoding */
62     protected String JavaDoc encoding = null;
63
64     /** Hostname */
65     protected String JavaDoc hostName = null;
66
67     /** Password */
68     protected String JavaDoc password = null;
69
70     /** The profileSql property */
71     protected String JavaDoc profileSql = "false";
72
73     /** The JDBC URL */
74     protected String JavaDoc url = null;
75
76     /** User name */
77     protected String JavaDoc user = null;
78
79     /** Should we construct the URL, or has it been set explicitly */
80     protected boolean explicitUrl = false;
81
82     /** Port number */
83     protected int port = 3306;
84
85     /**
86      * Default no-arg constructor for Serialization
87      */

88     public MysqlDataSource() {
89     }
90
91     /**
92      * Creates a new connection using the already configured username and
93      * password.
94      *
95      * @return a connection to the database
96      *
97      * @throws SQLException if an error occurs
98      */

99     public java.sql.Connection JavaDoc getConnection() throws SQLException JavaDoc {
100         return getConnection(user, password);
101     }
102
103     /**
104      * Creates a new connection with the given username and password
105      *
106      * @param userID the user id to connect with
107      * @param password the password to connect with
108      *
109      * @return a connection to the database
110      *
111      * @throws SQLException if an error occurs
112      */

113     public java.sql.Connection JavaDoc getConnection(String JavaDoc userID, String JavaDoc password)
114         throws SQLException JavaDoc {
115         Properties JavaDoc props = new Properties JavaDoc();
116
117         if (userID == null) {
118             userID = "";
119         }
120
121         if (password == null) {
122             password = "";
123         }
124
125         props.put("user", userID);
126         props.put("password", password);
127         props.put("profileSql", getProfileSql());
128
129         return getConnection(props);
130     }
131
132     /**
133      * Sets the database name.
134      *
135      * @param dbName the name of the database
136      */

137     public void setDatabaseName(String JavaDoc dbName) {
138         databaseName = dbName;
139     }
140
141     /**
142      * Gets the name of the database
143      *
144      * @return the name of the database for this data source
145      */

146     public String JavaDoc getDatabaseName() {
147         return (databaseName != null) ? databaseName : "";
148     }
149
150     /**
151      * Sets the log writer for this data source.
152      *
153      * @see javax.sql.DataSource#setLogWriter(PrintWriter)
154      */

155     public void setLogWriter(PrintWriter JavaDoc output) throws SQLException JavaDoc {
156         logWriter = output;
157     }
158
159     /**
160      * Returns the log writer for this data source
161      *
162      * @return the log writer for this data source
163      */

164     public java.io.PrintWriter JavaDoc getLogWriter() {
165         return logWriter;
166     }
167
168     /**
169      * DOCUMENT ME!
170      *
171      * @param seconds DOCUMENT ME!
172      *
173      * @throws SQLException DOCUMENT ME!
174      */

175     public void setLoginTimeout(int seconds) throws SQLException JavaDoc {
176     }
177
178     /**
179      * Returns the login timeout
180      *
181      * @return the login timeout
182      */

183     public int getLoginTimeout() {
184         return 0;
185     }
186
187     /**
188      * Sets the password
189      *
190      * @param pass the password
191      */

192     public void setPassword(String JavaDoc pass) {
193         password = pass;
194     }
195
196     /**
197      * Sets the database port.
198      *
199      * @param p the port
200      */

201     public void setPort(int p) {
202         port = p;
203     }
204
205     /**
206      * Returns the port number
207      *
208      * @return the port number
209      */

210     public int getPort() {
211         return port;
212     }
213
214     /**
215      * Sets the port number
216      *
217      * @param p the port
218      *
219      * @see #setPort
220      */

221     public void setPortNumber(int p) {
222         setPort(p);
223     }
224
225     /**
226      * Returns the port number
227      *
228      * @return the port number
229      */

230     public int getPortNumber() {
231         return getPort();
232     }
233
234     /**
235      * Sets the profileSql property
236      *
237      * @param flag true/false
238      */

239     public void setProfileSql(String JavaDoc flag) {
240         profileSql = flag;
241     }
242
243     /**
244      * Returns the value for the profileSql property
245      *
246      * @return the value for the profileSql property
247      */

248     public String JavaDoc getProfileSql() {
249         return profileSql;
250     }
251
252     /**
253      * Required method to support this class as a <CODE>Referenceable</CODE>.
254      *
255      * @return a Reference to this data source
256      *
257      * @throws NamingException if a JNDI error occurs
258      */

259     public Reference JavaDoc getReference() throws NamingException JavaDoc {
260         String JavaDoc factoryName = "com.mysql.jdbc.jdbc2.optional.MysqlDataSourceFactory";
261         Reference JavaDoc ref = new Reference JavaDoc(getClass().getName(), factoryName, null);
262         ref.add(new StringRefAddr JavaDoc("user", getUser()));
263         ref.add(new StringRefAddr JavaDoc("password", password));
264         ref.add(new StringRefAddr JavaDoc("serverName", getServerName()));
265         ref.add(new StringRefAddr JavaDoc("port", "" + getPort()));
266         ref.add(new StringRefAddr JavaDoc("databaseName", getDatabaseName()));
267         ref.add(new StringRefAddr JavaDoc("profileSql", getProfileSql()));
268
269         return ref;
270     }
271
272     /**
273      * Sets the server name.
274      *
275      * @param serverName the server name
276      */

277     public void setServerName(String JavaDoc serverName) {
278         hostName = serverName;
279     }
280
281     /**
282      * Returns the name of the database server
283      *
284      * @return the name of the database server
285      */

286     public String JavaDoc getServerName() {
287         return (hostName != null) ? hostName : "";
288     }
289
290     //
291
// I've seen application servers use both formats
292
// URL or url (doh)
293
//
294

295     /**
296      * Sets the URL for this connection
297      *
298      * @param url the URL for this connection
299      */

300     public void setURL(String JavaDoc url) {
301         setUrl(url);
302     }
303
304     /**
305      * Returns the URL for this connection
306      *
307      * @return the URL for this connection
308      */

309     public String JavaDoc getURL() {
310         return getUrl();
311     }
312
313     /**
314      * This method is used by the app server to set the url string specified
315      * within the datasource deployment descriptor. It is discovered using
316      * introspection and matches if property name in descriptor is "url".
317      *
318      * @param url url to be used within driver.connect
319      */

320     public void setUrl(String JavaDoc url) {
321         this.url = url;
322         explicitUrl = true;
323     }
324
325     /**
326      * Returns the JDBC URL that will be used to create the database
327      * connection.
328      *
329      * @return the URL for this connection
330      */

331     public String JavaDoc getUrl() {
332         if (!explicitUrl) {
333             String JavaDoc builtUrl = "jdbc:mysql://";
334             builtUrl = builtUrl + getServerName() + ":" + getPort() + "/"
335                 + getDatabaseName();
336
337             return builtUrl;
338         } else {
339             return this.url;
340         }
341     }
342
343     /**
344      * Sets the user ID.
345      *
346      * @param userID the User ID
347      */

348     public void setUser(String JavaDoc userID) {
349         user = userID;
350     }
351
352     /**
353      * Returns the configured user for this connection
354      *
355      * @return the user for this connection
356      */

357     public String JavaDoc getUser() {
358         return user;
359     }
360
361     /**
362      * Creates a connection using the specified properties.
363      *
364      * @param props the properties to connect with
365      *
366      * @return a connection to the database
367      *
368      * @throws SQLException if an error occurs
369      */

370     protected java.sql.Connection JavaDoc getConnection(Properties JavaDoc props)
371         throws SQLException JavaDoc {
372         String JavaDoc jdbcUrlToUse = null;
373
374         if (!explicitUrl) {
375             StringBuffer JavaDoc jdbcUrl = new StringBuffer JavaDoc("jdbc:mysql://");
376
377             if (hostName != null) {
378                 jdbcUrl.append(hostName);
379             }
380
381             jdbcUrl.append(":");
382             jdbcUrl.append(port);
383             jdbcUrl.append("/");
384
385             if (databaseName != null) {
386                 jdbcUrl.append(databaseName);
387             }
388
389             jdbcUrlToUse = jdbcUrl.toString();
390         } else {
391             jdbcUrlToUse = this.url;
392         }
393
394         return mysqlDriver.connect(jdbcUrlToUse, props);
395     }
396 }
397
Popular Tags