File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\jdbcx\JdbcDataSource.java.html

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