1010import org .hibernate .boot .registry .classloading .spi .ClassLoaderService ;
1111import org .hibernate .boot .registry .classloading .spi .ClassLoadingException ;
1212import org .hibernate .cfg .JdbcSettings ;
13+ import org .hibernate .engine .jdbc .connections .spi .ConnectionProviderConfigurationException ;
1314import org .hibernate .engine .jdbc .connections .spi .DataSourceBasedMultiTenantConnectionProviderImpl ;
1415import org .hibernate .engine .jdbc .connections .spi .MultiTenantConnectionProvider ;
1516import org .hibernate .resource .beans .internal .Helper ;
1617import org .hibernate .service .spi .ServiceException ;
1718import org .hibernate .service .spi .ServiceRegistryImplementor ;
1819
19- import org .jboss .logging .Logger ;
20-
2120import static org .hibernate .cfg .MultiTenancySettings .MULTI_TENANT_CONNECTION_PROVIDER ;
2221
2322/**
2625 * @author Steve Ebersole
2726 */
2827public class MultiTenantConnectionProviderInitiator implements StandardServiceInitiator <MultiTenantConnectionProvider <?>> {
29- private static final Logger log = Logger .getLogger ( MultiTenantConnectionProviderInitiator .class );
3028
3129 /**
3230 * Singleton access
@@ -64,23 +62,26 @@ else if ( configValue instanceof MultiTenantConnectionProvider<?> multiTenantCon
6462 return multiTenantConnectionProvider ;
6563 }
6664 else {
67- final var implClass = multiTenantConnectionProviderClass ( registry , configValue );
65+ final var providerClass = providerClass ( registry , configValue );
6866 try {
69- return implClass .newInstance ();
67+ return providerClass .newInstance ();
7068 }
7169 catch (Exception e ) {
72- log .warn ( "Unable to instantiate specified class [" + implClass .getName () + "]" , e );
73- throw new ServiceException ( "Unable to instantiate specified multi-tenant connection provider [" + implClass .getName () + "]" );
70+ throw new ServiceException ( "Unable to instantiate specified multi-tenant connection provider [" + providerClass .getName () + "]" , e );
7471 }
7572 }
7673 }
7774
78- private static Class <MultiTenantConnectionProvider <?>> multiTenantConnectionProviderClass (
75+ private static Class <? extends MultiTenantConnectionProvider <?>> providerClass (
7976 ServiceRegistryImplementor registry , Object configValue ) {
80- if ( configValue instanceof Class ) {
81- @ SuppressWarnings ("unchecked" )
82- final var clazz = (Class <MultiTenantConnectionProvider <?>>) configValue ;
83- return clazz ;
77+ if ( configValue instanceof Class <?> configType ) {
78+ if ( !MultiTenantConnectionProvider .class .isAssignableFrom ( configType ) ) {
79+ throw new ConnectionProviderConfigurationException ( "Class '" + configType .getName ()
80+ + "' does not implement 'MultiTenantConnectionProvider'" );
81+ }
82+ @ SuppressWarnings ("unchecked" ) // Safe, we just checked
83+ final var providerClass = (Class <? extends MultiTenantConnectionProvider <?>>) configType ;
84+ return providerClass ;
8485 }
8586 else {
8687 final String className = configValue .toString ();
@@ -89,8 +90,7 @@ private static Class<MultiTenantConnectionProvider<?>> multiTenantConnectionProv
8990 return classLoaderService .classForName ( className );
9091 }
9192 catch (ClassLoadingException cle ) {
92- log .warn ( "Unable to locate specified class [" + className + "]" , cle );
93- throw new ServiceException ( "Unable to locate specified multi-tenant connection provider [" + className + "]" );
93+ throw new ServiceException ( "Unable to locate specified multi-tenant connection provider [" + className + "]" , cle );
9494 }
9595 }
9696 }
0 commit comments