-
Notifications
You must be signed in to change notification settings - Fork 211
/
Driver.java
560 lines (516 loc) · 16.5 KB
/
Driver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/*
* $Id$
*
* This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
* project.
*
* Copyright (C) 1998-2019 OpenLink Software
*
* This project is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; only version 2 of the License, dated June 1991.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
package virtuoso.jdbc4;
import java.sql.*;
import java.util.*;
/**
* The Virtuoso DBMS Driver class is an implementation of the Driver interface
* in the JDBC API. It can be loaded in an application by :
* <pre>
* <code>Class.forName("virtuoso.jdbc4.Driver")</code>
* </pre>
* , or statically with the -Djdbc.drivers=virtuoso.jdbc4.Driver on the java
* interpreter command line, or in the java properties files with an entry like
* before.
* <u>Hints :</u> <ul>You can see the version of the current Virtuoso DBMS JDBC driver accessible in
* the <i>CLASSPATH</i> with the <b>java virtuoso.jdbc4.Driver</b>
* </ul>
*
* @version 1.0 (JDBC API 2.0 implementation)
* @see java.sql.Driver
* @see java.sql.DriverManager
* @see virtuoso.jdbc4.VirtuosoConnection
*/
public class Driver implements java.sql.Driver
{
// Specifically, it creates a new Driver instance and registers it.
static
{
try
{
DriverManager.registerDriver(new Driver());
}
catch(Exception e)
{
e.printStackTrace();
}
}
// The major and minor version number
protected static final int major = 3;
protected static final int minor = 115;
// Some variables
private String host = "localhost";
private String port = "1111";
private String user, password, database, charset, pwdclear;
private Integer timeout, log_enable;
private String keystore_cert, keystore_pass, keystore_path;
private String ssl_provider;
private Integer fbs, sendbs, recvbs;
private final String VirtPrefix = "jdbc:virtuoso://";
/**
* Constructs a Virtuoso DBMS Driver instance. This function has not to be called
* directly, this function is called only during the DriverManager registration
* done with :
* <pre>
* <code>Class.forName("virtuoso.jdbc4.Driver")</code>
* </pre>
*
* @exception java.sql.SQLException an error occurred in registering
*/
public Driver() throws SQLException
{
try
{
String log_file = System.getProperty(
"JDBC4_LOG"
);
//log_file="/home/O12/logs/log." + System.currentTimeMillis () + "." + new java.util.Random ().nextInt() + ".log";
if (log_file != null)
{
System.err.println ("RPC logfile=" + log_file);
try
{
VirtuosoFuture.rpc_log = new java.io.PrintWriter(
new java.io.FileOutputStream(log_file), true);
// VirtuosoFuture.rpc_log = new java.io.PrintStream (
// new java.io.BufferedOutputStream (
// new java.io.FileOutputStream (log_file), 4096));
}
catch (Exception e)
{
VirtuosoFuture.rpc_log = new java.io.PrintWriter(System.out, true);
}
//System.err.println ("rpc_log=" + VirtuosoFuture.rpc_log);
}
}
catch (Exception e)
{
VirtuosoFuture.rpc_log = null;
}
}
// --------------------------- JDBC 1.0 ------------------------------
/**
* Attempts to make a database connection to the given URL.
* The driver returns "null" if the URL given is not a Virtuoso DBMS JDBC
* URL. A URL under the Virtuoso DBMS JDBC driver should be :
* <pre>
* <code>jdbc:virtuoso://<i>host</i>:<i>port</i></code> , or
* <code>jdbc:virtuoso://<i>host</i>:<i>port</i>/UID=<i>username</i>/PWD=<i>userpassword</i></code>
* </pre>
* This function is only called through the DriverManager.getConnection function.
*
* @param url the URL of the database to which to connect
* @param info a list of arbitrary string tag/value pairs as
* connection arguments. Normally at least a "user" and
* "password" property should be included.
* @return a <code>Connection</code> object that represents a
* connection to the URL
* @exception virtuoso.jdbc4.VirtuosoException it is the right
* driver to connect to the given URL, but has trouble connecting to
* the database.
* @see java.sql.Connection#connect
*/
public Connection connect(String url, Properties info) throws VirtuosoException
{
try
{
// First check the URL
if(acceptsURL(url))
{
Properties props = urlToInfo(url, info);
return new VirtuosoConnection(url,host,Integer.parseInt(port),props);
}
}
catch(NumberFormatException e)
{
throw new VirtuosoException("Wrong port number : " + e.getMessage(),VirtuosoException.BADFORMAT);
}
return null;
}
/**
* Returns true if the driver thinks that it can open a connection
* to the given URL. Typically drivers will return true if they
* understand the subprotocol specified in the URL and false if
* they do not.
*
* @param url the URL of the database
* @return true if this driver can connect to the given URL
* @exception virtuoso.jdbc.VirtuosoException An internal error occurred.
* @see java.sql.Connection#acceptsURL
*/
public boolean acceptsURL(String url) throws VirtuosoException
{
if (url.startsWith (VirtPrefix))
return true;
return false;
}
protected Properties urlToInfo(String url, Properties _info)
{
host = "localhost";
port = "1111";
fbs = new Integer(VirtuosoTypes.DEFAULTPREFETCH);
sendbs = new Integer(32768);
recvbs = new Integer(32768);
Properties props = new Properties();
for (Enumeration en = _info.propertyNames(); en.hasMoreElements(); ) {
String key = (String)en.nextElement();
String property = (String)_info.getProperty(key);
props.setProperty(key.toLowerCase(), property);
}
char inQuote = '\0';
String attr = null; // name
StringBuffer buff = new StringBuffer();
String part = url.substring(VirtPrefix.length());
boolean isFirst = true;
for (int i = 0; i < part.length(); i++) {
char c = part.charAt(i);
switch (c) {
case '\'':
case '"':
if (inQuote == c)
inQuote = '\0';
else if (inQuote == '\0')
inQuote = c;
break;
case '/':
if (inQuote == '\0') {
String val = buff.toString().trim();
if (attr == null) { // "jdbc:virtuoso://local:5000/MyParam/UID=sa/"
attr = val;
val = "";
}
if (attr != null && attr.length() > 0) {
// The first part is the host:port stuff
if (isFirst) {
isFirst = false;
props.setProperty("_vhost", attr);
} else {
props.setProperty(attr.toLowerCase(), val);
}
}
attr = null;
buff.setLength(0);
} else {
buff.append(c);
}
break;
case '=':
if (inQuote == '\0') {
attr = buff.toString().trim();
buff.setLength(0);
} else {
buff.append(c);
}
break;
default:
buff.append(c);
break;
}
}
String val = buff.toString().trim();
if (attr == null) {
attr = val;
val = "";
}
if (attr != null && attr.length() > 0) {
if (isFirst)
props.put("_vhost", attr);
else
props.put(attr.toLowerCase(), val);
}
char fsep = System.getProperty("file.separator").charAt(0);
val = props.getProperty("kpath");
if (val != null) {
if (fsep != '\\') {
val = val.replace('\\', fsep);
props.put("kpath", val);
}
}
val = props.getProperty("ts");
if (val != null) {
if (fsep != '\\') {
val = val.replace('\\', fsep);
props.put("ts", val);
}
}
val = props.getProperty("ssl");
if (val != null) {
props.setProperty("ssl", "1");
}
val = props.getProperty("pwdtype");
if (val != null)
props.setProperty("pwdclear", val);
val = props.getProperty("uid");
if (val != null)
props.setProperty("user", val);
val = props.getProperty("pwd");
if (val != null)
props.setProperty("password", val);
val = props.getProperty("ts");
if (val != null)
props.setProperty("truststorepath", val);
val = props.getProperty("tspass");
if (val != null)
props.setProperty("truststorepass", val);
val = props.getProperty("kpath");
if (val != null)
props.setProperty("keystorepath", val);
val = props.getProperty("pass");
if (val != null)
props.setProperty("keystorepass", val);
val = props.getProperty("kpass");
if (val != null)
props.setProperty("keystorepass", val);
return props;
}
/**
* Gets information about the possible properties for this driver.
* <p>The getPropertyInfo method is intended to allow a generic GUI tool to
* discover what properties it should prompt a human for in order to get
* enough information to connect to a database. Note that depending on
* the values the human has supplied so far, additional values may become
* necessary, so it may be necessary to iterate though several calls
* to getPropertyInfo.
*
* @param url the URL of the database to which to connect
* @param info a proposed list of tag/value pairs that will be sent on
* connect open
* @return an array of DriverPropertyInfo objects describing possible
* properties. This array may be an empty array if no properties
* are required.
* @exception virtuoso.jdbc4.VirtuosoException An internal error occurred.
* @see java.sql.Connection#getPropertyInfo
*/
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws VirtuosoException
{
Vector pinfo = new Vector();
DriverPropertyInfo pr;
// First check the URL
if(acceptsURL(url))
{
if(info.get("user") == null)
{
pr = new DriverPropertyInfo("user",null);
pr.required = true;
pinfo.add(pr);
}
if(info.get("password") == null)
{
pr = new DriverPropertyInfo("password",null);
pr.required = true;
pinfo.add(pr);
}
if(info.get("database") == null)
{
pr = new DriverPropertyInfo("database",null);
pr.required = false;
pinfo.add(pr);
}
if(info.get("cert") == null)
{
pr = new DriverPropertyInfo("cert",null);
pr.required = false;
pinfo.add(pr);
}
if(info.get("keystorepass") == null)
{
pr = new DriverPropertyInfo("keystorepass",null);
pr.required = false;
pinfo.add(pr);
}
if(info.get("keystorepath") == null)
{
pr = new DriverPropertyInfo("keystorepath",null);
pr.required = false;
pinfo.add(pr);
}
if(info.get("provider") == null)
{
pr = new DriverPropertyInfo("provider",null);
pr.required = false;
pinfo.add(pr);
}
if(info.get("truststorepass") == null)
{
pr = new DriverPropertyInfo("truststorepass",null);
pr.required = false;
pinfo.add(pr);
}
if(info.get("truststorepath") == null)
{
pr = new DriverPropertyInfo("truststorepath",null);
pr.required = false;
pinfo.add(pr);
}
DriverPropertyInfo drv_info[] = new DriverPropertyInfo[pinfo.size()];
pinfo.copyInto(drv_info);
return drv_info;
}
pr = new DriverPropertyInfo("url",url);
pr.required = true;
pinfo.add(pr);
if(info.get("user") == null)
{
pr = new DriverPropertyInfo("user",null);
pr.required = true;
pinfo.add(pr);
}
if(info.get("password") == null)
{
pr = new DriverPropertyInfo("password",null);
pr.required = true;
pinfo.add(pr);
}
if(info.get("database") == null)
{
pr = new DriverPropertyInfo("database",null);
pr.required = false;
pinfo.add(pr);
}
if(info.get("fbs") == null)
{
pr = new DriverPropertyInfo("fbs",null);
pr.required = false;
pinfo.add(pr);
}
if(info.get("sendbs") == null)
{
pr = new DriverPropertyInfo("sendbs",null);
pr.required = false;
pinfo.add(pr);
}
if(info.get("recvbs") == null)
{
pr = new DriverPropertyInfo("recvbs",null);
pr.required = false;
pinfo.add(pr);
}
if(info.get("roundrobin") == null)
{
pr = new DriverPropertyInfo("roundrobin",null);
pr.required = false;
pinfo.add(pr);
}
if(info.get("cert") == null)
{
pr = new DriverPropertyInfo("cert",null);
pr.required = false;
pinfo.add(pr);
}
if(info.get("keystorepass") == null)
{
pr = new DriverPropertyInfo("keystorepass",null);
pr.required = false;
pinfo.add(pr);
}
if(info.get("keystorepath") == null)
{
pr = new DriverPropertyInfo("keystorepath",null);
pr.required = false;
pinfo.add(pr);
}
if(info.get("provider") == null)
{
pr = new DriverPropertyInfo("provider",null);
pr.required = false;
pinfo.add(pr);
}
if(info.get("truststorepass") == null)
{
pr = new DriverPropertyInfo("truststorepass",null);
pr.required = false;
pinfo.add(pr);
}
if(info.get("truststorepath") == null)
{
pr = new DriverPropertyInfo("truststorepath",null);
pr.required = false;
pinfo.add(pr);
}
if(info.get("usepstmtpool") == null)
{
pr = new DriverPropertyInfo("usepstmtpool",null);
pr.required = false;
pinfo.add(pr);
}
if(info.get("pstmtpoolsize") == null)
{
pr = new DriverPropertyInfo("pstmtpoolsize",null);
pr.required = false;
pinfo.add(pr);
}
DriverPropertyInfo drv_info[] = new DriverPropertyInfo[pinfo.size()];
pinfo.copyInto(drv_info);
return drv_info;
}
/**
* Gets the driver's major version number.
*
* @return this driver's major version number
*/
public int getMajorVersion()
{
return major;
}
/**
* Gets the driver's minor version number.
*
* @return this driver's minor version number
*/
public int getMinorVersion()
{
return minor;
}
/**
* Reports whether this driver is a genuine JDBC COMPLIANT driver.
*
* @return true if the JDBC Driver is compliant, false otherwise ... but
* our Virtuoso DBMS JDBC driver is compliant, so true is always returned.
*/
public boolean jdbcCompliant()
{
return true;
}
public static void main(String args[])
{
System.out.println("OpenLink Virtuoso(TM) Driver with SSL support for JDBC(TM) Version " + VIRT_JDBC_VER + " [Build " + major + "." + minor + "]");
}
#if JDK_VER >= 17
//------------------------- JDBC 4.1 -----------------------------------
/**
* Return the parent Logger of all the Loggers used by this driver. This
* should be the Logger farthest from the root Logger that is
* still an ancestor of all of the Loggers used by this driver. Configuring
* this Logger will affect all of the log messages generated by the driver.
* In the worst case, this may be the root Logger.
*
* @return the parent Logger for this driver
* @throws SQLFeatureNotSupportedException if the driver does not use <code>java.util.logging<code>.
* @since 1.7
*/
public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException
{
throw new VirtuosoFNSException ("getParentLogger() not supported", VirtuosoException.NOTIMPLEMENTED);
}
#endif
}