From 1f78c9685ef8ecf188d3adedc99015e6c0c5fa8d Mon Sep 17 00:00:00 2001 From: Strong Liu Date: Tue, 12 Jun 2012 01:12:37 +0800 Subject: [PATCH] HHH-7382 refact: unify the configuration implementation loading strategy --- .../internal/SessionFactoryImpl.java | 106 ++++-------------- .../internal/ConfigurationServiceImpl.java | 59 +++++++++- .../config/spi/ConfigurationService.java | 9 ++ .../internal/JtaPlatformInitiator.java | 30 +---- 4 files changed, 90 insertions(+), 114 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java index e12a83dbfc..f0172bf7f2 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -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 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 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 ); } diff --git a/hibernate-core/src/main/java/org/hibernate/service/config/internal/ConfigurationServiceImpl.java b/hibernate-core/src/main/java/org/hibernate/service/config/internal/ConfigurationServiceImpl.java index 0bbe388748..4c03d5f844 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/config/internal/ConfigurationServiceImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/service/config/internal/ConfigurationServiceImpl.java @@ -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 getSetting(String name, Converter converter) { return getSetting( name, converter, null ); @@ -58,4 +75,44 @@ public class ConfigurationServiceImpl implements ConfigurationService { return converter.convert( value ); } + @Override + public T getSetting(String name, Class expected, T defaultValue) { + Object value = settings.get( name ); + T target = cast( expected, value ); + return target !=null ? target : defaultValue; + } + @Override + public T cast(Class expected, Object candidate){ + if(candidate == null) return null; + if ( expected.isInstance( candidate ) ) { + return (T) candidate; + } + Class 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; + } + + } diff --git a/hibernate-core/src/main/java/org/hibernate/service/config/spi/ConfigurationService.java b/hibernate-core/src/main/java/org/hibernate/service/config/spi/ConfigurationService.java index 815a44bdd8..6f8381f604 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/config/spi/ConfigurationService.java +++ b/hibernate-core/src/main/java/org/hibernate/service/config/spi/ConfigurationService.java @@ -37,7 +37,16 @@ public interface ConfigurationService extends Service { public T getSetting(String name, Converter converter); public T getSetting(String name, Converter converter, T defaultValue); + public T getSetting(String name, Class expected, T defaultValue); + /** + * Cast candidate to the instance of expected 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 cast(Class expected, Object candidate); public static interface Converter { public T convert(Object value); } diff --git a/hibernate-core/src/main/java/org/hibernate/service/jta/platform/internal/JtaPlatformInitiator.java b/hibernate-core/src/main/java/org/hibernate/service/jta/platform/internal/JtaPlatformInitiator.java index a3db609ecb..5c7fb6d5cc 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/jta/platform/internal/JtaPlatformInitiator.java +++ b/hibernate-core/src/main/java/org/hibernate/service/jta/platform/internal/JtaPlatformInitiator.java @@ -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 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 jtaPlatformImplClass; - - if ( Class.class.isInstance( platform ) ) { - jtaPlatformImplClass = (Class) 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) {