Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/mysql/jdbc/jdbc2/optional/MysqlDataSource.java


1   /*
2    Copyright (C) 2002-2004 MySQL AB
3   
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of version 2 of the GNU General Public License as
6    published by the Free Software Foundation.
7    
8   
9    There are special exceptions to the terms and conditions of the GPL 
10   as it is applied to this software. View the full text of the 
11   exception exception in file EXCEPTIONS-CONNECTOR-J in the directory of this 
12   software distribution.
13  
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18  
19   You should have received a copy of the GNU General Public License
20   along with this program; if not, write to the Free Software
21   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  
23   */
24  package com.mysql.jdbc.jdbc2.optional;
25  
26  import java.io.PrintWriter;
27  import java.io.Serializable;
28  import java.sql.SQLException;
29  import java.util.Properties;
30  
31  import javax.naming.NamingException;
32  import javax.naming.Reference;
33  import javax.naming.Referenceable;
34  import javax.naming.StringRefAddr;
35  import javax.sql.DataSource;
36  
37  
38  /**
39   * A JNDI DataSource for a Mysql JDBC connection
40   *
41   * @author Mark Matthews
42   */
43  public class MysqlDataSource implements DataSource, Referenceable, Serializable {
44      /** The driver to create connections with */
45      protected static com.mysql.jdbc.Driver mysqlDriver = null;
46  
47      static {
48          try {
49              mysqlDriver = (com.mysql.jdbc.Driver) Class.forName(
50                      "com.mysql.jdbc.Driver").newInstance();
51          } catch (Exception E) {
52              throw new RuntimeException(
53                  "Can not load Driver class com.mysql.jdbc.Driver");
54          }
55      }
56  
57      /** Log stream */
58      protected transient PrintWriter logWriter = null;
59  
60      /** Database Name */
61      protected String databaseName = null;
62  
63      /** Character Encoding */
64      protected String encoding = null;
65  
66      /** Hostname */
67      protected String hostName = null;
68  
69      /** Password */
70      protected String password = null;
71  
72      /** The profileSql property */
73      protected String profileSql = "false";
74  
75      /** The JDBC URL */
76      protected String url = null;
77  
78      /** User name */
79      protected String user = null;
80  
81      /** Should we construct the URL, or has it been set explicitly */
82      protected boolean explicitUrl = false;
83  
84      /** Port number */
85      protected int port = 3306;
86  
87      /**
88       * Default no-arg constructor for Serialization
89       */
90      public MysqlDataSource() {
91      }
92  
93      /**
94       * Creates a new connection using the already configured username and
95       * password.
96       *
97       * @return a connection to the database
98       *
99       * @throws SQLException if an error occurs
100      */
101     public java.sql.Connection getConnection() throws SQLException {
102         return getConnection(user, password);
103     }
104 
105     /**
106      * Creates a new connection with the given username and password
107      *
108      * @param userID the user id to connect with
109      * @param password the password to connect with
110      *
111      * @return a connection to the database
112      *
113      * @throws SQLException if an error occurs
114      */
115     public java.sql.Connection getConnection(String userID, String password)
116         throws SQLException {
117         Properties props = new Properties();
118 
119         if (userID == null) {
120             userID = "";
121         }
122 
123         if (password == null) {
124             password = "";
125         }
126 
127         props.put("user", userID);
128         props.put("password", password);
129         props.put("profileSql", getProfileSql());
130 
131         return getConnection(props);
132     }
133 
134     /**
135      * Sets the database name.
136      *
137      * @param dbName the name of the database
138      */
139     public void setDatabaseName(String dbName) {
140         databaseName = dbName;
141     }
142 
143     /**
144      * Gets the name of the database
145      *
146      * @return the name of the database for this data source
147      */
148     public String getDatabaseName() {
149         return (databaseName != null) ? databaseName : "";
150     }
151 
152     /**
153      * Sets the log writer for this data source.
154      *
155      * @see javax.sql.DataSource#setLogWriter(PrintWriter)
156      */
157     public void setLogWriter(PrintWriter output) throws SQLException {
158         logWriter = output;
159     }
160 
161     /**
162      * Returns the log writer for this data source
163      *
164      * @return the log writer for this data source
165      */
166     public java.io.PrintWriter getLogWriter() {
167         return logWriter;
168     }
169 
170     /**
171      * DOCUMENT ME!
172      *
173      * @param seconds DOCUMENT ME!
174      *
175      * @throws SQLException DOCUMENT ME!
176      */
177     public void setLoginTimeout(int seconds) throws SQLException {
178     }
179 
180     /**
181      * Returns the login timeout
182      *
183      * @return the login timeout
184      */
185     public int getLoginTimeout() {
186         return 0;
187     }
188 
189     /**
190      * Sets the password
191      *
192      * @param pass the password
193      */
194     public void setPassword(String pass) {
195         password = pass;
196     }
197 
198     /**
199      * Sets the database port.
200      *
201      * @param p the port
202      */
203     public void setPort(int p) {
204         port = p;
205     }
206 
207     /**
208      * Returns the port number
209      *
210      * @return the port number
211      */
212     public int getPort() {
213         return port;
214     }
215 
216     /**
217      * Sets the port number
218      *
219      * @param p the port
220      *
221      * @see #setPort
222      */
223     public void setPortNumber(int p) {
224         setPort(p);
225     }
226 
227     /**
228      * Returns the port number
229      *
230      * @return the port number
231      */
232     public int getPortNumber() {
233         return getPort();
234     }
235 
236     /**
237      * Sets the profileSql property
238      *
239      * @param flag true/false
240      */
241     public void setProfileSql(String flag) {
242         profileSql = flag;
243     }
244 
245     /**
246      * Returns the value for the profileSql property
247      *
248      * @return the value for the profileSql property
249      */
250     public String getProfileSql() {
251         return profileSql;
252     }
253 
254     /**
255      * Required method to support this class as a <CODE>Referenceable</CODE>.
256      *
257      * @return a Reference to this data source
258      *
259      * @throws NamingException if a JNDI error occurs
260      */
261     public Reference getReference() throws NamingException {
262         String factoryName = "com.mysql.jdbc.jdbc2.optional.MysqlDataSourceFactory";
263         Reference ref = new Reference(getClass().getName(), factoryName, null);
264         ref.add(new StringRefAddr("user", getUser()));
265         ref.add(new StringRefAddr("password", password));
266         ref.add(new StringRefAddr("serverName", getServerName()));
267         ref.add(new StringRefAddr("port", "" + getPort()));
268         ref.add(new StringRefAddr("databaseName", getDatabaseName()));
269         ref.add(new StringRefAddr("profileSql", getProfileSql()));
270         ref.add(new StringRefAddr("explicitUrl", String.valueOf(this.explicitUrl)));
271         ref.add(new StringRefAddr("url", getUrl()));
272 
273         return ref;
274     }
275 
276     /**
277      * Sets the server name.
278      *
279      * @param serverName the server name
280      */
281     public void setServerName(String serverName) {
282         hostName = serverName;
283     }
284 
285     /**
286      * Returns the name of the database server
287      *
288      * @return the name of the database server
289      */
290     public String getServerName() {
291         return (hostName != null) ? hostName : "";
292     }
293 
294     //
295     // I've seen application servers use both formats
296     // URL or url (doh)
297     //
298 
299     /**
300      * Sets the URL for this connection
301      *
302      * @param url the URL for this connection
303      */
304     public void setURL(String url) {
305         setUrl(url);
306     }
307 
308     /**
309      * Returns the URL for this connection
310      *
311      * @return the URL for this connection
312      */
313     public String getURL() {
314         return getUrl();
315     }
316 
317     /**
318      * This method is used by the app server to set the url string specified
319      * within the datasource deployment descriptor.  It is discovered using
320      * introspection and matches if property name in descriptor is "url".
321      *
322      * @param url url to be used within driver.connect
323      */
324     public void setUrl(String url) {
325         this.url = url;
326         explicitUrl = true;
327     }
328 
329     /**
330      * Returns  the   JDBC URL that will be used to create the database
331      * connection.
332      *
333      * @return the URL for this connection
334      */
335     public String getUrl() {
336         if (!explicitUrl) {
337             String builtUrl = "jdbc:mysql://";
338             builtUrl = builtUrl + getServerName() + ":" + getPort() + "/"
339                 + getDatabaseName();
340 
341             return builtUrl;
342         } else {
343             return this.url;
344         }
345     }
346 
347     /**
348      * Sets the user ID.
349      *
350      * @param userID the User ID
351      */
352     public void setUser(String userID) {
353         user = userID;
354     }
355 
356     /**
357      * Returns the  configured user for this connection
358      *
359      * @return the user for this connection
360      */
361     public String getUser() {
362         return user;
363     }
364 
365     /**
366      * Creates a connection using the specified properties.
367      *
368      * @param props the properties to connect with
369      *
370      * @return a connection to the database
371      *
372      * @throws SQLException if an error occurs
373      */
374     protected java.sql.Connection getConnection(Properties props)
375         throws SQLException {
376         String jdbcUrlToUse = null;
377 
378         if (!explicitUrl) {
379             StringBuffer jdbcUrl = new StringBuffer("jdbc:mysql://");
380 
381             if (hostName != null) {
382                 jdbcUrl.append(hostName);
383             }
384 
385             jdbcUrl.append(":");
386             jdbcUrl.append(port);
387             jdbcUrl.append("/");
388 
389             if (databaseName != null) {
390                 jdbcUrl.append(databaseName);
391             }
392 
393             jdbcUrlToUse = jdbcUrl.toString();
394         } else {
395             jdbcUrlToUse = this.url;
396         }
397 
398         return mysqlDriver.connect(jdbcUrlToUse, props);
399        
400     }
401 }