source: Ballon/out/artifacts/geisa_artifact/WEB-INF/lib/mysql-connector-java-5.1.21/src/com/mysql/jdbc/jdbc2/optional/MysqlDataSource.java @ 848

Last change on this file since 848 was 766, checked in by npipsl, 11 years ago
File size: 10.1 KB
Line 
1/*
2 Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
3 
4
5  The MySQL Connector/J is licensed under the terms of the GPLv2
6  <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors.
7  There are special exceptions to the terms and conditions of the GPLv2 as it is applied to
8  this software, see the FLOSS License Exception
9  <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
10
11  This program is free software; you can redistribute it and/or modify it under the terms
12  of the GNU General Public License as published by the Free Software Foundation; version 2
13  of the License.
14
15  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
16  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17  See the GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License along with this
20  program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth
21  Floor, Boston, MA 02110-1301  USA
22
23
24
25 */
26package com.mysql.jdbc.jdbc2.optional;
27
28import java.io.PrintWriter;
29import java.io.Serializable;
30import java.sql.SQLException;
31import java.util.Iterator;
32import java.util.Properties;
33
34import javax.naming.NamingException;
35import javax.naming.Reference;
36import javax.naming.Referenceable;
37import javax.naming.StringRefAddr;
38import javax.sql.DataSource;
39
40import com.mysql.jdbc.ConnectionPropertiesImpl;
41import com.mysql.jdbc.NonRegisteringDriver;
42
43/**
44 * A JNDI DataSource for a Mysql JDBC connection
45 *
46 * @author Mark Matthews
47 */
48public class MysqlDataSource extends ConnectionPropertiesImpl implements
49                DataSource, Referenceable, Serializable  {
50
51        static final long serialVersionUID = -5515846944416881264L;
52
53        /** The driver to create connections with */
54        protected final static NonRegisteringDriver mysqlDriver;
55
56        static {
57                try {
58                        mysqlDriver = new NonRegisteringDriver();
59                } catch (Exception E) {
60                        throw new RuntimeException(
61                                        "Can not load Driver class com.mysql.jdbc.Driver");
62                }
63        }
64
65        /** Log stream */
66        protected transient PrintWriter logWriter = null;
67
68        /** Database Name */
69        protected String databaseName = null;
70
71        /** Character Encoding */
72        protected String encoding = null;
73
74        /** Hostname */
75        protected String hostName = null;
76
77        /** Password */
78        protected String password = null;
79
80        /** The profileSql property */
81        protected String profileSql = "false";
82
83        /** The JDBC URL */
84        protected String url = null;
85
86        /** User name */
87        protected String user = null;
88
89        /** Should we construct the URL, or has it been set explicitly */
90        protected boolean explicitUrl = false;
91
92        /** Port number */
93        protected int port = 3306;
94
95        /**
96         * Default no-arg constructor for Serialization
97         */
98        public MysqlDataSource() {
99        }
100
101        /**
102         * Creates a new connection using the already configured username and
103         * password.
104         *
105         * @return a connection to the database
106         *
107         * @throws SQLException
108         *             if an error occurs
109         */
110        public java.sql.Connection getConnection() throws SQLException {
111                return getConnection(this.user, this.password);
112        }
113
114        /**
115         * Creates a new connection with the given username and password
116         *
117         * @param userID
118         *            the user id to connect with
119         * @param password
120         *            the password to connect with
121         *
122         * @return a connection to the database
123         *
124         * @throws SQLException
125         *             if an error occurs
126         */
127        public java.sql.Connection getConnection(String userID, String pass)
128                        throws SQLException {
129                Properties props = new Properties();
130
131                if (userID != null) {
132                        props.setProperty(NonRegisteringDriver.USER_PROPERTY_KEY, userID);
133                }
134
135                if (pass != null) {
136                        props.setProperty(NonRegisteringDriver.PASSWORD_PROPERTY_KEY, pass);
137                }
138
139                exposeAsProperties(props);
140
141                return getConnection(props);
142        }
143
144        /**
145         * Sets the database name.
146         *
147         * @param dbName
148         *            the name of the database
149         */
150        public void setDatabaseName(String dbName) {
151                this.databaseName = dbName;
152        }
153
154        /**
155         * Gets the name of the database
156         *
157         * @return the name of the database for this data source
158         */
159        public String getDatabaseName() {
160                return (this.databaseName != null) ? this.databaseName : "";
161        }
162
163        /**
164         * Sets the log writer for this data source.
165         *
166         * @see javax.sql.DataSource#setLogWriter(PrintWriter)
167         */
168        public void setLogWriter(PrintWriter output) throws SQLException {
169                this.logWriter = output;
170        }
171
172        /**
173         * Returns the log writer for this data source
174         *
175         * @return the log writer for this data source
176         */
177        public java.io.PrintWriter getLogWriter() {
178                return this.logWriter;
179        }
180
181        /**
182         * DOCUMENT ME!
183         *
184         * @param seconds
185         *            DOCUMENT ME!
186         *
187         * @throws SQLException
188         *             DOCUMENT ME!
189         */
190        public void setLoginTimeout(int seconds) throws SQLException {
191        }
192
193        /**
194         * Returns the login timeout
195         *
196         * @return the login timeout
197         */
198        public int getLoginTimeout() {
199                return 0;
200        }
201
202        /**
203         * Sets the password
204         *
205         * @param pass
206         *            the password
207         */
208        public void setPassword(String pass) {
209                this.password = pass;
210        }
211
212        /**
213         * Sets the database port.
214         *
215         * @param p
216         *            the port
217         */
218        public void setPort(int p) {
219                this.port = p;
220        }
221
222        /**
223         * Returns the port number
224         *
225         * @return the port number
226         */
227        public int getPort() {
228                return this.port;
229        }
230
231        /**
232         * Sets the port number
233         *
234         * @param p
235         *            the port
236         *
237         * @see #setPort
238         */
239        public void setPortNumber(int p) {
240                setPort(p);
241        }
242
243        /**
244         * Returns the port number
245         *
246         * @return the port number
247         */
248        public int getPortNumber() {
249                return getPort();
250        }
251
252        /**
253         * DOCUMENT ME!
254         *
255         * @param ref
256         *            DOCUMENT ME!
257         *
258         * @throws SQLException
259         *             DOCUMENT ME!
260         */
261        public void setPropertiesViaRef(Reference ref) throws SQLException {
262                super.initializeFromRef(ref);
263        }
264
265        /**
266         * Required method to support this class as a <CODE>Referenceable</CODE>.
267         *
268         * @return a Reference to this data source
269         *
270         * @throws NamingException
271         *             if a JNDI error occurs
272         */
273        public Reference getReference() throws NamingException {
274                String factoryName = "com.mysql.jdbc.jdbc2.optional.MysqlDataSourceFactory";
275                Reference ref = new Reference(getClass().getName(), factoryName, null);
276                ref.add(new StringRefAddr(NonRegisteringDriver.USER_PROPERTY_KEY,
277                                getUser()));
278                ref.add(new StringRefAddr(NonRegisteringDriver.PASSWORD_PROPERTY_KEY,
279                                this.password));
280                ref.add(new StringRefAddr("serverName", getServerName()));
281                ref.add(new StringRefAddr("port", "" + getPort()));
282                ref.add(new StringRefAddr("databaseName", getDatabaseName()));
283                ref.add(new StringRefAddr("url", getUrl()));
284                ref.add(new StringRefAddr("explicitUrl", String
285                                .valueOf(this.explicitUrl)));
286
287                //
288                // Now store all of the 'non-standard' properties...
289                //
290                try {
291                        storeToRef(ref);
292                } catch (SQLException sqlEx) {
293                        throw new NamingException(sqlEx.getMessage());
294                }
295
296                return ref;
297        }
298
299        /**
300         * Sets the server name.
301         *
302         * @param serverName
303         *            the server name
304         */
305        public void setServerName(String serverName) {
306                this.hostName = serverName;
307        }
308
309        /**
310         * Returns the name of the database server
311         *
312         * @return the name of the database server
313         */
314        public String getServerName() {
315                return (this.hostName != null) ? this.hostName : "";
316        }
317
318        //
319        // I've seen application servers use both formats
320        // URL or url (doh)
321        //
322
323        /**
324         * Sets the URL for this connection
325         *
326         * @param url
327         *            the URL for this connection
328         */
329        public void setURL(String url) {
330                setUrl(url);
331        }
332
333        /**
334         * Returns the URL for this connection
335         *
336         * @return the URL for this connection
337         */
338        public String getURL() {
339                return getUrl();
340        }
341
342        /**
343         * This method is used by the app server to set the url string specified
344         * within the datasource deployment descriptor. It is discovered using
345         * introspection and matches if property name in descriptor is "url".
346         *
347         * @param url
348         *            url to be used within driver.connect
349         */
350        public void setUrl(String url) {
351                this.url = url;
352                this.explicitUrl = true;
353        }
354
355        /**
356         * Returns the JDBC URL that will be used to create the database connection.
357         *
358         * @return the URL for this connection
359         */
360        public String getUrl() {
361                if (!this.explicitUrl) {
362                        String builtUrl = "jdbc:mysql://";
363                        builtUrl = builtUrl + getServerName() + ":" + getPort() + "/"
364                                        + getDatabaseName();
365
366                        return builtUrl;
367                }
368
369                return this.url;
370        }
371
372        /**
373         * Sets the user ID.
374         *
375         * @param userID
376         *            the User ID
377         */
378        public void setUser(String userID) {
379                this.user = userID;
380        }
381
382        /**
383         * Returns the configured user for this connection
384         *
385         * @return the user for this connection
386         */
387        public String getUser() {
388                return this.user;
389        }
390
391        /**
392         * Creates a connection using the specified properties.
393         *
394         * @param props
395         *            the properties to connect with
396         *
397         * @return a connection to the database
398         *
399         * @throws SQLException
400         *             if an error occurs
401         */
402        protected java.sql.Connection getConnection(Properties props)
403                        throws SQLException {
404                String jdbcUrlToUse = null;
405
406                if (!this.explicitUrl) {
407                        StringBuffer jdbcUrl = new StringBuffer("jdbc:mysql://");
408
409                        if (this.hostName != null) {
410                                jdbcUrl.append(this.hostName);
411                        }
412
413                        jdbcUrl.append(":");
414                        jdbcUrl.append(this.port);
415                        jdbcUrl.append("/");
416
417                        if (this.databaseName != null) {
418                                jdbcUrl.append(this.databaseName);
419                        }
420
421                        jdbcUrlToUse = jdbcUrl.toString();
422                } else {
423                        jdbcUrlToUse = this.url;
424                }
425
426                //
427                // URL should take precedence over properties
428                //
429               
430                Properties urlProps = mysqlDriver.parseURL(jdbcUrlToUse, null);
431                urlProps.remove(NonRegisteringDriver.DBNAME_PROPERTY_KEY);
432                urlProps.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
433                urlProps.remove(NonRegisteringDriver.PORT_PROPERTY_KEY);
434               
435                Iterator<Object> keys = urlProps.keySet().iterator();
436               
437                while (keys.hasNext()) {
438                        String key = (String)keys.next();
439                       
440                        props.setProperty(key, urlProps.getProperty(key));
441                }
442               
443                return mysqlDriver.connect(jdbcUrlToUse, props);
444        }
445//
446//      public boolean isWrapperFor(Class<?> iface) throws SQLException {
447//              throw SQLError.notImplemented();
448//      }
449//
450//      public <T> T unwrap(Class<T> iface) throws SQLException {
451//              throw SQLError.notImplemented();
452//      }
453}
Note: See TracBrowser for help on using the repository browser.