/
FBAbstractCommonDataSource.java
722 lines (635 loc) · 19.9 KB
/
FBAbstractCommonDataSource.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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
/*
* Firebird Open Source JavaEE Connector - JDBC Driver
*
* Distributable under LGPL license.
* You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
*
* 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
* LGPL License for more details.
*
* This file was created by members of the firebird development team.
* All individual contributions remain the Copyright (C) of those
* individuals. Contributors to this file are either listed here or
* can be obtained from a source control history command.
*
* All rights reserved.
*/
package org.firebirdsql.ds;
import java.sql.SQLException;
import javax.naming.BinaryRefAddr;
import javax.naming.Reference;
import javax.naming.StringRefAddr;
import org.firebirdsql.gds.DatabaseParameterBuffer;
import org.firebirdsql.gds.TransactionParameterBuffer;
import org.firebirdsql.jdbc.FBConnectionProperties;
import org.firebirdsql.jdbc.FirebirdConnectionProperties;
/**
* Abstract class for properties and behaviour common to DataSources,
* XADataSources and ConnectionPoolDataSources
*
* @author <a href="mailto:mrotteveel@users.sourceforge.net">Mark Rotteveel</a>
* @since 2.2
*/
public abstract class FBAbstractCommonDataSource extends RootCommonDataSource implements FirebirdConnectionProperties {
protected static final String REF_DATABASE_NAME = "databaseName";
protected static final String REF_PORT_NUMBER = "portNumber";
protected static final String REF_SERVER_NAME = "serverName";
protected static final String REF_DESCRIPTION = "description";
protected static final String REF_PROPERTIES = "properties";
private String description;
private String serverName;
private int portNumber;
private String databaseName;
protected final Object lock = new Object();
private FBConnectionProperties connectionProperties = new FBConnectionProperties();
/**
* Method to check if this DataSource has not yet started.
* <p>
* Implementations should throw IllegalStateException when the DataSource is
* already in use and modifying properties is not allowed.
* </p>
*
* @throws IllegalStateException
* When the DataSource is already in use
*/
protected abstract void checkNotStarted() throws IllegalStateException;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public final String getServerName() {
synchronized (lock) {
return serverName;
}
}
public final void setServerName(String serverName) {
synchronized (lock) {
checkNotStarted();
this.serverName = serverName;
setDatabase();
}
}
public final int getPortNumber() {
synchronized (lock) {
return portNumber;
}
}
public final void setPortNumber(int portNumber) {
synchronized (lock) {
checkNotStarted();
this.portNumber = portNumber;
setDatabase();
}
}
public final String getDatabaseName() {
synchronized (lock) {
return databaseName;
}
}
/**
* Sets the databaseName of this datasource.
* <p>
* The databaseName is the filepath or alias of the database only, so it
* should not include serverName and portNumber.
* </p>
*
* @param databaseName
* Database name (filepath or alias)
*/
public final void setDatabaseName(String databaseName) {
synchronized (lock) {
checkNotStarted();
this.databaseName = databaseName;
setDatabase();
}
}
@Override
public final String getType() {
synchronized (lock) {
return connectionProperties.getType();
}
}
@Override
public final void setType(String type) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setType(type);
}
}
public String getUser() {
synchronized (lock) {
return connectionProperties.getUserName();
}
}
public void setUser(String user) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setUserName(user);
}
}
@Override
public String getPassword() {
synchronized (lock) {
return connectionProperties.getPassword();
}
}
@Override
public void setPassword(String password) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setPassword(password);
}
}
@Override
public String getRoleName() {
synchronized (lock) {
return connectionProperties.getRoleName();
}
}
@Override
public void setRoleName(String roleName) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setRoleName(roleName);
}
}
@Override
public final String getCharSet() {
synchronized (lock) {
return connectionProperties.getCharSet();
}
}
/**
* @param charSet
* Character set for the connection. Similar to
* <code>encoding</code> property, but accepts Java names instead
* of Firebird ones.
*/
@Override
public final void setCharSet(String charSet) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setCharSet(charSet);
}
}
@Override
public final String getEncoding() {
synchronized (lock) {
return connectionProperties.getEncoding();
}
}
/**
* @param encoding
* Firebird name of the character encoding for the connection.
* See Firebird documentation for more information.
*/
@Override
public final void setEncoding(String encoding) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setEncoding(encoding);
}
}
/**
* {@inheritDoc}
* <p>
* This property is an alias for the connectTimeout property.
* </p>
*/
@Override
public int getLoginTimeout() throws SQLException {
return getConnectTimeout();
}
/**
* {@inheritDoc}
* <p>
* This property is an alias for the connectTimeout property.
* </p>
*/
@Override
public void setLoginTimeout(int seconds) throws SQLException {
setConnectTimeout(seconds);
}
@Override
public int getConnectTimeout() {
synchronized (lock) {
return connectionProperties.getConnectTimeout();
}
}
@Override
public void setConnectTimeout(int connectTimeout) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setConnectTimeout(connectTimeout);
}
}
@Deprecated
@Override
public String getDatabase() {
synchronized(lock) {
return connectionProperties.getDatabase();
}
}
@Deprecated
@Override
public void setDatabase(String database) {
synchronized(lock) {
checkNotStarted();
connectionProperties.setDatabase(database);
}
}
@Override
public int getBlobBufferSize() {
synchronized (lock) {
return connectionProperties.getBlobBufferSize();
}
}
@Override
public void setBlobBufferSize(int bufferSize) {
synchronized (lock) {
connectionProperties.setBlobBufferSize(bufferSize);
}
}
@Override
public String getSqlDialect() {
synchronized (lock) {
return connectionProperties.getSqlDialect();
}
}
@Override
public void setSqlDialect(String sqlDialect) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setSqlDialect(sqlDialect);
}
}
@Override
public boolean isUseStreamBlobs() {
synchronized (lock) {
return connectionProperties.isUseStreamBlobs();
}
}
@Override
public void setUseStreamBlobs(boolean useStreamBlobs) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setUseStreamBlobs(useStreamBlobs);
}
}
@Override
public int getSocketBufferSize() {
synchronized (lock) {
return connectionProperties.getSocketBufferSize();
}
}
@Override
public void setSocketBufferSize(int socketBufferSize) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setSocketBufferSize(socketBufferSize);
}
}
@Override
public boolean isTimestampUsesLocalTimezone() {
synchronized (lock) {
return connectionProperties.isTimestampUsesLocalTimezone();
}
}
@Override
public void setTimestampUsesLocalTimezone(boolean timestampUsesLocalTimezone) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setTimestampUsesLocalTimezone(timestampUsesLocalTimezone);
}
}
@Deprecated
@Override
public String getUserName() {
return getUser();
}
@Deprecated
@Override
public void setUserName(String userName) {
setUser(userName);
}
@Override
public int getBuffersNumber() {
synchronized (lock) {
return connectionProperties.getBuffersNumber();
}
}
@Override
public void setBuffersNumber(int buffersNumber) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setBuffersNumber(buffersNumber);
}
}
@Override
public DatabaseParameterBuffer getDatabaseParameterBuffer() throws SQLException {
synchronized (lock) {
return connectionProperties.getDatabaseParameterBuffer();
}
}
@Override
public String getTpbMapping() {
synchronized (lock) {
return connectionProperties.getTpbMapping();
}
}
@Override
public void setTpbMapping(String tpbMapping) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setTpbMapping(tpbMapping);
}
}
@Override
public int getDefaultTransactionIsolation() {
synchronized (lock) {
return connectionProperties.getDefaultTransactionIsolation();
}
}
@Override
public void setDefaultTransactionIsolation(int defaultIsolationLevel) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setDefaultTransactionIsolation(defaultIsolationLevel);
}
}
@Override
public String getDefaultIsolation() {
synchronized (lock) {
return connectionProperties.getDefaultIsolation();
}
}
@Override
public void setDefaultIsolation(String isolation) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setDefaultIsolation(isolation);
}
}
@Override
public TransactionParameterBuffer getTransactionParameters(int isolation) {
synchronized (lock) {
return connectionProperties.getTransactionParameters(isolation);
}
}
@Override
public void setTransactionParameters(int isolation, TransactionParameterBuffer tpb) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setTransactionParameters(isolation, tpb);
}
}
@Override
public boolean isDefaultResultSetHoldable() {
synchronized (lock) {
return connectionProperties.isDefaultResultSetHoldable();
}
}
@Override
public void setDefaultResultSetHoldable(boolean isHoldable) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setDefaultResultSetHoldable(isHoldable);
}
}
@Override
public int getSoTimeout() {
synchronized (lock) {
return connectionProperties.getSoTimeout();
}
}
@Override
public void setSoTimeout(int soTimeout) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setSoTimeout(soTimeout);
}
}
@Override
public boolean isUseFirebirdAutocommit() {
synchronized (lock) {
return connectionProperties.isUseFirebirdAutocommit();
}
}
@Override
public void setUseFirebirdAutocommit(boolean useFirebirdAutocommit) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setUseFirebirdAutocommit(useFirebirdAutocommit);
}
}
@Override
public String getWireCrypt() {
synchronized (lock) {
return connectionProperties.getWireCrypt();
}
}
@Override
public void setWireCrypt(String wireCrypt) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setWireCrypt(wireCrypt);
}
}
@Override
public String getDbCryptConfig() {
synchronized (lock) {
return connectionProperties.getDbCryptConfig();
}
}
@Override
public void setDbCryptConfig(String dbCryptConfig) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setDbCryptConfig(dbCryptConfig);
}
}
@Override
public String getAuthPlugins() {
synchronized (lock) {
return connectionProperties.getAuthPlugins();
}
}
@Override
public void setAuthPlugins(String authPlugins) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setAuthPlugins(authPlugins);
}
}
@Override
public String getGeneratedKeysEnabled() {
synchronized (lock) {
return connectionProperties.getGeneratedKeysEnabled();
}
}
@Override
public void setGeneratedKeysEnabled(String generatedKeysEnabled) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setGeneratedKeysEnabled(generatedKeysEnabled);
}
}
@Override
public String getDataTypeBind() {
synchronized (lock) {
return connectionProperties.getDataTypeBind();
}
}
@Override
public void setDataTypeBind(String dataTypeBind) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setDataTypeBind(dataTypeBind);
}
}
@Override
public String getSessionTimeZone() {
synchronized (lock) {
return connectionProperties.getSessionTimeZone();
}
}
@Override
public void setSessionTimeZone(String sessionTimeZone) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setSessionTimeZone(sessionTimeZone);
}
}
@Override
public boolean isIgnoreProcedureType() {
synchronized (lock) {
return connectionProperties.isIgnoreProcedureType();
}
}
@Override
public void setIgnoreProcedureType(boolean ignoreProcedureType) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setIgnoreProcedureType(ignoreProcedureType);
}
}
@Override
public boolean isWireCompression() {
synchronized (lock) {
return connectionProperties.isWireCompression();
}
}
@Override
public void setWireCompression(boolean wireCompression) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setWireCompression(wireCompression);
}
}
/**
* Method that allows setting non-standard property in the form "key=value"
* form. This method is needed by some containers to specify properties
* in the configuration.
*
* @param propertyMapping mapping between property name (key) and its value.
* Name and value are separated with "=", ":" or whitespace character.
* Whitespace characters on the beginning of the string and between key and
* value are ignored. No escaping is possible: "\n" is backslash-en, not
* a new line mark.
* @see #setNonStandardProperty(String, String)
*/
@Override
public final void setNonStandardProperty(String propertyMapping) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setNonStandardProperty(propertyMapping);
}
}
/**
* Method to set properties which are not exposed through JavaBeans-style setters.
*
* @param key Name of the property (see Jaybird releasenotes)
* @param value Value of the property
* @see #setNonStandardProperty(String)
*/
@Override
public final void setNonStandardProperty(String key, String value) {
synchronized (lock) {
checkNotStarted();
connectionProperties.setNonStandardProperty(key, value);
}
}
/**
* Method to get the value of properties which are not exposed through JavaBeans-style setters.
*
* @param key Name of the property (see Jaybird releasenotes)
* @return Value of the property
* @see #setNonStandardProperty(String)
* @see #setNonStandardProperty(String, String)
*/
@Override
public final String getNonStandardProperty(String key) {
synchronized (lock) {
return connectionProperties.getNonStandardProperty(key);
}
}
/**
* Sets the database property of connectionProperties.
*/
protected final void setDatabase() {
synchronized (lock) {
// to getDatabasePath of the relevant GDSFactoryPlugin
StringBuilder sb = new StringBuilder();
if (serverName != null && serverName.length() > 0) {
sb.append("//").append(serverName);
if (portNumber > 0) {
sb.append(':').append(portNumber);
}
sb.append('/');
}
if (databaseName != null) {
sb.append(databaseName);
}
if (sb.length() > 0) {
connectionProperties.setDatabase(sb.toString());
} else {
connectionProperties.setDatabase(null);
}
}
}
protected final void setConnectionProperties(FBConnectionProperties connectionProperties) {
synchronized (lock) {
if (connectionProperties == null) {
throw new NullPointerException("null value not allowed for connectionProperties");
}
this.connectionProperties = connectionProperties;
}
}
protected final FBConnectionProperties getConnectionProperties() {
synchronized (lock) {
return connectionProperties;
}
}
/**
* Updates the supplied reference with RefAddr properties relevant to this class.
*
* @param ref Reference to update
* @param instance Instance of this class to obtain values
*/
protected static void updateReference(Reference ref, FBAbstractCommonDataSource instance) {
synchronized (instance.lock) {
ref.add(new StringRefAddr(REF_DESCRIPTION, instance.getDescription()));
ref.add(new StringRefAddr(REF_SERVER_NAME, instance.getServerName()));
if (instance.getPortNumber() != 0) {
ref.add(new StringRefAddr(REF_PORT_NUMBER, Integer.toString(instance.getPortNumber())));
}
ref.add(new StringRefAddr(REF_DATABASE_NAME, instance.getDatabaseName()));
byte[] data = DataSourceFactory.serialize(instance.connectionProperties);
ref.add(new BinaryRefAddr(REF_PROPERTIES, data));
}
}
}