6
6
package org .hibernate .reactive .pool .impl ;
7
7
8
8
9
+ import java .lang .invoke .MethodHandles ;
9
10
import java .net .URI ;
11
+ import java .util .Map ;
10
12
import java .util .concurrent .CompletionStage ;
11
13
14
+ import org .hibernate .HibernateError ;
12
15
import org .hibernate .engine .jdbc .spi .JdbcServices ;
13
16
import org .hibernate .engine .jdbc .spi .SqlStatementLogger ;
17
+ import org .hibernate .internal .util .config .ConfigurationHelper ;
18
+ import org .hibernate .reactive .logging .impl .Log ;
19
+ import org .hibernate .reactive .logging .impl .LoggerFactory ;
14
20
import org .hibernate .reactive .pool .ReactiveConnection ;
21
+ import org .hibernate .reactive .provider .Settings ;
15
22
import org .hibernate .reactive .vertx .VertxInstance ;
16
23
import org .hibernate .service .spi .ServiceRegistryAwareService ;
17
24
import org .hibernate .service .spi .ServiceRegistryImplementor ;
27
34
28
35
public class H2SqlClientPool extends SqlClientPool implements ServiceRegistryAwareService {
29
36
37
+ private static final Log LOG = LoggerFactory .make ( Log .class , MethodHandles .lookup () );
38
+
30
39
//Asynchronous shutdown promise: we can't return it from #close as we implement a
31
40
//blocking interface.
32
41
private volatile Future <Void > closeFuture = Future .succeededFuture ();
@@ -45,22 +54,29 @@ public void injectServices(ServiceRegistryImplementor serviceRegistry) {
45
54
sqlStatementLogger = serviceRegistry .getService ( JdbcServices .class ).getSqlStatementLogger ();
46
55
}
47
56
57
+ public void configure (Map configuration ) {
58
+ uri = jdbcUrl ( configuration );
59
+ }
60
+
48
61
public void start () {
49
62
if ( pools == null ) {
50
63
pools = createPool ( uri );
51
64
}
52
65
}
53
66
54
- public void stop () {
55
- if ( pools != null ) {
56
- this .closeFuture = pools .close ();
57
- }
67
+ @ Override
68
+ public CompletionStage <Void > getCloseFuture () {
69
+ return closeFuture .toCompletionStage ();
70
+ }
71
+
72
+ @ Override
73
+ protected Pool getPool () {
74
+ return pools ;
58
75
}
59
76
60
77
private Pool createPool (URI uri ) {
61
78
SqlClientPoolConfiguration configuration = serviceRegistry .getService ( SqlClientPoolConfiguration .class );
62
79
VertxInstance vertx = serviceRegistry .getService ( VertxInstance .class );
63
-
64
80
return createPool ( uri , configuration .connectOptions ( uri ), configuration .poolOptions (), vertx .getVertx () );
65
81
}
66
82
@@ -73,19 +89,36 @@ private Pool createPool(URI uri, SqlConnectOptions connectOptions, PoolOptions p
73
89
return pool ;
74
90
}
75
91
76
- @ Override
77
- protected Pool getPool () {
78
- return pools ;
92
+ private URI jdbcUrl (Map <?, ?> configurationValues ) {
93
+ String url = ConfigurationHelper .getString ( Settings .URL , configurationValues );
94
+ LOG .sqlClientUrl ( url );
95
+ return parse ( url );
79
96
}
80
97
81
- @ Override
82
- protected SqlStatementLogger getSqlStatementLogger () {
83
- return sqlStatementLogger ;
98
+ public void stop () {
99
+ if ( pools != null ) {
100
+ this .closeFuture = pools .close ();
101
+ }
102
+ }
103
+
104
+ public static URI parse (String url ) {
105
+
106
+ if ( url == null || url .trim ().isEmpty () ) {
107
+ throw new HibernateError (
108
+ "The configuration property '" + Settings .URL + "' was not provided, or is in invalid format. This is required when using the default DefaultSqlClientPool: " +
109
+ "either provide the configuration setting or integrate with a different SqlClientPool implementation" );
110
+ }
111
+
112
+ if ( url .startsWith ( "jdbc:" ) ) {
113
+ return URI .create ( url .substring ( 5 ) );
114
+ }
115
+
116
+ return URI .create ( url );
84
117
}
85
118
86
119
@ Override
87
- public CompletionStage < Void > getCloseFuture () {
88
- return closeFuture . toCompletionStage () ;
120
+ protected SqlStatementLogger getSqlStatementLogger () {
121
+ return sqlStatementLogger ;
89
122
}
90
123
91
124
@ Override
0 commit comments