HHH-7382 refact: unify the configuration implementation loading strategy
This commit is contained in:
parent
74b9ebc09a
commit
1f78c9685e
|
@ -552,54 +552,15 @@ public final class SessionFactoryImpl
|
|||
fetchProfiles.put( fetchProfile.getName(), fetchProfile );
|
||||
}
|
||||
|
||||
this.customEntityDirtinessStrategy = determineCustomEntityDirtinessStrategy( properties );
|
||||
this.currentTenantIdentifierResolver = determineCurrentTenantIdentifierResolver(
|
||||
cfg.getCurrentTenantIdentifierResolver(),
|
||||
properties
|
||||
);
|
||||
this.customEntityDirtinessStrategy = determineCustomEntityDirtinessStrategy();
|
||||
this.currentTenantIdentifierResolver = determineCurrentTenantIdentifierResolver( cfg.getCurrentTenantIdentifierResolver() );
|
||||
this.transactionEnvironment = new TransactionEnvironmentImpl( this );
|
||||
this.observer.sessionFactoryCreated( this );
|
||||
}
|
||||
|
||||
@SuppressWarnings( {"unchecked"})
|
||||
private CustomEntityDirtinessStrategy determineCustomEntityDirtinessStrategy(Properties properties) {
|
||||
final Object value = properties.get( AvailableSettings.CUSTOM_ENTITY_DIRTINESS_STRATEGY );
|
||||
if ( value != null ) {
|
||||
if ( CustomEntityDirtinessStrategy.class.isInstance( value ) ) {
|
||||
return CustomEntityDirtinessStrategy.class.cast( value );
|
||||
}
|
||||
Class<CustomEntityDirtinessStrategy> customEntityDirtinessStrategyClass;
|
||||
if ( Class.class.isInstance( value ) ) {
|
||||
customEntityDirtinessStrategyClass = Class.class.cast( value );
|
||||
}
|
||||
else {
|
||||
try {
|
||||
customEntityDirtinessStrategyClass = serviceRegistry.getService( ClassLoaderService.class )
|
||||
.classForName( value.toString() );
|
||||
}
|
||||
catch (Exception e) {
|
||||
LOG.debugf(
|
||||
"Unable to locate CustomEntityDirtinessStrategy implementation class %s",
|
||||
value.toString()
|
||||
);
|
||||
customEntityDirtinessStrategyClass = null;
|
||||
}
|
||||
}
|
||||
if ( customEntityDirtinessStrategyClass != null ) {
|
||||
try {
|
||||
return customEntityDirtinessStrategyClass.newInstance();
|
||||
}
|
||||
catch (Exception e) {
|
||||
LOG.debugf(
|
||||
"Unable to instantiate CustomEntityDirtinessStrategy class %s",
|
||||
customEntityDirtinessStrategyClass.getName()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// last resort
|
||||
return new CustomEntityDirtinessStrategy() {
|
||||
@SuppressWarnings({ "unchecked" })
|
||||
private CustomEntityDirtinessStrategy determineCustomEntityDirtinessStrategy() {
|
||||
CustomEntityDirtinessStrategy defaultValue = new CustomEntityDirtinessStrategy() {
|
||||
@Override
|
||||
public boolean canDirtyCheck(Object entity, EntityPersister persister, Session session) {
|
||||
return false;
|
||||
|
@ -623,53 +584,26 @@ public final class SessionFactoryImpl
|
|||
// todo : implement proper method body
|
||||
}
|
||||
};
|
||||
return serviceRegistry.getService( ConfigurationService.class ).getSetting(
|
||||
AvailableSettings.CUSTOM_ENTITY_DIRTINESS_STRATEGY,
|
||||
CustomEntityDirtinessStrategy.class,
|
||||
defaultValue
|
||||
);
|
||||
}
|
||||
|
||||
@SuppressWarnings( {"unchecked"})
|
||||
@SuppressWarnings({ "unchecked" })
|
||||
private CurrentTenantIdentifierResolver determineCurrentTenantIdentifierResolver(
|
||||
CurrentTenantIdentifierResolver explicitResolver,
|
||||
Properties properties) {
|
||||
CurrentTenantIdentifierResolver explicitResolver) {
|
||||
if ( explicitResolver != null ) {
|
||||
return explicitResolver;
|
||||
}
|
||||
|
||||
final Object value = properties.get( AvailableSettings.MULTI_TENANT_IDENTIFIER_RESOLVER );
|
||||
if ( value == null ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ( CurrentTenantIdentifierResolver.class.isInstance( value ) ) {
|
||||
return CurrentTenantIdentifierResolver.class.cast( value );
|
||||
}
|
||||
|
||||
Class<CurrentTenantIdentifierResolver> implClass;
|
||||
if ( Class.class.isInstance( value ) ) {
|
||||
implClass = Class.class.cast( value );
|
||||
}
|
||||
else {
|
||||
try {
|
||||
implClass = serviceRegistry.getService( ClassLoaderService.class ).classForName( value.toString() );
|
||||
}
|
||||
catch (Exception e) {
|
||||
LOG.debugf(
|
||||
"Unable to locate CurrentTenantIdentifierResolver implementation class %s",
|
||||
value.toString()
|
||||
return serviceRegistry.getService( ConfigurationService.class )
|
||||
.getSetting(
|
||||
AvailableSettings.MULTI_TENANT_IDENTIFIER_RESOLVER,
|
||||
CurrentTenantIdentifierResolver.class,
|
||||
null
|
||||
);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if ( implClass != null ) {
|
||||
try {
|
||||
return implClass.newInstance();
|
||||
}
|
||||
catch ( Exception e ) {
|
||||
LOG.debugf(
|
||||
"Unable to instantiate CurrentTenantIdentifierResolver class %s",
|
||||
implClass.getName()
|
||||
);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings( {"ThrowableResultOfMethodCallIgnored"})
|
||||
|
@ -975,8 +909,8 @@ public final class SessionFactoryImpl
|
|||
fetchProfiles.put( fetchProfile.getName(), fetchProfile );
|
||||
}
|
||||
|
||||
this.customEntityDirtinessStrategy = determineCustomEntityDirtinessStrategy( properties );
|
||||
this.currentTenantIdentifierResolver = determineCurrentTenantIdentifierResolver( null, properties );
|
||||
this.customEntityDirtinessStrategy = determineCustomEntityDirtinessStrategy();
|
||||
this.currentTenantIdentifierResolver = determineCurrentTenantIdentifierResolver( null );
|
||||
this.transactionEnvironment = new TransactionEnvironmentImpl( this );
|
||||
this.observer.sessionFactoryCreated( this );
|
||||
}
|
||||
|
|
|
@ -26,13 +26,25 @@ package org.hibernate.service.config.internal;
|
|||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import org.hibernate.internal.CoreMessageLogger;
|
||||
import org.hibernate.service.classloading.spi.ClassLoaderService;
|
||||
import org.hibernate.service.classloading.spi.ClassLoadingException;
|
||||
import org.hibernate.service.config.spi.ConfigurationService;
|
||||
import org.hibernate.service.spi.ServiceRegistryAwareService;
|
||||
import org.hibernate.service.spi.ServiceRegistryImplementor;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class ConfigurationServiceImpl implements ConfigurationService {
|
||||
public class ConfigurationServiceImpl implements ConfigurationService, ServiceRegistryAwareService {
|
||||
private static final CoreMessageLogger LOG = Logger.getMessageLogger(
|
||||
CoreMessageLogger.class,
|
||||
ConfigurationServiceImpl.class.getName()
|
||||
);
|
||||
private final Map settings;
|
||||
private ServiceRegistryImplementor serviceRegistry;
|
||||
|
||||
@SuppressWarnings( "unchecked" )
|
||||
public ConfigurationServiceImpl(Map settings) {
|
||||
|
@ -44,6 +56,11 @@ public class ConfigurationServiceImpl implements ConfigurationService {
|
|||
return settings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
|
||||
this.serviceRegistry = serviceRegistry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getSetting(String name, Converter<T> converter) {
|
||||
return getSetting( name, converter, null );
|
||||
|
@ -58,4 +75,44 @@ public class ConfigurationServiceImpl implements ConfigurationService {
|
|||
|
||||
return converter.convert( value );
|
||||
}
|
||||
@Override
|
||||
public <T> T getSetting(String name, Class<T> expected, T defaultValue) {
|
||||
Object value = settings.get( name );
|
||||
T target = cast( expected, value );
|
||||
return target !=null ? target : defaultValue;
|
||||
}
|
||||
@Override
|
||||
public <T> T cast(Class<T> expected, Object candidate){
|
||||
if(candidate == null) return null;
|
||||
if ( expected.isInstance( candidate ) ) {
|
||||
return (T) candidate;
|
||||
}
|
||||
Class<T> target;
|
||||
if ( Class.class.isInstance( candidate ) ) {
|
||||
target = Class.class.cast( candidate );
|
||||
}
|
||||
else {
|
||||
try {
|
||||
target = serviceRegistry.getService( ClassLoaderService.class ).classForName( candidate.toString() );
|
||||
}
|
||||
catch ( ClassLoadingException e ) {
|
||||
LOG.debugf( "Unable to locate %s implementation class %s", expected.getName(), candidate.toString() );
|
||||
target = null;
|
||||
}
|
||||
}
|
||||
if ( target != null ) {
|
||||
try {
|
||||
return target.newInstance();
|
||||
}
|
||||
catch ( Exception e ) {
|
||||
LOG.debugf(
|
||||
"Unable to instantiate %s class %s", expected.getName(),
|
||||
target.getName()
|
||||
);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -37,7 +37,16 @@ public interface ConfigurationService extends Service {
|
|||
|
||||
public <T> T getSetting(String name, Converter<T> converter);
|
||||
public <T> T getSetting(String name, Converter<T> converter, T defaultValue);
|
||||
public <T> T getSetting(String name, Class<T> expected, T defaultValue);
|
||||
|
||||
/**
|
||||
* Cast <tt>candidate</tt> to the instance of <tt>expected</tt> type.
|
||||
*
|
||||
* @param expected The type of instance expected to return.
|
||||
* @param candidate The candidate object to be casted.
|
||||
* @return The instance of expected type or null if this cast fail.
|
||||
*/
|
||||
public <T> T cast(Class<T> expected, Object candidate);
|
||||
public static interface Converter<T> {
|
||||
public T convert(Object value);
|
||||
}
|
||||
|
|
|
@ -27,12 +27,12 @@ import java.util.Map;
|
|||
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.internal.CoreMessageLogger;
|
||||
import org.hibernate.internal.util.jndi.JndiHelper;
|
||||
import org.hibernate.service.classloading.spi.ClassLoaderService;
|
||||
import org.hibernate.service.config.spi.ConfigurationService;
|
||||
import org.hibernate.service.jta.platform.spi.JtaPlatform;
|
||||
import org.hibernate.service.jta.platform.spi.JtaPlatformException;
|
||||
import org.hibernate.service.spi.BasicServiceInitiator;
|
||||
|
@ -61,33 +61,9 @@ public class JtaPlatformInitiator implements BasicServiceInitiator<JtaPlatform>
|
|||
if ( platform == null ) {
|
||||
return new NoJtaPlatform();
|
||||
}
|
||||
return registry.getService( ConfigurationService.class )
|
||||
.cast( JtaPlatform.class, platform );
|
||||
|
||||
if ( JtaPlatform.class.isInstance( platform ) ) {
|
||||
return (JtaPlatform) platform;
|
||||
}
|
||||
|
||||
final Class<JtaPlatform> jtaPlatformImplClass;
|
||||
|
||||
if ( Class.class.isInstance( platform ) ) {
|
||||
jtaPlatformImplClass = (Class<JtaPlatform>) platform;
|
||||
}
|
||||
else {
|
||||
final String platformImplName = platform.toString();
|
||||
final ClassLoaderService classLoaderService = registry.getService( ClassLoaderService.class );
|
||||
try {
|
||||
jtaPlatformImplClass = classLoaderService.classForName( platformImplName );
|
||||
}
|
||||
catch ( Exception e ) {
|
||||
throw new HibernateException( "Unable to locate specified JtaPlatform class [" + platformImplName + "]", e );
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return jtaPlatformImplClass.newInstance();
|
||||
}
|
||||
catch ( Exception e ) {
|
||||
throw new HibernateException( "Unable to create specified JtaPlatform class [" + jtaPlatformImplClass.getName() + "]", e );
|
||||
}
|
||||
}
|
||||
|
||||
private Object getConfiguredPlatform(Map configVales, ServiceRegistryImplementor registry) {
|
||||
|
|
Loading…
Reference in New Issue