Source code: org/hsqldb/jdbcDataSource.java
1 /* Copyright (c) 2001-2002, The HSQL Development Group
2 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of the HSQL Development Group nor the names of its
15 * contributors may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31
32 package org.hsqldb;
33
34 import java.io.Serializable;
35 import java.io.PrintWriter;
36 import java.sql.SQLException;
37 import java.sql.Connection;
38 import java.util.Properties;
39 import javax.naming.NamingException;
40 import javax.naming.Reference;
41 import javax.naming.Referenceable;
42 import javax.naming.StringRefAddr;
43 import javax.sql.DataSource;
44
45 // fredt@users 20020130 - patch 416437 by deforest@users - jdbc DataSource
46 public class jdbcDataSource
47 implements Serializable, Referenceable, DataSource {
48
49 /**
50 * Login timeout
51 */
52 private int loginTimeout = 0;
53
54 /**
55 * Log writer
56 */
57 private transient PrintWriter logWriter;
58
59 /**
60 * Default password to use for connections
61 */
62 private String password = "";
63
64 /**
65 * Default user to use for connections
66 */
67 private String user = "";
68
69 /**
70 * Signature
71 */
72 private static final String sStartURL = "jdbc:hsqldb:";
73
74 /**
75 * Database location
76 */
77 private String database = "";
78
79 /**
80 * Constructor
81 */
82 public jdbcDataSource() {}
83
84 /**
85 * Forward with current user/password
86 */
87 public Connection getConnection() throws java.sql.SQLException {
88 return getConnection(user, password);
89 }
90
91 /**
92 * getConnection method comment.
93 */
94 public Connection getConnection(String user,
95 String password) throws SQLException {
96
97 Properties props = new Properties();
98
99 if (user != null) {
100 props.put("user", user);
101 }
102
103 if (password != null) {
104 props.put("password", password);
105 }
106
107 return new jdbcConnection(database, props);
108 }
109
110 /**
111 * Return database
112 */
113 public String getDatabase() {
114 return database;
115 }
116
117 /**
118 * getLoginTimeout method comment.
119 */
120 public int getLoginTimeout() throws java.sql.SQLException {
121 return loginTimeout;
122 }
123
124 /**
125 * getLogWriter method comment.
126 */
127 public java.io.PrintWriter getLogWriter() throws java.sql.SQLException {
128 return null;
129 }
130
131 /**
132 * getReference method comment.
133 */
134 public Reference getReference() throws NamingException {
135
136 String cname = "org.hsqldb.jdbcDataSourceFactory";
137 Reference ref = new Reference(getClass().getName(), cname, null);
138
139 ref.add(new StringRefAddr("database", getDatabase()));
140 ref.add(new StringRefAddr("user", getUser()));
141 ref.add(new StringRefAddr("password", password));
142
143 return ref;
144 }
145
146 /**
147 * @return user ID for the connection
148 */
149 public String getUser() {
150 return user;
151 }
152
153 /**
154 * Set database location
155 */
156 public void setDatabase(String database) {
157 this.database = database;
158 }
159
160 /**
161 * Not yet implemented
162 */
163 public void setLoginTimeout(int ignore) throws java.sql.SQLException {
164 this.loginTimeout = ignore;
165 }
166
167 /**
168 * setLogWriter method comment.
169 */
170 public void setLogWriter(PrintWriter logWriter)
171 throws java.sql.SQLException {
172 this.logWriter = logWriter;
173 }
174
175 /**
176 * Sets the password to use for connecting to the database
177 * @param password the password
178 */
179 public void setPassword(String password) {
180 this.password = password;
181 }
182
183 /**
184 * Sets the userid
185 * @param user the user id
186 */
187 public void setUser(String user) {
188 this.user = user;
189 }
190 }