Debian Codegraph @

src/main/org/h2/jdbcx/JdbcDataSource.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.jdbcx;
7
8 import java.io.IOException;
9 import java.io.ObjectInputStream;
10 import java.io.PrintWriter;
11 import java.io.Serializable;
12 import java.sql.Connection;
13 import java.sql.SQLException;
14 import java.util.logging.Logger;
15 import javax.naming.Reference;
16 import javax.naming.Referenceable;
17 import javax.naming.StringRefAddr;
18 import javax.sql.ConnectionPoolDataSource;
19 import javax.sql.DataSource;
20 import javax.sql.PooledConnection;
21 import javax.sql.XAConnection;
22 import javax.sql.XADataSource;
23 import org.h2.jdbc.JdbcConnection;
24 import org.h2.message.DbException;
25 import org.h2.message.TraceObject;
26 import org.h2.util.StringUtils;
27
28 /**
29 * A data source for H2 database connections. It is a factory for XAConnection
30 * and Connection objects. This class is usually registered in a JNDI naming
31 * service. To create a data source object and register it with a JNDI service,
32 * use the following code:
33 *
34 * <pre>
35 * import org.h2.jdbcx.JdbcDataSource;
36 * import javax.naming.Context;
37 * import javax.naming.InitialContext;
38 * JdbcDataSource ds = new JdbcDataSource();
39 * ds.setURL(&quot;jdbc:h2:&tilde;/test&quot;);
40 * ds.setUser(&quot;sa&quot;);
41 * ds.setPassword(&quot;sa&quot;);
42 * Context ctx = new InitialContext();
43 * ctx.bind(&quot;jdbc/dsName&quot;, ds);
44 * </pre>
45 *
46 * To use a data source that is already registered, use the following code:
47 *
48 * <pre>
49 * import java.sql.Connection;
50 * import javax.sql.DataSource;
51 * import javax.naming.Context;
52 * import javax.naming.InitialContext;
53 * Context ctx = new InitialContext();
54 * DataSource ds = (DataSource) ctx.lookup(&quot;jdbc/dsName&quot;);
55 * Connection conn = ds.getConnection();
56 * </pre>
57 *
58 * In this example the user name and password are serialized as
59 * well; this may be a security problem in some cases.
60 */
61 public final class JdbcDataSource extends TraceObject implements XADataSource, DataSource, ConnectionPoolDataSource,
62 Serializable, Referenceable, JdbcDataSourceBackwardsCompat {
63
64 private static final long serialVersionUID = 1288136338451857771L;
65
66 private transient JdbcDataSourceFactory factory;
67 private transient PrintWriter logWriter;
68 private int loginTimeout;
69 private String userName = "";
70 private char[] passwordChars = { };
71 private String url = "";
72 private String description;
73
74 /**
75 * The public constructor.
76 */
77 public JdbcDataSource() {
78 initFactory();
79 int id = getNextId(TraceObject.DATA_SOURCE);
80 setTrace(factory.getTrace(), TraceObject.DATA_SOURCE, id);
81 }
82
83 /**
84 * Called when de-serializing the object.
85 *
86 * @param in the input stream
87 * @throws IOException on failure
88 * @throws ClassNotFoundException on failure
89 */
90 private void readObject(ObjectInputStream in) throws IOException,
91 ClassNotFoundException {
92 initFactory();
93 in.defaultReadObject();
94 }
95
96 private void initFactory() {
97 factory = new JdbcDataSourceFactory();
98 }
99
100 /**
101 * Get the login timeout in seconds, 0 meaning no timeout.
102 *
103 * @return the timeout in seconds
104 */
105 @Override
106 public int getLoginTimeout() {
107 debugCodeCall("getLoginTimeout");
108 return loginTimeout;
109 }
110
111 /**
112 * Set the login timeout in seconds, 0 meaning no timeout.
113 * The default value is 0.
114 * This value is ignored by this database.
115 *
116 * @param timeout the timeout in seconds
117 */
118 @Override
119 public void setLoginTimeout(int timeout) {
120 debugCodeCall("setLoginTimeout", timeout);
121 this.loginTimeout = timeout;
122 }
123
124 /**
125 * Get the current log writer for this object.
126 *
127 * @return the log writer
128 */
129 @Override
130 public PrintWriter getLogWriter() {
131 debugCodeCall("getLogWriter");
132 return logWriter;
133 }
134
135 /**
136 * Set the current log writer for this object.
137 * This value is ignored by this database.
138 *
139 * @param out the log writer
140 */
141 @Override
142 public void setLogWriter(PrintWriter out) {
143 debugCodeCall("setLogWriter(out)");
144 logWriter = out;
145 }
146
147 /**
148 * Open a new connection using the current URL, user name and password.
149 *
150 * @return the connection
151 */
152 @Override
153 public Connection getConnection() throws SQLException {
154 debugCodeCall("getConnection");
155 return new JdbcConnection(url, null, userName, StringUtils.cloneCharArray(passwordChars), false);
156 }
157
158 /**
159 * Open a new connection using the current URL and the specified user name
160 * and password.
161 *
162 * @param user the user name
163 * @param password the password
164 * @return the connection
165 */
166 @Override
167 public Connection getConnection(String user, String password)
168 throws SQLException {
169 if (isDebugEnabled()) {
170 debugCode("getConnection(" + quote(user) + ", \"\")");
171 }
172 return new JdbcConnection(url, null, user, password, false);
173 }
174
175 /**
176 * Get the current URL.
177 *
178 * @return the URL
179 */
180 public String getURL() {
181 debugCodeCall("getURL");
182 return url;
183 }
184
185 /**
186 * Set the current URL.
187 *
188 * @param url the new URL
189 */
190 public void setURL(String url) {
191 debugCodeCall("setURL", url);
192 this.url = url;
193 }
194
195 /**
196 * Get the current URL.
197 * This method does the same as getURL, but this methods signature conforms
198 * the JavaBean naming convention.
199 *
200 * @return the URL
201 */
202 public String getUrl() {
203 debugCodeCall("getUrl");
204 return url;
205 }
206
207 /**
208 * Set the current URL.
209 * This method does the same as setURL, but this methods signature conforms
210 * the JavaBean naming convention.
211 *
212 * @param url the new URL
213 */
214 public void setUrl(String url) {
215 debugCodeCall("setUrl", url);
216 this.url = url;
217 }
218
219 /**
220 * Set the current password.
221 *
222 * @param password the new password.
223 */
224 public void setPassword(String password) {
225 debugCodeCall("setPassword", "");
226 this.passwordChars = password == null ? null : password.toCharArray();
227 }
228
229 /**
230 * Set the current password in the form of a char array.
231 *
232 * @param password the new password in the form of a char array.
233 */
234 public void setPasswordChars(char[] password) {
235 if (isDebugEnabled()) {
236 debugCode("setPasswordChars(new char[0])");
237 }
238 this.passwordChars = password;
239 }
240
241 private static String convertToString(char[] a) {
242 return a == null ? null : new String(a);
243 }
244
245 /**
246 * Get the current password.
247 *
248 * @return the password
249 */
250 public String getPassword() {
251 debugCodeCall("getPassword");
252 return convertToString(passwordChars);
253 }
254
255 /**
256 * Get the current user name.
257 *
258 * @return the user name
259 */
260 public String getUser() {
261 debugCodeCall("getUser");
262 return userName;
263 }
264
265 /**
266 * Set the current user name.
267 *
268 * @param user the new user name
269 */
270 public void setUser(String user) {
271 debugCodeCall("setUser", user);
272 this.userName = user;
273 }
274
275 /**
276 * Get the current description.
277 *
278 * @return the description
279 */
280 public String getDescription() {
281 debugCodeCall("getDescription");
282 return description;
283 }
284
285 /**
286 * Set the description.
287 *
288 * @param description the new description
289 */
290 public void setDescription(String description) {
291 debugCodeCall("getDescription", description);
292 this.description = description;
293 }
294
295 /**
296 * Get a new reference for this object, using the current settings.
297 *
298 * @return the new reference
299 */
300 @Override
301 public Reference getReference() {
302 debugCodeCall("getReference");
303 String factoryClassName = JdbcDataSourceFactory.class.getName();
304 Reference ref = new Reference(getClass().getName(), factoryClassName, null);
305 ref.add(new StringRefAddr("url", url));
306 ref.add(new StringRefAddr("user", userName));
307 ref.add(new StringRefAddr("password", convertToString(passwordChars)));
308 ref.add(new StringRefAddr("loginTimeout", Integer.toString(loginTimeout)));
309 ref.add(new StringRefAddr("description", description));
310 return ref;
311 }
312
313 /**
314 * Open a new XA connection using the current URL, user name and password.
315 *
316 * @return the connection
317 */
318 @Override
319 public XAConnection getXAConnection() throws SQLException {
320 debugCodeCall("getXAConnection");
321 return new JdbcXAConnection(factory, getNextId(XA_DATA_SOURCE),
322 new JdbcConnection(url, null, userName, StringUtils.cloneCharArray(passwordChars), false));
323 }
324
325 /**
326 * Open a new XA connection using the current URL and the specified user
327 * name and password.
328 *
329 * @param user the user name
330 * @param password the password
331 * @return the connection
332 */
333 @Override
334 public XAConnection getXAConnection(String user, String password)
335 throws SQLException {
336 if (isDebugEnabled()) {
337 debugCode("getXAConnection(" + quote(user) + ", \"\")");
338 }
339 return new JdbcXAConnection(factory, getNextId(XA_DATA_SOURCE),
340 new JdbcConnection(url, null, user, password, false));
341 }
342
343 /**
344 * Open a new pooled connection using the current URL, user name and
345 * password.
346 *
347 * @return the connection
348 */
349 @Override
350 public PooledConnection getPooledConnection() throws SQLException {
351 debugCodeCall("getPooledConnection");
352 return getXAConnection();
353 }
354
355 /**
356 * Open a new pooled connection using the current URL and the specified user
357 * name and password.
358 *
359 * @param user the user name
360 * @param password the password
361 * @return the connection
362 */
363 @Override
364 public PooledConnection getPooledConnection(String user, String password)
365 throws SQLException {
366 if (isDebugEnabled()) {
367 debugCode("getPooledConnection(" + quote(user) + ", \"\")");
368 }
369 return getXAConnection(user, password);
370 }
371
372 /**
373 * Return an object of this class if possible.
374 *
375 * @param iface the class
376 * @return this
377 */
378 @Override
379 @SuppressWarnings("unchecked")
380 public <T> T unwrap(Class<T> iface) throws SQLException {
381 try {
382 if (isWrapperFor(iface)) {
383 return (T) this;
384 }
385 throw DbException.getInvalidValueException("iface", iface);
386 } catch (Exception e) {
387 throw logAndConvert(e);
388 }
389 }
390
391 /**
392 * Checks if unwrap can return an object of this class.
393 *
394 * @param iface the class
395 * @return whether or not the interface is assignable from this class
396 */
397 @Override
398 public boolean isWrapperFor(Class<?> iface) throws SQLException {
399 return iface != null && iface.isAssignableFrom(getClass());
400 }
401
402 /**
403 * [Not supported]
404 */
405 @Override
406 public Logger getParentLogger() {
407 return null;
408 }
409
410 /**
411 * INTERNAL
412 */
413 @Override
414 public String toString() {
415 return getTraceObjectName() + ": url=" + url + " user=" + userName;
416 }
417
418 }