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