HHH-12417 - default strategy based on registrations with StrategySelector

This commit is contained in:
Steve Ebersole 2018-03-21 15:01:42 -05:00
parent dae31640a8
commit 953f956989
3 changed files with 37 additions and 22 deletions

View File

@ -6,6 +6,9 @@
*/
package org.hibernate.boot.registry.selector.internal;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.Callable;
@ -170,6 +173,16 @@ public class StrategySelectorImpl implements StrategySelector {
);
}
@Override
@SuppressWarnings("unchecked")
public Collection getRegisteredStrategyImplementors(Class strategy) {
final Map<String, Class> registrations = namedStrategyImplementorByStrategyMap.get( strategy );
if ( registrations == null ) {
return Collections.emptySet();
}
return new HashSet( registrations.values() );
}
@SuppressWarnings("unchecked")
@Override
public <T> T resolveStrategy(

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.boot.registry.selector.spi;
import java.util.Collection;
import java.util.concurrent.Callable;
import org.hibernate.service.Service;
@ -139,4 +140,11 @@ public interface StrategySelector extends Service {
<T> T resolveStrategy(Class<T> strategy, Object strategyReference, Callable<T> defaultResolver, StrategyCreator<T> creator);
<T> T resolveStrategy(Class<T> strategy, Object strategyReference, T defaultValue, StrategyCreator<T> creator);
/**
* Retrieve all of the registered implementors of the given strategy. Useful
* to allow defaulting the choice to the single registered implementor when
* only one is registered
*/
<T> Collection<T> getRegisteredStrategyImplementors(Class<T> strategy);
}

View File

@ -6,11 +6,11 @@
*/
package org.hibernate.cache.internal;
import java.util.Collection;
import java.util.Map;
import java.util.Properties;
import org.hibernate.boot.registry.StandardServiceInitiator;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.registry.selector.spi.StrategySelector;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cfg.AvailableSettings;
@ -72,29 +72,23 @@ public class RegionFactoryInitiator implements StandardServiceInitiator<RegionFa
);
}
if ( regionFactory == NoCachingRegionFactory.INSTANCE ) {
// todo (5.3) : make this configurable?
boolean allowDefaulting = true;
if ( allowDefaulting ) {
final StrategySelector selector = registry.getService( StrategySelector.class );
final Collection<RegionFactory> implementors = selector.getRegisteredStrategyImplementors( RegionFactory.class );
if ( implementors != null && implementors.size() == 1 ) {
regionFactory = selector.resolveStrategy( RegionFactory.class, implementors.iterator().next() );
}
else {
LOG.debugf( "Cannot default RegionFactory based on registered strategies as `%s` RegionFactory strategies were registered" );
}
}
}
LOG.debugf( "Cache region factory : %s", regionFactory.getClass().getName() );
return regionFactory;
}
/**
* Map legacy names unto the new corollary.
*
* TODO: temporary hack for org.hibernate.cfg.SettingsFactory.createRegionFactory()
*
* @param name The (possibly legacy) factory name
*
* @return The factory name to use.
*/
public static String mapLegacyNames(final String name) {
if ( "org.hibernate.cache.EhCacheRegionFactory".equals( name ) ) {
return "org.hibernate.cache.ehcache.EhCacheRegionFactory";
}
if ( "org.hibernate.cache.SingletonEhCacheRegionFactory".equals( name ) ) {
return "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory";
}
return name;
}
}